Spaces:
Sleeping
Sleeping
File size: 1,206 Bytes
a89f25d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# Python Example: Classes and Object-Oriented Programming
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
self.transactions = []
def deposit(self, amount):
if amount > 0:
self.balance += amount
self.transactions.append(f"Deposited: ${amount}")
return True
return False
def withdraw(self, amount):
if amount <= self.balance and amount > 0:
self.balance -= amount
self.transactions.append(f"Withdrew: ${amount}")
return True
return False
def get_balance(self):
return f"${self.balance}"
def show_transactions(self):
print(f"Transaction history for {self.owner}:")
for transaction in self.transactions:
print(f" - {transaction}")
# Create account
account = BankAccount("John Doe", 1000)
print(f"Account created for {account.owner}")
print(f"Initial balance: {account.get_balance()}")
# Perform transactions
account.deposit(500)
account.withdraw(200)
account.deposit(100)
print(f"Current balance: {account.get_balance()}")
account.show_transactions() |