0% found this document useful (0 votes)
7 views3 pages

d_que3.cpp

The document describes a simple banking system implemented in C++ with two classes: BankAccount and Bank. It includes functionalities for depositing, withdrawing, and checking account balances, but contains a bug in the findAccount function that causes the program to crash. The code demonstrates the creation of bank accounts and basic operations on them.

Uploaded by

sidgft1
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)
7 views3 pages

d_que3.cpp

The document describes a simple banking system implemented in C++ with two classes: BankAccount and Bank. It includes functionalities for depositing, withdrawing, and checking account balances, but contains a bug in the findAccount function that causes the program to crash. The code demonstrates the creation of bank accounts and basic operations on them.

Uploaded by

sidgft1
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/ 3

// 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;
}

You might also like