This commit is contained in:
bronku 2025-12-06 12:35:51 +01:00
parent a47ef094af
commit c516afc400
13 changed files with 240 additions and 184 deletions

View file

@ -1,34 +1,18 @@
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})"
def write_to_file(filename, content):
with open(filename, 'w') as file:
file.write(content)
print(f"Written to {filename}")
account = BankAccount("Alice", 1000)
print(account)
def read_from_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
return content
except FileNotFoundError:
return "File not found"
print(f"Deposit $500: {account.deposit(500)}")
print(f"Balance: ${account.get_balance()}")
write_to_file("test_output.txt", "Hello, World!\nThis is a test.\n")
content = read_from_file("test_output.txt")
print(f"File content:\n{content}")
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()}")
print(read_from_file("nonexistent.txt"))