Create a Class Account that manage the transactions, the withdraw and making dep
ID: 3707006 • Letter: C
Question
Create a Class Account that manage the transactions, the withdraw and making deposit operations.
The class should include a constructor, a setter to set the interest rate, a method to make deposit, a method to calculate the interest, a method to check if possible to withdraw and the getters.
There should be a seperation between the specification (Account.h) and implementation ( Account.cpp)
// Header file
#ifndef Account_h
#define Account_h
class Account
{
private:
double balance, interestRate;
public:
Account();
Account(double b, double rate);
double getBalance();
double getInterestRate();
void setInterestRate();
void deposit(double depositAmount);
double calcInterest();
bool checkWithdraw(double withdrawalAmount);
void withdraw(double withdrawAmount);
};
#endif /* Account_h */
Explanation / Answer
Account.cpp
#include <iostream>
using namespace std;
#include "Account.h"
Account::Account()
{
balance = 0.0;
interestRate = 0.0;
}
Account::Account(double b, double rate)
{
balance = b;
interestRate = rate;
}
double Account::getBalance()
{
return balance;
}
double Account::getInterestRate()
{
return interestRate;
}
void Account::setInterestRate(double rate)
{
interestRate = rate;
}
void Account::deposit(double depositAmount)
{
balance += depositAmount;
}
double Account::calcInterest()
{
return (balance*interestRate);
}
bool Account::checkWithdraw(double withdrawalAmount)
{
if(balance > withdrawalAmount)
return true;
else
return false;
}
void Account::withdraw(double withdrawAmount)
{
balance -= withdrawAmount;
}
main.cpp
int main() {
Account acct(2330.60,0.02);
cout<<"Current balance :$ "<<acct.getBalance();
acct.deposit(345.67);
cout<<" Current balance after deposit 345.67:$ "<<acct.getBalance();
if(acct.checkWithdraw(2500.00))
acct.withdraw(2500.00);
cout<<" Current balance after withdrawal of 2500.00:$ "<<acct.getBalance();
return 0;
}
Output:
Current balance :$ 2330.6
Current balance after deposit 345.67:$ 2676.27
Current balance after withdrawal of 2500.00:$ 176.27
Do ask if any doubt. Please upvote.