#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
class Account {
private:
int accountNumber;
string accountHolder;
double balance;
vector<string> transactionHistory;
public:
Account(int accNum, string accHolder, double initialBalance)
: accountNumber(accNum), accountHolder(accHolder), balance(initialBalance) {}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
transactionHistory.push_back("Deposited $" + to_string(amount));
} else {
cout << "Invalid deposit amount." << endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
transactionHistory.push_back("Withdrew $" + to_string(amount));
} else {
cout << "Insufficient funds or invalid amount." << endl;
}
}
void checkBalance() const {
cout << fixed << setprecision(2);
cout << "Balance: $" << balance << endl;
}
void showTransactionHistory() const {
cout << "Transaction History:" << endl;
for (const auto& transaction : transactionHistory) {
cout << transaction << endl;
}
}
int getAccountNumber() const {
return accountNumber;
}
};
class Bank {
private:
vector<Account> accounts;
public:
void createAccount(int accNum, string accHolder, double initialBalance) {
accounts.emplace_back(accNum, accHolder, initialBalance);
cout << "Account created successfully!" << endl;
}
Account* findAccount(int accNum) {
for (auto& account : accounts) {
if (account.getAccountNumber() == accNum) {
return &account;
}
}
return nullptr;
}
void deposit(int accNum, double amount) {
Account* account = findAccount(accNum);
if (account) {
account->deposit(amount);
} else {
cout << "Account not found." << endl;
}
}
void withdraw(int accNum, double amount) {
Account* account = findAccount(accNum);
if (account) {
account->withdraw(amount);
} else {
cout << "Account not found." << endl;
}
}
void checkBalance(int accNum) const {
const Account* account = findAccount(accNum);
if (account) {
account->checkBalance();
} else {
cout << "Account not found." << endl;
}
}
void showTransactionHistory(int accNum) const {
const Account* account = findAccount(accNum);
if (account) {
account->showTransactionHistory();
} else {
cout << "Account not found." << endl;
}
}
};
int main() {
Bank bank;
int choice, accNum;
string accHolder;
double amount;
do {
cout << "\nBank Management System" << endl;
cout << "1. Create Account" << endl;
cout << "2. Deposit Money" << endl;
cout << "3. Withdraw Money" << endl;
cout << "4. Check Balance" << endl;
cout << "5. View Transaction History" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter account number: ";
cin >> accNum;
cout << "Enter account holder name: ";
cin.ignore(); // Ignore leftover newline character
getline(cin, accHolder);
cout << "Enter initial balance: $";
cin >> amount;
bank.createAccount(accNum, accHolder, amount);
break;
case 2:
cout << "Enter account number: ";
cin >> accNum;
cout << "Enter amount to deposit: $";
cin >> amount;
bank.deposit(accNum, amount);
break;
case 3:
cout << "Enter account number: ";
cin >> accNum;
cout << "Enter amount to withdraw: $";
cin >> amount;
bank.withdraw(accNum, amount);
break;
case 4:
cout << "Enter account number: ";
cin >> accNum;
bank.checkBalance(accNum);
break;
case 5:
cout << "Enter account number: ";
cin >> accNum;
bank.showTransactionHistory(accNum);
break;
case 6:
cout << "Exiting the system. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please select a valid option." << endl;
break;
}
} while (choice != 6);
return 0;
}