In: Computer Science
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++
#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;
}