I need help with this My first code kind of works but it isn
I need help with this. My first code kind of works, but it isn\'t showing the balance after each transaction.
Develop a banking program using an Account class. Create SavingsAccount and CheckingAccount objects. For each account, the user to use an amount of money to withdraw from the account using member function, debit, and an amount of money to deposit into the account using member function, credit. As you process each account, determine its type. If an Account is aSavingsAccount, calculate the amount of interest owed to the account using member function, calculateInterest, then add the interest to the account balance using member function, credit. After processing an account, print the updated account balance obtained by invoking base-class member function, getBalance. If an Account is a CheckingAccount, calculate the fees (2%) charged per transaction.
The Account Inheritance Hierarchy is as follows:
Create a base class Account and derived classes, SavingsAccount and CheckingAccount that inherit from class Account. Base class Account should include one data member of type double to represent the account balance. The class should provide at least three member functions:
Member function, credit, should add an amount to the current balance.
Member function, debit, should withdraw money from the account and ensure the debit amount does not exceed the account’s balance. If it does, the balance should be left unchanged and the function should print the message “Debit amount exceeded account balance.”
Member function, getBalance, should return the current balance.
The SavingsAccount should have a data member of type double indicating the interest rate assigned to the account. It also includes a member function, calculateInterest, that returns a double indicating the amount of interest earned by an account, by multiplying the interest rate by the account balance.
The CheckingAccount should have a data member of type constant double indicating the fee charged per transaction. Every time there is a debit or a credit, it should subtract the fee from the account balance. CheckingAccount’s debit function should charge a fee only if money is actually withdrawn (i.e., the debit amount does not exceed the account balance.)
Use constructors to initialize the data members and make sure the initial balance is greater than or equal to zero.
Write a program to test your classes designed above by using the following numbers and transactions:
-500.00
(Yes, the last deposit into savings is a negative amount).
Make sure you display the balance after each transaction.
If there is an error, make sure you state the error and what caused it.
Turn in your code and your output file with the transactions above showing deposits and withdraws including all error messages.
| Checking Starting Balance | 500.00 |
| Savings Starting Balance | 1000.00 |
| Withdraw from Checking | 230.00 |
| Withdraw from Savings | 100.85 |
| Deposit into Checking | 400.25 |
| Deposit into Checking | 250.00 |
| Deposit into Savings | 500.00 |
| Withdraw from Checking | 2000.00 |
| Withdraw from Savings | 1000.00 |
| Withdraw from Savings | 1500.00 |
| Deposit into Savings | -500.00 |
Solution
#include <iostream>
#include \"BankAccountClass.cpp\"
#include <fstream>
#include <new>
using namespace std;
ifstream File(\"Accounts.txt\");
//Operational Functions
void startMenu();
void BeginPrompt(BankAccount account[], int max_accounts, int num_accounts);
int accountNumPrompt();
void error(string msg);
int read_accts(BankAccount account[], int max_accounts);
void print_accts(BankAccount account[], int num_accts);
int find_acct(BankAccount account[], int num_accts, int request_acct);
void withdrawl(BankAccount account[], int num_accts);
void deposit(BankAccount account[], int num_accts);
int newAccount(BankAccount account[], int max_accounts, int num_accts);
void balance(BankAccount account[], int num_accts);
void accountInfo(BankAccount account[], int num_accts);
int closeAccount(BankAccount account[], int max_accounts, int num_accts);
void quit();
int main(){
//Max Number of Back Accounts
const int MAX_NUM = 200;
//Bank Account array
BankAccount account[MAX_NUM];
//amount of accounts
int aAccounts = 0;
//set the amount of accounts to how much is read in from the file
aAccounts = read_accts(account, MAX_NUM);
//print out account information
print_accts(account, aAccounts);
//show start menu
startMenu();
//start program
BeginPrompt(account, MAX_NUM, aAccounts);
return 0;
}
//Main Menu Functions -------------------------------------------------------------------------------------------------
void startMenu(){
//Welcome Screen
cout << endl << \"Welcome User!\" << endl;
}
void BeginPrompt(BankAccount account[], int max_accounts, int num_accounts){
//Allow user to choose which transaction to make
string choice;
cout << endl <<\"Type \'W\' - Widthdrawl\" << endl;
cout << \"Type \'D\' - Deposit\" << endl;
cout << \"Type \'N\' - New Account\" << endl;
cout << \"Type \'B\' - Balance\" << endl;
cout << \"Type \'I\' - Account Info\" << endl;
cout << \"Type \'C\' - Close Account\" << endl;
cout << \"Type \'Q\' - Quit\" << endl;
cin >> choice;
if(choice == \"W\"){
withdrawl(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"D\"){
deposit(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"N\"){
num_accounts = newAccount(account, max_accounts, num_accounts);
print_accts(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"B\"){
balance(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"I\"){
accountInfo(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"C\"){
num_accounts = closeAccount(account, max_accounts, num_accounts);
print_accts(account, num_accounts);
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}else if(choice == \"Q\"){
quit();
}else{
cout << endl << choice << \" is not a valid choice.\" << endl;
//Restart Transaction Prompt
BeginPrompt(account, max_accounts, num_accounts);
}
}
int accountNumPrompt(){
int accountnumber = 0;
cout << endl << \"Please enter the account number\" << endl;
cin >> accountnumber;
return accountnumber;
}
//This function is to allow for a standard way of showing there was an error.
void error(string msg){
//More noticible for users to see the Error message.
cout << endl << \"---------------------------\" << endl;
cout << endl << \"ERROR: \" << msg << endl;
cout << endl << \"---------------------------\" << endl;
}
//Account Functions --------------------------------------------------------------------------------------------------------
int read_accts(BankAccount account[], int max_accounts){
//start counter
int count = 0;
//temporary variables
string fname, lname, type;
int SSN;
double acctbalance;
//count how moany entries are in the file.
while(!File.eof() && count < max_accounts){
File >> fname >> lname >> SSN >> type >> acctbalance;
count++;
}
//loop back to top of file.
File.clear();
File.seekg(0, ios::beg);
//counter for our current account
int i = 0;
//create pointer for changing the values at the direct address in memory
BankAccount * b;
//loop trhough file and save and set values
while(!File.eof() && i < count){
//set our current account into our pointer variable
b = &account[i];
//define our file format
File >> fname >> lname >> SSN >> type >> acctbalance;
//set all of our values
b->setFirstName(fname);
b->setLastName(lname);
b->setSSN(SSN);
b->setAccountNumber(i);
b->setAccountType(type);
b->setAccountBalance(acctbalance);
i++;
}
return count;
}
void print_accts(BankAccount account[], int num_accts){
//create pointer variable
BankAccount * b;
for(int i=0; i<num_accts; i++){
//stores our current account
b = &account[i];
cout << \"name: \" << b->getFirstName() << \" \" << b->getLastName() << endl;
cout << \"\\t\\tAccount Number: \" << b->getAccountNumber() << endl;
cout << \"\\t\\tAccount Type: \" << b->getAccountType() << endl;
cout << \"\\t\\tAccount Balance: \" << b->getAccountBalance() << endl;
}
}
int find_acct(BankAccount account[], int num_accts, int request_acct){
//creats pointer variable
BankAccount * b;
int num = 0;
//bool for telling when we have found the right account
bool found = false;
for(int i=0; i<num_accts; i++){
//store current account
b = &account[i];
if(b->getAccountNumber() == request_acct){
//store account number when it matches the requested number
num = i;
found = true;
break;
}
}
if(found){
return num;
}else{
return -1;
}
}
void withdrawl(BankAccount account[], int num_accts){
int accountnum = 0;
//prompt the user for an account number
accountnum = accountNumPrompt();
//validate that account number
accountnum = find_acct(account, num_accts, accountnum);
if(accountnum == -1){
//give error if its not found
string message = \"We couldn\'t find an account by that number\";
error(message);
withdrawl(account, num_accts);
}else{
//keep asking until you get a valid amount
BankAccount * b = &account[accountnum];
while(true){
double howmuch = 0.00;
cout << endl << \"How much do you want to withdraw?\" << endl;
cin >> howmuch;
if(b->withdraw(howmuch)){
cout << endl << howmuch << \" has been withdrawn from account: \" << b->getAccountNumber() << endl;
break;
}else{
cout << \"Acount \" << b->getAccountNumber() << \" does not have sufficient funds.\" << endl;
}
}
}
}
void deposit(BankAccount account[], int num_accts){
int accountnum = 0;
accountnum = accountNumPrompt();
accountnum = find_acct(account, num_accts, accountnum);
if(accountnum == -1){
string message = \"We couldn\'t find an account by that number\";
error(message);
deposit(account, num_accts);
}else{
BankAccount * b = &account[accountnum];
double howmuch = 0.00;
cout << endl << \"How much do you want to deposit?\" << endl;
cin >> howmuch;
b->addAccountBalance(howmuch);
cout << endl << howmuch << \" has been deposited int account: \" << b->getAccountNumber() << endl;
}
}
int newAccount(BankAccount account[], int max_accounts, int num_accts){
BankAccount * b;
//make sure there is space left
if(num_accts+1 <= max_accounts){
b = &account[num_accts];
string fname, lname, type;
int SSN;
double acctbalance;
cout << endl << \"Please enter first name: \";
cin >> fname;
cout << endl << \"Please enter last name: \";
cin >> lname;
cout << endl << \"Please enter SSN (Social Security Number): \";
cin >> SSN;
cout << endl << \"Please enter Account Type: \";
cin >> type;
cout << endl << \"Please enter Account starting Balance: \";
cin >> acctbalance;
b->setFirstName(fname);
b->setLastName(lname);
b->setSSN(SSN);
//increment the account number
int greatest = 0;
BankAccount * g;
for(int i=0; i < num_accts; i++){
g = &account[i];
if(g->getAccountNumber() > greatest){
greatest = g->getAccountNumber();
}
}
greatest++;
b->setAccountNumber(greatest);
b->setAccountType(type);
b->setAccountBalance(acctbalance);
num_accts++;
return num_accts;
}
}
void balance(BankAccount account[], int num_accts){
int accountnum = 0;
accountnum = accountNumPrompt();
accountnum = find_acct(account, num_accts, accountnum);
if(accountnum == -1){
string message = \"We couldn\'t find an account by that number\";
error(message);
balance(account, num_accts);
}else{
double howmuch = 0.00;
cout << endl << \"Here is your balance: $\" << account[accountnum].getAccountBalance() << endl;
}
}
void accountInfo(BankAccount account[], int num_accts){
int accountnum = 0;
accountnum = accountNumPrompt();
accountnum = find_acct(account, num_accts, accountnum);
if(accountnum == -1){
string message = \"We couldn\'t find an account by that number\";
error(message);
accountInfo(account, num_accts);
}else{
cout << endl << \"Here is your Account Info.\" << endl << endl;
cout << \"name: \" << account[accountnum].getFirstName() << \" \" << account[accountnum].getLastName() << endl ;
cout << \"\\t\\tAccount Number: \" << account[accountnum].getAccountNumber() << endl;
cout << \"\\t\\tAccount Type: \" << account[accountnum].getAccountType() << endl;
cout << \"\\t\\tAccount Balance: \" << account[accountnum].getAccountBalance() << endl;
}
}
int closeAccount(BankAccount account[], int max_accounts, int num_accts){
int accountnum = 0;
accountnum = accountNumPrompt();
accountnum = find_acct(account, num_accts, accountnum);
if(accountnum == -1){
string message = \"We couldn\'t find an account by that number\";
error(message);
closeAccount(account, max_accounts, num_accts);
}else{
if(num_accts-1 >= 0){
BankAccount * b = &account[accountnum];
//delete all values
b->setFirstName(\"\");
b->setLastName(\"\");
b->setSSN(0);
b->setAccountNumber(0);
b->setAccountType(\"\");
b->setAccountBalance(0);
int i=accountnum;
int j = 0;
// make sure there is no empty spaces in the account array
while(i+1 < num_accts){
j = i+1;
b = &account[i];
b->setFirstName(account[j].getFirstName());
b->setLastName( account[j].getLastName());
b->setSSN(account[j].getSSN());
b->setAccountNumber(account[j].getAccountNumber());
b->setAccountType(account[j].getAccountType());
b->setAccountBalance(account[j].getAccountBalance());
i++;
}
num_accts--;
return num_accts;
}
}
}
void quit(){
cout << endl << \"Program is now ending!\" << endl;
}










