1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
#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; } |
Explanation
- Include Libraries:
#include <iostream>
: For input and output operations.#include <vector>
: For using vectors to manage multiple accounts and transactions.#include <string>
: For string operations.#include <iomanip>
: For formatting output (e.g., setting decimal precision).
- Account Class:
- Private Data Members:
accountNumber
: Unique identifier for the account.accountHolder
: Name of the account holder.balance
: Current balance of the account.transactionHistory
: List of transaction descriptions.
- Public Methods:
deposit(double amount)
: Adds money to the balance and logs the transaction.withdraw(double amount)
: Removes money from the balance if sufficient funds are available and logs the transaction.checkBalance() const
: Displays the current balance.showTransactionHistory() const
: Displays all transactions.getAccountNumber() const
: Returns the account number.
- Private Data Members:
- Bank Class:
- Private Data Member:
accounts
: List of all accounts in the bank.
- Public Methods:
createAccount(int accNum, string accHolder, double initialBalance)
: Creates and adds a new account to the bank.findAccount(int accNum)
: Searches for an account by its number.deposit(int accNum, double amount)
: Deposits money into a specified account.withdraw(int accNum, double amount)
: Withdraws money from a specified account.checkBalance(int accNum) const
: Displays the balance of a specified account.showTransactionHistory(int accNum) const
: Displays the transaction history of a specified account.
- Private Data Member:
- Main Function (
main
):- Provides a menu-driven interface to interact with the bank system.
- Handles user choices to create accounts, deposit, withdraw, check balances, and view transaction histories.
- Repeats the menu until the user chooses to exit.