Question

In: Computer Science

Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...

Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount.

Both files will be pipe delimited (|) and their format will be the following:

accounts.txt : File with bank account info (account number, account type, balance)

AcctNumber|AcctType|Balance (this first line is not part of the file)

1346782901|L|11045.43

1346782902|S|100.43

transactions.txt : File with transactions (account #, deposit/withdrawal, amount)

AcctNumber|TransactionType|Amount (this first line is not part of the file)

1346782901|D|1

1346782902|W|445.05

0346782902|W|5.06

You will need to read in all the account information from the first file and use an array of structs to store your data. Assume less than 50 records per file. You will then need a loop to read in a transaction from the second file, update the balance of the respective account according to the transaction type and then continue to the next line until all transactions are completed. If an account is not found, issue an error and continue with the next transaction. If a withdrawal is more than the current account balance, issue an error, do not perform the withdrawal and continue with the next transaction.

At the end of the program, display all accounts with their final balances in a nicely formatted manner. Moreover, create a new output file named newaccounts.txt and write all account info in the same format as the original accounts.txt file (i.e. pipe delimited). Create your own test files for accounts.txt and transactions.txt and make sure you test for all possibilities.

please show screenshots of your programming working and including the final display of the accounts and their balances.

It must be c++

Solutions

Expert Solution

#include<iostream> //for input and output

#include<fstream> //for file input and output means file reading and writting

#include<string.h> //for string and its method

using namespace std;

//Create a Account Class

struct Account{

//3 instance variable

string accNumber;

char type;

double amount;

//set all values

void setAll(string accNumber,char type,double amount){

this->accNumber = accNumber;

this->type = type;

this->amount = amount;

}

//deposit ammount code here if the deposit ammount is less than 0 raise an error

void deposit(double amn){

if(amn <0){

cout<<"Deposit Amount must not be less than 0"<<endl;

}else{

this->amount += amn;

}

}

//Withdrawn amount code here If the withdrawn is less than current account or less then 0 raise an error

void withdrawn(double amn){

if(this->amount < amn ){

cout<<"Insufficient Balance for account number "<<this->accNumber<<endl;

}else if(amn <0){

cout<<"Withdrawn Amount must not be less than 0 "<<endl;

}else{

this->amount -= amn;

}

}

//this returns account number

string getAccNum(){

return accNumber;

}

//this returns type of account

char getType(){

return type;

}

//this return amount

double getAmount(){

return amount;

}

};

//this is the main method

int main(){

//Make an array of 50 Account

Account account[50];

int index=0;

//make an accountFile Read pointer

ifstream accFile("accounts.txt");

//this string store the readed line from the account file

string myText;

//Read data from file line by line

while(getline(accFile,myText)){

//make an char pointer to store the string

char * cstr = new char[myText.length()+1];

//Here we convert the string to char array

strcpy(cstr,myText.c_str());

//Here we tokenize the readed string into parts

//and split string by |

char *token = strtok(cstr, "|");

//Make an array of string to store all the split data

string temp[3];

int i=0; //counter for this temp

//complete split code

while(token !=0){

temp[i++]= token;

token = strtok(NULL,"|");

}

delete[] cstr; //delete char pointer

//add data to the accounts array

account[index++].setAll(temp[0],temp[1][0],stod(temp[2]));

}

accFile.close(); //Close the account file

//Read transaction file

ifstream transFile("transactions.txt");

string myText2;

while(getline(transFile,myText2)){

char * cstr = new char[myText2.length()+1];

strcpy(cstr,myText2.c_str());

char *token = strtok(cstr, "|");

string temp[3];

int i=0;

while(token !=0){

temp[i++]= token;

token = strtok(NULL,"|");

}

delete[] cstr;

//This flag keeps track of whether account is found or not

bool flag = false;

for(int i=0;i<index;i++){

//If account found then we check the type of transaction whether deposit or withdrawn

if(account[i].getAccNum().compare(temp[0])==0){

flag = true; //if account found make flag true

if(temp[1][0]=='D'){

account[i].deposit(stod(temp[2]));

}else if(temp[1][0]=='W'){

account[i].withdrawn(stod(temp[2]));

}

}

}

//If flag value false means no account found raise an error

if(flag ==false){

cout<<"Account Not found"<<endl;

}

}

//close transaction file

transFile.close();

//make an output file pointer

ofstream outFile("newaccounts.txt");

//Iterate through the account array and add each record to the newAccount file

for(int i=0;i<index;i++){

string s = "";

s += account[i].getAccNum() + "|" + account[i].getType() + "|" + to_string(account[i].getAmount()) + "\n";

cout<<s;

outFile << s;

}

//close the outFile

outFile.close();

return 0;

}


Related Solutions

Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
C programming A small company provided you three files. 1) emp.txt : this file contains list...
C programming A small company provided you three files. 1) emp.txt : this file contains list of employees where each line represents data of an employee. An employee has an id (String max length 20), last name (string max length 100), and salary (int). See the example emp.txt file. 2) dept.txt: This text file contains list of departments of the employees. Each line of the file contains an employee id (string max length 20) and department name for that employee...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Please Fix code and make Copy/Paste avaliable // Application allows user to enter a series of words // and displays them in reverse order import java.util.*; public class DebugEight4 {    public static void main(String[] args)    {       Scanner input = new Scanner(System.in);       int...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18...
Code needed in C++, make changes to the file provided (18-1, has 3 files) Chapter 18 Stacks and Queues ----------------------------------------------------------------------------------------------------- capacity is just 5 1. push 6 numbers on the stack 2. catch the overflow error in a catch block 3. pop one element, which means your capacity is now down to 4 4. push the element that was rejected earlier 5. verify your entire stack by popping to show the new numbers. IntStack.h #include <memory> using namespace std; class...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first...
Write a mail merge application titled EmailMerge.java You will use two files for this program.  The first is a text file that contains a template letter. template.txt [ Dear <>, Because you are <> years old and <>, we have a free gift for you. You have absolutely nothing to buy; just pay the shipping and handling charge of $9.99. To claim your gift, call us immediately. Thank you, Office of Claims Department ] The tags <>, <>, and <> are...
C# Simple Text File Merging Application - The idea is to merge two text files located...
C# Simple Text File Merging Application - The idea is to merge two text files located in the same folder to create a new txt file (also in the same folder). I have the idea, and I can merge the two txt files together, however I would like to have an empty line between the two files. How would I implement this in the code below? if (File.Exists(fileNameWithPath1)) { try { string[] file1 = File.ReadAllLines(fileNameWithPath1); string[] file2 = File.ReadAllLines(fileNameWithPath2); //dump...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
Please, Use C++ The files in question are Square.cpp, Square.h, SquareContainer.cpp, SquareContainer.h and ClassTest.cpp. Please download,...
Please, Use C++ The files in question are Square.cpp, Square.h, SquareContainer.cpp, SquareContainer.h and ClassTest.cpp. Please download, compile and run these files, and convince yourself that you understand what they do. Question Is there a problem with the fact that SquareContainer.h defines two classes? Coding (finish in 60min) - just try to finish as many as you can Add an overloaded assignment operator to the Square class. Add a "<" operator for Square objects, which returns "true" when a Square object's...
C++ If you tried to compile a source file that doesn't contain a main() function, would...
C++ If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer. Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT