Bank Management System Project in C++ Source Code
This article will guide you through the development of a basic Bank Management System using C++. This project will cover core functionalities like creating accounts, depositing and withdrawing funds, and displaying account information.
Project Structure
The project will be structured with the following files:
main.cpp
: The main program file, handling user interaction and calling relevant functions.account.cpp
: Contains classes and functions for managing account data.account.h
: Header file for the account class.
1. Account Class (account.h
and account.cpp
)
account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include
class Account {
private:
int accountNumber;
std::string accountHolderName;
double balance;
public:
// Constructor
Account(int accNo, std::string name, double initialBalance);
// Getters
int getAccountNumber() const;
std::string getAccountHolderName() const;
double getBalance() const;
// Methods
void deposit(double amount);
void withdraw(double amount);
};
#endif
account.cpp
#include "account.h"
#include
// Constructor
Account::Account(int accNo, std::string name, double initialBalance) {
accountNumber = accNo;
accountHolderName = name;
balance = initialBalance;
}
// Getters
int Account::getAccountNumber() const {
return accountNumber;
}
std::string Account::getAccountHolderName() const {
return accountHolderName;
}
double Account::getBalance() const {
return balance;
}
// Methods
void Account::deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposit successful. New balance: " << balance << std::endl;
} else {
std::cout << "Invalid deposit amount." << std::endl;
}
}
void Account::withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrawal successful. New balance: " << balance << std::endl;
} else {
std::cout << "Insufficient funds or invalid withdrawal amount." << std::endl;
}
}
2. Main Program (main.cpp
)
#include
#include "account.h"
#include
using namespace std;
int main() {
vector accounts;
int choice;
int accountNumber;
do {
cout << "\nBank Management System" << endl;
cout << "1. Create Account" << endl;
cout << "2. Deposit" << endl;
cout << "3. Withdraw" << endl;
cout << "4. Display Account Details" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: { // Create Account
string name;
double initialBalance;
cout << "Enter account holder name: ";
cin.ignore(); // Consume newline character from previous input
getline(cin, name);
cout << "Enter initial balance: ";
cin >> initialBalance;
// Generate a unique account number (replace with your logic)
accountNumber = accounts.size() + 1;
accounts.push_back(Account(accountNumber, name, initialBalance));
cout << "Account created successfully. Account Number: " << accountNumber << endl;
break;
}
case 2: { // Deposit
cout << "Enter account number: ";
cin >> accountNumber;
// Find the account (add error handling)
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accountNumber) {
double amount;
cout << "Enter deposit amount: ";
cin >> amount;
acc.deposit(amount);
break;
}
}
break;
}
case 3: { // Withdraw
cout << "Enter account number: ";
cin >> accountNumber;
// Find the account (add error handling)
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accountNumber) {
double amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
acc.withdraw(amount);
break;
}
}
break;
}
case 4: { // Display Account Details
cout << "Enter account number: ";
cin >> accountNumber;
// Find the account (add error handling)
for (auto& acc : accounts) {
if (acc.getAccountNumber() == accountNumber) {
cout << "Account Number: " << acc.getAccountNumber() << endl;
cout << "Account Holder: " << acc.getAccountHolderName() << endl;
cout << "Balance: " << acc.getBalance() << endl;
break;
}
}
break;
}
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice." << endl;
}
} while (choice != 5);
return 0;
}
Explanation:
- Account Class:
- The
Account
class encapsulates data (account number, name, balance) and methods (deposit, withdraw, getters) for managing individual accounts.
- The
- Main Program:
- The main program handles user interaction through a menu.
- It uses a
vector
to storeAccount
objects, representing all accounts in the system. - Create Account:
- Prompts the user for account information (name, initial balance).
- Generates a unique account number (replace with your logic for a real system).
- Creates an
Account
object and adds it to theaccounts
vector.
- Deposit and Withdraw:
- Prompts the user for the account number and amount.
- Iterates through the
accounts
vector to find the matching account. - Calls the appropriate
deposit
orwithdraw
method on the found account.
- Display Account Details:
- Prompts for the account number.
- Finds the account and displays its details using the getter methods.
Enhancements:
- Error Handling: Add error handling for invalid inputs (e.g., negative amounts, non-numeric values).
- File Persistence: Implement functionality to save and load account data to a file, so data is not lost when the program exits.
- Account Types: Extend the project to support different account types (e.g., savings, current) with specific features.
- Security: Implement basic security measures like password protection for accounts.
This basic Bank Management System is a starting point. You can build upon this foundation by adding more features and refining its functionality to create a more robust and feature-rich system. Remember to test your code thoroughly to ensure its correctness and stability.