c++ program. can you please do commenting.
Question 1 [25]. (The Account Class) Design a class named Account that contains (keep the data fields private):
a) An int data field named id for the account.
b) A double data field named balance for the account.
c) A double data field named annualInterestRate that stores the current interest rate.
d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0.
e) The accessor and mutator functions for id, balance, and annualInterestRate.
f) A function named getMonthlyInterestRate() that returns the monthly interest rate.
g) A function named withdraw(amount) that withdraws a specified amount from the account.
h) A function named deposit(amount) that deposits a specified amount to the account.
Implement the class. Write a test program that creates an Account object with an account ID of 1122, a balance of 20000, and an annual interest rate of 4.5%. Use the withdraw function to withdraw $2500, use the deposit function to deposit $3000, and print the balance, the monthly interest.
The below mentioned code works as per the requirement stated in the question.
The code is commented to understand the flow.
#include <iostream>
using namespace std;
// create a class Account
class Account {
private: // private members
int id;
double balance;
double annualInterestRate;
public:
// constructor
Account(int i, double bal, double interest)
{ id = id;
balance = bal;
annualInterestRate = interest;
}
// accessors and mutators
void setAccountId (int x)
{
id = x;
}
int getAccountId()
{
return id;
}
void setBalance(double x)
{
balance = x;
}
double getBalance()
{
return balance;
}
void setInterest(double x)
{
annualInterestRate = x;
}
double getInterest()
{
return annualInterestRate;
}
double getMonthlyInterestRate()
{
double mInterestRate;
mInterestRate = annualInterestRate / 12; // to calculate monthly interest rate
return mInterestRate;
}
bool withdraw(double amount) // to withdraw balance function to deduct balance from current balance
{ if (balance >= amount)
{
balance -= amount;
return true;
}
else
{
return false;
}
}
void deposit(double amount) // to add balance to current balance
{
balance += amount;
}
};
int main()
{
int choice;
double amt;
string flag = "true";
Account ac(1122, 20000, 4.5); // create object of Account
while (!flag.compare("true")){
cout<<"Welcome to banking system: ";
cout << "Enter 1 to make a deposit" << endl;
cout << "Enter 2 to make a withdraw" << endl;
cout << "Enter 3 to check balance" << endl;
cout << "Enter 4 to check monthly interest rate" << endl;
cout << "Enter 5 to display account summary" << endl;
cout << "Enter 0 to exit" << endl;
cin >> choice;
switch (choice) // cases to every option mentioned above
{
case 1:
cout<< "Enter the amount to deposite: ";
cin >> amt;
ac.deposit (amt);
break;
case 2:
cout<< "Enter the amount to withdraw: ";
cin >> amt;
ac.withdraw (amt);
break;
case 3:
cout<< "Your current balance is : " ;
cout<< ac.getBalance() << endl;
break;
case 4:
cout << "Monthly interest rate: " << ac.getMonthlyInterestRate() << endl;
break;
case 5:
cout << "Account ID: " << ac.getAccountId() << endl;
cout << "Current Balance: " << ac.getBalance() << endl;
cout << "Monthly interest rate: " << ac.getMonthlyInterestRate() << endl;
break;
case 0:
exit;
default:
cout << "Invalid option" << endl;
}
cout<< "continue? true/false" <<endl;
cin >> flag; // flag to continue the while loop
}
return 0;
}
Output screenshot:
Get Answers For Free
Most questions answered within 1 hours.