Advertisement
rajeshinternshala

Untitled

Dec 22nd, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. class BankingSystemLevel3:
  2.     def __init__(self):
  3.         self.accounts = {}
  4.         self.transfers = {}
  5.         self.transfer_counter = 1
  6.  
  7.     def create_account(self, timestamp, account_id):
  8.         if account_id in self.accounts:
  9.             return "false"
  10.         self.accounts[account_id] = 0
  11.         return "true"
  12.  
  13.     def deposit(self, timestamp, account_id, amount):
  14.         if account_id not in self.accounts:
  15.             return ""
  16.         self.accounts[account_id] += int(amount)
  17.         return str(self.accounts[account_id])
  18.  
  19.     def transfer(self, timestamp, source_account_id, target_account_id, amount):
  20.         amount = int(amount)
  21.         timestamp = int(timestamp)
  22.  
  23.         if source_account_id == target_account_id or \
  24.                 source_account_id not in self.accounts or \
  25.                 target_account_id not in self.accounts or \
  26.                 self.accounts[source_account_id] < amount:
  27.             return ""
  28.  
  29.         transfer_id = f"transfer{self.transfer_counter}"
  30.         self.transfer_counter += 1
  31.  
  32.         self.accounts[source_account_id] -= amount
  33.  
  34.         self.transfers[transfer_id] = {
  35.             'source': source_account_id,
  36.             'target': target_account_id,
  37.             'amount': amount,
  38.             'timestamp': timestamp,
  39.             'expired': False
  40.         }
  41.         return transfer_id
  42.  
  43.     def accept_transfer(self, timestamp, account_id, transfer_id):
  44.         timestamp = int(timestamp)
  45.  
  46.         if transfer_id not in self.transfers:
  47.             return "false"
  48.         transfer = self.transfers[transfer_id]
  49.  
  50.         if timestamp - transfer['timestamp'] > 86400000:  # Check for transfer expiry
  51.             # Refund only if the transfer wasn't previously marked as expired
  52.             if not transfer['expired']:
  53.                 self.accounts[transfer['source']] += transfer['amount']
  54.             transfer['expired'] = True
  55.             return "false"
  56.  
  57.         # Check if the transfer is already expired or account is incorrect
  58.         if transfer['expired'] or transfer['target'] != account_id:
  59.             return "false"
  60.  
  61.         # Transfer is successful
  62.         self.accounts[account_id] += transfer['amount']
  63.         transfer['expired'] = True  # Mark transfer as expired after acceptance
  64.         return "true"
  65.  
  66.     def process_queries(self, queries):
  67.         results = []
  68.         for query in queries:
  69.             operation = query[0]
  70.             timestamp = int(query[1])
  71.             params = query[2:]
  72.  
  73.             if operation == "CREATE_ACCOUNT":
  74.                 results.append(self.create_account(timestamp, *params))
  75.             elif operation == "DEPOSIT":
  76.                 results.append(self.deposit(timestamp, *params))
  77.             elif operation == "TRANSFER":
  78.                 results.append(self.transfer(timestamp, *params))
  79.             elif operation == "ACCEPT_TRANSFER":
  80.                 results.append(self.accept_transfer(timestamp, *params))
  81.  
  82.         return results
  83.  
  84. # Example usage:
  85. # banking_system = BankingSystemLevel3()
  86. # output = banking_system.process_queries(your_query_list_here)
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement