CREATING BANKING SYSTEM IN C++

Table of Contents

Introduction

The program we’re about to discuss is a banking system created in C++ using advanced programming concepts such as object-oriented programming (OOP), multithreading, and file handling. This system allows users to perform common banking activities, such as account creation, deposits, withdrawals, and interest calculations. The program also saves the account details in a file for later retrieval.

Banking systems are used in many sectors, particularly in financial institutions, fintech startups, and e-commerce platforms to manage users’ accounts, transactions, and financial data securely.

CREATING BANKING SYSTEM IN C++ by Srijan Acharya

Purpose of the Program

The main objectives of this program are:

  • Account Management: Create and manage different types of bank accounts (Savings and Current).
  • Transactions: Handle deposits and withdrawals.
  • Interest Calculation: Add interest to savings accounts.
  • Multithreading: Simulate parallel banking operations.
  • Persistence: Save account data into a file for later use.

Explanation of the Code

Let’s go through the program step by step:

Header Inclusions

#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
C++

#include <iostream>: Allows the use of standard input/output streams (e.g., cout, cin).

#include <fstream>: Used for file handling, allowing us to read from and write to files.

#include <vector>: Used to store a collection of objects (in this case, bank accounts).

#include <thread>: Provides multithreading functionality for running functions in parallel.

#include <chrono>: Used to work with time intervals, mainly for simulating delays.

Class Definitions

class BankAccount {
protected:
    string name;
    string accountNumber;
    double balance;

public:
    BankAccount(string name, string accountNumber, double balance = 0.0) 
        : name(name), accountNumber(accountNumber), balance(balance) {}
C++

BankAccount class: This is a base class that holds common account attributes (name, account number, and balance).

protected: This keyword ensures that derived classes have access to the data members but they are not publicly accessible.

public: Methods like deposit, withdraw, and displayInfo are accessible outside the class.

The constructor initializes a new BankAccount object.

    virtual void displayInfo() {
        cout << "Account Holder: " << name << endl;
        cout << "Account Number: " << accountNumber << endl;
        cout << "Balance: $" << balance << endl;
    }
C++

displayInfo(): Displays account details. It’s a virtual function, which means it can be overridden by derived classes to provide specialized behavior

    virtual void deposit(double amount) {
        balance += amount;
        cout << "$" << amount << " deposited successfully. Current Balance: $" << balance << endl;
    }
C++

deposit(): Adds a specified amount to the account balance.

    virtual void withdraw(double amount) {
        if (amount > balance) {
            cout << "Insufficient balance!" << endl;
        } else {
            balance -= amount;
            cout << "$" << amount << " withdrawn successfully. Current Balance: $" << balance << endl;
        }
    }
C++

withdraw(): Withdraws a specified amount from the account, but ensures that the balance does not go below zero.

    virtual void calculateInterest(double rate) {
        // Base class does not provide interest calculation
    }
};
C++

calculateInterest(): This function is intended to calculate interest, but it is not implemented in the base class. Derived classes can implement their version.

class SavingsAccount : public BankAccount {
public:
    SavingsAccount(string name, string accountNumber, double balance = 0.0)
        : BankAccount(name, accountNumber, balance) {}

    void calculateInterest(double rate) override {
        double interest = balance * (rate / 100);
        balance += interest;
        cout << "Interest of $" << interest << " added. New Balance: $" << balance << endl;
    }
};
C++

SavingsAccount: Inherits from BankAccount and overrides the calculateInterest() function to add interest based on the account balance

class CurrentAccount : public BankAccount {
public:
    CurrentAccount(string name, string accountNumber, double balance = 0.0)
        : BankAccount(name, accountNumber, balance) {}

    void calculateInterest(double rate) override {
        // No interest for current accounts
        cout << "No interest for current accounts." << endl;
    }
};
C++

CurrentAccount: This type of account does not accumulate interest, so the calculateInterest() function simply informs the user.

Multithreading Function

void simulateBanking(BankAccount* account) {
    account->deposit(500);
    this_thread::sleep_for(chrono::seconds(1));  // Simulates time delay
    account->withdraw(200);
}
C++

simulateBanking(): This function simulates banking activities (deposit and withdraw) for multithreading purposes.

Main Function

int main() {
    vector<BankAccount*> accounts;
C++

vector<BankAccount*> accounts: A vector is used to store pointers to BankAccount objects, allowing multiple account types to be stored in a single collection.

Menu Interface

do {
    cout << "\n--- Banking System Menu ---\n";
    cout << "1. Create Savings Account\n";
    cout << "2. Create Current Account\n";
    cout << "3. Display Account Info\n";
    cout << "4. Deposit Money\n";
    cout << "5. Withdraw Money\n";
    cout << "6. Calculate Interest\n";
    cout << "7. Save Account Info to File\n";
    cout << "8. Exit\n";
    cout << "Enter your choice: ";
    cin >> choice;
C++

The program offers a menu to interact with various banking functions. The user’s choice determines what operation is performe

Account Creation

case 1: {
    string name, accountNumber;
    double initialDeposit;
    cout << "Enter Name: ";
    cin >> name;
    cout << "Enter Account Number: ";
    cin >> accountNumber;
    cout << "Enter Initial Deposit: ";
    cin >> initialDeposit;
    BankAccount* account = new SavingsAccount(name, accountNumber, initialDeposit);
    accounts.push_back(account);
    cout << "Savings Account Created Successfully!\n";
    break;
}
C++

This case creates a new SavingsAccount, stores it in the vector, and informs the user of success.

File Handling

case 7: {
    ofstream outFile("accounts.txt");
    for (BankAccount* account : accounts) {
        outFile << "Account Holder: " << account->getAccountNumber() << "\n";
        outFile << "Balance: $" << account->getBalance() << "\n";
        outFile << "---------------------------\n";
    }
    outFile.close();
    cout << "Account info saved to file.\n";
    break;
}
C++

File Handling: The account information is written to a file named accounts.txt for later retrieval.

Multithreading Example

if (!accounts.empty()) {
    thread t1(simulateBanking, accounts[0]);
    thread t2(simulateBanking, accounts[1]);
    t1.join();
    t2.join();
}
C++

Multithreading: The program creates two threads to perform deposit and withdrawal operations simultaneously on different accounts

Output Explanation

After running the program, the output will depend on the user’s interactions through the menu. For example:

If a savings account is created, you will see:

Enter Name: John
Enter Account Number: 12345
Enter Initial Deposit: 5000
Savings Account Created Successfully!
Bash

If you deposit money into an account:

Enter Account Number: 12345
Enter Amount to Deposit: 1000
$1000 deposited successfully. Current Balance: $6000
Bash

If you display the account info:

Account Holder: John
Account Number: 12345
Balance: $6000
Bash

If interest is calculated (for a savings account):

Enter Account Number: 12345
Enter Interest Rate: 5
Interest of $300 added. New Balance: $6300
Bash

Use Cases for the Program

  • Financial Sector: Banks and financial institutions use such systems for account management, transaction handling, and interest calculations.
  • Fintech Startups: Companies providing digital wallets or payment platforms use similar code to manage user accounts.
  • E-commerce: Platforms handling refunds, gift cards, or digital balances can adapt this system.
  • Educational Purposes: This program is an excellent example for teaching OOP, file handling, and multithreading in C++.

References

  • Bjarne Stroustrup, The C++ Programming Language, Addison-Wesley.
  • Nicolai M. Josuttis, The C++ Standard Library: A Tutorial and Reference, Addison-Wesley.
  • C++ reference website: cplusplus.com

Posted in C++

Leave a Reply

Your email address will not be published. Required fields are marked *

Resize text
Scroll to Top