I need Help Completeing My Project Assignment. Please someone Help me add the fo
ID: 3805591 • Letter: I
Question
I need Help Completeing My Project Assignment. Please someone Help me add the following options to my Project1.cpp file and create a bank.h header file and a bank.cpp file.
Add three more options to your main program:
o List all records in the increasing order of account number;
o List all the records in the increasing order of account balance;
o List all the transactions (there is no order requirement).
To implement the new options “List all records in the increasing order of account number” and “List all the records in the increasing order of account balance”, your main program needs to use the data structure and functions declared and defined in bank.h and bank.cpp that you create.
Your bank.h file should contain (1) the declaration of a data structure (say account), which is the node of a linked list used to store account information; and (2) at least three function declarations (two are used to insert a node into the linked list and one is used to show the contents in the linked list).
Your bank.cpp file should contain the definitions (implementations) of the functions declared in bank.h file.
To list the records in some order, first you should read all the accounts from the database: for each account, create a node (instance of data structure, say account) and insert it (in some order) into a linked list, and second you display the records in the linked list.
Project1.cpp
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct bank
{
int accNumber;
string Fname, Lname;
float balance;
};
static int j = 0;
struct bank B[5];
void read_from_file(const char* filename)
{
/*Variable declarations*/
ifstream infile;
/*Opening file*/
infile.open (filename);
int i=0;
if (infile.is_open())
{
/*Reading all details from file*/
while(!infile.eof()) // To get you all the lines.
{
infile>>B[i].accNumber;
infile>>B[i].Fname; // Saves the line in STRING.
infile>>B[i].Lname;
infile>>B[i].balance;
/*To prevent read last line twice*/
if( infile.eof() )
{
break;
}
j++;
i++;
}
/*Printing all account details after reading*/
for (i = 0; i<j; i++){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << ' ';
}
infile.close();
}
}
int addAccount()
{
int accNum,i;
string fname, lname;
float balance;
cout << " --- Provide details --- ";
cout << "Acc Number: "; cin >> accNum;
for (i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
j = j + 1;
B[i].accNumber = accNum;
cout << "first name: "; cin >> B[i].Fname;
cout << "last name: "; cin >> B[i].Lname;
cout << "Balance:"; cin >> B[i].balance;
}
void deleteAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
for (int k = i; k<j - 1; k++)
B[k] = B[k + 1];
}
}
}
void searchAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
}
}
int withdraw()
{
float withd;
int accNum,flag=0;
cout << "Enter account number from you want to withdraw:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
flag=0;
cout << "Enter withdraw amount : ";
cin >> withd;
if (B[i].balance >= withd){
B[i].balance = B[i].balance - withd;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int deposit()
{
float dep;
int accNum,flag=0;
cout << "Enter account number you want to Deposit:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
flag=0;
if (B[i].accNumber == accNum)
{
cout << "Enter deposit amount : ";
cin >> dep;
B[i].balance = B[i].balance + dep;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
flag=1;
}
}
if(flag==1){
cout << "Error: No changes should be made to the database ";
return 0;
}
}
int transfer()
{
float tran;
int fromAcc, toAcc, fr, to, i, f1, f2;
cout << "Enter account number you want to transfer from "; cin >> fromAcc;
cout << "to "; cin >> toAcc;
for (i = 0; i<j; i++)
if (B[i].accNumber == fromAcc){
f1 = 1;
fr = i;
}
for (i = 0; i<j; i++)
if (B[i].accNumber == toAcc){
f2 = 1;
to = i;
}
if (f1 == 1 && f2 == 1)
{
cout << "Enter amount to transfer : ";
cin >> tran;
if (B[fr].balance >= tran){
B[to].balance = B[to].balance + tran;
B[fr].balance = B[fr].balance - tran;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
void list()
{
for (int i = 0; i<j; i++)
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
/*Write into file*/
void write_into_file(const char* filename){
int i;
ofstream myfile (filename);
if (myfile.is_open()) {
for(i=0;i<j;i++){
myfile<<B[i].accNumber<<" ";
myfile<<B[i].Fname<<" "; // Saves the line in STRING.
myfile<<B[i].Lname<<" ";
myfile<<B[i].balance<<" ";
}
}
}
int main()
{
int op,i;
char ch;
read_from_file("account.txt");
do{
cout << "-- Operations menu --- ";
cout << " 1. Add an Account "
"2. Delete an Account "
"3. Search for an account by account number and display the account info "
"4. Make a Withdraw "
"5. Make a Deposit "
"6. Transfer money from one account to another "
"7. List all accounts ";
cout << "Choose the operation : ";
cin >> op;
switch (op){
case 1: addAccount();
break;
case 2: deleteAccount();
break;
case 3: searchAccount();
break;
case 4: withdraw();
break;
case 5: deposit();
break;
case 6: transfer();
break;
case 7: list();
break;
default: cout << "Invalid operation ";
exit(0);
}
cout << "Do you wants more operation (Y/y) by default No : ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
/*Writing into File at the End of all operations*/
write_into_file("account.txt");
}
Explanation / Answer
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "header/bank.h"
using namespace std;
int main()
{
Bank * head = NULL;
Bank * temp = new Bank;
bool exit = false;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
while (exit == false)
{
introText();
int response, acc;
double bal, withdrawAmount, depositAmount;
string first_name, last_name, sort;
cout << "Your selection" << " ";
cin >> response;
cout << " ";
switch (response)
{
case 1:
cout << "Enter account number : ";
cin >> acc;
cout << "Enter first name : ";
cin >> first_name;
cout << "Enter last name : ";
cin >> last_name;
cout << "Enter account balance : ";
cin >> bal;
addAcc(acc, first_name, last_name, bal);
break;
case 2:
cout << "Enter account number you wish to delete : ";
cin >> acc;
delAcc(acc);
break;
case 3:
cout << "Enter account number you are looking for : ";
cin >> acc;
searchAcc(acc);
break;
case 4:
cout << "Enter account number you want to withdraw from ";
cin >> acc;
cout << "Enter amount you want to withdraw ";
cin >> withdrawAmount;
withdraw(acc, withdrawAmount);
break;
case 5:
cout << "Enter account number you want to deposit into ";
cin >> acc;
cout << "Enter amount you want to deposit ";
cin >> depositAmount;
deposit(acc, depositAmount);
break;
case 6:
int transFromAcc, transToAcc;
double transferAmount;
cout << "Enter account you want to transfer from ";
cin >> transFromAcc;
cout << "Enter account you want to transfer in ";
cin >> transToAcc;
cout << "Enter amount you want to transfer ";
cin >> transferAmount;
transfer(transFromAcc, transToAcc, transferAmount);
break;
case 7:
listAcc();
break;
case 8:
sort = "account";
sortBy(head, temp, sort);
printList(head);
freeList(head);
break;
case 9:
sort = "balance";
sortBy(head, temp, sort);
printList(head);
freeList(head);
break;
case 10:
listTransactions();
break;
case 11:
cout << "Enter account number to Update: ";
cin >> acc;
updateAcc(acc);
break;
case 12:
cout << "Enter account number : ";
cin >> acc;
accInfo(acc);
break;
case 13:
cout << "Enter account number you want to analyze : ";
cin >> acc;
analyze(acc);
break;
case 14:
exit = true;
break;
default:
cout << "Wrong selection. Response must be between 1 and 14 ";
}
}
return 0;
}
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <ctime>
#include <locale>
#include <fstream>
#include <chrono>
#include "bank.h"
#include "transaction.h"
#pragma warning(disable :4996)
using namespace std;
void introText()
{
cout << " ************************************ ";
cout << "Please make your selections: ";
cout << "1. Add an account ";
cout << "2. Delete an account ";
cout << "3. Search an account ";
cout << "4. Make a withdraw ";
cout << "5. Make a deposit ";
cout << "6. Transfer money ";
cout << "7. List all records (no order required) ";
cout << "8. List all records in the increasing order of account number ";
cout << "9. List all records in the increasing order of account balance ";
cout << "10. List all transactions (no order required) ";
cout << "11. Update customer Information ";
cout << "12. List an account information ";
cout << "13. Analyze an account transaction ";
cout << "14. Exit the program ";
cout << "************************************ ";
}
void addAcc(int accNum, string fname, string lname, double accBal)
{
ifstream fin("bank.txt");
int count = 0, acc;
double bal;
string first_name, last_name;
while (fin >> acc >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
count++;
ofstream temp("temp.txt");
temp << acc << " " << first_name << " " << last_name << " " << bal;
temp.close();
}
}
fin.close();
if (count == 0)
{
ofstream fout("bank.txt", ios::app);
fout << left << setw(10) << accNum << left << setw(12) << fname << " " << left << setw(12) << lname << " " << left << setw(12) << accBal << " ";
cout << " New Account Inserted. ";
fout.close();
ofstream transout("transactions.txt", ios::app);
transout << left << setw(8) << accNum << left << setw(12) << "Deposit" << left << setw(10) << accBal << left << setw(10) << transTime() << " ";
transout.close();
remove("temp.txt");
}
//Same account number found. Print it out.
if (count > 0)
{
ifstream output("temp.txt");
string custInfo;
while (getline(output, custInfo))
{
cout << " " << custInfo << " ";
}
cout << "Same account is found. The account can't be added. ";
output.close();
remove("temp.txt");
}
}
void listAcc()
{
ifstream fin("bank.txt");
string custInfo;
int i = 1, count = 0;
cout << left << setw(10) << "A/C" << left << setw(13) << "First" << left << setw(13) << "Last" << left << setw(12) << "Balance" << " ";
cout << "****************************************** ";
while (getline(fin, custInfo))
{
cout << custInfo << " ";
i++;
count++;
}
if (count == 0)
{
cout << "Your database is empty. Press 1 to add an account.";
}
fin.close();
}
void searchAcc(int acc)
{
ifstream fin("bank.txt");
int accNum, count = 0;
string first_name, last_name;
double bal;
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
ofstream fout("temp.txt");
fout << accNum << " " << first_name << " " << last_name << " " << bal;
count++;
fout.close();
}
}
if (count == 0)
{
cout << " Could not find this account ";
}
fin.close();
ifstream temp("temp.txt");
string custInfo;
while (getline(temp, custInfo))
{
cout << " " << custInfo << " ";
}
temp.close();
remove("temp.txt");
}
void delAcc(int acc)
{
ifstream fin("bank.txt");
ofstream fout("temp.txt");
int accNum, count = 0;
string first_name, last_name;
double bal;
//All accounts that we do not want to delete should be copied into another file.
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc != accNum)
{
fout << left << setw(10) << accNum << left << setw(12) << first_name << " " << left << setw(12) << last_name << " " << left << setw(12) << bal << " ";
}
else
{
count++;
}
}
//Account could not be found.
if (count == 0)
{
cout << " Could not find this account. ";
}
fin.close();
fout.close();
//Rename temp file back to bank.txt and delete temp file. Account deleted.
if (count > 0)
{
ifstream temp("temp.txt");
string custInfo;
temp.close();
remove("bank.txt");
rename("temp.txt", "bank.txt");
cout << " Account deleted.";
}
}
void withdraw(int acc, double withdrawAmount)
{
ifstream fin("bank.txt");
ofstream fout("temp.txt");
int accNum, custNum;
double custAccBal, bal;
string custFirstName, custLastName, first_name, last_name;
int count = 0;
//If account number is found, save the each word/number from that account to a variable.
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
count++;
custNum = acc;
custFirstName = first_name;
custLastName = last_name;
custAccBal = bal;
}
//This will copy all copy all account that does not match account we want to withdraw from into temp file
if (acc != accNum)
{
fout << left << setw(10) << accNum << left << setw(12) << first_name << " " << left << setw(12) << last_name << " " << left << setw(12) << bal << " ";
}
}
fin.close();
fout.close();
if (count > 0)
{
//check if account balance is greater than amount to be withdrawn
if (withdrawAmount > custAccBal)
{
cout << " Not enough money in the account. ";
}
//If amount to be withdrawn is equal to or less than account balance, then withdraw.
if (withdrawAmount <= custAccBal)
{
custAccBal = custAccBal - withdrawAmount;
ofstream fout("temp.txt", ios::app);
fout << left << setw(10) << custNum << left << setw(12) << custFirstName << " " << left << setw(12) << custLastName << " " << left << setw(12) << custAccBal << " ";
remove("bank.txt");
fout.close();
rename("temp.txt", "bank.txt");
cout << " Money was successfully withdrawn.";
ofstream transout("transactions.txt", ios::app);
transout << left << setw(8) << custNum << left << setw(12) << "Withdraw" << left << setw(10) << withdrawAmount << left << setw(10) << transTime() << " ";
transout.close();
}
}
if (count == 0) //Account not found.
{
cout << " This account does not exist. ";
remove("temp.txt");
}
}
void deposit(int acc, double depositAmount)
{
ifstream fin("bank.txt");
ofstream fout("temp.txt");
int accNum, custNum;
double custAccBal, bal;
string custFirstName, custLastname, first_name, last_name;
int count = 0;
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
count++;
custNum = accNum;
custFirstName = first_name;
custLastname = last_name;
custAccBal = bal;
}
else
{
fout << left << setw(10) << accNum << left << setw(12) << first_name << " " << left << setw(12) << last_name << " " << left << setw(12) << bal << " ";
}
}
fin.close();
fout.close();
if ((count > 0) && (depositAmount >= 0))
{
custAccBal = custAccBal + depositAmount;
ofstream fout("temp.txt", ios::app);
fout << left << setw(10) << custNum << left << setw(12) << custFirstName << " " << left << setw(12) << custLastname << " " << left << setw(12) << custAccBal << " ";
remove("bank.txt");
fout.close();
rename("temp.txt", "bank.txt");
cout << " Money was successfully deposited.";
ofstream transout("transactions.txt", ios::app);
transout << left << setw(8) << custNum << left << setw(12) << "Deposit" << left << setw(10) << depositAmount << left << setw(10) << transTime() << " ";
transout.close();
}
//Account not found.
if ((count == 0) || (depositAmount < 0))
{
cout << " This account does not exist, or you are depositing a negative amount ";
remove("temp.txt");
}
}
void transfer(int transFromAcc, int transToAcc, double transferAmount)
{
ifstream fin("bank.txt");
int fromAcc, toAcc, accNum;
double fromBal, toBal, bal;
bool boolfromAcc = false, booltoAcc = false, boolTransBal = false;
string fromFirstName, fromLastName, toFirstName, toLastName, first_name, last_name;
int count = 1;
//By default, this loop will save all accounts that are not part of the transfer process (withdraw and deposit.)
while (fin >> accNum >> first_name >> last_name >> bal)
{
if ((accNum != transFromAcc) && (accNum != transToAcc))
{
ofstream fout("temp.txt", ios::app);
fout << left << setw(10) << accNum << left << setw(12) << first_name << " " << left << setw(12) << last_name << " " << left << setw(12) << bal << " ";
fout.close();
}
//If transferring account is found on file, it's data should be stored in variables. And then return true.
if (accNum == transFromAcc)
{
fromAcc = accNum;
fromFirstName = first_name;
fromLastName = last_name;
fromBal = bal;
boolfromAcc = true;
//If Amount to be transferred is greater than the available balance, then return true.
if (transferAmount <= fromBal)
{
boolTransBal = true;
}
}
//If depositing account is found on file, it's data should be stored in variables. Then return true.
if (accNum == transToAcc)
{
toAcc = accNum;
toFirstName = first_name;
toLastName = last_name;
toBal = bal;
booltoAcc = true;
}
}
fin.close();
if ((boolfromAcc) && (booltoAcc) && (boolTransBal))
{
fromBal = fromBal - transferAmount;
toBal = toBal + transferAmount;
ofstream fout("temp.txt", ios::app);
fout << left << setw(10) << fromAcc << left << setw(12) << fromFirstName << " " << left << setw(12) << fromLastName << " " << left << setw(12) << fromBal << " ";
fout << left << setw(10) << toAcc << left << setw(12) << toFirstName << " " << left << setw(12) << toLastName << " " << left << setw(12) << toBal << " ";
ofstream transout("transactions.txt", ios::app);
string transactionTime = transTime();
transout << left << setw(8) << fromAcc << left << setw(12) << "Withdraw" << left << setw(10) << transferAmount << left << setw(10) << transactionTime << " ";
transout << left << setw(8) << toAcc << left << setw(12) << "Deposit" << left << setw(10) << transferAmount << left << setw(10) << transactionTime << " ";
transout.close();
remove("bank.txt");
fout.close();
rename("temp.txt", "bank.txt");
cout << " Transfer successful. ";
}
if ((boolfromAcc) && (booltoAcc) && (!boolTransBal))
{
cout << " Balance in first account is less than amount you want to transfer ";
remove("temp.txt");
}
if ((!boolfromAcc) || (!booltoAcc))
{
cout << " Either one of the accounts does not exist";
remove("temp.txt");
}
}
void listTransactions()
{
ifstream fin("transactions.txt");
string transactions;
int i = 1;
int count = 0;
while (getline(fin, transactions))
{
cout << i << ". " << transactions << " ";
i++;
count++;
}
if (count == 0)
{
cout << "There are no transactions. Withdraw, Deposit or Transfer Money.";
}
fin.close();
}
string transTime()
{
stringstream timeString;
time_t current_time = time(NULL);
//timeString << put_time(localtime(¤t_time), "%a %d %b %Y %I:%M:%S%p");
string myTime = timeString.str();
return myTime;
}
void printList(Bank * head)
{
Bank* temp = head;
cout << left << setw(10) << "A/C" << left << setw(12) << "First" << left << setw(12) << "Last" << left << setw(12) << "Balance" << " ";
cout << "****************************************** ";
while (temp != NULL)
{
cout << left << setw(10) << temp->account << left << setw(12) << temp->first_name << left << setw(12) << temp->last_name << left << setw(12) << temp->balance << " ";
temp = temp->next;
}
}
void freeList(Bank *& head)
{
Bank * temp = head;
Bank * next;
while (temp != NULL)
{
next = temp->next;
free(temp);
temp = next;
}
head = NULL;
}
void orderByAcc(Bank *& head, int acc, string first_name, string last_name, double bal)
{
Bank *data = new Bank;
data->account = acc;
data->first_name = first_name;
data->last_name = last_name;
data->balance = bal;
data->next = NULL;
if (head == NULL)
{
head = data;
}
else if (data->account <= head->account)
{
data->next = head;
head = data;
}
else
{
Bank * t1 = head;
Bank * t2 = t1->next;
while (t2 != NULL && data->account > t2->account)
{
t1 = t2;
t2 = t2->next;
}
data->next = t2;
t1->next = data;
}
}
void orderByBal(Bank *& head, int acc, string first_name, string last_name, double bal)
{
Bank *data = new Bank;
data->account = acc;
data->first_name = first_name;
data->last_name = last_name;
data->balance = bal;
data->next = NULL;
if (head == NULL)
{
head = data;
}
else if (data->balance <= head->balance)
{
data->next = head;
head = data;
}
else
{
Bank * t1 = head;
Bank * t2 = t1->next;
while (t2 != NULL && data->balance > t2->balance)
{
t1 = t2;
t2 = t2->next;
}
data->next = t2;
t1->next = data;
}
}
void sortBy(Bank *& head, Bank * data, string sort)
{
ifstream fin("bank.txt");
int acc, count = 0; string first_name, last_name; double bal;
while (fin >> acc >> first_name >> last_name >> bal)
{
count++;
if (sort == "account")
orderByAcc(head, acc, first_name, last_name, bal);
else if (sort == "balance")
orderByBal(head, acc, first_name, last_name, bal);
}
if (count == 0)
cout << "Bank Database is empty. ";
fin.close();
}
void updateAcc(int acc)
{
ifstream fin("bank.txt");
ofstream fout("temp.txt");
int accNum, aNum, count = 0;
string fname, first_name, lname, last_name;
double accBal, bal;
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
aNum = accNum;
fname = first_name;
lname = last_name;
accBal = bal;
count++;
}
else
{
fout << accNum << " " << first_name << " " << last_name << " " << bal << " ";
}
}
fin.close();
fout.close();
if (count == 0)
{
cout << " Could not find this account. ";
}
else
{
cout << "The first name of this account holder is : " << fname << " ";
cout << "The last name of this account holder is : " << lname << " ";
string newFirstName;
string newLastName;
cout << "Enter new first name : ";
cin >> newFirstName;
cout << "Enter new last name : ";
cin >> newLastName;
bank_account Customer(aNum, fname, lname, accBal);
Customer.reset_first_name(newFirstName);
Customer.reset_last_name(newLastName);
ofstream fout("temp.txt", ios::app);
fout << Customer.get_id() << " " << Customer.get_first_name() << " " << Customer.get_last_name() << " " << Customer.get_balance() << " ";
fout.close();
remove("bank.txt");
rename("temp.txt", "bank.txt");
cout << " The customer name was successfully updated. ";
}
}
void accInfo(int acc)
{
ifstream fin("bank.txt");
int accNum, custNum = 0;
double custAccBal = 0, bal;
string custFirstName, custLastname, first_name, last_name;
int count = 0;
while (fin >> accNum >> first_name >> last_name >> bal)
{
if (acc == accNum)
{
count++;
custNum = accNum;
custFirstName = first_name;
custLastname = last_name;
custAccBal = bal;
}
}
fin.close();
if (count > 0)
{
bank_account Customer(custNum, custFirstName, custLastname, custAccBal);
cout << " The account information is listed below : ";
cout << "Customer Name : " << Customer.get_first_name() << " " << Customer.get_last_name() << " ";
cout << "Account Balance is : " << Customer.get_balance() << " ";
}
else //Account not found(deleted :)) in bank.txt, but there could be transactions on the account in transactions.txt
cout << " Account deleted/does not exist. But here are their transactions. ";
fin.close();
ifstream ftrans("transactions.txt");
string transType, weekDay, month, time;
int dayOfTrans, year;
double amount;
cout << left << setw(32) << "Date & Time" << "Amount ";
cout << "*************************************** ";
while (ftrans >> accNum >> transType >> amount >> weekDay >> dayOfTrans >> month >> year >> time)
{
if (acc == accNum)
{
string date_and_time = weekDay + " " + to_string(dayOfTrans) + " " + month + " " + to_string(year) + " " + time;
transaction transaction(accNum, transType, amount, date_and_time);
transaction.output();
}
}
ftrans.close();
}
void analyze(int acc)
{
account_analysis AA(acc);
ifstream fin("transactions.txt");
string transType, weekDay, month, time;
int dayOfTrans, year, accNum, count = 0;
double amount;
while (fin >> accNum >> transType >> amount >> weekDay >> dayOfTrans >> month >> year >> time)
{
if (acc == accNum)
{
count++;
string date_and_time = weekDay + " " + to_string(dayOfTrans) + " " + month + " " + to_string(year) + " " + time;
transaction T(accNum, transType, amount, date_and_time);
AA.insert_transaction(T);
}
}
if (count != 0)
{
cout << " The number of transactions is : " << AA.number_transactions() << " ";
cout << "The average deposit is : $" << AA.average_deposit() << " ";
cout << "The maximum deposit is : $" << AA.max_deposit() << " ";
cout << "The Average withdraw is : $" << AA.average_withdraw() << " ";
cout << "The maximum withdraw is : $" << AA.max_withdraw() << " ";
}
else
cout << " Either this account does not exist or has no transaction. ";
fin.close();
}
transaction.cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include "header/transaction.h"
//initialize bankaccount and set Customer_ID to id, first_name to fname, last_name to lname, and balance to b
bank_account::bank_account(int id, string fname, string lname, double b)
{
Customer_ID = id;
first_name = fname;
last_name = lname;
balance = b;
}
bank_account::bank_account()
{
Customer_ID = 9999;
first_name = "anonymous";
last_name = "anonymous";
balance = 0;
}
//rest first_name to s
void bank_account::reset_first_name(string s)
{
first_name = s;
}
//reset last_name to s
void bank_account::reset_last_name(string s)
{
last_name = s;
}
int bank_account::get_id()
{
return Customer_ID;
}
string bank_account::get_first_name()
{
return first_name;
}
//return last name
string bank_account::get_last_name()
{
return last_name;
}
// return balance
double bank_account::get_balance()
{
return balance;
}
transaction::transaction(int act, string type, double a, string day) : account_number(act), transaction_type(type), amount(a), date_and_time(day)
{
}
transaction::transaction()
{
account_number = 9999;
transaction_type = "unknown";
amount = 0;
date_and_time = "unknown";
}
int transaction::get_account_number()
{
return account_number;
}
string transaction::get_transaction_type()
{
return transaction_type;
}
double transaction::get_amount()
{
return amount;
}
string transaction::get_transaction_date()
{
return date_and_time;
}
void transaction::output()
{
if (transaction_type == "Deposit")
{
cout << date_and_time << " " << "+" << amount << " USD ";
}
else if (transaction_type == "Withdraw")
{
cout << date_and_time << " " << "-" << amount << " USD ";
}
}
//constructor
account_analysis::account_analysis(int id)
{
this->id = id;
}
//insert a transaction into the vector
void account_analysis::insert_transaction(transaction t)
{
all_trans.push_back(t);
}
//return the number of transactions in the vector
int account_analysis::number_transactions()
{
return all_trans.size();
}
//return the average amount of deposit
double account_analysis::average_deposit()
{
int i = 0;
double totalDeposit = 0;
for (vector<transaction>::iterator Iter = all_trans.begin(); Iter != all_trans.end(); ++Iter)
{
if (Iter->get_transaction_type() == "Deposit")
{
i++;
totalDeposit += Iter->get_amount();
}
}
if (i == 0)
return 0;
else
return (totalDeposit / static_cast<double>(i));
}
//return the average amount of withdraw
double account_analysis::average_withdraw()
{
int i = 0;
double totalWithdraw = 0;
for (vector<transaction>::iterator Iter = all_trans.begin(); Iter != all_trans.end(); ++Iter)
{
if (Iter->get_transaction_type() == "Withdraw")
{
i++;
totalWithdraw += Iter->get_amount();
}
}
if (i == 0)
return 0;
else
return (totalWithdraw / static_cast<double>(i));
}
//return the maximum amount of deposit
double account_analysis::max_deposit()
{
vector<double> maxDeposit;
for (vector<transaction>::iterator Iter = all_trans.begin(); Iter != all_trans.end(); ++Iter)
{
if (Iter->get_transaction_type() == "Deposit")
{
maxDeposit.push_back(Iter->get_amount());
}
}
if (maxDeposit.empty())
return 0;
else
{
vector<double>::iterator max;
max = max_element(maxDeposit.begin(), maxDeposit.end());
return *max;
}
}
//return the maximum amount of withdraw
double account_analysis::max_withdraw()
{
vector<double> maxWithdraw;
for (vector<transaction>::iterator Iter = all_trans.begin(); Iter != all_trans.end(); ++Iter)
{
if (Iter->get_transaction_type() == "Withdraw")
{
maxWithdraw.push_back(Iter->get_amount());
}
}
if (maxWithdraw.empty())
return 0;
else
{
vector<double>::iterator max;
max = max_element(maxWithdraw.begin(), maxWithdraw.end());
return *max;
}
}
bank.h
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
using namespace std;
struct Bank
{
int account;
string first_name, last_name;
double balance;
Bank * next;
};
void introText();
void addAcc(int, string, string, double);
void delAcc(int);
void searchAcc(int);
void withdraw(int, double);
void deposit(int, double);
void transfer(int, int, double);
void listAcc();
/*This function is used to call orderByAcc or orderByBal depending on what we want to sort account by. */
void sortBy(Bank *& head, Bank * data, string);
void orderByBal(Bank *& head, int, string, string, double);
void orderByAcc(Bank *& head, int, string, string, double);
void freeList(Bank *& head);
void printList(Bank * head);
void listTransactions();
void updateAcc(int);
void accInfo(int);
string transTime();
void analyze(int acc);
transaction.h
//do not make changes to this file
#include <string>
#include <vector>
using namespace std;
class bank_account
{
private:
int Customer_ID;
string first_name;
string last_name;
double balance;
public:
bank_account(int, string, string, double);
bank_account();
void reset_first_name(string);
void reset_last_name(string);
int get_id();
string get_first_name();
string get_last_name();
double get_balance();
};
class transaction
{
private:
int account_number;
string transaction_type;
double amount;
string date_and_time;
public:
transaction(int, string, double, string);
transaction();
int get_account_number();
string get_transaction_type();
double get_amount();
string get_transaction_date();
void output();
};
class account_analysis
{
private:
int id; //account number
vector<transaction> all_trans;
public:
account_analysis(int);
void insert_transaction(transaction);
int number_transactions();
double average_deposit();
double average_withdraw();
double max_deposit();
double max_withdraw();
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.