Bank Account System with Object-Oriented Programming in C++

In this tutorial, we will use all our knowledge of Object-Oriented Programming in C++, and create a real example of application, a bank account.

How to make a Bank Account System
Exercise: Bank Account

Create a system in which a user bank account is created, where he can check his balance, deposit and withdraw money, use object-oriented knowledge.

Your system should start by displaying a menu with these options, and allow the user to perform these operations as many times as he wants, and decide when to close the system.


The class: Client.h

The customer's balance will be stored in the cash variable. It is initialized with a value of 0, in the constructor function, that is, the customer obviously starts with 0 of money.

We have the menu() function, which will display the options menu, that is, the screen of the ATM of the banking system. It invokes the action() function, which will call each specific operation.

The first operation is checkBalance(), which will check the balance, the account balance.

Then there is the depositCash() function, which is used to add money to your account.

Finally, the withdraw() function, to withdraw money.

Our class looks like this:

#ifndef CLIENT_H
#define CLIENT_H


class Client
{
    public:
        Client();
        void menu();
        void action(int);
        void checkBalance();
        void depositCash();
        void withdraw();

    private:
        double cash;
};

#endif // CLIENT_H

Source: Client.cpp

Everything will start where you should start, with the Client() constructor function.

It simply clears your money, as it is as if your account was created the moment you instantiated the object.

The menu() function will be displaying a menu of options for the user, and asking him to enter a value. This value is stored in the op variable and sent to the action() function, this is done within a do while loop, which will only end if you type 0. In other words, you only exit the menu, if you type 0 to exit.

The action() will take an action, evaluating the op value received from the menu(). This is done in a switch conditional test.

If it is 0, it says that the system is shutting down.

If it is 1, invoke the checkBalance() function, which will simply display the cash variable. Nothing more.

At depositCash (), we ask the user for a value. But we have to test whether this value is positive, because obviously there is no way to deposit a negative value. This test is done on an IF ELSE. If the value is positive, we simply add it to the cash and display the user's balance.

In the withdraw() function, we ask for the amount the user wants to withdraw. Before removing this value from the cash variable, we need to assess whether it is less than cash. After all, you can only withdraw something below, or at most equal to your balance. This test is done with an IF ELSE as well.

Here's how the code looks:

#include "Client.h"
#include <iostream>
using namespace std;

Client::Client()
{
    cash = 0;
    menu();
}

void Client::menu()
{
    int op=0;

    do{
        cout<<"0. Exit"<<endl;
        cout<<"1. Check balance"<<endl;
        cout<<"2. Deposit money"<<endl;
        cout<<"3. Withdraw money"<<endl;
        cin >> op;

        action(op);

    }while(op);
}

void Client::action(int op)
{
    switch(op)
    {
        case 0:
                cout << "Shutting down system."<<endl;
                break;
        case 1:
                checkBalance();
                break;

        case 2:
                depositCash();
                break;

        case 3:
                withdraw();
                break;

        default:
                cout << "Invalid option"<<endl;
    }

}

void Client::checkBalance()
{
    cout<<"\nYour balance is: $" << cash <<endl;
}

void Client::depositCash()
{
    double val;

    cout<< "Value to deposit: ";
    cin >> val;

    if(val>0){
        cash += val;
        checkBalance();
    }
    else
        cout<<"Invalid value, try again"<<endl;

}

void Client::withdraw()
{
    double val;

    cout<< "Value to withdraw: ";
    cin >> val;

    if(val<=cash){
        cash -= val;
        checkBalance();
    }
    else
        cout<<"Insufficient funds"<<endl;
}

main.cpp

Our main will simply instantiate an object. Automatically it activates the constructor function, and everything starts to happen:

#include "Client.h"

int main()
{
    Client c;
    return 0;
}

See how our main() is simple, clean and to the point.


Hacker challenge

There is a way to hack this system. To add money to your account in a 'hacker' way. Can you find out how it is? Type in the comments.

No comments:

Post a Comment