Skip to content

Commit

Permalink
Update transaction_processing.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 10, 2024
1 parent bf8c4a9 commit 9ce2191
Showing 1 changed file with 36 additions and 46 deletions.
82 changes: 36 additions & 46 deletions banking/transaction_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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:
"""
Expand All @@ -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

0 comments on commit 9ce2191

Please sign in to comment.