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.
For example:
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.
Use the class as part of the interactive program provided below.
THERE ARE 3 DOCUMENTS
FILE 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;
}
FILE Account.h:
#pragma once
class Account
{
private:
double balance;
double rate;
public:
Account();
Account(double);//enter balance
double getBalance();
double getRate();
};
FILE Source.cpp:
#include <iostream>
#include "Account.h"
using namespace std;
int menu();
void clearScreen();
void pauseScreen();
int main()
{
int input = 0;
double balance = 0.0, amount = 0.0;
int month = 0;
Account acct(100.00); //create instance of
account
do
{
input = menu();
switch (input)
{
case 1: cout
<< acct.getBalance();
pauseScreen();
break;
case 2: cout
<< acct.getRate();
pauseScreen();
break;
case 3:
cout << "Enter amount :";
cin >> amount;
if(amount < 0.0)
{
cout << "Amount should
be more than 0." << endl;
}
else
{
acct.deposit(amount);
}
break;
case 4:
balance = acct.getBalance();
cout << "Enter amount: ";
cin >> amount;
if(amount > balance || amount < 0.0)
{
cout << "balance is
less than entered amount OR you entered nagative amount." <<
endl;
}
else
{
acct.withdrown(amount);
}
break;
case 5:
cout << "Enter month: " <<
endl;
cin >> month;
if(month < 1)
{
cout << "Month should
be more than 0" << endl;
}
acct.AccrueInterest(month);
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();
}
/*******************************Account.h******************************/
#pragma once
class Account{
private:
double balance;
double rate;
public:
Account();
Account(double);//enter
balance
double getBalance();
double getRate();
void deposit(double);
void withdrown(double);
void AccrueInterest(int);
};
/****************************************Account.cpp*******************************/
#include<iostream>
#include "Account.h"
//constructor
Account::Account()
{
balance = 0;
rate = .05;
}
Account::Account(double startingBalance)
{
balance = startingBalance;
rate = .05;
}
double Account::getBalance()
{
return balance;
}
double Account::getRate(){
return rate;
}
void Account::deposit(double amount){
balance = balance + amount;
}
void Account::withdrown(double amount){
balance = balance - amount;
}
void Account::AccrueInterest(int month){
balance = ((balance * 0.05)*3/12) + balance;
}
/***********************************Source.cpp*************************/
#include <iostream>
#include "Account.h"
#include "Account.cpp"
using namespace std;
int menu();
void clearScreen();
void pauseScreen();
int main()
{
int input = 0;
double balance = 0.0, amount = 0.0;
int month = 0;
Account acct(100.00); //create instance of account
do
{
input = menu();
switch (input)
{
case 1: cout << acct.getBalance();
pauseScreen();
break;
case 2: cout << acct.getRate();
pauseScreen();
break;
case 3:
cout << "Enter amount :";
cin >> amount;
if(amount < 0.0)
{
cout << "Amount should be more than 0." << endl;
}
else
{
acct.deposit(amount);
}
break;
case 4:
balance = acct.getBalance();
cout << "Enter amount: ";
cin >> amount;
if(amount > balance || amount < 0.0)
{
cout << "balance is less than entered amount OR you entered
nagative amount." << endl;
}
else
{
acct.withdrown(amount);
}
break;
case 5:
cout << "Enter month: " << endl;
cin >> month;
if(month < 1)
{
cout << "Month should be more than 0" << endl;
}
acct.AccrueInterest(month);
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();
}
/**********************output*********************************/
Enter the number for th eoperation you wish to perform from the
menu.
1. Check Balance
2. Check Current Rate
3. Deposit to Account
4. Withdraw from Account
5. Accrue Interest
6. Exit program
Enter Choice: 1
100
---Enter to Continue!---
Enter the number for th eoperation you wish to perform from the
menu.
1. Check Balance
2. Check Current Rate
3. Deposit to Account
4. Withdraw from Account
5. Accrue Interest
6. Exit program
Enter Choice: 2
0.05
---Enter to Continue!---
Please let me know if you have any doubt or modify the answer, Thanks :)