Skip to content

Instantly share code, notes, and snippets.

@ymollard
Created July 5, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymollard/008b4a798f03eee247a9dfe3814e0b37 to your computer and use it in GitHub Desktop.
Save ymollard/008b4a798f03eee247a9dfe3814e0b37 to your computer and use it in GitHub Desktop.
from datetime import datetime
class BankAccount:
def __init__(self, owner: str, initial_balance:int):
self._owner = owner
self._balance = initial_balance
def print(self):
print(f"This account belongs to {self._owner} with a balance of ${self._balance}")
def _credit(self, value: int):
self._balance += value
def transfer_to(self, recipient: "BankAccount", value: int, transaction_date: datetime):
self._credit(-value)
recipient._credit(value)
class InsufficientBalance(ValueError):
pass
class BlockedBankAccount(BankAccount):
def transfer_to(self, recipient: "BankAccount", value: int, transaction_date: datetime):
if self._balance < value:
raise InsufficientBalance(f"{self._owner} does not have a sufficient balance to execute this transfer")
super().transfer_to(recipient, value, transaction_date)
class AgiosBankAccount(BankAccount):
def transfer_to(self, recipient: "BankAccount", value: int, transaction_date: datetime):
super().transfer_to(recipient, value, transaction_date)
bank = BankAccount("LCL", 10000)
walmart = BlockedBankAccount("Walmart", 5000)
alice = BlockedBankAccount("Alice Worz", 500)
bob = BlockedBankAccount("Bob Müller", 100)
try:
#1.4.1. Alice buys $100 of goods at Walmart
alice.transfer_to(walmart, 100, datetime(2022, 12, 1))
#1.4.2. Bob buys $100 of goods at Walmart
bob.transfer_to(walmart, 100, datetime(2022, 12, 3))
#1.4.3. Alice makes a donation of $100 to Bob
alice.transfer_to(bob, 100, datetime(2022, 12, 7))
#1.4.4. Bob buys $200 at Walmart
bob.transfer_to(walmart, 200, datetime(2022, 12, 10))
except InsufficientBalance:
print("This scenario cannot be executed")
for account in [bank, walmart, alice, bob]:
account.print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment