C++ Project – Bank Management System using File Part – 2

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

//Object Oriented Based Banking System with File Storage

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class BankAccount 
{
private:
    int accNo;  // Account number (unique ID)
    string name; // Account holder's name
    double balance; // Current balance in the account

public:
    BankAccount() 
    {
        accNo = 0;
        balance = 0.0;
    }

    void createAccount() 
    {
        cout << "\nEnter Account Number: ";
        cin >> accNo;
        cin.ignore(); // flush newline
        cout << "Enter Account Holder Name: ";
        getline(cin, name);
        cout << "Enter Initial Balance: ";
        cin >> balance;
    }

    void display() 
    {
        cout << "\nAccount No: " << accNo;
        cout << "\nName: " << name;
        cout << "\nBalance: " << balance << endl;
    }

    void deposit(double amt) 
    {
        balance += amt;
    }

    void withdraw(double amt) 
    {
        if (amt > balance)
            cout << "\n Insufficient Balance!\n";
        else
            balance -= amt;
    }

    int getAccNo() const 
    {
        return accNo;
    }

    void writeToFile() 
    {
        ofstream fout("bank_accounts_new.dat", ios::binary | ios::app);
        fout.write((char*)this, sizeof(*this));
        fout.close();
        cout << "\n Account created and saved to file.\n";
    }

    static void updateInFile(int acc, double amt, bool isDeposit) 
    {
        BankAccount temp;
        fstream file("bank_accounts_new.dat", ios::binary | ios::in | ios::out);
        bool found = false;

        while (file.read((char*)&temp, sizeof(temp))) {
            if (temp.getAccNo() == acc) {
                found = true;
                if (isDeposit) {
                    temp.deposit(amt);
                    cout << "\n Deposit successful.\n";
                } else {
                    temp.withdraw(amt);
                    cout << "\n Withdrawal processed.\n";
                }

                int pos = -1 * (int)sizeof(temp);
                file.seekp(pos, ios::cur); // move back to the record's position
                file.write((char*)&temp, sizeof(temp));
                break;
            }
        }

        file.close();

        if (!found)
            cout << "\n Account not found.\n";
    }

    static void showAccount(int acc) {
        BankAccount temp;
        ifstream fin("bank_accounts_new.dat", ios::binary);
        bool found = false;

        while (fin.read((char*)&temp, sizeof(temp))) {
            if (temp.getAccNo() == acc) {
                temp.display();
                found = true;
                break;
            }
        }

        fin.close();
        if (!found)
            cout << "\n Account not found.\n";
    }
};

int main()
 {
    int choice, accNo;
    double amount;

    do {
        cout << "\n\n---------------SBI BANK MENU ---------------";
        cout << "\n1. Create Account";
        cout << "\n2. Deposit Amount";
        cout << "\n3. Withdraw Amount";
        cout << "\n4. Display Account Info";
        cout << "\n5. Exit";
        cout << "\n-----------------------------------------------------";
        cout << "\nEnter your choice: ";
        cin >> choice;

        switch (choice) 
        {
        case 1: 
        {
            BankAccount acc;
            acc.createAccount();
            acc.writeToFile();
            break;
        }

        case 2:
            cout << "\nEnter Account Number: ";
            cin >> accNo;
            cout << "Enter Amount to Deposit: ";
            cin >> amount;
            BankAccount::updateInFile(accNo, amount, true);
            break;

        case 3:
            cout << "\nEnter Account Number: ";
            cin >> accNo;
            cout << "Enter Amount to Withdraw: ";
            cin >> amount;
            BankAccount::updateInFile(accNo, amount, false);
            break;

        case 4:
            cout << "\nEnter Account Number: ";
            cin >> accNo;
            BankAccount::showAccount(accNo);
            break;

        case 5:
            cout << "\nThank you for using SBI Bank!\n";
            break;

        default:
            cout << "\n Invalid choice. Try again.\n";
        }

    } while (choice != 5);

    return 0;
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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