In: Computer Science
You work for a bank that has a program with a dependency of the account class. Unfortunately the hard drive that contained the source code for the account class went bad and no backup can be found. Obviously, this means you bank needs to address this issue. However, your task is to recreate the account class. The good news is that your company was able to locate a tester of the account class and one helper function to output the class. Using these two files, you should be able to re-engineer account.h and account.cpp to work as originally designed.
Implement the account class.
main.cpp:
#include <iostream> #include "account.h" #include "account_output.h" using namespace std; using namespace DS; int main() { cout << "Account Tester" << std::endl; //Create an account with 3.0% interest rate account savings(0.03); //Deposit one dollar savings.deposit(1.00); //Account should have exactly $1 in it cout << savings << endl; //Move up a week savings.advanceDay(7); cout << savings << endl; //Deposit $450.55 savings.deposit(450.55); cout << savings << endl; //Move up 23 days, should see our first compound //However, the average balance not 451.55, since the balance was $1 for 7 of the 30 days savings.advanceDay(23); cout << savings << endl; //Advance a bunch with balance unchanged, two compoudings happen savings.advanceDay(65); cout << savings << endl; //Deposit more savings.deposit(1000); cout << savings << endl; //Advance a bunch with balance unchanged, at least two more interest calculations savings.advanceDay(65); cout << savings << endl; //Will not mutate object, amount must be > 0 savings.deposit(-1000); cout << savings << endl; //Will not mutate object, amount must be > 0 savings.withdraw(-1000); cout << savings << endl; //Will not mutate object, amount must < balance savings.withdraw(-10000); cout << savings << endl; //lower balance by 50 savings.withdraw(50); cout << savings << endl; savings.advanceDay(30); cout << savings << endl; return 0; }
account_output.h: Overload of the << operator to display the balance and week number.
#ifndef PROJECT_SAVINGS_ACCOUNT_OUTPUT_H #define PROJECT_SAVINGS_ACCOUNT_OUTPUT_H #include <ostream> #include <iomanip> namespace DS { //Precondition: None //Postcondition: Output to stream, in the format of // day: DAYNUM, balance: $x.xx std::ostream &operator<<(std::ostream &, const account &); std::ostream &operator<<(std::ostream &os, const account &account) { os << "day: " << account.getDayNumber() << ", balance: $" << std::fixed << std::setprecision(2) << account.getBalance(); return os; } } #endif
Due 09/17/2019 11:59pm
Class declarion in account.h
//account.h
#ifndef account_h
#define account_h
#include<iostream>
#include <ostream>
#include <iomanip>
using namespace std;
class account
{
private:
int dayNumber;
double balance;
public:
account(double);
void withdraw(double);
void deposit(double);
void advanceDay(int);
int getDayNumber();
double getBalance();
friend ostream &operator<<(ostream &,
const account &);
};
#endif account_h
---------------------------------------------------------------------------------
class implementation file, account.cpp
//account.cpp
#include<iostream>
#include"account.h"
using namespace std;
//constructor to set balance
account::account(double bal)
{
//set balance and dayNumber to 1
balance=bal;
dayNumber=1;
}
//withdraw method
void account::withdraw(double amt)
{
if(balance>amt)
balance=balance-amt;
}
//deposit method
void account::deposit(double amt)
{
balance=balance+amt;
}
//Method to advance the dayNumber with integer,num
void account::advanceDay(int num)
{
dayNumber=dayNumber+num;
}
//Return dayNumber value
int account::getDayNumber()
{
return dayNumber;
}
//Return balance
double account::getBalance()
{
return balance;
}
//Overloaded extraction << operator to print dayNumber and
balnace with two decimal places
ostream &operator<<(ostream &os, const account
&account)
{
os << "day: "
<< account.dayNumber
<< ", balance: $"
<< std::fixed
<< std::setprecision(2)
<< account.balance;
return os;
}
---------------------------------------------------------------------------------
//Tester program for account.h class
//main.cpp:
#include <iostream>
#include "account.h"
using namespace std;
int main()
{
cout << "Account Tester" <<
std::endl;
//Create an account with 3.0% interest rate
account savings(0.03);
//Deposit one dollar
savings.deposit(1.00);
//Account should have exactly $1 in it
cout << savings << endl;
//Move up a week
savings.advanceDay(7);
cout << savings << endl;
//Deposit $450.55
savings.deposit(450.55);
cout << savings << endl;
//Move up 23 days, should see our first
compound
//However, the average balance not 451.55, since the
balance was $1 for 7 of the 30 days
savings.advanceDay(23);
cout << savings << endl;
//Advance a bunch with balance unchanged, two
compoudings happen
savings.advanceDay(65);
cout << savings << endl;
//Deposit more
savings.deposit(1000);
cout << savings << endl;
//Advance a bunch with balance unchanged, at least
two more interest calculations
savings.advanceDay(65);
cout << savings << endl;
//Will not mutate object, amount must be >
0
savings.deposit(-1000);
cout << savings << endl;
//Will not mutate object, amount must be >
0
savings.withdraw(-1000);
cout << savings << endl;
//Will not mutate object, amount must <
balance
savings.withdraw(-10000);
cout << savings << endl;
//lower balance by 50
savings.withdraw(50);
cout << savings << endl;
savings.advanceDay(30);
cout << savings << endl;
system("pause");
return 0;
}
---------------------------------------------------------------------------------
Sample Output: