Created
January 25, 2022 16:03
-
-
Save ymollard/3759c27edceb8bbcc83edb9bf3a21027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from typing import Optional | |
class BankAccount: | |
def __init__(self, owner: str, initial_balance: int): | |
self.balance = initial_balance | |
self.owner = owner | |
def print(self): | |
print(f"This account of {self.owner} has a balance of {self.balance}") | |
def _credit(self, value: float, transaction_date: datetime): | |
self.balance += value | |
def transfer_to(self, recipient: "BankAccount", value: float, transaction_date: datetime): | |
self.balance -= value | |
recipient._credit(value, transaction_date) | |
class InsufficientBalance(ValueError): | |
pass | |
class BlockedBankAccount(BankAccount): | |
def transfer_to(self, recipient: "BankAccount", value: float, transaction_date: datetime): | |
if self.balance < value: | |
raise InsufficientBalance(f"{self.owner} does not have enough money") | |
super().transfer_to(recipient, value, transaction_date) | |
class AgiosBankAccount(BankAccount): | |
def __init__(self, owner: str, initial_balance: int, bank: "BankAccount"): | |
self.bank = bank | |
self.__negative_from: Optional[datetime] = None | |
super().__init__(owner, initial_balance) | |
def transfer_to(self, recipient: "BankAccount", value: float, transaction_date: datetime): | |
if self.__negative_from is None and self.balance < value: | |
self.__negative_from = transaction_date | |
super().transfer_to(recipient, value, transaction_date) | |
def _credit(self, value: float, transaction_date: datetime): | |
super(AgiosBankAccount, self)._credit(value, transaction_date) | |
if self.__negative_from is not None: | |
agios_value = (transaction_date - self.__negative_from).days | |
self.__negative_from = None | |
self.transfer_to(self.bank, agios_value, transaction_date) | |
bank = BankAccount("LCL", 10000) | |
walmart = BlockedBankAccount("Walmart", 5000) | |
alice = BlockedBankAccount("Alice Worz", 500) | |
bob = AgiosBankAccount("Bob Müller", 100, bank) | |
try: | |
#1.4.1. Alice buys $100 of goods at Walmart | |
alice.transfer_to(walmart, 100, datetime(2022, 1, 1)) | |
#1.4.2. Bob buys $100 of goods at Walmart | |
bob.transfer_to(walmart, 100, datetime(2022, 1, 2)) | |
#1.4.3. Alice makes a donation of $100 to Bob | |
alice.transfer_to(bob, 100, datetime(2022, 1, 3)) | |
#1.4.4. Bob buys $200 at Walmart | |
bob.transfer_to(walmart, 200, datetime(2022, 1, 4)) | |
alice.transfer_to(bob, 200, datetime(2022, 1, 9)) | |
except InsufficientBalance: | |
print("This scenario cannot be executed because of insufficient balance") | |
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