Write a program that defines a class BankAccount and implement it as required. T
ID: 3565970 • Letter: W
Question
Write a program that defines a class BankAccount and implement it as required. The class
BankAccount should consist of two private member variables: balance of type double and
interestRate of type double. The class BankAccount should also include the following
member functions:
1. A default constructor to initialize balance and interestRate to 0.0.
2. A constructor that initializes balance and interestRate with user-specified values.
3. getBalance to return the account balance
4. getRate to return the account interest rate
5. setBalance to set the account balance to user-specified value
6. setRate to set the account interest rate to user-specified value
7. deposit to add the user-specified amount to the account balance
8. withdraw to subtract the user-specified amount from the account balance
9. update to add the interest to the account balance, the interest is calculated using formula,
interest = balance * interest rate%
10. printInfo to print the account balance and interest rate on the screen
In each function, your program should check the validity of data, for example, the balance and
interest rate must be positive numbers, the withdrawal amount should not exceed the current
balance, etc.
Explanation / Answer
//----------------------------------------------
// Bank Account
//----------------------------------------------
#include<iostream>
#include<string>
using namespace std;
//----------------------------------------------
// Class BankAccount
//----------------------------------------------
class BankAccount{
private:
double balance;
double interestRate;
public:
BankAccount(){
balance = 0;
interestRate = 0;
}
BankAccount(double x, double y){
if(x > 0 && y > 0){
balance = x;
interestRate = y;
}else{
cout << "Invalid Input" << endl;
}
}
double getBalance(){
return balance;
}
double getRate(){
return interestRate;
}
void setBalance(double x){
if(x > 0){
balance = x;
}else{
cout << "Invalid Input" << endl;
}
}
void setRate(double x){
if(x > 0){
interestRate = x;
}else{
cout << "Invalid Input" << endl;
}
}
void deposit(double x){
balance += x;
}
void withdraw(double x){
if(balance > x){
balance -= x;
}else{
cout << "Insuficient Balance" << endl;
}
}
void update(){
double interest = balance * (interestRate/100);
balance += interest;
}
void printInfo(){
cout << "Balance = " << balance << endl;
cout << "Interest Rate = " << interestRate << endl;
}
};
int main(){
BankAccount b1;
BankAccount b2(1000, 4);
b1.setBalance(5000);
b1.setRate(4);
cout << b2.getBalance() << endl;
cout << b2.getRate() << endl;
b1.deposit(1000);
b2.withdraw(2000);
b1.update();
b2.update();
b1.printInfo();
b2.printInfo();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.