Skip to content
Snippets Groups Projects
TransactionExecutor.py 1.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • Abe Zukor's avatar
    Abe Zukor committed
    from collections import defaultdict
    from Transaction import Transaction, TransactionType
    
    class InvalidTransactionError(Exception):
        """This transaction is invalid"""
        pass
    
    class TransactionExecutor:
        def __init__(self) -> None:
            self.accounts = defaultdict(int)
        
        def deposit(self, transaction: Transaction) -> None:
    
            self.accounts[transaction.receiver_account]+=transaction.amount
    
    Abe Zukor's avatar
    Abe Zukor committed
    
        def transfer(self, transaction: Transaction) -> None:
            if transaction.sender_account not in self.accounts or self.accounts[transaction.sender_account]<transaction.amount:
                raise InvalidTransactionError
            self.accounts[transaction.sender_account]-=transaction.amount
            self.accounts[transaction.receiver_account]+=transaction.amount
        
        def transaction(self, transaction: Transaction) -> None:
            try:
                if transaction.type==TransactionType.DEPOSIT:
                    self.deposit(transaction)
                elif transaction.type==TransactionType.TRANSFER:
                    self.transfer(transaction)
            except InvalidTransactionError:
                pass
            print(self.accounts)