44.7k views
4 votes
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 savingsBalance 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.

1 Answer

2 votes

Answer:

Step-by-step explanation:

#include <iostream>

#include <iomanip>

using namespace std;

#include "SavingsAccount.h"

double SavingsAccount::annualInterestRate = 0.0;

SavingsAccount::SavingsAccount( double bal, double AiR )

{

savingsBalance = ( bal >= 0.0 ? bal : 0.0 );

setAnnualInterestRate( AiR ); //AiR = AnnualInterestRate

}

void SavingsAccount::setAnnualInterestRate( double AiR)

{

annualInterestRate = ( AiR >= 0.0 && AiR <= 1.0) ? AiR : .03;

}

double SavingsAccount::getAnnualInterestRate()

{

return annualInterestRate;

}

void SavingsAccount::calculateMonthlyInterest()

{

savingsBalance += savingsBalance * ( annualInterestRate / 12);

}

void SavingsAccount::modifyInterestRate( double interest )

{

annualInterestRate = (interest >= 0.0 && interest <= 1.0) ? interest : .04;

}

void &SavingsAccount::print() const

{

cout << fixed << setprecision(2) << "$" << savingsBalance;

}

int main()

{

SavingsAccount saver1( 2000.0, .03 );

SavingsAccount saver2( 3000.0, .03 );

cout << "Initial Balance For saver1 Is: " << saver1.print;

cout << "\\Initial Balance For saver2 Is: " << saver2.print << "\\\\";

cout << "Interest Rate For Both Accounts is " << saver1.getAnnualInterestRate << "%";

saver1.calculateMonthlyInterest();

saver2.calculateMonthlyInterest();

cout << "Balance After 3% interest For saver1: " << saver1.print;

cout << "\\Balance After 3% interest For saver2: " << saver2.print;

cout << "\\\\Setting Annual Interest Rate to 4%";

saver1.modifyInterestRate( .04 );

saver2.modifyInterestRate( .04 );

saver1.calculateMonthlyInterest();

saver2.calculateMonthlyInterest();

cout << "\\Balance After 4% Interest For saver1: " << saver1.print;

cout << "\\Balance After 4% Interest For saver2: " << saver2.print;

cout << endl;

return 0;

}

User Sentence
by
5.1k points