Excercise 10.8
Create a SavingsAccount class. Use a static data member annualInterestRate to store the annual interest rate for each of the savers. Each member of the class contains a private data member savingsBalance indicating the amount the saver currently has on deposit. Provide member function calculateMonthlyInterest that calculates the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate that sets the static annualInterestRate to a new value. Write a driver program to test class SavingsAccount. Instantiate two different objects of class SavingsAccount, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set the annualInterestRate to 3 percent. Then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 4 percent, calculate the next month's interest and print the new balances for each of the savers.
Solution
Header file: SavingsAccount.h
#ifndef SAVINGS_ACCOUNT_H
#define SAVINGS_ACCOUNT_H
class SavingsAccount
{
public:
// constructor sets balance to value greater than or equal to zero
SavingsAccount( double b )
{
savingsBalance = ( b >= 0.0 ? b : 0.0 );
} // end SavingsAccount constructor
void calculateMonthlyInterest(); // calculate interest; add to balance
static void modifyInterestRate( double );
void printBalance() const;
private:
double savingsBalance; // the account balance
static double annualInterestRate; // the interest rate of all accounts
}; // end class SavingsAccount
#endif
Source code file: SavingsAccount.cpp
#include <iostream>
using std::cout;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include "SavingsAccount.h" // SavingsAccount class definition
// initialize static data member
double SavingsAccount::annualInterestRate = 0.0;
// calculate monthly interest for this savings account
void SavingsAccount::calculateMonthlyInterest()
{
savingsBalance += savingsBalance * ( annualInterestRate / 12.0 );
} // end function calculateMonthlyInterest
// function for modifying static member variable annualInterestRate
void SavingsAccount::modifyInterestRate( double i )
{
annualInterestRate = ( i >= 0.0 && i <= 1.0 ) ? i : 0.03;
} // end function modifyInterestRate
// prints balance of the savings account
void SavingsAccount::printBalance() const
{
cout << fixed << '$' << setprecision( 2 ) << savingsBalance;
} // end function printBalance
driver program for test: test.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include "SavingsAccount.h" // SavingsAccount class definition
int main()
{
SavingsAccount saver1( 2000.0 );
SavingsAccount saver2( 3000.0 );
SavingsAccount::modifyInterestRate( .03 ); // change interest rate
cout << "Initial balances:\nSaver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << "\n\nBalances after 1 month's interest applied at .03:\n"
<< "Saver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();
SavingsAccount::modifyInterestRate( .04 ); // change interest rate
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
cout << "\n\nBalances after 1 month's interest applied at .04:\n"
<< "Saver 1: ";
saver1.printBalance();
cout << "\tSaver 2: ";
saver2.printBalance();
cout << endl;
return 0;
} // end main