Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please do Chapter 15, #14 on page 969 for Starting Out with C++ (8th Edition). 1

ID: 3855564 • Letter: P

Question

Please do Chapter 15, #14 on page 969 for Starting Out with C++ (8th Edition).

14. Bank Accounts

This program should be designed and written by a team of students. Here are some

suggestions:

• One or more students may work on a single class.

• The requirements of the program should be analyzed so each student is given about

the same work load.

• The parameters and return types of each function and class member function should

be decided in advance.

• The program will be best implemented as a multi-file program.

Design a generic class to hold the following information about a bank account:

Balance

Number of deposits this month

Number of withdrawals

Annual interest rate

Monthly service charges

The class should have the following member functions:

Constructor : Accepts arguments for the balance and annual interest rate.

deposit : A virtual function that accepts an argument for the amount of the

deposit. The function should add the argument to the account balance.

It should also increment the variable holding the number of deposits.

withdraw : A virtual function that accepts an argument for the amount of the

withdrawal. The function should subtract the argument from the

balance. It should also increment the variable holding the number of

withdrawals.

calcInt : A virtual function that updates the balance by calculating the monthly

interest earned by the account, and adding this interest to the balance.

This is performed by the following formulas:

Monthly Interest Rate = (Annual Interest Rate / 12)

Monthly Interest = Balance * Monthly Interest Rate

Balance = Balance + Monthly Interest

monthlyProc : A virtual function that subtracts the monthly service charges from the

balance, calls the calcInt function, and then sets the variables that

hold the number of withdrawals, number of deposits, and monthly

service charges to zero.

Next, design a savings account class, derived from the generic account class. The savings

account class should have the following additional member:

status (to represent an active or inactive account)

If the balance of a savings account falls below $25, it becomes inactive. (The status

member could be a flag variable.) No more withdrawals may be made until the balance

is raised above $25, at which time the account becomes active again. The savings

account class should have the following member functions:

withdraw : A function that checks to see if the account is inactive before a withdrawal

is made. (No withdrawal will be allowed if the account is not

active.) A withdrawal is then made by calling the base class version of

the function.

deposit : A function that checks to see if the account is inactive before a deposit

is made. If the account is inactive and the deposit brings the balance

above $25, the account becomes active again. The deposit is then

made by calling the base class version of the function.

monthlyProc : Before the base class function is called, this function checks the number

of withdrawals. If the number of withdrawals for the month is

more than 4, a service charge of $1 for each withdrawal above 4 is

added to the base class variable that holds the monthly service charges.

(Don’t forget to check the account balance after the service charge

is taken. If the balance falls below $25, the account becomes inactive.)

Next, design a checking account class, also derived from the generic account class. It

should have the following member functions:

withdraw : Before the base class function is called, this function will determine if a

withdrawal (a check written) will cause the balance to go below $0. If

the balance goes below $0, a service charge of $15 will be taken from

the account. (The withdrawal will not be made.) If there isn’t enough

in the account to pay the service charge, the balance will become negative

and the customer will owe the negative amount to the bank.

monthlyProc : Before the base class function is called, this function adds the monthly

fee of $5 plus $0.10 per withdrawal (check written) to the base class

variable that holds the monthly service charges.

Write a complete program that demonstrates these classes by asking the user to enter

the amounts of deposits and withdrawals for a savings account and checking account.

The program should display statistics for the month, including beginning balance, total

amount of deposits, total amount of withdrawals, service charges, and ending balance.

Explanation / Answer

#include "savings.h"
#include "checking.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

//Function prototypes.
int printMenu(fstream*, fstream*);
void sWithdraw(Savings*, double, fstream*);
void cWithdraw(Checking*, double, fstream*);
void dispTotal(Savings*, Checking*, fstream*);

//------------------------------------------------------------------------------
// main method - Command line arguments.
int main(int argc, char* argv[])
{
   // If there aren't two file arguments, then exit.
   if(argc < 3)
       exit(1);
   else
   {
       //Create the two file streams.
       fstream inFile, outFile;
      
       //Open the input file.
       inFile.open(argv[1], ios::in);
  
       if(!inFile)
       {
           cout << "InFile not found!";
           exit(1);
       }
      
       //Open the output file.
       outFile.open(argv[2], ios::out);
      
       if(!outFile)
       {
           cout << "OutFile not found!";
           exit(1);
       }


       //If both are opened successfully, then...
       if(inFile && outFile)
       {
           //Create the two accounts and set some variables.
           Checking cAccount;
           Savings sAccount;
           int choice = 0;
           double amount = 0;
          
           //A do-while for the menu.
           do
           {
               //Get the choice of the user input.
               choice = printMenu(&inFile, &outFile);
              
               //Switch statement for the choices.
               switch(choice)
               {
                   //1 & 2 are similar, so I grouped it together.
                   case 1:
                   case 2:
                       outFile << "Enter amount to deposit: ";
                       inFile >> amount;
                       //1 = Savings, 2 = Checking.
                       if(choice == 1)
                           //Deposit to the savings account.
                           sAccount.deposit(amount);
                       else
                           //Deposit to the checking account.
                           cAccount.deposit(amount);
                       break;
                   //Same reasoning as aove.
                   case 3:
                   case 4:
                       outFile << "Enter amount to withdraw: ";
                       inFile >> amount;
                       //3 = savings, 2 = checking.
                       if(choice == 3)
                           //Withdraw from savings.
                           sWithdraw(&sAccount, amount, &outFile);
                       else
                           //Withdraw from checking.
                           cWithdraw(&cAccount, amount, &outFile);
                       break;
                   case 5:
                       //Display the statistics.
                       dispTotal(&sAccount, &cAccount, &outFile);
                       break;
                   case 6:
                       break;
                   default:
                       break;
               }
           }
           while(choice != 6);

           //Close file streams.
           inFile.close();
           outFile.close();  
       }
   }
}


//------------------------------------------------------------------------------
// printMenu(fstream*, fstream*) - Will print out the menu. If not valid choices
// then it will loop until a valid choice is found.
int printMenu(fstream* inFile, fstream* outFile)
{
   *outFile << " ";
   *outFile << "******** BANK ACCOUNT MENU ******** ";
   *outFile << "1. Savings Account Deposit ";
   *outFile << "2. Checking Account Deposit ";
   *outFile << "3. Savings Account Withdrawal ";
   *outFile << "4. Checking Account Withdrawal ";
   *outFile << "5. Update and Display Account Statistics ";
   *outFile << "6. Exit ";
   *outFile << "Your choice, please: (1-6) ";

   int choice;
   *inFile >> choice;

   while(choice > 6 || choice < 1)
   {
       *outFile << "Enter a number from 1 through 6 please: ";
       *inFile >> choice;
   }  

   return choice;
}

//------------------------------------------------------------------------------
// sWithdraw(Savings*, double, fstream*) - This will withdraw from the savings
// account. Since there are 3 possibilities, each one needs a seperate output
// 1 - Withdraw successful. -1 - Inactive account. -2 - Withdraw went through,
// but caused the account to be deactivated.
void sWithdraw(Savings* acc, double amount, fstream* outFile)
{
   int result = acc->withdraw(amount);
   if(result == -1)
       *outFile << "Account is inactive. ";
   else if(result == -2)
   {
       *outFile << " Your account has fallen below $25.00.";  
       *outFile << " It will be deactivated. ";
   }
}

//------------------------------------------------------------------------------
// cWithdraw(Checking*, double, fstream*) - Will withdraw money from the
// checking account. If the result from it is -1, then that means it failed
// and a service fee of 15$ was added.
void cWithdraw(Checking* acc, double amount, fstream* outFile)
{
   int result = acc->withdraw(amount);
   if(result == -1)
   {
       *outFile << "You are attempting to withdraw more ";
       *outFile << "than the account balance. ";
   }
}

//------------------------------------------------------------------------------
// dispTotal(Savings*, Checking*, fstream*) - Will display the statistics
// of both the accounts.
void dispTotal(Savings* sAcc, Checking* cAcc, fstream* outFile)
{
   //Fixed and setprecision for the decimals in balance and service charges.
   *outFile << fixed << setprecision(2) << " ";
   *outFile << "SAVINGS ACCOUNT MONTHLY STATISTICS: ";
   *outFile << "Withdrawals:       " << sAcc->getWithdrawals() << " ";
   *outFile << "Deposits:       " << sAcc->getDeposits() << " ";
   *outFile << "Service Charges:   " << sAcc->monthlyProc() << " ";
   *outFile << " Ending Balance:    " << sAcc->getBalance() << " ";  
   *outFile << " ";
   *outFile << "CHECKING ACCOUNT MONTHLY STATISTICS: ";
   *outFile << "Withdrawals:       " << cAcc->getWithdrawals() << " ";
   *outFile << "Deposits:       " << cAcc->getDeposits() << " ";
   *outFile << "Service Charges:   " << cAcc->monthlyProc() << " ";
   *outFile << " Ending Balance:    " << cAcc->getBalance() << " ";
   *outFile << " ";
}

account.cpp
#include "account.h"


//------------------------------------------------------------------------------
// Constructor (No arguments) - Sets everything to 0. Annual interest rate of
// 5% is the given.
Account::Account()
{
   balance = 0.00;
   deposits = 0;
   withdrawals = 0;
   interestRate = 0.05;
   serviceCharges = 0.00;
}

//------------------------------------------------------------------------------
// Constructor (2 arguments) - This is the same as basic constructor, but
// allows the user to create an Account with specified balance and interest.
Account::Account(double bal, double interest)
{
   balance = bal;
   deposits = 0;
   withdrawals = 0;
   interestRate = interest;
   serviceCharges = 0.00;  
}

//------------------------------------------------------------------------------
// calcInt() - Will calculate the monthly interest and then add it to the
// balance. Monthly interest is annual interets / 12.
void Account::calcInt()
{
   balance += (interestRate/12) * balance;
}

//------------------------------------------------------------------------------
// deposit(double) - This will add the specified amount to the balance, and then
// increment the deposits count.
void Account::deposit(double amount)
{
   balance += amount;
  
   deposits ++;
}

//------------------------------------------------------------------------------
// withdraw(double) - This will subtract the specified amount from the balance,
// and then increment the withdrawals count. Will return 1 if successful.
int Account::withdraw(double amount)
{
   balance -= amount;

   withdrawals ++;

   //Since this is just the parent class, return 1.
   return 1;
}

//------------------------------------------------------------------------------
// monthlyProc() - This will calculate the interest and add it to the balance,
// and then it will take away any service charges. Then, reset everything back
// to zero. It will return the service charges before reset.
double Account::monthlyProc()
{
   calcInt();
   balance -= serviceCharges;
   withdrawals = 0;
   deposits = 0;

   double total = serviceCharges;

   serviceCharges = 0;

   return total;
}

//------------------------------------------------------------------------------
// getWithdrawals() - Getter method for withdrawals.
int Account::getWithdrawals()
{
   return withdrawals;
}


//------------------------------------------------------------------------------
// getDeposits() - Getter method for deposits.
int Account::getDeposits()
{
   return deposits;
}

//------------------------------------------------------------------------------
// getBalance() - Getter method for balance.
double Account::getBalance()
{
   return balance;
}


account.h


#ifndef ACCOUNT_H
#define ACCOUNT_H

class Account
{
protected:
   double balance;
   int deposits;
   int withdrawals;
   double interestRate;
   double serviceCharges;
   virtual void calcInt();
public:
   Account();
   Account(double, double);
   virtual void deposit(double);
   virtual int withdraw(double);
   virtual double monthlyProc();
  
   int getWithdrawals();
   int getDeposits();
   double getBalance();
};

#endif


checking.cpp
#include "checking.h"

//------------------------------------------------------------------------------
// withdraw(double) - First, if the amount to withdraw will cause the balance
// to drop below 0, the withdrawal will not go through. It will return -1 since
// it failed. Also, it will add a 15$ service charge. Otherwise, the withdrawal
// goes through, and withdraws gets incremented.
int Checking::withdraw(double amount)
{
   double newBal = balance - amount;

   //Must make sure it's a valid withdrawal.
   if(newBal < 0)
   {
       serviceCharges -= 15;
       return -1;
   }
   else
   {
       balance = newBal;
       withdrawals ++;
   }
  
   return 1;
}

//------------------------------------------------------------------------------
// deposit(double) - Technically not needed, but this was added since the header
// file contained this method. Simply calls the base method for deposit.
void Checking::deposit(double amount)
{
   Account::deposit(amount);
}

//------------------------------------------------------------------------------
// monthlyProc() - First, it will add a $5 charge, and then 10 cents per every
// withdrawal. After that, it will call the base class function. It will return
// the service charges before it gets reset.
double Checking::monthlyProc()
{
   serviceCharges += 5 + (0.1 * withdrawals);

   //The base class returns the total service charges.
   return Account::monthlyProc();
}


checking.h

#ifndef CHECKING_H
#define CHECKING_H

#include "account.h"

class Checking : public Account
{
public:
   Checking() : Account()
       {}
   Checking(double bal, double interest) : Account(bal, interest)
       {}
   int withdraw(double);
   void deposit(double);
   double monthlyProc();
};

#endif

savings.cpp

#include "savings.h"

//------------------------------------------------------------------------------
// withdraw(double) - This method will return 1, -1, or -2. It will return 1
// if the withdraw goes through. It will return -1 if the account is inactive.
// Lastly, it will return -2 if the withdraw goes through, but now the account is
// inactive.
int Savings::withdraw(double amount)
{
   //First check if it's active to be withdraw first.
   if(status) //If it is, withdraw the amount.
       Account::withdraw(amount);
   else //Else return -1
       return -1;

   //Now, check it again.
   status = balance >= 25;

   //If the status is now inactive, return -2;
   if(!status)
       return -2;

   return 1;
}

//------------------------------------------------------------------------------
// deposit(double) - This will first check if the account is active or not. Then
// it will check if the new balance, of the amount included, will cause the
// account to be active again.

void Savings::deposit(double amount)
{
   //If it's already active, then it won't bother checking.
   if(!status && (balance + amount) >= 25)
       status = true;

   //Deposit the amount.
   Account::deposit(amount);
}

//------------------------------------------------------------------------------
// monthlyProc() - After 4 withdrawals, a charge of $1 per withdrawal is added.
double Savings::monthlyProc()
{
   if(withdrawals > 4)
       serviceCharges += withdrawals;

   //Get the total from the base class method.
   double total = Account::monthlyProc();
  
   //Change the status if it is inactive now.
   status = balance >= 25;  

   //Return the total service chargse.
   return total;
}
savings.h

#ifndef SAVINGS_H
#define SAVINGS_H

#include "account.h"

class Savings : public Account
{
protected:
   bool status;
public:
   //Inline constructor.
   Savings() : Account()
   {
       //Set the status to false since it starts at 0.00 for balance.  
       status = false;
   }
   Savings(double bal, double interest) : Account(bal, interest)
       {}
   int withdraw(double);
   void deposit(double);
   double monthlyProc();
};

#endif