CIS 250 - Bank Accounts (Inheritance)

Objectives

  1. implement classes which use inheritance (derived classes, subclasses)
  2. write constructors
  3. write destructors
  4. overload operators (assignment, insertion)
  5. write accessor functions
  6. write mutator functions
  7. correctly program classes that use dynamic memory allocation
  8. correctly use access modifiers for data members
  9. correctly implement static and instance data members and functions

Preliminary steps

  1. Create a new directory to hold this assignment: mkdir bank
  2. Make the new directory your current working directory: cd bank
  3. Copy the starting files into your current working directory: cp ~dklick/examples/bank/* .
  4. Change the permissions on the new directory to 700: chmod 700 .

Resources

There are three classes in this assignment. You are given the header files for all three classes and a test driver program for the whole project. You are also given starting files for implementation of the three classes. Your task is to complete the implementation files for the three classes.

The programs you are given are as follows:

  1. TestBankAccount.cpp
  2. BankAccount.h
  3. CheckingAccount.h
  4. SavingsAccount.h
  5. BankAccount.cpp
  6. CheckingAccount.cpp
  7. SavingsAccount.cpp

The above code is also available on kermit in the ~dklick/examples/bank/ directory.

Requirements

You must complete the implementation (.cpp files) of the BankAccount, CheckingAccount, and SavingsAccount classes.

  1. BankAccount.cpp
    1. initialize the static int data member
    2. implement the init method
    3. implement the two constructors
    4. implement the destructor
    5. implement the assignment operator
    6. implement the setAccountNumber method
  2. CheckingAccount.cpp
    1. implement the constructor
    2. implement the three mutator methods
    3. implement the postInterest method
    4. implement the verifyMinimumBalance method
    5. implement the withdraw method
    6. implement the insertion operator overload and make sure the formatting matches the sample output
  3. SavingsAccount.cpp
    1. implement the constructor
    2. implement the postInterest method
    3. implement the withdraw method
    4. implement the insertion operator overload and make sure the formatting matches the sample output

Grading rubric

Note: Program must be on the server (kermit) and be able to be compiled and run.

  1. 5 pts: Style conventions are followed and documentation comments have your name, date, etc. in all three source files (.cpp) that you have to modify. The test driver code and the header files should be left as they are.
  2. 15 pts: BankAccount.cpp works properly and meets all requirements
  3. 15 pts: CheckingAccount.cpp works properly and meets all requirements
  4. 15 pts: SavingsAccount.cpp works properly and meets all requirements

Test driver code

TestBankAccount.cpp.txt

This code is also available on kermit at ~dklick/examples/bank/TestBankAccount.cpp

#include "BankAccount.h"
#include "CheckingAccount.h"
#include "SavingsAccount.h"
#include <iostream>
using std::cout;

int main(int argc, char* argv[]) {
    BankAccount b1("Ralph"), b2("Ann", 27.51);
    CheckingAccount c1("Jack", 1000, 800, 0.06, 15.00);
    CheckingAccount c2("Lisa", 450);
    CheckingAccount c3("Andy", 750, 20, 0.57, 120.00);
    CheckingAccount c4(c3);
    CheckingAccount c5(c3);
    c4.setAccountNumber("Alice");
    c5 = c2;
    c5.setAccountNumber("Maxwell");
    SavingsAccount s1("Samir", 9300);
    SavingsAccount s2("Rita", 32);

    cout << "Initial bank accounts ****\n";
    cout << b1 << '\n';
    cout << b2 << '\n';
    cout << c1 << '\n';
    cout << c2 << '\n';
    cout << c3 << '\n';
    cout << c4 << '\n';
    cout << c5 << '\n';
    cout << s1 << '\n';
    cout << s2 << '\n';

    cout << "\nBank accounts so far: " << BankAccount::getCount() << '\n';
    {
        SavingsAccount s3(s2);
        cout << "\nAfter creating another: " << BankAccount::getCount() << '\n';
    }
    cout << "\nAnd then deleting one: " << BankAccount::getCount() << '\n';


    cout << "\nAfter changing accounts:\n";
    b1.setAccountNumber("Melvin");
    b1.withdraw(50.00);
    c3.setInterestRate(c3.getInterestRate() - 0.50);
    c3.setServiceCharge(c3.getServiceCharge() - 110.00);
    c3.setMinimumBalance(c3.getMinimumBalance() + 580.00);
    s2.setInterestRate(s2.getInterestRate() + 0.05);
    cout << b1 << '\n';
    cout << c3 << '\n';
    cout << s2 << '\n';

    cout << "\nAfter posting interest *****\n";
    c1.postInterest();
    c2.postInterest();
    c3.postInterest();
    s1.postInterest();
    s2.postInterest();

    cout << c1 << '\n';
    cout << c2 << '\n';
    cout << c3 << '\n';
    cout << s1 << '\n';
    cout << s2 << '\n';

    cout << "\nTrying to do withdrawals:\n";
    double amtc1 = 250.00, amtc2 = 300.00, amtc3 = 100.00;
    double amts1 = 120.00, amts2 = 290.00;

    cout << c1.getAccountNumber() << "'s account ";
    cout << (c1.verifyMinimumBalance(amtc1) ?
            "has sufficient funds\n" :
            "will go below the minimum balance\n");
    cout << c2.getAccountNumber() << "'s account ";
    cout << (c2.verifyMinimumBalance(amtc2) ?
            "has sufficient funds\n" :
            "will go below the minimum balance\n");
    cout << c3.getAccountNumber() << "'s account ";
    cout << (c3.verifyMinimumBalance(amtc3) ?
            "has sufficient funds\n" :
            "will go below the minimum balance\n");
    c1.writeCheck(amtc1);
    c2.writeCheck(amtc2);
    c3.writeCheck(amtc3);
    s1.withdraw(amts1);
    s2.withdraw(amts2);

    cout << "\nAfter withdrawing money *****\n";
    cout << c1 << '\n';
    cout << c2 << '\n';
    cout << c3 << '\n';
    cout << s1 << '\n';
    cout << s2 << '\n';


    cout << "\nTrying to do a couple more withdrawals:\n";
    if (c1.withdraw(150.00)) cout << "Withdrew";
    else cout << "Failed to withdraw";
    cout << " $150.00 from " << c1.getAccountNumber() << "'s account\n";

    if (c1.withdraw(50.00)) cout << "Withdrew";
    else cout << "Failed to withdraw";
    cout << " $50.00 from " << c1.getAccountNumber() << "'s account\n";

    if (c2.writeCheck(50.00)) cout << "Withdrew";
    else cout << "Failed to withdraw";
    cout << " $50.00 from " << c2.getAccountNumber() << "'s account\n";

    if (c3.writeCheck(105.00)) cout << "Withdrew";
    else cout << "Failed to withdraw";
    cout << " $105.00 from " << c3.getAccountNumber() << "'s account\n";

    if (c3.writeCheck(800.00)) cout << "Withdrew";
    else cout << "Failed to withdraw";
    cout << " $800.00 from " << c3.getAccountNumber() << "'s account\n";

    cout << "\nAfter final withdrawals *****\n";
    cout << c1 << '\n';
    cout << c2 << '\n';
    cout << c3 << '\n';

    return 0;
}

Sample output from running test driver program

Initial bank accounts ****
Bank account: Ralph, Balance: $0.00
Bank account: Ann, Balance: $27.51
Checking account: Jack, Balance: $1000.00
Checking account: Lisa, Balance: $450.00
Checking account: Andy, Balance: $750.00
Checking account: Alice, Balance: $750.00
Checking account: Maxwell, Balance: $450.00
Savings account: Samir, Balance: $9300.00
Savings account: Rita, Balance: $32.00

Bank accounts so far: 9

After creating another: 10

And then deleting one: 9

After changing accounts:
Bank account: Melvin, Balance: $-50.00
Checking account: Andy, Balance: $750.00
Savings account: Rita, Balance: $32.00

After posting interest *****
Checking account: Jack, Balance: $1060.00
Checking account: Lisa, Balance: $468.00
Checking account: Andy, Balance: $802.50
Savings account: Samir, Balance: $9858.00
Savings account: Rita, Balance: $35.52

Trying to do withdrawals:
Jack's account has sufficient funds
Lisa's account will go below the minimum balance
Andy's account has sufficient funds

After withdrawing money *****
Checking account: Jack, Balance: $810.00
Checking account: Lisa, Balance: $148.00
Checking account: Andy, Balance: $702.50
Savings account: Samir, Balance: $9738.00
Savings account: Rita, Balance: $35.52

Trying to do a couple more withdrawals:
Withdrew $150.00 from Jack's account
Withdrew $50.00 from Jack's account
Withdrew $50.00 from Lisa's account
Withdrew $105.00 from Andy's account
Failed to withdraw $800.00 from Andy's account

After final withdrawals *****
Checking account: Jack, Balance: $580.00
Checking account: Lisa, Balance: $78.00
Checking account: Andy, Balance: $587.50