C++ problem:
You are writing some simple code to model operations in a bank. For simplicity, all operations are in pennies. The bank has two different types of accounts: simple accounts and interest accounts.
A SimpleAccount has these behaviors:
create an account, with the given customer name and with the initial deposit amount
deposit – deposit some amount and return new balance
withdraw – withdraw some amount and return amount withdrawn. If insufficient balance, whole balance is withdrawn.
assessMonthlyFee – reduce balance by the given fee amount and return the fee collected. If insufficient balance, whole balance is collected.
getBalance – return the current balance
getName – return the account owner
An InterestAccount is like a simple account but the rules are a bit different. The monthly fee is waived if the balance is 100000 or more and the account accrues interest at the current monthly interest rate. The account has these new behaviors:
setMonthlyRate – set the monthly interest rate to the given rate
accrueInterest – add the accrued interest to the current balance and return the interest paid. E.g.: if rate is 2% and the balance is 200, the balance becomes 204.
a) Please give the interface and implementation of the SimpleAccount and InterestAccount classes. (10 points)
b) Please implement a function named payInterest that takes a vector of InterestAccounts and pays the monthly accrual of interest to each account. The function returns the total interest paid. (5 points)
c) Please implement a function named collectFee that takes a pointer to an account and a fee amount and assesses the given monthly fee on that account. It returns the fee collected. The function should work correctly for both types of accounts. (5 points)
Expert Answer
Here is the code for the question. main.cpp shows the usage of the classes. Sample output also shown. Please don’t forget to rate the answer if it helped. Thank you very much.
compilation : g++ account.cpp main.cpp -o main
execute: ./main
account.h
#include <iostream>
using namespace std;
class SimpleAccount
{
protected:
string customerName;
double balance;
public:
SimpleAccount(string name, double bal);
double deposit(double amount);
double withdraw(double amount);
// virtual function
virtual double assessMonthlyFee(double fee);
double getBalance();
string getName();
void display();
};
class InterestAccount : public SimpleAccount
{
private:
double monthlyRate;
public:
InterestAccount(string name, double bal, double rate):SimpleAccount(name, bal){monthlyRate = rate;}
void setMonthlyRate(double rate);
double accrueInterest();
double assessMonthlyFee(double fee);
};
account.cpp
#include “account.h”
#include <iostream>
using namespace std;
SimpleAccount::SimpleAccount(string name, double bal)
{
customerName = name;
balance = bal;
}
double SimpleAccount::deposit(double amount)
{
balance += amount;
return balance;
}
double SimpleAccount::withdraw(double amount)
{
double draw = 0;
if(balance != 0)
{
if(amount > balance)
{
draw = balance;
balance = 0;
}
else
{
draw = amount;
balance -= draw;
}
}
return draw;
}
double SimpleAccount::getBalance()
{
return balance;
}
string SimpleAccount::getName()
{
return customerName;
}
double SimpleAccount::assessMonthlyFee(double fee)
{
double amt = 0;
if(fee < balance)
{
amt = fee;
balance -= fee;
}
else
{
amt = balance;
balance = 0;
}
return amt;
}
void SimpleAccount::display()
{
cout<<“Name: “<<customerName << ” Balance: $”<< balance<<endl;
}
//===============
void InterestAccount::setMonthlyRate(double rate)
{
monthlyRate = rate;
}
double InterestAccount::accrueInterest()
{
double interest = balance * monthlyRate / 100;
balance += interest;
return interest;
}
double InterestAccount::assessMonthlyFee(double fee)
{
if(balance >= 100000)
return 0;
else
return SimpleAccount::assessMonthlyFee(fee);
}
main.cpp
#include “account.h”
#include <iostream>
#include <vector>
using namespace std;
//accounts is vector of InterestAccount passed by reference. pays the monthly interest
//returns the total interest paid to all accounts
double payInterest(vector<InterestAccount> &accounts)
{
double total = 0;
for(int i = 0 ; i < accounts.size(); ++i)
{
total += accounts[i].accrueInterest();
}
return total;
}
double collectFee(SimpleAccount *account, double fee)
{
return account->assessMonthlyFee(fee);
}
//function to display the vector of interest accounts
void displayAccounts(vector<InterestAccount> &accounts)
{
for(int i = 0 ; i < accounts.size() ; i ++)
accounts[i].display();
}
int main()
{
//create simple and interest accounts
SimpleAccount acc1(“Alice”,30), acc2(“Jane”,2000);
//create a vector of interest accounts
vector<InterestAccount> intAccounts;
intAccounts.push_back(InterestAccount(“John”, 100000, 4.5));
intAccounts.push_back(InterestAccount(“Bob”,50000, 3.0));
cout<<“The accounts created are “<<endl;
acc1.display();
acc2.display();
displayAccounts(intAccounts);
//pay interest to interest accounts
double amount = payInterest(intAccounts);
cout<<“nTotal Amount paid in interest is “<<amount<<endl;
cout<<“Now interest accounts are “<<endl;
displayAccounts(intAccounts);
//collect fees
double fee = 50;
cout<<“nCollecting fees from all accounts”<<endl;
amount = 0;
amount += collectFee(&acc1, fee);
amount += collectFee(&acc2, fee);
for(int i = 0; i < intAccounts.size(); ++i)
{
amount += collectFee(&intAccounts[i], fee);
}
cout<<“nTotal fee collected is $”<<amount<<endl;
acc1.display();
acc2.display();
displayAccounts(intAccounts);
}
output
he accounts created are
Name: Alice Balance: $30
Name: Jane Balance: $2000
Name: John Balance: $100000
Name: Bob Balance: $50000
Total Amount paid in interest is 6000
Now interest accounts are
Name: John Balance: $104500
Name: Bob Balance: $51500
Collecting fees from all accounts
Total fee collected is $130
Name: Alice Balance: $0
Name: Jane Balance: $1950
Name: John Balance: $104500
Name: Bob Balance: $51450