diff --git a/banking/transaction_processing.py b/banking/transaction_processing.py index bc86a6e49..21924650b 100644 --- a/banking/transaction_processing.py +++ b/banking/transaction_processing.py @@ -13,23 +13,17 @@ def deposit(account_id: int, amount: float) -> bool: Returns: bool: True if the deposit was successful, False otherwise. """ - # implementation - logging.info(f"Depositing {amount} into account {account_id}") - # Retrieve the account from the database or in-memory data structure - account = get_account_by_id(account_id) - - # Validate the input parameters - if amount < 0: - raise ValueError("Amount cannot be negative") - - # Update the account balance - account['balance'] += amount - - # Save the updated account back to the database or in-memory data structure - save_account(account) - - # Return True to indicate success - return True + try: + logging.info(f"Depositing {amount} into account {account_id}") + account = get_account_by_id(account_id) + if amount < 0: + raise ValueError("Amount cannot be negative") + account['balance'] += amount + save_account(account) + return True + except Exception as e: + logging.error(f"Error depositing into account {account_id}: {e}") + return False def withdraw(account_id: int, amount: float) -> bool: """ @@ -42,25 +36,19 @@ def withdraw(account_id: int, amount: float) -> bool: Returns: bool: True if the withdrawal was successful, False otherwise. """ - # implementation - logging.info(f"Withdrawing {amount} from account {account_id}") - # Retrieve the account from the database or in-memory data structure - account = get_account_by_id(account_id) - - # Validate the input parameters - if amount < 0: - raise ValueError("Amount cannot be negative") - if account['balance'] < amount: - raise ValueError("Insufficient balance") - - # Update the account balance - account['balance'] -= amount - - # Save the updated account back to the database or in-memory data structure - save_account(account) - - # Return True to indicate success - return True + try: + logging.info(f"Withdrawing {amount} from account {account_id}") + account = get_account_by_id(account_id) + if amount < 0: + raise ValueError("Amount cannot be negative") + if account['balance'] < amount: + raise ValueError("Insufficient balance") + account['balance'] -= amount + save_account(account) + return True + except Exception as e: + logging.error(f"Error withdrawing from account {account_id}: {e}") + return False def transfer(from_account_id: int, to_account_id: int, amount: float) -> bool: """ @@ -74,13 +62,15 @@ def transfer(from_account_id: int, to_account_id: int, amount: float) -> bool: Returns: bool: True if the transfer was successful, False otherwise. """ - # implementation - logging.info(f"Transferring {amount} from account {from_account_id} to account {to_account_id}") - # Withdraw the amount from the source account - withdraw(from_account_id, amount) - - # Deposit the amount into the destination account - deposit(to_account_id, amount) - - # Return True to indicate success - return True + try: + logging.info(f"Transferring {amount} from account {from_account_id} to account {to_account_id}") + if not withdraw(from_account_id, amount): + return False + if not deposit(to_account_id, amount): + # Rollback the withdrawal if deposit fails + deposit(from_account_id, amount) + return False + return True + except Exception as e: + logging.error(f"Error transferring between accounts: {e}") + return False