34 lines
No EOL
912 B
Text
34 lines
No EOL
912 B
Text
class BankAccount:
|
|
def __init__(self, owner, balance=0):
|
|
self.owner = owner
|
|
self.balance = balance
|
|
|
|
def deposit(self, amount):
|
|
if amount > 0:
|
|
self.balance += amount
|
|
return True
|
|
return False
|
|
|
|
def withdraw(self, amount):
|
|
if 0 < amount <= self.balance:
|
|
self.balance -= amount
|
|
return True
|
|
return False
|
|
|
|
def get_balance(self):
|
|
return self.balance
|
|
|
|
def __str__(self):
|
|
return f"Account({self.owner}, Balance: ${self.balance:.2f})"
|
|
|
|
account = BankAccount("Alice", 1000)
|
|
print(account)
|
|
|
|
print(f"Deposit $500: {account.deposit(500)}")
|
|
print(f"Balance: ${account.get_balance()}")
|
|
|
|
print(f"Withdraw $200: {account.withdraw(200)}")
|
|
print(f"Balance: ${account.get_balance()}")
|
|
|
|
print(f"Withdraw $2000: {account.withdraw(2000)}")
|
|
print(f"Final balance: ${account.get_balance()}") |