Lab1 SavingsBalance

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


 


 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值