Question

In: Computer Science

You work for a bank that has a program with a dependency of the account class....

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

Solutions

Expert Solution


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:


Related Solutions

(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Write a Java program for a simple bank account. You shall define a Customer class: A...
Write a Java program for a simple bank account. You shall define a Customer class: A customer has a first name, last name, and social security number. The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9. If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created. You shall define a...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and...
JAVA PROGRAM: Creates a Bank Account class with one checking account and methods to withdraw and deposit. Test the methods in the main function.
c++ The above Account class was created to model a bank account. An account has the...
c++ The above Account class was created to model a bank account. An account has the properties (keep the properties private) account number, balance, and annual interest rate, date created, and functions to deposit and withdraw. Create two derived classes for checking and saving accounts. A checking account has an overdraft limit, but a savings account cannot be overdrawn. Define a constant virtual toString() function in the Account class and override it in the derived classes to return the account...
Write a program to create a bank account and to process transactions. Call this class bankAccount...
Write a program to create a bank account and to process transactions. Call this class bankAccount A bank account can only be given an initial balance when it is instantiated. By default, a new bank account should have a balance of 0. A bank account should have a public get method, but no public set method. A bank account should have a process method with a double parameter to perform deposits and withdrawals. A negative parameter represents a withdrawal. It...
1.   Design a class, Account, that models the basic workings of a bank account. The class...
1.   Design a class, Account, that models the basic workings of a bank account. The class should perform the following tasks: a.   Save the account balance. b.   Save the number of transactions performed on the account. c.   Allow deposits to be made to the account. d.   Allow withdrawals to be taken from the account. e.   Calculate interest for the period. f.   Report the current account balance at any time. g.   Report the current number of transactions at any time The...
Write in Java the following: A.  A bank account class that has three protected attributes: account number...
Write in Java the following: A.  A bank account class that has three protected attributes: account number (string), customer name (string), and balance (float). The class also has a parameterized constructor and a method public void withDraw (float amount) which subtracts the amount provided as a parameter from the balance. B. A saving account class that is a child class of the bank account class with a private attribute: penality rate (float). This class also has a parameterized constructor and a...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and...
Object Design Example: Bank Account Object Implement the bank account class that stores account number and the balance. It has methods to deposit and withdraw money. In addition, the default constructor sets account number to 0000 and balance 0. Other constructor should take the account number and the initial balance as parameters. Finally, the account class should have a static data member that stores how many accounts are created for a given application. a) Draw the UML diagram b) Write...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has...
C++ exercise ! Change the WEEK‐4 program to work through the GradeBook class. This program has some functionality that you can use with the Gradebook class. So, please revise Gradebook class so that the class can use sort() and display() functions of WEEK4 program . week-4 program : #include #include using namespace std; void sort(char nm[][10]); void display(char name[][10]); int main() { char names[10][10]; int i; for (i=0; i<10; i++) { cout << "Enter name of the student : ";...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT