C++ Programming: Program Design Including Data Structures, 7th Edition Chapter 1
ID: 665163 • Letter: C
Question
C++ Programming: Program Design Including Data Structures, 7th Edition
Chapter 12
PE #5
C++, using Visual Studio
Banks offer various types of accounts, such as savings, checking, certificate of deposits, and money market, to attract customers as well as meet their specific needs. Two of the most commonly used accounts are savings and checking. Each of these accounts has various options. For example, you may have a savings account that requires no minimum balance but has a lower interest rate. Similarly, you may have a checking account that limits the number of checks you may write. Another type of account that is used to save money for the long term is certificate of deposit (CD). In this programming exercise, you use abstract classes and pure virtual functions to design classes to manipulate various types of accounts. For simplicity, assume that the bank offers three types of accounts: savings, checking, and certificate of deposit, as described next.
Savings accounts: Suppose that the bank offers two types of savings accounts: one that has no minimum balance and a lower interest rate and another that requires a minimum balance and has a higher interest rate.
Checking accounts: Suppose that the bank offers three types of checking accounts: one with a monthly service charge, limited check writing, no minimum balance, and no interest; another with no monthly service charge, a minimum balance requirement, unlimited check writing and lower interest; and a third with no monthly service charge, a higher minimum requirement, a higher interest rate, and unlimited check writing.
Certificate of deposit (CD): In an account of this type, money is left for some time, and these accounts draw higher interest rates than savings or checking accounts. Suppose that you purchase a CD for six months. Then we say that the CD will mature in six months. The penalty for early withdrawal is stiff.
Figure 12-25 shows the inheritance hierarchy of these bank accounts.
Note that the classes bankAccount and checkingAccount are abstract. That is, we cannot instantiate objects of these classes. The other classes in Figure 12-25 are not abstract.
bankAccount: Every bank account has an account number, the name of the owner, and a balance. Therefore, instance variables such as name, accountNumber, and balance should be declared in the abstract class bankAccount. Some operations common to all types of accounts are retrieve account owner’s name, account number, and account balance; make deposits; withdraw money; and create monthly statements. So include functions to implement these operations. Some of these functions will be pure virtual.
checkingAccount: A checking account is a bank account. Therefore, it inherits all the properties of a bank account. Because one of the objectives of a checking account is to be able to write checks, include the pure virtual function writeCheck to write a check.
serviceChargeChecking: A service charge checking account is a checking account. Therefore, it inherits all the properties of a checking account. For simplicity, assume that this type of account does not pay any interest, allows the account holder to write a limited number of checks each month, and does not require any minimum balance. Include appropriate named constants, instance variables, and functions in this class.
noServiceChargeChecking: A checking account with no monthly service charge is a checking account. Therefore, it inherits all the properties of a checking account. Furthermore, this type of account pays interest, allows the account holder to write checks, and requires a minimum balance.
highInterestChecking: A checking account with high interest is a checking account with no monthly service charge. Therefore, it inherits all the properties of a no service charge checking account. Furthermore, this type of account pays higher interest and requires a higher minimum balance than the no service charge checking account.
savingsAccount: A savings account is a bank account. Therefore, it inherits all the properties of a bank account. Furthermore, a savings account also pays interest.
highInterestSavings: A high-interest savings account is a savings account. Therefore, it inherits all the properties of a savings account. It also requires a minimum balance.
certificateOfDeposit: A certificate of deposit account is a bank account. Therefore, it inherits all the properties of a bank account. In addition, it has instance variables to store the number of CD maturity months, interest rate, and the current CD month.
Write the definitions of the classes described in this programming exercise and a program to test your classes.
Explanation / Answer
bankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <String>
using namespace std;
class bankAccount {
public:
bankAccount(string name, double initialBalance);
string getName();
unsigned int getAccountNumber();
double getBalance();
double getInterestRate();
string getStatementString();
void deposit(double amount);
void deposit(double amount, string statement);
void withdraw(double amount);
void withdraw(double amount, string statement);
virtual void printStatement() = 0;
protected:
void addStatementLine(string statement, double amount);
private:
string name;
unsigned int accountNumber;
double balance;
static unsigned int nextAccountNumber;
string statementString;
};
#endif /* BANKACCOUNT_H */
certificateOfDeposit.cpp
#include "certificateOfDeposit.h"
certificateOfDeposit::certificateOfDeposit(string name, double initialBalance, int maturityMonths) : bankAccount(name, initialBalance)
{
this->maturityMonths = maturityMonths;
interestRate = 0.03;
currentCDMonth = 0;
withdrawalPenaltyMonths = 3;
}
certificateOfDeposit::certificateOfDeposit(string name, double initialBalance) : bankAccount(name, initialBalance)
{
maturityMonths = 6;
interestRate = 0.03;
currentCDMonth = 0;
withdrawalPenaltyMonths = 3;
}
int certificateOfDeposit::getCurrentCDMonth()
{
return currentCDMonth;
}
double certificateOfDeposit::getInterestRate()
{
return interestRate;
}
int certificateOfDeposit::getWithdrawalPenaltyMonths()
{
return withdrawalPenaltyMonths;
}
int certificateOfDeposit::getMaturityMonths()
{
return maturityMonths;
}
void certificateOfDeposit::withdraw()
{
if (getCurrentCDMonth() < getMaturityMonths())
bankAccount::withdraw(getBalance()*getInterestRate()*getWithdrawalPenaltyMonths(), "Early Withdrawal Penalty");
bankAccount::withdraw(getBalance(), "Close Account");
}
void certificateOfDeposit::incrementMonth()
{
bankAccount::deposit(getBalance()*getInterestRate(), "Monthly Interest");
if (getCurrentCDMonth() < getMaturityMonths())
{
currentCDMonth++;
}
else
withdraw();
}
void certificateOfDeposit::printStatement()
{
cout << "Certificate of Deposit Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl;
cout << "Interest Rate: " << getInterestRate() * 100 << "%" << endl;
cout << "Maturity Month: " << getMaturityMonths() << ", Current Month: " << getCurrentCDMonth() << endl;
cout << "Early Withdrawal Penalty: " << getWithdrawalPenaltyMonths() << " (months)" << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
certificationOfDeposit.h
#ifndef CERTIFICATEOFDEPOSIT_H
#define CERTIFICATEOFDEPOSIT_H
#include "bankAccount.h"
#include <iostream>
using namespace std;
class certificateOfDeposit : public bankAccount{
public:
certificateOfDeposit(string name, double initialBalance, int maturityMonths);
certificateOfDeposit(string name, double initialBalance);
int getMaturityMonths();
double getInterestRate();
int getCurrentCDMonth();
int getWithdrawalPenaltyMonths();
void withdraw();
void incrementMonth();
void printStatement();
private:
int maturityMonths;
double interestRate;
int currentCDMonth;
int withdrawalPenaltyMonths;
void withdraw(double amount);
void withdraw(double amount, string statement);
void deposit(double amount);
};
#endif /* CERTIFICATEOFDEPOSIT_H */
checkingAccount.cpp
#include "checkingAccount.h"
checkingAccount::checkingAccount(string name, double initialBalance) : bankAccount(name, initialBalance)
{
}
highInterestChecking.cpp
#include "highInterestChecking.h"
highInterestChecking::highInterestChecking(string name, double initialBalance) : noServiceChargeChecking(name, initialBalance, 2000, 0.01)
{
}
void highInterestChecking::printStatement()
{
bankAccount::deposit(getBalance() * getInterestRate(), "Interest");
cout << "High Interest Checking Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl;
cout << "Interest Rate: " << getInterestRate() * 100 << "%" << endl;
cout << "Minimum Balance: " << getMinimumBalance() << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
highInterestChecking.h
#ifndef HIGHINTERESTCHECKING_H
#define HIGHINTERESTCHECKING_H
#include "noServiceChargeChecking.h"
#include <string>
using namespace std;
class highInterestChecking : public noServiceChargeChecking {
public:
highInterestChecking(string name, double initialBalance);
void printStatement();
private:
};
#endif /* HIGHINTERESTCHECKING_H */
highInterestSavings.cpp
#include "highInterestSavings.h"
highInterestSavings::highInterestSavings(string name, double initialBalance) : savingsAccount(name, initialBalance, 0.02)
{
minimumBalance = 5000;
}
int highInterestSavings::getMinimumBalance()
{
return minimumBalance;
}
highInterestsavings.h
#ifndef HIGHINTERESTSAVINGS_H
#define HIGHINTERESTSAVINGS_H
#include "savingsAccount.h"
#include <iostream>
using namespace std;
class highInterestSavings : public savingsAccount{
public:
highInterestSavings(string name, double initialBalance);
int getMinimumBalance();
void printStatement();
void withdraw(double amount, string statement);
void withdraw(double amount);
private:
int minimumBalance;
};
#endif /* HIGHINTERESTSAVINGS_H */
void highInterestSavings::withdraw(double amount, string statement)
{
if (amount + getMinimumBalance() <= getBalance())
{
bankAccount::withdraw(amount, statement);
}
else
{
addStatementLine(statement.append(" Overdraft. Below Minimum Balance."), amount);
}
}
void highInterestSavings::withdraw(double amount)
{
withdraw(amount, "Withdrawal");
}
void highInterestSavings::printStatement()
{
bankAccount::deposit(getBalance() * getInterestRate(), "Interest");
cout << "High Interest Savings Account Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl;
cout << "Minimum Balance: " << getMinimumBalance() << endl;
cout << "Interest Rate: " << getInterestRate() * 100 << "%" << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
noServiceChargeChecking.cpp
#include "noServiceChargeChecking.h"
#include <sstream>
#include <iostream>
noServiceChargeChecking::noServiceChargeChecking(string name, double initialBalance) : checkingAccount(name, initialBalance)
{
minimumBalance = 500;
this->interestRate = 0.001;
}
noServiceChargeChecking::noServiceChargeChecking(string name, double initialBalance, int minBalance, double interestRate) : checkingAccount(name, initialBalance)
{
minimumBalance = minBalance;
this->interestRate = interestRate;
}
void noServiceChargeChecking::writeCheck(double amount, int checkNumber)
{
stringstream output;
output << "Check #" << checkNumber;
withdraw(amount, output.str());
}
void noServiceChargeChecking::withdraw(double amount, string statement)
{
if (amount + getMinimumBalance() <= getBalance())
{
bankAccount::withdraw(amount, statement);
}
else
{
addStatementLine(statement.append(" Overdraft. Below Minimum Balance."), amount);
}
}
void noServiceChargeChecking::withdraw(double amount)
{
withdraw(amount, "Withdrawal");
}
void noServiceChargeChecking::printStatement()
{
bankAccount::deposit(getBalance() * getInterestRate(), "Interest");
cout << "No Service Charge Checking Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl;
cout << "Interest Rate: " << getInterestRate() * 100 << "%" << endl;
cout << "Minimum Balance: " << getMinimumBalance() << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
int noServiceChargeChecking::getMinimumBalance()
{
return minimumBalance;
}
double noServiceChargeChecking::getInterestRate()
{
return interestRate;
}
noServiceChargeChecking.h
#ifndef NOSERVICECHARGECHECKING_H
#define NOSERVICECHARGECHECKING_H
#include "checkingAccount.h"
#include <iostream>
using namespace std;
class noServiceChargeChecking : public checkingAccount {
public:
noServiceChargeChecking(string name, double initialBalance);
void writeCheck(double amount, int checkNumber);
void printStatement();
void withdraw(double amount, string statement);
void withdraw(double amount);
double getInterestRate();
int getMinimumBalance();
protected:
noServiceChargeChecking(string name, double initialBalance, int minBalance, double interestRate);
private:
double interestRate;
int minimumBalance;
};
#endif /* NOSERVICECHARGECHECKING_H */
savingsAccount.cpp
#include "savingsAccount.h"
savingsAccount::savingsAccount(string name, double initialBalance) : bankAccount(name, initialBalance)
{
interestRate = .005;
}
savingsAccount::savingsAccount(string name, double initialBalance, double interestRate) : bankAccount(name, initialBalance)
{
this->interestRate = interestRate;
}
double savingsAccount::getInterestRate()
{
return interestRate;
}
void savingsAccount::printStatement()
{
bankAccount::deposit(getBalance() * getInterestRate(), "Interest");
cout << "Savings Account Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl;
cout << "Interest Rate: " << getInterestRate() * 100 << "%" << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
savingsAccount.h
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include "bankAccount.h"
#include <iostream>
using namespace std;
class savingsAccount : public bankAccount {
public:
savingsAccount(string name, double initialBalance);
double getInterestRate();
void printStatement();
protected:
savingsAccount(string name, double initialBalance, double interestRate);
private:
double interestRate;
};
#endif /* SAVINGSACCOUNT_H */
serviceChargeChecking.cpp
#include "serviceChargeChecking.h"
#include <sstream>
#include <iostream>
using namespace std;
serviceChargeChecking::serviceChargeChecking(string name, double initialBalance) : checkingAccount(name, initialBalance)
{
bankAccount::withdraw(SERVICE_CHARGE, "Service Charge");
checksWritten = 0;
}
void serviceChargeChecking::writeCheck(double amount, int checkNumber)
{
stringstream output;
if (++checksWritten <= CHECK_LIMIT)
{
output << "Check #" << checkNumber;
bankAccount::withdraw(amount, output.str());
}
else
{
output << "Maximum Limit of Checks Reached. Check # " << checkNumber << " bounced";
addStatementLine(output.str(), amount);
}
}
void serviceChargeChecking::printStatement()
{
cout << "Service Charge Checking Statement" << endl;
cout << "Name: " << getName() << endl;
cout << "Account Number: " << getAccountNumber() << endl << endl;
cout << getStatementString() << endl;
cout << "Final Balance: " << getBalance() << endl << endl;
}
serviceChargeChecking.h
#ifndef SERVICECHARGECHECKING_H
#define SERVICECHARGECHECKING_H
#include "checkingAccount.h"
#include <String>
using namespace std;
class serviceChargeChecking : public checkingAccount {
public:
serviceChargeChecking(string name, double initialBalance);
void writeCheck(double amount, int checkNumber);
void printStatement();
private:
int checksWritten;
static const int CHECK_LIMIT = 6;
static const int SERVICE_CHARGE = 5;
};
#endif /* SERVICECHARGECHECKING_H */
bankAccount.cpp
#include "bankAccount.h"
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
unsigned int bankAccount::nextAccountNumber = 1;
bankAccount::bankAccount(string name, double initialBalance)
{
stringstream output;
this->name = name;
balance = initialBalance;
accountNumber = nextAccountNumber++;
output << setw(60) << left << "Transaction" << setw(10) << "Amount" << " " << "Balance" << endl;
statementString = output.str();
addStatementLine("Initial Deposit", initialBalance);
}
string bankAccount::getName()
{
return name;
}
unsigned int bankAccount::getAccountNumber()
{
return accountNumber;
}
double bankAccount::getBalance()
{
return balance;
}
void bankAccount::addStatementLine(string statement, double amount)
{
stringstream output;
output << setw(60) << left << statement << setw(10) << amount << " " << getBalance() << endl;
//.append(statement);
statementString.append(output.str());
}
void bankAccount::deposit(double amount)
{
deposit(amount, "Deposit");
}
void bankAccount::deposit(double amount, string statement)
{
balance += amount;
addStatementLine(statement, amount);
}
void bankAccount::withdraw(double amount)
{
withdraw(amount, "Withdrawal");
}
void bankAccount::withdraw(double amount, string statement)
{
if (balance >= amount)
{
balance -= amount;
addStatementLine(statement, amount);
}
else
{
addStatementLine(statement.append(" Overdraft"), amount);
}
}
string bankAccount::getStatementString()
{
return statementString;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.