// The code represents a simple banking system with two classes: BankAccount and
Bank. The BankAccount class represents a bank account with an account number,
account holder, and balance. The Bank class represents a bank with a vector of bank
accounts. The code provides functions to deposit and withdraw money from an
account, as well as to get the account balance.
// However, there is a bug in the code that causes it to crash when run. The bug is
related to the way the findAccount function is implemented.
#include <bits/stdc++.h>
// Class to represent a bank account
class BankAccount {
public:
std::string accountNumber;
std::string accountHolder;
double balance;
// Constructor to initialize bank account
BankAccount(std::string accountNumber, std::string accountHolder, double
balance) {
this->accountNumber = accountNumber;
this->accountHolder = accountHolder;
this->balance = balance;
}
// Function to deposit money into the account
void deposit(double amount) {
if (amount < 0) {
throw std::invalid_argument("Deposit amount cannot be negative");
}
balance += amount;
}
// Function to withdraw money from the account
void withdraw(double amount) {
if (amount < 0) {
throw std::invalid_argument("Withdrawal amount cannot be negative");
}
if (amount > balance) {
throw std::runtime_error("Insufficient funds");
}
balance -= amount;
}
// Function to get the account balance
double getBalance() {
return balance;
}
};
// Class to represent a bank
class Bank {
public:
std::vector<BankAccount *> accounts;
// Function to add a new account to the bank
void addAccount(BankAccount* account) {
accounts.push_back(account);
}
// Function to find an account by account number
BankAccount* findAccount(const std::string& accountNumber) {
for (auto *account : accounts) {
if (account->accountNumber == accountNumber) {
return account;
}
}
return nullptr;
}
// Function to deposit money into an account
void deposit(const std::string& accountNumber, double amount) {
BankAccount* account = findAccount(accountNumber);
if (account != nullptr) {
account->deposit(amount);
} else {
throw std::invalid_argument("Account not found");
}
}
// Function to withdraw money from an account
void withdraw(const std::string& accountNumber, double amount) {
BankAccount* account = findAccount(accountNumber);
if (account != nullptr) {
account->withdraw(amount);
} else {
throw std::invalid_argument("Account not found");
}
}
// Function to get the account balance
double getBalance(const std::string& accountNumber) {
BankAccount* account = findAccount(accountNumber);
if (account != nullptr) {
return account->getBalance();
} else {
throw std::invalid_argument("Account not found");
}
}
};
int main() {
Bank bank;
// Create some bank accounts
BankAccount* account1 = new BankAccount("12345", "John Doe", 1000.0);
BankAccount* account2 = new BankAccount("67890", "Jane Doe", 500.0);
// Add accounts to the bank
bank.addAccount(account1);
bank.addAccount(account2);
// Deposit money into an account
bank.deposit("12345", 500.0);
// Withdraw money from an account
bank.withdraw("67890", 200.0);
// Get the account balance
double balance = bank.getBalance("12345");
// Print the account balance
std::cout << "Account balance: " << balance << std::endl;
return 0;
}