Write a program that maintains the monthly balance of a bank account. This accou
ID: 3772902 • Letter: W
Question
Write a program that maintains the monthly balance of a bank account. This account can have one trasaction a month, either withdrawal or a deposit. The program will also keep track of the interest that is earned each month. Interest is computed at the b eginning of the month, before the deposit or withdrawal is made. The program will read from the keyboard the name of the account holder, the starting balance of the bank account and the interest rate. The program will continiu computing interest and asking for either a deposit amount or a withdrawal amount until the user indicates he or she wants to quit the program. Make sure the account does not have a negative balance. If a withdrawal gives a negative balance state that fact and end the program (Use two tests in the loop). Create a menu prompt: it will have 3 choices - deposit, withdrawal, or end program.
Explanation / Answer
Program:
#include <iostream>
#include <string>
using namespace std;
//BankAccount class declarations
class BankAccount
{
//data members
private:
string customerName;
string customerID;
double customerBalance;
double interestrate;
public:
//constructor and methods are declared as public
BankAccount(string, string, double, double);
string getCustomerName();
string getCustomerID();
double getCustomerBalance();
double getinterestrate();
void withdraw(double amount);
void deposit(double amount);
};
//parameterized constructor BankAccount() that takes two string
//parametes and one double parameter. These are used to initialize the
//variables
BankAccount::BankAccount(string name, string ID, double balance,double rate)
{
customerName = name;
customerID = ID;
customerBalance = balance;
interestrate=rate;
}
//getCustomerName(): returns the customer's name
string BankAccount::getCustomerName()
{
return customerName;
}
//getCustomerID(): returns the customer's ID
string BankAccount::getCustomerID()
{
return customerID;
}
//getCustomerBalance(): returns the customer's account balance
double BankAccount::getCustomerBalance()
{
return customerBalance;
}
//getCustomerBalance(): returns the customer's account balance
double BankAccount::getinterestrate()
{
return interestrate;
}
//withdraw() takes double as parameter and deducts the parameter
//amount from the current balance
void BankAccount::withdraw(double amount)
{
customerBalance -=amount;
}
//deposit() takes double as parameter and adds the parameter
//amount from the current balance
void BankAccount::deposit(double amount)
{
customerBalance +=amount;
}
//main function
int main()
{
//declare the string, character, integer and double variables
string name, id;
double amount,rate;
char conti;
int choice;
//display the header
cout<<"Welcome to XXXX Bank"<<endl<<endl;
//prompt the user for input details:
cout<<"Customer please enter the prompted details: "<<endl;
cout<<"Customer's Name: ";
cin>>name;
cout<<"Customer's Account ID: ";
cin>>id;
cout<<"Initial balance to deposit: $";
cin>>amount;
if(amount<0)
{
cout<<"Error.Initial balance to deposit: $";
cin>>amount;
}
cout<<"Enter rate of interest:$";
cin>>rate;
amount+= amount*rate/100;
//create an object of the BankAccount class
BankAccount bacc(name, id, amount,rate);
cout<<endl;
//loop to display menu and perform the operation as per
//user request
do
{
//menu
cout<<"Transactions menu"<<endl;
cout<<"1. Deposit"<<endl;
cout<<"2. Withdraw"<<endl;
cout<<"3. Print Details"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Enter your choice of operation: ";
cin>>choice;
//depending on the user input choice value
//switch to respective operation
switch(choice)
{
//prompts the user for the amount to deposit and
//calls the deposit method by passing user amount.
//Prints the current balance
case 1:
cout<<"Enter the amount to deposit: $";
cin>>amount;
bacc.deposit(amount);
cout<<"Amount of $"<<amount<<" is deposited successfully!"<<endl;
cout<<"Current balance: $"<<bacc.getCustomerBalance()<<endl<<endl;
break;
//prompts the user for the amount to withdraw and
//calls the withdraw method by passing user amount.
//Prints the current balance.
case 2:
cout<<"Enter the amount to withdraw: $";
cin>>amount;
bacc.withdraw(amount);
cout<<"Amount of $"<<amount<<" is withdrawn successfully!"<<endl;
cout<<"Current balance: $"<<bacc.getCustomerBalance()<<endl<<endl;
break;
//prints all the customer details along with the current balance.
case 3:
cout<<"Your account details are: "<<endl;
cout<<"Customer's Name: "<<bacc.getCustomerName()<<endl;
cout<<"Customer's Account ID: "<<bacc.getCustomerID()<<endl;
cout<<"Account Balance: $"<<bacc.getCustomerBalance()<<endl<<endl;
break;
case 4:
exit(0);
default:
cout<<"Please enter valid operation number."<<endl<<endl;
}
cout<<"Would you like perform another transaction? ('y' /'Y')";
cin>>conti;
}while(conti=='y' || conti=='Y');
system("pause");
return 0;
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.