Last active
March 28, 2022 18:24
-
-
Save ymollard/b4042c3498fde2bde842c9107c33d4f5 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 | |
class BankAccount: | |
def __init__(self, owner: str, initial_balance: int = 0): | |
self._owner = owner | |
self._balance = initial_balance | |
def print(self): | |
print(f"This account belongs to {self._owner} and has a balance of ${self._balance}") | |
def _credit(self, value: int, transaction_date: datetime): | |
self._balance += value | |
def transfer_to(self, recipient: "BankAccount", value:int, transaction_date:datetime): | |
self._balance -= value | |
recipient._credit(value, transaction_date) | |
# ValueError, TypeError, KeyError, | |
class InsufficientBalance(ValueError): | |
pass | |
class BlockedBankAccount(BankAccount): | |
def transfer_to(self, recipient: "BankAccount", value:int, transaction_date:datetime): | |
if value > self._balance: | |
raise InsufficientBalance("This account does not have enough money to transfer") | |
super().transfer_to(recipient, value, transaction_date) | |
class AgiosBankAccount(BankAccount): | |
def __init__(self, owner: str, initial_balance: int = 0, bank: "BankAccount" = None): | |
super().__init__(owner, initial_balance) | |
self._bank = bank | |
self._negative_from = None | |
def transfer_to(self, recipient: "BankAccount", value:int, transaction_date:datetime): | |
super().transfer_to(recipient, value, transaction_date) | |
if self._negative_from is None and self._balance < 0: | |
self._negative_from = transaction_date | |
def _credit(self, value: int, transaction_date: datetime): | |
super()._credit(value, transaction_date) | |
if self._negative_from is not None: | |
agios = (transaction_date - self._negative_from).days | |
self.transfer_to(self._bank, agios, transaction_date) | |
if self._balance > 0: | |
self._negative_from = None | |
else: | |
self._negative_from = transaction_date | |
bank = BankAccount("LCL", 100000) | |
walmart = BankAccount("Walmart", 10000) | |
alice = BankAccount("Alice", 500) | |
bob = AgiosBankAccount("Bob", 100, bank) | |
try: | |
alice.transfer_to(walmart, 100, datetime(2022, 1, 1)) | |
bob.transfer_to(walmart, 100, datetime(2022, 1, 2)) | |
alice.transfer_to(bob, 100, datetime(2022, 1, 3)) | |
bob.transfer_to(walmart, 200, datetime(2022, 1, 4)) | |
bob.print() | |
print(bob._negative_from) | |
alice.transfer_to(bob, 200, datetime(2022, 1, 8)) | |
except InsufficientBalance: | |
print("This scenario is not authorized") | |
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