Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views4 pages

Banking Management System Code

The document is a C++ program that implements a simple banking management system, allowing users to create accounts, deposit and withdraw money, and save or load account data from a file. It defines a BankAccount class with methods for account operations and maintains a vector of accounts. The main function provides a menu-driven interface for user interaction.

Uploaded by

ÃBDUL QAYYUM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Banking Management System Code

The document is a C++ program that implements a simple banking management system, allowing users to create accounts, deposit and withdraw money, and save or load account data from a file. It defines a BankAccount class with methods for account operations and maintains a vector of accounts. The main function provides a menu-driven interface for user interaction.

Uploaded by

ÃBDUL QAYYUM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

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

class BankAccount {
private:
int accountNumber;
string name;
double balance;

public:
BankAccount() {
accountNumber = 0;
name = "";
balance = 0.0;
}

BankAccount(int accNo, string accName, double bal) {


accountNumber = accNo;
name = accName;
balance = bal;
}

void displayAccount() const {


cout << "Account Number: " << accountNumber << endl;
cout << "Account Holder: " << name << endl;
cout << "Balance: $" << balance << endl;
}

void deposit(double amount) {


balance += amount;
cout << "Deposited $" << amount << " successfully." << endl;
}

void withdraw(double amount) {


if (amount > balance) {
cout << "Insufficient balance! Withdrawal failed." << endl;
} else {
balance -= amount;
cout << "Withdrawn $" << amount << " successfully." << endl;
}
}

int getAccountNumber() const {


return accountNumber;
}

double getBalance() const {


return balance;
}

void saveToFile(ofstream &out) const {


out << accountNumber << " " << name << " " << balance << endl;
}

void loadFromFile(ifstream &in) {


in >> accountNumber >> name >> balance;
}
};

vector<BankAccount> accounts;

void createAccount() {
int accNo;
string name;
double initialBalance;

cout << "Enter Account Number: ";


cin >> accNo;
cout << "Enter Account Holder Name: ";
cin >> name;
cout << "Enter Initial Balance: ";
cin >> initialBalance;

BankAccount acc(accNo, name, initialBalance);


accounts.push_back(acc);

cout << "Account Created Successfully!\n";


}

void displayAllAccounts() {
if (accounts.empty()) {
cout << "No accounts to display.\n";
return;
}

for (auto &acc : accounts) {


acc.displayAccount();
cout << "----------------------\n";
}
}

BankAccount* findAccount(int accNo) {


for (auto &acc : accounts) {
if (acc.getAccountNumber() == accNo)
return &acc;
}
return nullptr;
}

void depositToAccount() {
int accNo;
double amount;
cout << "Enter Account Number: ";
cin >> accNo;
BankAccount* acc = findAccount(accNo);

if (acc) {
cout << "Enter amount to deposit: ";
cin >> amount;
acc->deposit(amount);
} else {
cout << "Account not found!\n";
}
}
void withdrawFromAccount() {
int accNo;
double amount;
cout << "Enter Account Number: ";
cin >> accNo;
BankAccount* acc = findAccount(accNo);

if (acc) {
cout << "Enter amount to withdraw: ";
cin >> amount;
acc->withdraw(amount);
} else {
cout << "Account not found!\n";
}
}

void saveAllAccounts() {
ofstream out("accounts.txt");
for (auto &acc : accounts) {
acc.saveToFile(out);
}
out.close();
cout << "Accounts saved to file.\n";
}

void loadAllAccounts() {
ifstream in("accounts.txt");
if (!in) {
cout << "No saved data found.\n";
return;
}

accounts.clear();
while (!in.eof()) {
BankAccount acc;
acc.loadFromFile(in);
if (in) accounts.push_back(acc);
}
in.close();
cout << "Accounts loaded from file.\n";
}

int main() {
int choice;
loadAllAccounts();

do {
cout << "\n=== Banking Management System ===\n";
cout << "1. Create Account\n";
cout << "2. Display All Accounts\n";
cout << "3. Deposit Money\n";
cout << "4. Withdraw Money\n";
cout << "5. Save Accounts to File\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
createAccount();
break;
case 2:
displayAllAccounts();
break;
case 3:
depositToAccount();
break;
case 4:
withdrawFromAccount();
break;
case 5:
saveAllAccounts();
break;
case 6:
cout << "Exiting... Goodbye!\n";
break;
default:
cout << "Invalid choice!\n";
}
} while (choice != 6);

return 0;
}

You might also like