Skip to main content

Bank Management System in C++ | Simple Project with Source Code

 Bank Management System in C++: A Complete Guide



Introduction

Bank Management System is a software application that helps in managing various banking operations such as account creation, deposit, withdrawal, balance inquiry, and more. In this post, we will develop a Bank Management System using C++ with file handling to store account details.


Features of the Bank Management System

Our system will include the following features:

  1. Create a New Account

  2. Deposit Money

  3. Withdraw Money

  4. Check Balance

  5. View Account Details

  6. Modify Account Information

  7. Delete an Account

  8. Exit the Program


Implementation of Bank Management System in C++

The system will use Object-Oriented Programming (OOP) principles like classes and objects. The file handling feature will be used to store account details permanently.


Code for the Bank Management System

Here is the complete code for the Bank Management System in C++:

cpp
#include <iostream>
#include <fstream>
#include<iomanip> using namespace std; 
class BankAccount
int accountNumber;
char name[50];
double balance; 
public
void createAccount();
void showAccount() const;
void deposit(double amount);
void withdraw(double amount);
int getAccountNumber() 
const {
return accountNumber; 
} double getBalance() 
const {
return balance; 
}
void updateAccount();
 }; // Function to create a new account 
void BankAccount::createAccount() {
 cout << "\nEnter Account Number: ";
 cin >> accountNumber;
 cout << "Enter Account Holder Name: ";
 cin.ignore();
 cin.getline(name, 50);
 cout << "Enter Initial Deposit: "
 cin >> balance; cout << "\nAccount Created Successfully!\n"
} // Function to display account details 
void BankAccount::showAccount() const 
 cout << "\nAccount Number: " << accountNumber; cout << "\nAccount Holder: " << name;
 cout << "\nBalance: $" << fixed << setprecision(2) << balance << endl;
 } // Function to deposit money 
void BankAccount::deposit(double amount)
 {
 balance += amount; 
} // Function to withdraw money 
void BankAccount::withdraw(double amount) { 
if (amount > balance) { cout << "\nInsufficient Balance!\n"
 }
else { balance -= amount; cout << "\nWithdrawal Successful!\n"
 } 
// Function to update account details void BankAccount::updateAccount() { cout << "\nEnter New Account Holder Name: "
 cin.ignore();
 cin.getline(name, 50); cout << "Enter New Balance: ";
 cin >> balance;
 } 
// Function to write account details to a file void saveAccount(BankAccount &acc) { ofstream outFile("accounts.dat"
ios::binary | ios::app);
 outFile.write(reinterpret_cast<char *>(&acc), 
sizeof(BankAccount));
 outFile.close(); 
} // Function to display all accounts void displayAllAccounts() { BankAccount acc;
ifstream inFile("accounts.dat"
ios::binary); if (!inFile) { 
 cout << "\nNo Accounts Found!\n"
return
 } 
 cout << "\n----- Account List -----\n";
while (inFile.read(reinterpret_cast<char *>(&acc), sizeof(BankAccount))) {
 acc.showAccount(); cout << "----------------------\n";
 }
 inFile.close();
 } 
// Function to search an account void searchAccount(int accNum) {
 BankAccount acc;
ifstream inFile("accounts.dat", ios::binary);
bool found = false;
while (inFile.read(reinterpret_cast<char *>(&acc), sizeof(BankAccount))) {
if (acc.getAccountNumber() == accNum) { acc.showAccount(); 
 found = true;
break;
 }
 } inFile.close();
if (!found) { cout << "\nAccount Not Found!\n";
 } } 
// Function to update account details void updateAccount(int accNum) { fstream file("accounts.dat", ios::binary | ios::in | ios::out); BankAccount acc;
bool found = false;
while (!file.eof() && file.read(reinterpret_cast<char *>(&acc), sizeof(BankAccount))) { if
(acc.getAccountNumber() == accNum) {
 acc.updateAccount(); 
 file.seekp(-static_cast<int>(sizeof(BankAccount)), ios::cur);
 file.write(reinterpret_cast<char *>(&acc), sizeof(BankAccount));
 found = true; cout << "\nAccount Updated Successfully!\n";
break
 } } file.close();
if (!found) { cout << "\nAccount Not Found!\n"
 } } 
// Function to delete an account void deleteAccount(int accNum) { 
 BankAccount acc;
ifstream inFile("accounts.dat", ios::binary);
ofstream outFile("temp.dat", ios::binary);
bool found = false;
while (inFile.read(reinterpret_cast<char *>(&acc), sizeof(BankAccount))) {
if (acc.getAccountNumber() == accNum) { found = true;
 }
else { outFile.write(reinterpret_cast<char *>(&acc),
sizeof(BankAccount)); } } inFile.close();
 outFile.close(); 
remove("accounts.dat"); 
rename("temp.dat", "accounts.dat"); 
if (found) {
 cout << "\nAccount Deleted Successfully!\n"; } 
else
 cout << "\nAccount Not Found!\n"
 } 
// Main Function 
int main() {
int choice, accNum;
 BankAccount acc; double amount; 
do {
 cout << "\n------ Bank Management System ------\n";
 cout << "1. Create Account\n"
 cout << "2. View All Accounts\n"
 cout << "3. Search Account\n";
 cout << "4. Deposit Money\n";
 cout << "5. Withdraw Money\n"
 cout << "6. Update Account\n"
 cout << "7. Delete Account\n";
 cout << "8. Exit\n"
 cout << "Enter Your Choice: ";
 cin >> choice;
switch (choice)
 { 
case 1: acc.createAccount(); saveAccount(acc); break
case 2: displayAllAccounts(); break
case 3: cout << "\nEnter Account Number to Search: "; cin >> accNum; searchAccount(accNum); break
case 4: cout << "\nEnter Account Number: "; cin >> accNum; cout << "Enter Amount to Deposit: "; cin >> amount; updateAccount(accNum); break;
case 5: cout << "\nEnter Account Number: "; cin >> accNum; cout << "Enter Amount to Withdraw: "; cin >> amount; updateAccount(accNum); break; case 6: cout << "\nEnter Account Number to Update: "; cin >> accNum; updateAccount(accNum); break
case 7: cout << "\nEnter Account Number to Delete: "; cin >> accNum; deleteAccount(accNum);
break
case 8:
 cout << "\nThank you for using the Bank Management System!\n"; break; default:
 cout << "\nInvalid Choice! Try Again.\n"; } } while 
(choice != 8);
return 0;
 }

Explanation of the Code

  1. Class BankAccount

    • Stores account details.

    • Provides functions to create an account, show account details, deposit, withdraw, and update account information.

  2. File Handling

    • saveAccount() stores account details in a file.

    • displayAllAccounts() reads and displays all accounts from the file.

    • searchAccount() searches for a specific account.

    • updateAccount() modifies an existing account.

    • deleteAccount() removes an account.

  3. Menu-Driven System

    • Users can interact with the program through a menu.


Conclusion

This Bank Management System in C++ demonstrates Object-Oriented Programming and File Handling. You can enhance it by adding password protectioninterest calculation, or database integration.

Would you like any modifications or additional features