In: Computer Science
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero).The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to be withdrawn than what is available in the account, nor should you allow for negative deposits.Finally, there should be a function that adds interest to the balance (interest accrual) at the current interest rate. This function should have a parameter indicating how many months’ worth of interest are to be added (for example, 6 indicate the account should have 6 months’ added). Interest is accrued from an annual rate. The month is representative of 1/12 that amount. Each month should be calculated and added to the total separately.
1 month accrued at 5% APR is Balance = ((Balance * 0.05)/12) + Balance;
For 3 months the calculations you repeat the statement above 3 times.
//account.cpp
#include "Account.h"
//constructor
Account::Account()
{
balance = 0;
rate = .05;
}
Account::Account(double startingBalance)
{
balance = startingBalance;
rate = .05;
}
double Account::getBalance()
{
return balance;
}
-------------------------------------------
//Account.h
#pragma once
class Account
{
private:
double balance;
double rate;
public:
Account();
Account(double);//enter balance
double getBalance();
double getRate();
};
--------------------------------------------
//source.cpp
#include "Account.h"
#include <iostream>
using namespace std;
int menu();
void clearScreen();
void pauseScreen();
int main()
{
int input = 0;
Account acct(100.00); //create instance of account
do
{
input = menu();
switch (input)
{
case 1: cout << acct.getBalance();
pauseScreen();
break;
case 2: //ToDo
break;
case 3: //ToDo
break;
case 4: //ToDo
break;
case 5: //ToDo
break;
case 6: //ToDo
cout << "Goodbye";
pauseScreen();
}
clearScreen();
} while (input != 6);
}
int menu()
{
int input;
cout << "Enter the number for th eoperation you wish to perform from the
menu." << endl
<< "1. Check Balance" << endl
<< "2. Check Current Rate" << endl
<< "3. Deposit to Account" << endl
<< "4. Withdraw from Account" << endl
<< "5. Accrue Interest" << endl
<< "6. Exit program" << endl << endl;
cout << "Enter Choice: ";
cin >> input;
while (input < 1 or input > 6)
{
cout << "Enter a valid Choice from the menu: ";
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(1000, '\n');
}
cin >> input;
}
return input;
}
void clearScreen()
{
system("CLS");
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(1000, '\n');
}
}
void pauseScreen()
{
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\n---Enter to Continue!---\n";
std::cin.get();
}
//Test program for Account class
//source.cpp
#include "Account.h"
#include <iostream>
using namespace std;
//function prototypes
int menu();
void clearScreen();
void pauseScreen();
int main()
{
int input = 0;
int numMonths=0;
double amount=0;
//create instance of account
Account acct(100.00);
do
{
//Get user choice and store in
input value
input = menu();
//switch case to match the
appropriate user input choice
switch (input)
{
case 1:
//print current balance
cout << "Current balance :
$"<<acct.getBalance()<<endl;
pauseScreen();
break;
case 2:
//print current rate
cout << "Current rate :
"<<acct.getRate()<<endl;
pauseScreen();
break;
case
3:
//Ask to enter amount to deposit
cout<<"Enter amount ,$ to deposit:
";
cin>>amount;
acct.deposit(amount);
break;
case 4:
//Ask amount to withdraw from account,acct
object
cout<<"Enter amount ,$ to withdraw:
";
cin>>amount;
acct.withdraw(amount);
break;
case 5:
//Ask number of months to add interest
amount
cout<<"Enter number of months to accure
interest :";
cin>>numMonths;
acct.addInterest(numMonths);
cout << "Current balance :
$"<<acct.getBalance()<<endl;
pauseScreen();
break;
case 6:
//print message before exit from program
cout << "Goodbye";
pauseScreen();
break;
}
clearScreen();
} while (input != 6);
}
//Menu function to prompt user to get the menu choice
int menu()
{
int input;
cout << "Enter the number for th eoperation you
wish to perform from the menu." << endl;
cout <<"1. Check Balance" << endl;
cout <<"2. Check Current Rate" <<
endl;
cout <<"3. Deposit to Account" <<
endl;
cout << "4. Withdraw from Account" <<
endl;
cout << "5. Accrue Interest" <<
endl;
cout << "6. Exit program" << endl <<
endl;
cout << "Enter Choice: ";
cin >> input;
while (input < 1 || input > 6)
{
cout << "Enter a valid Choice
from the menu: ";
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(1000, '\n');
}
cin >> input;
}
return input;
}
//Function to clear the screen on each menu display
void clearScreen()
{
system("CLS");
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(1000, '\n');
}
}
//Function to pause the program output on console and
continue
//upon user input any key
void pauseScreen()
{
std::cin.clear();
std::cin.ignore(1000, '\n');
std::cout << "\n---Enter to
Continue!---\n";
std::cin.get();
}
----------------------------------------*----------------------------------------*----------------------------------------
//Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
private:
double balance;
double rate;
public:
Account();
Account(double);//enter balance
double getBalance();
double getRate();
//Adding method declaraions
void deposit(double );
void withdraw(double);
void addInterest(int months);
};
#endif ACCOUNT_H
----------------------------------------*----------------------------------------*----------------------------------------
//Implemenation file
//Account.cpp
#include<iostream>
#include "Account.h"
using namespace std;
//constructor
Account::Account()
{
balance = 0;
rate = .05;
}
/*Parameter constructor to set the starting balance*/
Account::Account(double startingBalance)
{
balance = startingBalance;
rate = .05;
}
/*Method to get balance*/
double Account::getBalance()
{
return balance;
}
/*Method to get rate */
double Account::getRate()
{
return rate;
}
/*Method to add amount to balance */
void Account::deposit(double amount )
{
balance=balance+amount;
}
/*Method to withdraw funds from account balance*/
void Account::withdraw(double amount)
{
if(balance>amount)
balance=balance-amount;
else
cout<<"No sufficient
funds"<<endl;
}
/*Method to add the interest amount to balance*/
void Account::addInterest(int months)
{
for(int month=1;month<months;month++)
balance =balance+ ((balance *
0.05)/12) ;
}
----------------------------------------*----------------------------------------*----------------------------------------
Sample Output: