In: Computer Science
1. Design a class, Account, that models the basic
workings of a bank account. The class should perform the following
tasks:
a. Save the account balance.
b. Save the number of transactions performed on the
account.
c. Allow deposits to be made to the account.
d. Allow withdrawals to be taken from the
account.
e. Calculate interest for the period.
f. Report the current account balance at any
time.
g. Report the current number of transactions at any
time
The private member variables needed by the class are:
- balance (a double to hold the current account
balance)
- interestRate (a double that holds the interest rate
for the period)
- interest (a double that holds the interest earned for
the current period)
- transactions (an integer that holds the current
number of transactions)
The public member functions needed are:
- constructor to initially store balance (0) and
interest rate(0.045 or 4.5%)
- setInterestRate()
- makeDeposit()
- withDraw()
- calcInterest()
- getInterestRate()
- getBalance()
- getInterest()
- getTransactions()
#include<iostream>
#include<iomanip>
using namespace std;
//account class
class Account
{ //data member declarations
private:
double
balance,interestRate,interest;
static int transactions;
//member functions
public:
Account() ;
void setInterestRate();
void makeDeposit();
void withDraw();
void calcInterest();
double getInterestRate();
double getBalance();
double getInterest();
int getTransactions();
};
//constructor
Account :: Account()
{
balance=0; //set balance to 0
interestRate= 4.5; //set interest rate to
4.5
}
//setInterestRate() method
void Account :: setInterestRate()
{
cout<<endl<<"Enter the Rate of
Interest";
cin>>interestRate; //set the interest rate
}
//method to deposit
void Account :: makeDeposit()
{
double d;
cout<<endl<<"Enter the amount to
deposit";
cin>>d; //Inout teh amount too deposit
if(d<0) //validate the amount
cout<<endl<<"Invalid amount for
deposit";
else
balance = balance +d; //update the balance
transactions++; //update the transactions
}
//method to withdraw
void Account :: withDraw()
{
double d;
cout<<endl<<"Enter the amount
for withdraw";
cin>>d;//input the balance
if(d<0) //validate the balance
cout<<endl<<"Invalid
Amount";
else
if(d>balance) //check for
sufficient balanc3e or not
cout<<endl<<"Insufficient balance";
else
balance = balance-d; //update the
balance
transactions++;//update the
transactions
}
//method to calculate the interest
void Account :: calcInterest()
{
interest = (balance *
interestRate)/100; //compute the interest
balance = balance + interest;
//update the balance
transactions++;//update the
transactions
}
//return the interest rate
double Account :: getInterestRate()
{
return interestRate;
}
//return the balance
double Account :: getBalance()
{
return balance;
}
//return the interest amount
double Account :: getInterest()
{
return interest;
}
//return the number of transactions
int Account :: getTransactions()
{
return transactions;
}
//defination of static variable
int Account::transactions;
//driver program
int main()
{
Account aobj; //creat object
int opt;
//infinite loop
while(1)
{
//display the
menu
cout<<endl<<"1. Set InterestRate\n2. Make Deposit\n3.
Withdraw\n4. Interest Calculation\n5. Print InterestRate\n6. Print
Balance\n7. Print Total Interest\n 8. Print Number of
transactions\n 0. Exit";
cout<<endl<<"Enter choice";
cin>>opt;//ask for choice
//perform the
operation according to the choice given by user
if(opt==1)
aobj.setInterestRate();
else
if(opt==2)
aobj.makeDeposit();
else
if(opt==3)
aobj.withDraw();
else
if(opt==4)
aobj.calcInterest();
else
if(opt==5)
cout<<endl<<"Rate of
Interest :
"<<fixed<<setprecision(2)<<aobj.getInterestRate();
else
if(opt==6)
cout<<endl<<"Total Balance :
$"<<fixed<<setprecision(2)<<aobj.getBalance();
else
if(opt==7)
cout<<endl<<"Total Interest :
$"<<fixed<<setprecision(2)<<aobj.getInterest();
else
if(opt==8)
cout<<endl<<"Number of transactions performed :
"<<aobj.getTransactions();
else
if(opt==0)
exit(0);
}
}
OUTPUT
NOTE: NUMBER OF TRANSACTION INCLUDES ONLY FOR WITHDRAW,DEPOSIT AND CALCULATION OF INTEREST. IF YOU WANT TO INCLUDE FOR ALLTHE METHODS THEN INFORM. I WILL UPDATE.