File for the Account class. #include \"Account.h\" bool Account::withdraw(double
ID: 3771745 • Letter: F
Question
File for the Account class.
#include "Account.h"
bool Account::withdraw(double amount)
{
if (balance < amount)
return false; // Not enough in the account
else
{
balance -= amount;
transactions++;
return true;
}
}
//*********************************************************************************************************
class Account // Account.h
{
private:
double balance; // Account balance
double interestRate; // Interest rate for the period
double interest; // Interest earned for the period
int transactions; // Number of transactions
public:
Account(double iRate = 0.045, double bal = 0)
{ balance = bal;
interestRate = iRate;
interest = 0;
transactions = 0; }
void setInterestRate(double iRate)
{ interestRate = iRate; }
void makeDeposit(double amount)
{ balance += amount; transactions++; }
bool withdraw(double amount); // Defined in Account.cpp
void calcInterest( )
{ interest = balance * interestRate; balance += interest; }
double getInterestRate( ) const
{ return interestRate; }
double getBalance( ) const
{ return balance; }
double getInterest( ) const
{ return interest; }
int getTransactions( ) const
{ return transactions; }
};
Make the following changes to the Account class:
1. Add an array or vector to hold a sequence of transaction messages for the bank account. For each transaction, add an appropriate string message to this array or vector.
2. Add a string label (accountName) to the Account class to help identify the name of the account on the statement.
3. Write a member function called getStatement() that creates an account statement. The statement should include the account name along with the sequence of recent transaction messages. Ideally this method should return a string version of the statement to the calling program (i.e., that code prints the statement). If that’s not clear, it will also be acceptable to print the statement directly from this member function.
4. Account for the following types of transactions: New Account, initial deposit, deposit, withdrawal, interest payment.
5. Add a method called clearStatement() to reset or clear the statement.
Include a screenshot showing your Account class tested with the following program:
Create an Account called Mary Smith Savings, with an initial deposit of $10,000.
Transactions: withdraw $420, deposit $125, withdraw $215, interest payment at 1.1%
Print the statement Clear the Statement Transactions: withdraw $1894, deposit $825, interest payment at 1.1%
Print the statement
Expected Results
---------------- --------[Begin Statement]--------------------
Account: Mary Smith Savings New Account: Mary Smith Savings
Initial deposit of $10000.00
Withdrawal of $420.00
Deposit of $125.00
Withdrawal of $215.00
Interest payment at 1.10% of $104.39
--------
Ending balance: $9594.39
Number of transactions: 3
--------[End Statement]----------------------
--------[Begin Statement]--------------------
Account: Mary Smith Savings
Initial balance of $9594.39
Withdrawal of $1894.00
Deposit of $825.00
Interest payment at 1.10% of $93.78
--------
Ending balance: $8619.17
Number of transactions: 2
--------[End Statement]--------------------
Explanation / Answer
/*
* Account.cpp
*
* Created on: 14-Dec-2015
* Author: Ravi
*/
#include "Account.h"
#include <iostream>
#include<sstream>
using namespace std;
string Account::toString(double val) {
std::stringstream sstm;
sstm << val;
return sstm.str();
}
bool Account::withdraw(double amount) {
if (balance < amount)
return false; // Not enough in the account
else {
history.push_back("Withdrawl of $"+toString(amount));
balance -= amount;
transactions++;
return true;
}
}
string Account::getStatement() {
history.push_back("Ending balance: $"+toString(balance));
history.push_back("Number of transaction: "+toString(transactions));
int i;
string statement = "Account:" + accountName + " Savings ";
for (i = 0; i < history.size(); i++) {
statement += history[i] + " ";
}
return statement;
}
void Account::clearStatement() {
history.clear();
history.push_back("Initial balance of $"+toString(balance));
}
int main() {
Account acc = Account("Mary Smith",1.1, 10000);
acc.withdraw(420);
acc.makeDeposit(125);
acc.withdraw(215);
acc.calcInterest();
cout << "-------------------[Begin Statement]--------------" <<endl;
cout << acc.getStatement() << endl;
cout << "-------------------[End Statement]----------------" <<endl;
acc.clearStatement();
return 0;
}
/*
* Account.h
*
* Created on: 14-Dec-2015
* Author: Ravi
*/
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <vector>
#include<string>
using namespace std;
class Account // Account.h
{
private:
vector<string> history; //to store transaction history
double balance; // Account balance
double interestRate; // Interest rate for the period
double interest; // Interest earned for the period
int transactions; // Number of transactions
string accountName;
string toString(double val);
public:
Account(double iRate = 0.045, double bal = 0) {
balance = bal;
interestRate = iRate;
interest = 0;
transactions = 0;
history= vector<string>();
history.push_back(
" New Account: Iniital deposit of $"
+toString( balance));
}
Account(string accname, double iRate = 0.045, double bal = 0) {
balance = bal;
interestRate = iRate;
interest = 0;
transactions = 0;
accountName = accname;
history= vector<string>();
history.push_back(
" New Account: " + accname
+ " Iniital deposit of $" + toString( balance));
}
void setInterestRate(double iRate) {
interestRate = iRate;
}
void makeDeposit(double amount) {
balance += amount;
transactions++;
history.push_back("Deposit of $"+toString(amount));
}
bool withdraw(double amount); // Defined in Account.cpp
void calcInterest() {
interest = balance * interestRate;
balance += interest;
history.push_back("Interest payment at "+toString(interestRate)+"% of $"+toString(interest));
}
double getInterestRate() const {
return interestRate;
}
double getBalance() const {
return balance;
}
double getInterest() const {
return interest;
}
int getTransactions() const {
return transactions;
}
string getAccountName() const {
return accountName;
}
// void setAccountName(string name) {
// accountName = name;
// }
string getStatement();
void clearStatement();
};
#endif /* ACCOUNT_H_ */
--output---
-------------------[Begin Statement]--------------
Account:Mary Smith Savings
New Account: Mary Smith
Iniital deposit of $10000
Withdrawl of $420
Deposit of $125
Withdrawl of $215
Interest payment at 1.1% of $10439
Ending balance: $19929
Number of transaction: 3
-------------------[End Statement]----------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.