In: Computer Science
1. Design a class called BankAccount. The member
fields of the class are: Account Name, Account Number and Account
Balance. There are also other variables called MIN_BALANCE=9.99,
REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04. They look like
constants, but for now, they are variables of type double
Here is the UML for the class:
BankAccount
-string accountName // First and Last name of Account holder
-int accountNumber // integer
-double accountBalance // current balance amount
+
BankAccount()
//default constructor that sets name to “”, account number to 0 and
balance to 0
+BankAccount(string accountName, int accountNumber, double
accountBalance) // regular constructor
+getAccountBalance(): double // returns the balance
+getAccountName: string // returns name
+getAccountNumber: int
+setAccountBalance(double amount) : void
+withdraw(double amount) : bool //deducts from balance and returns
true if resulting balance is less than minimum balance
+deposit(double amount): void //adds amount to balance. If amount
is greater than rewards amount, calls
// addReward method
-addReward(double amount) void // adds rewards rate * amount to
balance
+toString(): String // return the account information
as a string with three lines. “Account Name: “ name
“Account Number:” number
“Account Balance:” balance
2. Create a file called BankAccount.cpp which implements the BankAccount class as given in the UML diagram above. The class will have member variables( attributes/data) and instance methods(behaviours/functions that initialize, access and process data)
3. Create a driver class to do the following:
a. Declare and instantiate a bank account called
accountZero using the default constructor
b. Declare and instantiate a bank account called
accountOne with name= “Matilda Patel” number =1232,
balance=-4.00
c. Declare and instantiate a bank account called
accountTwo with name = “Fernando Diaz”, number=1234,
balance=250
d. Declare and instantiate a bank account called
accountThree with name=”Howard Chen”, number=1236, balance =
194.56
e. Display the bank accounts in the three line format
as above
f. Deposit 999 dollars in Fernando’s account and
1000.25 in Howards account
g. Display their account information
h. Withdraw 10000 from Matildas account and 90 dollars
from Feranandos account
i. Display the results. If withdrawal is not possible
your program should say “Insufficient funds” otherwise it should
say “Remaining Balance :” balance amount
j. Print the total amount of all the bank accounts
created.
Thanks for the question.
Here is the completed code for this problem.
Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
Notes : Refer the screenshot for the files you need to create, there will be in total 3 files for this project.
If you are satisfied with the solution, please rate the answer.
Thanks
===========================================================================
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using std::string;
class BankAccount
{
public:
BankAccount();
BankAccount(string,int,double);
double getAccountBalance()
const;
string getAccountName()
const;
int getAccountNumber() const;
void
setAccountBalance(double);
bool withdraw(double);
void deposit(double);
void addReward(double);
string toString() const;
static double rate;
private:
string accountName;
int accountNumber;
double accountBalance;
};
#endif
================================================================
#include "BankAccount.h"
#include <string>
#include <sstream>
using namespace std;
double BankAccount::rate =0.0;
BankAccount::BankAccount():accountName(""),accountNumber(0),accountBalance(0.0){
}
BankAccount::BankAccount(string name,int number ,double
balance):
accountName(name),accountNumber(number),
accountBalance(balance){
}
double BankAccount::getAccountBalance() const{
return accountBalance;
}
string BankAccount::getAccountName() const{
return accountName;
}
int BankAccount::getAccountNumber() const{
return accountNumber;
}
void BankAccount::setAccountBalance(double balance){
accountBalance=balance;
}
bool BankAccount::withdraw(double amount){
if(amount>accountBalance){
return false;
}
accountBalance-=amount;
return true;
}
void BankAccount::deposit(double amount){
accountBalance+=amount;
}
void BankAccount::addReward(double amount){
accountBalance+=amount*rate;
}
string BankAccount::toString() const{
string accountNum ,accountBal;
stringstream ss;
ss<<accountNumber;
ss>>accountNum;
ss.clear();
ss<<accountBalance;
ss>>accountBal;
string desc = "Account Name: "+accountName + ",
Acccount Number: "+ accountNum +
", Account Balance: $"+accountBal;
return desc;
}
================================================================
#include <iostream>
#include "BankAccount.h"
using namespace std;
int main(){
//a. Declare and instantiate a bank account called
accountZero using the default constructor
BankAccount account = BankAccount();
cout<<account.toString()<<endl;
//b. Declare and instantiate a bank account called
accountOne with name= “Matilda Patel” number =1232,
balance=-4.00
BankAccount mat = BankAccount("Matilda
Patel",1232,-4.00);
//c. Declare and instantiate a bank account called
accountTwo with name = “Fernando Diaz”, number=1234,
balance=250
BankAccount fer = BankAccount("Fernando
Diaz",1234,250);
// d. Declare and instantiate a bank account called
accountThree with name=”Howard Chen”, number=1236, balance =
194.56
BankAccount how = BankAccount("Howard
Chen",1236,194.56);
//e. Display the bank accounts in the three line
format as above
cout<<mat.toString()<<endl;
cout<<fer.toString()<<endl;
cout<<how.toString()<<endl;
//f. Deposit 999 dollars in Fernando’s account and
1000.25 in Howards accoun
fer.deposit(999);
how.deposit(1000.25);
//g. Display their account information
cout<<mat.toString()<<endl;
cout<<fer.toString()<<endl;
cout<<how.toString()<<endl;
//h. Withdraw 10000 from Matildas account and 90
dollars from Feranandos account
bool flag= mat.withdraw(10000);
if(!flag){
cout<<"Insufficient
funds.\n";
}else{
cout<<mat.toString()<<endl;
}
flag = fer.withdraw(90);
if(!flag){
cout<<"Insufficient funds.\n";
}else{
cout<<fer.toString()<<endl;
}
//j. Print the total amount of all the bank accounts
created.
double total =
mat.getAccountBalance()+fer.getAccountBalance()+how.getAccountBalance();
cout<<"Total Balance:
$"<<total<<endl;
}
================================================================