#include <iostream>
using namespace std;
class BankAccount {
private:
double balance_;
public:
BankAccount(double initial_balance = 0.0) {
if (initial_balance < 0) {
cout << "Initial balance cannot be negative, automatically set to 0" << endl;
balance_ = 0.0;
} else {
balance_ = initial_balance;
}
}
void deposit(double amount) {
if (amount > 0) {
balance_ += amount;
cout << "Deposited " << amount << " successfully" << endl;
} else {
cout << "Deposit amount must be positive!" << endl;
}
}
bool withdraw(double amount) {
if (amount <= 0) {
cout << "Withdrawal amount must be positive!" << endl;
return false;
}
if (amount > balance_) {
cout << "Insufficient balance, withdrawal failed! (Current balance: " << balance_ << ")" << endl;
return false;
}
balance_ -= amount;
cout << "Withdrew " << amount << " successfully" << endl;
return true;
}
double getBalance() const {
return balance_;
}
};
int main() {
BankAccount myAccount(1000.0);
cout << "====== Initial State ======" << endl;
cout << "Current balance: " << myAccount.getBalance() << endl;
cout << "\n====== Deposit 500 ======" << endl;
myAccount.deposit(500);
cout << "Current balance: " << myAccount.getBalance() << endl;
cout << "\n====== Withdraw 200 ======" << endl;
if (myAccount.withdraw(200)) {
cout << "Withdrawal completed" << endl;
}
cout << "Current balance: " << myAccount.getBalance() << endl;
cout << "\n====== Attempt to withdraw 2000 (exceeds balance) ======" << endl;
if (!myAccount.withdraw(2000)) {
cout << "Withdrawal rejected, balance unchanged" << endl;
}
cout << "Current balance: " << myAccount.getBalance() << endl;
cout << "\n====== Attempt to deposit -100 ======" << endl;
myAccount.deposit(-100);
cout << "Current balance: " << myAccount.getBalance() << endl;
cout << "\n====== Final Account Balance ======" << endl;
cout << myAccount.getBalance() << endl;
return 0;
}