Advertisement
rajeshinternshala

Untitled

Dec 22nd, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. class BankingSystemLevel3(BankingSystem):
  2.     def __init__(self):
  3.         super().__init__()
  4.         # Additional data structures for handling transfers and their state
  5.         self.transfers = {}
  6.         self.next_transfer_id = 1
  7.         self.transfer_expiry_time = 86400000  # 24 hours in milliseconds
  8.  
  9.     def transfer(self, timestamp, source_account_id, target_account_id, amount):
  10.         # Initiate a transfer between accounts
  11.         if source_account_id not in self.accounts or target_account_id not in self.accounts:
  12.             return ""
  13.         if self.accounts[source_account_id] < amount:
  14.             return ""
  15.         if source_account_id == target_account_id:
  16.             return ""
  17.  
  18.         # Create a transfer record
  19.         transfer_id = f"transfer{self.next_transfer_id}"
  20.         self.next_transfer_id += 1
  21.         self.transfers[transfer_id] = {
  22.             'source_account_id': source_account_id,
  23.             'target_account_id': target_account_id,
  24.             'amount': amount,
  25.             'timestamp': int(timestamp),
  26.             'status': 'pending'
  27.         }
  28.  
  29.         # Deduct the amount from the source account balance
  30.         self.accounts[source_account_id] -= amount
  31.         return transfer_id
  32.  
  33.     def accept_transfer(self, timestamp, account_id, transfer_id):
  34.         # Accept a pending transfer if it is valid and has not expired
  35.         timestamp = int(timestamp)
  36.         if transfer_id not in self.transfers:
  37.             return "false"
  38.        
  39.         transfer = self.transfers[transfer_id]
  40.         if transfer['status'] != 'pending':
  41.             return "false"
  42.         if transfer['target_account_id'] != account_id:
  43.             return "false"
  44.         if timestamp - transfer['timestamp'] > self.transfer_expiry_time:
  45.             # Transfer has expired, refund the source account
  46.             self.accounts[transfer['source_account_id']] += transfer['amount']
  47.             transfer['status'] = 'expired'
  48.             return "false"
  49.  
  50.         # Transfer is accepted
  51.         self.accounts[account_id] += transfer['amount']
  52.         transfer['status'] = 'accepted'
  53.         # Update transaction totals for both accounts
  54.         self.transaction_totals[source_account_id] = self.transaction_totals.get(source_account_id, 0) + amount
  55.         self.transaction_totals[target_account_id] = self.transaction_totals.get(target_account_id, 0) + amount
  56.         return "true"
  57.    
  58.     def process_queries(self, queries):
  59.         results = []
  60.         for query in queries:
  61.             operation, *params = query
  62.             if operation == "TRANSFER":
  63.                 results.append(self.transfer(*params))
  64.             elif operation == "ACCEPT_TRANSFER":
  65.                 results.append(self.accept_transfer(*params))
  66.             else:
  67.                 # For other operations, use the base class methods
  68.                 result = super().process_queries([query])[0]
  69.                 results.append(result)
  70.         return results
  71.  
  72. # Create an instance of the banking system with level 3 functionality
  73. banking_system_level3 = BankingSystemLevel3()
  74.  
  75. # Example queries for level 3
  76. queries_level_3 = [
  77.     ["CREATE_ACCOUNT", "1", "account1"],
  78.     ["CREATE_ACCOUNT", "2", "account2"],
  79.     ["DEPOSIT", "3", "account1", "2000"],
  80.     ["DEPOSIT", "4", "account2", "3000"],
  81.     ["TRANSFER", "5", "account1", "account2", "500"],
  82.     # ... (other queries as per the level 3 specification)
  83. ]
  84.  
  85. # Process the level 3 queries
  86. output_level_3 = banking_system_level3.process_queries(queries_level_3)
  87. output_level_3
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement