For this lab you must write a complete class in C++. This will be a class named
ID: 652801 • Letter: F
Question
For this lab you must write a complete class in C++. This will be a class named BankAccount. This class must have the following private variables:
1. accountHolderName : string
2. balance : double
3. interestRate: double
This class must have the following public constructor:
1. BancAccount(name : string, balance : double, rate : double)
This class must have the following public member functions:
1. getAccountHolderName() : string a. returns the account holders name
2. getBalance() : double a. returns the current balance
3. getInterestRate() : double a. returns the current interest rate
4. deposit(amount : double) : void a. adds the amount to the balance
5. withdraw(amount : double) : void a. subtracts the amount from the balance
6. applyInterest() : void a. adds interest (balance * interestRate) to the balance
You must write the complete and correct BankAccount.h file that will contain the class declaration.
You must write the complete and correct BankAccount.cpp file that will contain the implementation code.
The following .cpp code is given:
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
#include "BankAccount.h"
void displayMenu()
{
cout << "-------------------------" << endl;
cout << "1) Deposit" << endl;
cout << "2) Withdraw" << endl;
cout << "3) Aplpy Interest" << endl;
cout << "4) Display Balance" << endl;
cout << "5) Exit" << endl;
cout << endl;
}
int getUsersMenuChoice()
{
int usersMenuChoice = 0;
do
{
cin.clear(); cin.sync();
cout << "---> ";
cin >> usersMenuChoice;
} while (usersMenuChoice < 1 || usersMenuChoice > 5);
return usersMenuChoice;
}
double getDouble(string prompt)
{
double ammount = -1;
do
{
cin.clear(); cin.sync();
cout << prompt;
cin >> ammount;
} while (ammount < 0);
return ammount;
}
BankAccount createNewAccount()
{
string name;
double balance, interestRate, amount;
cout << "Enter account holder's name : ";
getline(cin, name);
// get initial balance
balance = getDouble("Enter Initial balance :");
// get account interest rate
interestRate = getDouble("Enter interest rate (0.0 - 1.0) : ");
// create the bank account object
BankAccount account(name, balance, interestRate);
return account;
}
int main()
{
const int DEPOSIT = 1;
const int WITHDRAW = 2;
const int APPLY_INTERSET = 3;
const int DISPLAY_BALANCE = 4;
const int EXIT = 5;
int usersMenuChoice = 0;
double amount;
BankAccount account = createNewAccount();
do
{
displayMenu();
usersMenuChoice = getUsersMenuChoice();
switch (usersMenuChoice)
{
case DEPOSIT:
amount = getDouble("Enter amount : ");
account.deposit(amount);
break;
case WITHDRAW:
amount = getDouble("Enter amount : ");
account.withdraw(amount);
break;
case APPLY_INTERSET:
account.applyInterest();
break;
case DISPLAY_BALANCE:
cout << "Balance is: " << account.getBalance() << endl;
break;
case EXIT:
cout << "Bye bye!!!" << endl;
break;
default:
cout << "WAT!?!?!" << endl;
break;
}
} while (usersMenuChoice != EXIT);
cin.sync(); cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.