In: Computer Science
Accounting Program in c++
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.
If you have any problem with the code feel free to comment
Program
#include <iostream>
#include <stdlib.h>
using namespace std;
class BankAccount{
private:
//instances of the class
float balance, interestRate;
public:
//default constructor
BankAccount(){
balance = 0;
interestRate = 0;
}
//getters and setters
void setBalance(float balance){
this->balance = balance;
}
float getBalance(){
return balance;
}
void setInterestRate(float interestRate){
this->interestRate = interestRate;
}
float getInterestRate(){
return interestRate;
}
//for performing deposit function
void deposit(float amount){
//deposit only when amount is more than 0
//else keep the balance unchanged
if(amount > 0)
balance += amount;
}
//for performing withdraw function
void withdraw(float amount){
//withdraw only when the amount is less than the balance
//else keep the balance unchanged
if(amount < balance)
balance -= amount;
}
//calculating the interest and adding it to the balance
void addInterest(int months){
interestRate = interestRate/100;
for(int i =0; i<months; i++){
balance = ((balance * interestRate)/12) + balance;
}
}
};
int main(){
BankAccount obj;
cout<<"Initial Balance:
$"<<obj.getBalance()<<endl;
cout<<"Initial Interest Rate:
"<<obj.getInterestRate()<<"%"<<endl;
//setting balance and interest rate
obj.setBalance(2000);
obj.setInterestRate(5);
cout<<"\nAfter Setting the Balance and Interest
Rate"<<endl;
cout<<"Balance: $"<<obj.getBalance()<<endl;
cout<<"Interest Rate:
"<<obj.getInterestRate()<<"%"<<endl;
//depositing
obj.deposit(1500);
cout<<"\nBalance after deposit:
$"<<obj.getBalance()<<endl;
//withdrawing
obj.withdraw(1000);
cout<<"\nBalance after withdraw:
$"<<obj.getBalance()<<endl;
//after interest added
obj.addInterest(6);
cout<<"\nBalance after interest added:
$"<<obj.getBalance()<<endl;
}
Output