1
votes

I'm currently wrapping up my bank account program, but I've ran into some problems en route to completion. The problem seems pretty easy to resolve, but I can't quite wrap my head around how I should go about actually fixing it.

I'll first include the assignment below:

  1. Implement a class Account. An account has a balance, functions to add and withdraw money, and a function to inquire the current balance. Pass a value into a constructor to set an initial balance. If no value is passed the initial balance should be set to $0. Charge a $5 penalty if an attempt is made to withdraw more money than available in the account. Enhance the Account class to compute interest on the current balance.

  2. Implement a class Bank. This bank has two objects, checking and savings, of the type Account that was developed in the preceding exercise.

Implement four instance methods:

deposit(double amount, String account)
withdraw(double amount, String account)
transfer(double amount, String account)
printBalances()

Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected. For a transfer it indicates the account from which the money is taken; the money is automatically transferred to the other account.

The only problem appears to be with actually storing the information for each account in the balance variable in the Account.cpp file. It just stays at 0, and that's why I feel this issue should be easy to fix. I'd imagine I'm just forgetting something very basic about class implementations, but that is why I am here! Now that I think about it, I think part of my confusion comes from the fact that I've implemented similar programs before but used only arrays instead of variables, and I did not experience this same problem. The data seemed to get stored into the array regardless, so this may be my problem? The code follows:

Account.h:

#include <iostream>
#include <string>
#include <iomanip>      

using namespace std;

class Account
{
public:
    Account();
    Account(double balance);
    void Add(double money);
    void Withdraw(double money);
    double GetBalance();

private:
    double balance; 
};

Account.cpp:

#include "Account.h"

//  Penalty Fee Constant
const double PENALTY_FEE = 5.00;

Account::Account()
{
    balance = 0.00;
}

Account::Account(double money)
{
    balance = money;
}

void Account::Add(double money)
{
    balance += money;
}

void Account::Withdraw(double money)
{
    if(money > balance)
        balance += PENALTY_FEE;
    else
        balance -= money;
}

double Account::GetBalance()
{
    return balance;
}

Bank.cpp:

#include "Account.h"

void deposit(double, string);
void withdraw(double, string);
void transfer(double, string);
void printBalances();

int main()
{
    string accountChoice;
    int selection;
    double transaction = 0;

    //  !!!!!!!!!!!!!!!!!!HAVE TO STILL COMPUTE INTEREST!!!!!!!!!!!!!!!!

    cout << fixed << showpoint << setprecision(2);

    do
    {
        cout << "Please make a selection:" << endl;
        cout << "1.) Deposit" << endl;
        cout << "2.) Withdraw" << endl;
        cout << "3.) Transfer" << endl;
        cout << "4.) Print balances" << endl;
        cout << "5.) Quit" << endl;
        cin >> selection;

        if(selection == 1)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be deposited:" << endl;
            cin >> transaction;
            cout << endl;

            deposit(transaction, accountChoice);
        }
        else if(selection == 2)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be withdrawn:" << endl;
            cin >> transaction;
            cout << endl;

            withdraw(transaction, accountChoice);
        }
        else if(selection == 3)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be transferred:" << endl;
            cin >> transaction;
            cout << endl;

            transfer(transaction, accountChoice);
        }
        else if(selection == 4)
            printBalances();
        else
            cout << "Closing program -- Thank you for using the ATM teller!" << endl;
    }while(selection != 5);


    system("pause");
    return 0;
}

void deposit(double amount, string account)
{
    Account savings, checking;

    if(account == "S" || account == "s")
        savings.Add(amount);
    else
        checking.Add(amount);
}

void withdraw(double amount, string account)
{
    Account savings, checking;

    if(account == "S" || account == "s")
        savings.Withdraw(amount);
    else
        checking.Withdraw(amount);
}

void transfer(double amount, string account)
{
    Account savings, checking;

    if(account == "S" || account == "s")
    {
        savings.Withdraw(amount);
        checking.Add(amount);
    }
    else
    {
        checking.Withdraw(amount);
        savings.Add(amount);
    }
}

void printBalances()
{
    Account savings, checking;

    cout << "The current balance in savings is: " << savings.GetBalance() << endl;
    cout << "The current balance in checking is: " << checking.GetBalance() << endl << endl;
}
2
In the action functions, you create a couple accounts: 'Account savings, checking;', call methods and then return, ensuring that the accounts get RAII'd away. - Martin James
Sorry if I didn't quite pick up on your terminology correctly, but are you suggesting that I change these methods from type void to a return type? That makes total sense to me, but I just want to verify! - Gooeybanana
Oh wait - the action functions are not member functions of Account! - Martin James
This is a bit messed up. - Martin James
You can't solve this by flipping switches blindly; you must understand what the code is doing and what's wrong with it. You should always develop new functionality in isolation, but since you haven't done that I suggest you reduce it to a minimal complete example, for your sake as much as for ours. - Beta

2 Answers

1
votes

I think it might be clearer overall if you declare another class 'Customer', and give them a name, customer number and a checking and saving account each. The Customers should be instantiated somewhere with process lifetime so that they are not deleted, eg. in a static container, eg std::map.

ATM, (sorry!), you seem to have some non-member functions that instantiate accounts, do things with them and then they are deleted upon function return.

0
votes

You are creating a new Account object every time you need it. Of course it will be 0 when you print it as the default constructor initializes the balance to 0.

Rather when the App starts, as the user to identify his account or something and create a corresponding account instance. The instance need to be there throughout the whole time user operates on it.

So instantiate not in the methods but in the main function. And pass the instance to the methods as a way of modifying the instance as required.