#include <iostream>
class BankAccount {
private:
double balance;
public:
BankAccount(double balance) : balance(balance) {}
bool withdraw(const double amount) {
if (balance >= amount) {
balance -= amount;
return true;
}
return false;
}
bool deposit(const double amount) {
balance += amount;
return true;
}
double getBalance() const {
return balance;
}
};
class Transaction {
private:
BankAccount* from;
BankAccount* to;
double amount;
public:
Transaction(BankAccount* from, BankAccount* to, double amount)
: from(from), to(to), amount(amount) {}
bool execute() {
if (from->withdraw(amount)) {
to->deposit(amount);
return true;
}
return false;
}
};
int main() {
BankAccount* account1 = new BankAccount(1000.0);
BankAccount* account2 = new BankAccount(500.0);
Transaction* transaction = new Transaction(account1, account2, 500.0);
bool res = transaction->execute();
if (res) {
std::cout << "Transaction successful" << std::endl;
} else {
std::cout << "Transaction failed" << std::endl;
}
std::cout << account1->getBalance() << std::endl;
std::cout << account2->getBalance() << std::endl;
delete transaction;
delete account1;
delete account2;
account1 = new BankAccount(1000.0);
account2 = new BankAccount(500.0);
transaction = new Transaction(account1, account2, 5000.0);
res = transaction->execute();
if (res) {
std::cout << "Transaction successful" << std::endl;
} else {
std::cout << "Transaction failed" << std::endl;
}
std::cout << account1->getBalance() << std::endl;
std::cout << account2->getBalance() << std::endl;
delete transaction;
delete account1;
delete account2;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: