Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Banks have many different types of accounts often with different rules for fees

ID: 3716384 • Letter: B

Question

Banks have many different types of accounts often with different rules for fees associated with transactions such as withdrawals. Customers are allowed to transfer funds between accounts incurring the appropriate fees associated with withdrawal of funds from one account.

Write a program with a base class for a bank account and two derived classes (as described below) representing accounts with different rules for withdrawing funds. Also write a function that transfers funds from one account (of any type) to another.

A transfer is a withdrawal from one account and a deposit into the other. Since the transfer can be done at any time with any type of account the withdraw function in the classes must be virtual. Write a main program that creates three accounts (one from each class) and tests the transfer function.

For the classes, create a base class called BankAccount that has the name of the owner of the account (a string) and the balance in the account (double) as data members. Include member functions deposit and withdraw (each with a double for the amount as an argument) and accessor functions getName and getBalance. Deposit
will add the amount to the balance (assuming the amount is nonnegative) and withdraw will subtract the amount from the balance (assuming the amount is nonnegative and less than or equal to the balance). Also create a class called MoneyMarketAccount that is derived from BankAccount. In a MoneyMarketAccount the user gets 2 free withdrawals in a given period of time (don't worry about the time for this problem).

After the free withdrawals have been used, a withdrawal fee of $1.50 is deducted from the balance per withdrawal. Hence, the class must have a data member to keep track of the number of withdrawals. It also must override the withdraw definition.

Finally, create a CDAccount class (to model a Certificate of Deposit) derived from BankAccount which in addition to having the name and balance also has an interest rate. CDs incur penalties for early withdrawal of funds. Assume that a withdrawal of funds (any amount) incurs a penalty of 25% of the annual interest earned on the account.


Assume the amount withdrawn plus the penalty are deducted from the account balance. Again the withdraw function must override the one in the base class. For all three classes, the withdraw function should return an integer indicating the status (either ok or insufficient funds for the withdrawal to take place). For the purposes
of this exercise do not worry about other functions and properties of these accounts (such as when and how interest is paid).

using the starting code:

//*************************************************************

//

// Accounts.cpp

//

// This program contains a BankAccount class and two derived

// classes - MoneyMarketAccount and CDAccount - and a function

// for transferring from one account to another. The three

// classes have different rules for withdrawing funds; hence,

// the withdraw function is virtual. The main function tests

// the classes and the transfer function.

//

//*************************************************************

#include <iostream>

#include <string>

using namespace std;

//

// Constants to indicate status after withdrawal attempt

//

const int OK = 0;

const int INSUFFICIENT_FUNDS = -1;

//=================================

// BankAccount Class Declaration

//=================================

class BankAccount

{

public:

BankAccount(string name, double balance);

// Sets up a BankAccount object with the given name and balance

BankAccount(); // Default Constructor Added to the exercise

string getName() const;

// Returns the name on the BankAccount

double getBalance() const;

// Returns the balance on the BankAccount

void deposit(double amt);

// Precondition: amt is a nonnegative number

// Postcondition: amt has been added to the account balance

virtual int withdraw(double amt);

// Precondition: amt is a nonnegative number

// Postcondition: amt has been subtracted from the balance if

// there were sufficient funds (balance >= amt)

// Returns 0 (OK) if the withdrawal was completed; -1 (INSUFFICIENT_FUNDS)

// if there were insufficient funds.

protected:

string acctName;

double acctBalance;

};

//================================

// MoneyMarketClass Declaration

//================================

class MoneyMarketAccount: public BankAccount

{

public:

MoneyMarketAccount(string name, double balance);

// Sets up a MoneyMarketAccount with the given name and balance

virtual int withdraw(double amt);

// Precondition: amt is a nonnegative integer

// Postcondition: If numWithdrawals < FREE_WITHDRAWALS and

// amt <= acctBalance, amt is subtracted from acctBalance and

// numWithdrawals is incremented; if numWithdrawals >= FREE_WITHDRAWALS

// and amt + WITHDRAWAL_FEE <= acctBalance, amt + WITHDRAWAL_FEE is

// subtracted from acctBalance.

// Returns 0 (OK) if either of the above occurred; otherwise returns

// INSUFFICIENT_FUNDS (-1).

int getNumWithdrawals() const;

// Returns the number of withdrawals

private:

int numWithdrawals;

static const int FREE_WITHDRAWALS;

static const double WITHDRAWAL_FEE;

};

// Allocate Memory and Initialize MoneyMarketAccount static fields

const int MoneyMarketAccount::FREE_WITHDRAWALS = 2;

const double MoneyMarketAccount::WITHDRAWAL_FEE = 1.50;

//==========================

// CDAccount Declaration

//==========================

class CDAccount: public BankAccount

{

public:

CDAccount(string name, double balance, double rate);

// Sets up a CDAccount object with the name and balance and interestRate

// equal to rate/100 (rate converted to decimal form)

virtual int withdraw(double amt);

// Precondition: amt is a nonnegative number

// Postcondition: If amt + the penalty (PENALTY*interestRate*acctBalance)

// is less than or equal to the account balance, the amount and the

// penalty are subtracted from the balance.  

// Returns OK (0) if there were sufficient funds for the withdrawal and

// returns INSUFFICENT_FUNDS (-1) otherwise.

private:

double interestRate;

static const double PENALTY; //Will use 25% of interest

};

// Allocate Memory and Initialize CDAccount static field

const double CDAccount::PENALTY = 0.25; //25% of interest

void transfer (double amt, BankAccount& fromAcct, BankAccount& toAcct);

// Precondition: amt is a nonnegative number, fromAcct and toAccount

// exist.

// Postcondition: If there are sufficient funds in the fromAccount

// to cover amt plus any charges (withdrawal fee or penalties, depending

// on the type of account), amt plus any charges are subtracted from

// fromAcct and amt is added to toAcct.

//====================

// main function

//====================

int main()

{

string name;

double balance;

double interestRate;

double amount;

int choice;

char moreTransfers;

cout << endl;

cout << "Transferring Money..." << endl;

cout << "First set up three accounts - a basic account, a " << endl;

cout << "Money Market account, and a Certificate of Deposit account."

<< endl;

cout << endl;

cout << "Enter the name of owner of the accounts: ";

cin >> name;

cout << "Enter the opening balance in the basic account: ";

cin >> balance;

BankAccount basicAcct(name, balance);

cout << "Enter the opening balance in the Money Market Account: ";

cin >> balance;

MoneyMarketAccount moneyMarket(name, balance);

cout << "Enter the opening balance for the CD: ";

cin >> balance;

cout << "Enter the interest rate for the CD (for example, 2 for 2%): ";

cin >> interestRate;

CDAccount CD(name, balance, interestRate);

cout << endl;

do

{

cout << "Choose a direction to transfer funds: " << endl;

cout << "1. From the basic account to the Money Market Account" << endl;

cout << "2. From the basic account to the CD" << endl;

cout << "3. From the Money Market Account to the basic account" << endl;

cout << "4. From the Money Market Account to the CD" << endl;

cout << "5. From the CD to the basic account" << endl;

cout << "6. From the CD to the Money Market Account" << endl;

cout << "Enter the number of your choice: ";

cin >> choice;

cout << endl;

cout << "Enter the amount to transfer: ";

cin >> amount;

cout << endl;

switch (choice)

{

case 1:

transfer(amount, basicAcct, moneyMarket);

break;

case 2:

transfer (amount, basicAcct, CD);

break;

case 3:

transfer (amount, moneyMarket, basicAcct);

break;

case 4:

transfer (amount, moneyMarket, CD);

break;

case 5:

transfer (amount, CD, basicAcct);

break;

case 6:

transfer (amount, CD, moneyMarket);

break;

default:

cout << "Invalid choice." << endl;

}

cout << endl;

cout << "Current Balances: " << endl;

cout << "Basic Account: " << basicAcct.getBalance() << endl;

cout << "Money Market Account: " << moneyMarket.getBalance() << endl;

cout << "CD Account: " << CD.getBalance() << endl;

cout << "Transfer more money? (y/n): ";

cin >> moreTransfers;

cout << endl;

}

while (moreTransfers == 'y' || moreTransfers == 'Y');

return 0;

}

//===========================

// BankAccount Definitions

//===========================

BankAccount::BankAccount(string name, double balance)

{

acctName = name;

acctBalance = balance;

}

BankAccount::BankAccount()

{

acctName = "Not Given";

acctBalance = 0.0;

}

string BankAccount::getName() const

{

return acctName;

}

double BankAccount::getBalance() const

{

return acctBalance;

}

void BankAccount::deposit(double amt)

{

if (amt < 0)

{

cout << "Attempt to deposit negative amount - program terminated."

<< endl;

exit(1);

}

acctBalance += amt;

}

int BankAccount::withdraw(double amt)

{

int status = OK;

if (amt < 0)

{

cout << "Attempt to withdraw negative amount - program terminated."

<< endl;

exit(1);

}

if (amt <= acctBalance)

acctBalance -= amt;

else

{

cout << "Amount exceeds balance - withdrawal denied." << endl;

status = INSUFFICIENT_FUNDS;

}

return status;

}

// --------------------------------

// ----- ENTER YOUR CODE HERE -----

// WRITE THE CODE FOR THE FOLLOWING 3 MEMBER FUNCTIONS

// FOR THE MoneyMarketAccount CLASS

// --------------------------------

MoneyMarketAccount::MoneyMarketAccount(string name, double balance)

{

}

int MoneyMarketAccount::getNumWithdrawals() const

{

return 0; // dummy statement for code to compile

}

int MoneyMarketAccount::withdraw(double amt)

{

return 0; // dummy statement for code to compile

}

// --------------------------------

// --------- END USER CODE --------

// WRITE THE CODE FOR THE FOLLOWING 3 MEMBER FUNCTIONS

// FOR THE CDAccount CLASS

// --------------------------------

CDAccount::CDAccount(string name, double balance, double rate)

{

}

int CDAccount::withdraw(double amt)

{

return 0; // dummy statement for code to compile  

}

void transfer (double amt, BankAccount& fromAcct, BankAccount& toAcct)

{

}

  

Explanation / Answer

//Test program
main.cpp

--------------------------------------------------------------

//BankAccount.h
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <string>
using namespace std;
// Constants to indicate status after withdrawal attempt
const int OK = 0;
const int INSUFFICIENT_FUNDS = -1;
//=================================
// BankAccount Class Declaration
//=================================
class BankAccount
{

public:
    BankAccount(string name, double balance);
    // Sets up a BankAccount object with the given name and balance
    BankAccount();      // Default Constructor Added to the exercise
    string getName() const;
    // Returns the name on the BankAccount
    double getBalance() const;
    // Returns the balance on the BankAccount
    void deposit(double amt);
    // Precondition: amt is a nonnegative number
    // Postcondition: amt has been added to the account balance
    virtual int withdraw(double amt);

    // Precondition: amt is a nonnegative number
    // Postcondition: amt has been subtracted from the balance if
    // there were sufficient funds (balance >= amt)
    // Returns 0 (OK) if the withdrawal was completed; -1 (INSUFFICIENT_FUNDS)
    // if there were insufficient funds.
protected:
    string acctName;
    double acctBalance;
};
#endif BANKACCOUNT_H
---------------------------------------------------------------------


//BankAccount.cpp
#include "BankAccount.h"
// BankAccount Definitions
BankAccount::BankAccount(string name, double balance)
{
    acctName = name;
    acctBalance = balance;
}
BankAccount::BankAccount()
{
    acctName = "Not Given";
    acctBalance = 0.0;
}
string BankAccount::getName() const
{
    return acctName;
}
double BankAccount::getBalance() const
{
    return acctBalance;
}
void BankAccount::deposit(double amt)
{
    if (amt < 0)
    {
        cout << "Attempt to deposit negative amount - program terminated."
             << endl;
        exit(1);
    }
    acctBalance += amt;
}
int BankAccount::withdraw(double amt)
{
    int status = OK;
    if (amt < 0)
    {
        cout << "Attempt to withdraw negative amount - program terminated."
             << endl;
        exit(1);
    }

    if (amt <= acctBalance)
        acctBalance -= amt;
    else
    {
        cout << "Amount exceeds balance - withdrawal denied." << endl;
        status = INSUFFICIENT_FUNDS;
    }
    return status;
}

  
---------------------------------------------------------------------

// MoneyMarketClass Declaration
#ifndef MONEYMARKETACCOUNT_H
#define MONEYMARKETACCOUNT_H
#include <iostream>
#include "BankAccount.h"
#include <string>
using namespace std;
class MoneyMarketAccount: public BankAccount
{
public:
    MoneyMarketAccount(string name, double balance);

    // Sets up a MoneyMarketAccount with the given name and balance
    virtual int withdraw(double amt);
    // Precondition: amt is a nonnegative integer
    // Postcondition: If numWithdrawals < FREE_WITHDRAWALS and
    // amt <= acctBalance, amt is subtracted from acctBalance and
    // numWithdrawals is incremented; if numWithdrawals >= FREE_WITHDRAWALS
    // and amt + WITHDRAWAL_FEE <= acctBalance, amt + WITHDRAWAL_FEE is
    // subtracted from acctBalance.
    // Returns 0 (OK) if either of the above occurred; otherwise returns
    // INSUFFICIENT_FUNDS (-1).

    int getNumWithdrawals() const;

    // Returns the number of withdrawals

private:

    int numWithdrawals;
    static const int FREE_WITHDRAWALS;
    static const double WITHDRAWAL_FEE;

};
#endif MONEYMARKETACCOUNT_H

---------------------------------------------------------------------

#include "MoneyMarketAccount.h"
//WRITE THE CODE FOR THE FOLLOWING 3 MEMBER FUNCTIONS
//FOR THE MoneyMarketAccount CLASS
// Allocate Memory and Initialize MoneyMarketAccount static fields
const int MoneyMarketAccount::FREE_WITHDRAWALS = 2;
const double MoneyMarketAccount::WITHDRAWAL_FEE = 1.50;
MoneyMarketAccount::MoneyMarketAccount(string name, double balance)
{
   this->acctName=name;
   this->acctBalance=balance;
}

//Returns number of withdrawls
int MoneyMarketAccount::getNumWithdrawals() const
{
   return numWithdrawals;   
}

int MoneyMarketAccount::withdraw(double amt)
{

   // Precondition: amt is a nonnegative integer
    // Postcondition: If numWithdrawals < FREE_WITHDRAWALS and
    // amt <= acctBalance, amt is subtracted from acctBalance and
    // numWithdrawals is incremented;
   //if numWithdrawals >= FREE_WITHDRAWALS
    // and amt + WITHDRAWAL_FEE <= acctBalance, amt + WITHDRAWAL_FEE is
    // subtracted from acctBalance.
    // Returns 0 (OK) if either of the above occurred; otherwise returns
    // INSUFFICIENT_FUNDS (-1).

   if(getBalance()>0)
   {
       if(getNumWithdrawals()<FREE_WITHDRAWALS)
       {
           if(amt<getBalance())
           {
               //subtract amt from main account balance
               this->acctBalance=this->acctBalance-amt;
               //increment the number of withdrawls
               numWithdrawals++;
               return 0 ;
           }
       }
       else if(getNumWithdrawals()>=FREE_WITHDRAWALS && (amt+WITHDRAWAL_FEE)<getBalance())
       {
           this->acctBalance=this->acctBalance-amt-WITHDRAWAL_FEE;
           //increment the number of withdrawls
           numWithdrawals++;
           return 0;
       }
   }
   else
       return INSUFFICIENT_FUNDS;  
}

---------------------------------------------------------------------
// MoneyMarketClass Declaration
#ifndef CDACCOUNT_H
#define CDACCOUNT_H
#include <iostream>
#include "BankAccount.h"
#include <string>
using namespace std;
// CDAccount Declaration
class CDAccount: public BankAccount
{
public:
    CDAccount(string name, double balance, double rate);
    // Sets up a CDAccount object with the name and balance and interestRate
    // equal to rate/100 (rate converted to decimal form)

    virtual int withdraw(double amt);
    // Precondition: amt is a nonnegative number
    // Postcondition: If amt + the penalty (PENALTY*interestRate*acctBalance)
    // is less than or equal to the account balance, the amount and the
    // penalty are subtracted from the balance.
    // Returns OK (0) if there were sufficient funds for the withdrawal and
    // returns INSUFFICENT_FUNDS (-1) otherwise.

private:
    double interestRate;
    static const double PENALTY;     //Will use 25% of interest
};
#endif CDACCOUNT_H

---------------------------------------------------------------------


#include "CDAccount.h"
// Allocate Memory and Initialize CDAccount static field
const double CDAccount::PENALTY = 0.25; //25% of interest
CDAccount::CDAccount(string name, double balance, double rate)
{
   this->acctName=name;
   this->acctBalance=balance;
   this->interestRate=rate;
}
// Precondition: amt is a nonnegative number
    // Postcondition: If amt + the penalty (PENALTY*interestRate*acctBalance)
    // is less than or equal to the account balance, the amount and the
    // penalty are subtracted from the balance.
    // Returns OK (0) if there were sufficient funds for the withdrawal and
    // returns INSUFFICENT_FUNDS (-1) otherwise.
int CDAccount::withdraw(double amt)
{

   double penalty=0;

   if(getBalance()>0)
   {
       penalty=PENALTY*interestRate*acctBalance;

       if(amt+penalty<= getBalance())
       {
           this->acctBalance=this->acctBalance-amt-penalty;
           return 0;
       }
   }
   else
       return INSUFFICIENT_FUNDS;
  
}

---------------------------------------------------------------------

//tester program
#include<iostream>
#include<string.h>
//Include header files in header file section
#include "BankAccount.h"
#include "MoneyMarketAccount.h"
#include "CDAccount.h"
using namespace std;

//function prototypes
void transfer(double amount, BankAccount &basicAcct, MoneyMarketAccount &moneyMarket);
void transfer (double amount, BankAccount &basicAcct, CDAccount &CD);
void transfer (double amount, MoneyMarketAccount &moneyMarket, BankAccount &basicAcct);
void transfer (double amount, MoneyMarketAccount &moneyMarket, CDAccount &CD);
void transfer (double amount, CDAccount &CD, BankAccount &basicAcct);
void transfer (double amount, CDAccount &CD, MoneyMarketAccount &moneyMarket);


//   main function
int main()
{
    string name;
    double balance;
    double interestRate;
    double amount;
    int choice;
    char moreTransfers;
    cout << endl;
    cout << "Transferring Money..." << endl;
    cout << "First set up three accounts - a basic account, a " << endl;
    cout << "Money Market account, and a Certificate of Deposit account." << endl;
    cout << endl;
    cout << "Enter the name of owner of the accounts: ";
    cin >> name;
    cout << "Enter the opening balance in the basic account: ";
    cin >> balance;
    BankAccount basicAcct(name, balance);
    cout << "Enter the opening balance in the Money Market Account: ";
    cin >> balance;
    MoneyMarketAccount moneyMarket(name, balance);
    cout << "Enter the opening balance for the CD: ";
    cin >> balance;
    cout << "Enter the interest rate for the CD (for example, 2 for 2%): ";
    cin >> interestRate;
    CDAccount CD(name, balance, interestRate);
    cout << endl;
  
  
   do
    {
        cout << "Choose a direction to transfer funds: " << endl;
        cout << "1. From the basic account to the Money Market Account" << endl;
        cout << "2. From the basic account to the CD" << endl;
        cout << "3. From the Money Market Account to the basic account" << endl;
        cout << "4. From the Money Market Account to the CD" << endl;
        cout << "5. From the CD to the basic account" << endl;
        cout << "6. From the CD to the Money Market Account" << endl;
        cout << "Enter the number of your choice: ";
        cin >> choice;
        cout << endl;       
        cout << "Enter the amount to transfer: ";
        cin >> amount;
        cout << endl;
        switch (choice)
        {
        case 1:
            transfer(amount, basicAcct, moneyMarket);
            break;
        case 2:
            transfer (amount, basicAcct, CD);
            break;
        case 3:
            transfer (amount, moneyMarket, basicAcct);
            break;
        case 4:
            transfer (amount, moneyMarket, CD);
            break;
        case 5:
            transfer (amount, CD, basicAcct);
            break;
        case 6:
            transfer (amount, CD, moneyMarket);
            break;
        default:
            cout << "Invalid choice." << endl;
        }

        cout << endl;
        cout << "Current Balances: " << endl;
        cout << "Basic Account: " << basicAcct.getBalance() << endl;
        cout << "Money Market Account: " << moneyMarket.getBalance() << endl;
        cout << "CD Account: " << CD.getBalance() << endl;
        cout << "Transfer more money? (y/n): ";
        cin >> moreTransfers;
        cout << endl;

    }while (moreTransfers == 'y' || moreTransfers == 'Y');
   system("pause");
    return 0;
}

//The method defintions of transfer of money from three account ,total 6 possibiliteis of transfer of accounts
void transfer(double amount, BankAccount &basicAcct, MoneyMarketAccount &moneyMarket)
{
   //withdraw the amount from basicAcct object
   basicAcct.withdraw(amount);
   //Add the amount to the moneyMarket object
   moneyMarket.deposit(amount);
}
void transfer (double amount, BankAccount &basicAcct, CDAccount &CD)
{
   //withdraw the amount from basicAcct object
   basicAcct.withdraw(amount);
   //Add the amount to the CD object
   CD.deposit(amount);
}
void transfer (double amount, MoneyMarketAccount &moneyMarket, BankAccount &basicAcct)
{
   //withdraw the amount from moneyMarket object
   moneyMarket.withdraw(amount);
   //Add the amount to the basicAcct object
   basicAcct.deposit(amount);
}
void transfer (double amount, MoneyMarketAccount &moneyMarket, CDAccount &CD)
{
   //withdraw the amount from moneyMarket object
   moneyMarket.withdraw(amount);
   //Add the amount to the CD object
   CD.deposit(amount);

}
void transfer (double amount, CDAccount &CD, BankAccount &basicAcct)
{
   //withdraw the amount from CD object
   CD.withdraw(amount);
   //Add the amount to the basicAcct object
   basicAcct.deposit(amount);
}
void transfer (double amount, CDAccount &CD, MoneyMarketAccount &moneyMarket)
{
   //withdraw the amount from CD object
   CD.withdraw(amount);
   //Add the amount to the basicAcct object
   moneyMarket.deposit(amount);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote