In: Computer Science
C++ 7th edition
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. bank Account checking Account certificateofDeposit 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.
Copyable code:
File Name: MyBankAccount.h
#ifndef _BANK_ACCOUNT_H
#define _BANK_ACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class MyBankAccount
{
public:
MyBankAccount(int mactNum, string name, double actIntBal)
{
bAccntNum = mactNum;
bName = name;
custBal = actIntBal;
}
~MyBankAccount(void){}
string getBankCustName()
{
return bName;
}
int getBankCustAccntNum()
{
return bAccntNum;
}
double getBankCustAccntBalance()
{
return custBal;
}
void accntDeposit(double depAmt)
{
custBal += depAmt;
cout << "$" << depAmt << " DEPOSITED." << endl;
}
virtual void accntWithdraw(double depAmt) = 0;
virtual void displayAccntStat() = 0;
virtual void displayAccntSum()
{
cout <<"============================================="<<endl;
cout << endl << setw(25) << "" << "Account Summary" << endl << endl;
cout << setw(25) << "Name: " << bName << endl;
cout << setw(25) << "Account #: " << bAccntNum << endl;
cout << setw(25) << "Current Balance: $" << custBal << endl;
}
public:
string bName;
int bAccntNum;
double custBal;
};
#endif
File Name:MyCheckingAccount.h
#ifndef _CHECKING_ACCOUNT_H
#define _CHECKING_ACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
#include "MyBankAccount.h"
using namespace std;
class MyCheckingAccount :public MyBankAccount
{
public:
MyCheckingAccount(int m_acntNum, string atName, double startBal)
: MyBankAccount(m_acntNum, atName, startBal)
{
}
~MyCheckingAccount(void){}
virtual void write_Check(double depAmt) = 0;
void accntWithdraw(double depAmt)
{
if (custBal-depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
if (custBal - depAmt < accntMinBal)
{
cout << "WITHDRAW DENIED DUE TO MINIMUM BALANCE VIOLATION" << endl;
return;
}
custBal -= depAmt;
}
void displayAccntStat()
{
displayAccntSum();
}
protected:
double accntIntRate;
int accntChecksRem;
double accntMinBal;
};
#endif
File Name: SavingsAccnt.h
#include <iostream>
#include <iomanip>
#include <string>
#include "MyBankAccount.h"
using namespace std;
class SavingsAccnt :
public MyBankAccount
{
public:
SavingsAccnt(int m_acntNum, string atName, double startBal)
: MyBankAccount(m_acntNum, atName, startBal)
{
accntIntRate = 3.99;
}
~SavingsAccnt(void)
{
}
void accntWithdraw(double depAmt)
{
if (custBal-depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
custBal -= depAmt;
}
void displayAccntSum()
{
MyBankAccount::displayAccntSum();
cout << setw(25) << "Interest rate: " << accntIntRate << "%" << endl << endl;
cout <<"============================================="<<endl;
}
void displayAccntStat()
{
displayAccntSum();
}
protected:
double accntIntRate;
};
File Name: ServiceChargeAccount.h
#include <iostream>
#include <iomanip>
#include <string>
#include "MyCheckingAccount.h"
using namespace std;
const int ALLOWEDCHECKS=5;
const double SERVICE_CHARGE=10.0l;
class ServiceChargeAccount :
public MyCheckingAccount
{
public:
ServiceChargeAccount(int m_acntNum, string atName, double startBal)
: MyCheckingAccount(m_acntNum, atName, startBal)
{
accntIntRate = 0;
accntChecksRem = ALLOWEDCHECKS;
accntMinBal = 0;
}
~ServiceChargeAccount(void){}
void write_Check(double depAmt)
{
if (accntChecksRem == 0)
{
cout << "NO CHECKS REMAINING" << endl;
return;
}
if (custBal - depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
accntChecksRem--;
custBal -= depAmt;
}
void displayAccntSum()
{
MyBankAccount::displayAccntSum();
cout << setw(25) << "REMAINING CHECKS: " << accntChecksRem << endl;
cout << setw(25) << "MONTHLY SERVICE CHARGE: $" << SERVICE_CHARGE << endl;
cout <<"============================================="<<endl;
}
};
File Name: NoServiceChargeAccount.h
#include <iostream>
#include <iomanip>
#include <string>
#include "MyCheckingAccount.h"
using namespace std;
class NoServiceChargeAccount :
public MyCheckingAccount
{
public:
NoServiceChargeAccount(int m_acntNum, string atName, double startBal)
: MyCheckingAccount(m_acntNum, atName, startBal)
{
accntIntRate = 2.5;
accntChecksRem = -1;
accntMinBal = 500;
}
~NoServiceChargeAccount(void){}
void write_Check(double depAmt)
{
if (custBal - depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
custBal -= depAmt;
}
void displayAccntSum()
{
MyBankAccount::displayAccntSum();
cout << setw(25) << "Interest rate: " << accntIntRate << "%" << endl;
cout << setw(25) << "Minimum Balance: $" << accntMinBal << endl;
cout <<"============================================="<<endl;
}
};
File Name: HighInterestSavingsAccnt.h
#include <iostream>
#include <iomanip>
#include <string>
#include "SavingsAccnt.h"
using namespace std;
class HighInteresetSavingAccnt :
public SavingsAccnt
{
public:
HighInteresetSavingAccnt(int m_acntNum, string atName, double startBal)
: SavingsAccnt(m_acntNum, atName, startBal)
{
accntMinBal = 5000;
}
~HighInteresetSavingAccnt(void){}
void accntWithdraw(double depAmt)
{
if (custBal-depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
if (custBal - depAmt < accntMinBal)
{
cout << "WITHDRAW DENIED DUE TO MINIMUM BALANCE VIOLATION" << endl;
return;
}
custBal -= depAmt;
}
void displayAccntSum()
{
MyBankAccount::displayAccntSum();
cout << setw(25) << "Interest rate: " << accntIntRate << "%" << endl;
cout << setw(25) << "Minimum Balance: $" << accntMinBal << endl << endl;
cout <<"============================================="<<endl;
}
protected:
double accntMinBal;
};
File Name: HighInterestCheckAccount.h
#include <iostream>
#include <iomanip>
#include <string>
#include "NoServiceChargeAccount.h"
using namespace std;
class HighInteresetCheckAccount :
public NoServiceChargeAccount
{
public:
HighInteresetCheckAccount(int m_acntNum, string atName, double startBal)
: NoServiceChargeAccount(m_acntNum, atName, startBal)
{
accntIntRate = 5.0;
accntChecksRem = -1;
accntMinBal = 1000;
}
~HighInteresetCheckAccount(void){}
};
File Name: CertOfDeposit.h
#include <iostream>
#include <iomanip>
#include <string>
#include "MyBankAccount.h"
using namespace std;
class CertOfDeposit :
public MyBankAccount
{
public:
CertOfDeposit(int mactNum, string name, double actIntBal, int matMon)
: MyBankAccount(mactNum, name, actIntBal)
{
accntMautMonth = matMon;
accntCurrMonth = 1;
accntIntRate = 4.75;
}
~CertOfDeposit(void){}
void accntWithdraw(double depAmt)
{
if (custBal-depAmt < 0)
{
cout << "LOW BALANCE" << endl;
return;
}
custBal -= depAmt;
}
void displayAccntSum()
{
MyBankAccount::displayAccntSum();
cout << setw(25) << "Interest rate: " << accntIntRate << "%" << endl ;
cout << setw(25) << "Maturity Months: " << accntMautMonth << endl ;
cout << setw(25) << "Current Month: " << accntCurrMonth << endl;
cout << endl << setw(60) << setfill('-') << "" << setfill(' ') << endl;
}
void displayAccntStat()
{
displayAccntSum();
}
private:
double accntIntRate;
int accntMautMonth;
int accntCurrMonth;
};
File Name: main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "CertOfDeposit.h"
#include "HighInterestCheckAccount.h"
#include "HighInterestSavingsAccnt.h"
#include "ServiceChargeAccount.h"
using namespace std;
void testServiceCheckAccnt()
{
ServiceChargeAccount myact(100,"David Cameron", 2000);
char inp=0;
double amnt;
cout << "Performing operations on Service Charge Checking Account" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Summary" << endl;
cout << "4 - Display Monthly Statement" << endl;
cout << "5 - Write check" << endl;
cout << "x - Quit" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case '5':
cout << "Enter amnt: ";
cin >> amnt;
myact.write_Check(amnt);
break;
case '6':
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
void testNoServiceCheckAccnt()
{
NoServiceChargeAccount myact(100,"David Cameron", 2000);
char inp=0;
double amnt;
cout << "Performing Operation on Checking Account with No Service Charge" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Summary" << endl;
cout << "4 - Display Monthly Statement" << endl;
cout << "5 - Write check" << endl;
cout << "x - Quit" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case '5':
cout << "Enter amount: ";
cin >> amnt;
myact.write_Check(amnt);
break;
case '6':
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
void testHighInterestCheckAccnt()
{
HighInteresetCheckAccount myact(100,"David Cameron", 2000);
char inp=0;
double amnt;
cout << "Performing Operation on High Interest Checking Account" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Account Summary" << endl;
cout << "4 - Display Monthly Account Statement" << endl;
cout << "5 - Write a check" << endl;
cout << "x - Quit" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case '5':
cout << "Enter amount: ";
cin >> amnt;
myact.write_Check(amnt);
break;
case '6':
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
void testSavingsAccnt()
{
SavingsAccnt myact(100,"David Cameron", 2000);
char inp=0;
double amnt;
cout << "Performing Operation on Savings Account" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Account Summary" << endl;
cout << "4 - Display Monthly Account Statement" << endl;
cout << "x - Quit" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
void testHighInterestSavingsAccnt()
{
HighInteresetSavingAccnt myact(123,"John Doe", 8000);
char inp=0;
double amnt;
cout << "Performing Operation on High Interest Savings Account" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Account Summary" << endl;
cout << "4 - Display Monthly Account Statement" << endl;
cout << "x - Quit" << endl;
cout << "Enter choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
void testCOD()
{
CertOfDeposit myact(123,"John Doe", 10000, 6);
char inp=0;
double amnt;
cout << "Performing Operation on Certificate Of Deposit" << endl << endl;
cout << "Account Initial Information:" << endl;
myact.displayAccntSum();
cout << endl;
while (inp != 'x')
{
cout << "Choose Operation:" << endl;
cout << "1 - Perform Withdrawal" << endl;
cout << "2 - Perform Deposit" << endl;
cout << "3 - Display Account Summary" << endl;
cout << "4 - Display Monthly Account Statement" << endl;
cout << "x - Quit" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
cout << "Enter amount: ";
cin >> amnt;
myact.accntWithdraw(amnt);
break;
case '2':
cout << "Enter amount: ";
cin >> amnt;
myact.accntDeposit(amnt);
break;
case '3':
myact.displayAccntSum();
break;
case '4':
myact.displayAccntStat();
break;
case 'x':
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
myact.displayAccntSum();
cout << endl;
}
}
int main()
{
char inp;
cout << "Program to Perform Operation on Various Bank Accounts" << endl << endl;
cout << "Select a Bank Account" << endl;
cout << "1 - CHECKING ACCOUNT WITH SERVICE CHARGE" << endl;
cout << "2 - CHECKING ACCOUNT WITH NO SERVICE CHARGE "<< endl;
cout << "3 - CHECKING ACCOUNT WITH HIGH INTEREST" << endl;
cout << "4 - SAVINGS ACCOUNT" << endl;
cout << "5 - SAVINGS ACCOUNT WITH HIGH INTEREST" << endl;
cout << "6 - CERTIFICATE OF DEPOSIT" << endl;
cout << "Enter your choice: ";
cin >> inp;
switch (inp)
{
case '1':
testServiceCheckAccnt();
break;
case '2':
testNoServiceCheckAccnt();
break;
case '3':
testHighInterestCheckAccnt();
break;
case '4':
testSavingsAccnt();
break;
case '5':
testHighInterestSavingsAccnt();
break;
case '6':
testCOD();
break;
default:
cout << "WRONG INPUT" << endl;
break;
}
system("pause");
return 0;
}