Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankingSystemLevel3(BankingSystem):
- def __init__(self):
- super().__init__()
- # Additional data structures for handling transfers and their state
- self.transfers = {}
- self.next_transfer_id = 1
- self.transfer_expiry_time = 86400000 # 24 hours in milliseconds
- def transfer(self, timestamp, source_account_id, target_account_id, amount):
- # Initiate a transfer between accounts
- if source_account_id not in self.accounts or target_account_id not in self.accounts:
- return ""
- if self.accounts[source_account_id] < amount:
- return ""
- if source_account_id == target_account_id:
- return ""
- # Create a transfer record
- transfer_id = f"transfer{self.next_transfer_id}"
- self.next_transfer_id += 1
- self.transfers[transfer_id] = {
- 'source_account_id': source_account_id,
- 'target_account_id': target_account_id,
- 'amount': amount,
- 'timestamp': int(timestamp),
- 'status': 'pending'
- }
- # Deduct the amount from the source account balance
- self.accounts[source_account_id] -= amount
- return transfer_id
- def accept_transfer(self, timestamp, account_id, transfer_id):
- # Accept a pending transfer if it is valid and has not expired
- timestamp = int(timestamp)
- if transfer_id not in self.transfers:
- return "false"
- transfer = self.transfers[transfer_id]
- if transfer['status'] != 'pending':
- return "false"
- if transfer['target_account_id'] != account_id:
- return "false"
- if timestamp - transfer['timestamp'] > self.transfer_expiry_time:
- # Transfer has expired, refund the source account
- self.accounts[transfer['source_account_id']] += transfer['amount']
- transfer['status'] = 'expired'
- return "false"
- # Transfer is accepted
- self.accounts[account_id] += transfer['amount']
- transfer['status'] = 'accepted'
- # Update transaction totals for both accounts
- self.transaction_totals[source_account_id] = self.transaction_totals.get(source_account_id, 0) + amount
- self.transaction_totals[target_account_id] = self.transaction_totals.get(target_account_id, 0) + amount
- return "true"
- def process_queries(self, queries):
- results = []
- for query in queries:
- operation, *params = query
- if operation == "TRANSFER":
- results.append(self.transfer(*params))
- elif operation == "ACCEPT_TRANSFER":
- results.append(self.accept_transfer(*params))
- else:
- # For other operations, use the base class methods
- result = super().process_queries([query])[0]
- results.append(result)
- return results
- # Create an instance of the banking system with level 3 functionality
- banking_system_level3 = BankingSystemLevel3()
- # Example queries for level 3
- queries_level_3 = [
- ["CREATE_ACCOUNT", "1", "account1"],
- ["CREATE_ACCOUNT", "2", "account2"],
- ["DEPOSIT", "3", "account1", "2000"],
- ["DEPOSIT", "4", "account2", "3000"],
- ["TRANSFER", "5", "account1", "account2", "500"],
- # ... (other queries as per the level 3 specification)
- ]
- # Process the level 3 queries
- output_level_3 = banking_system_level3.process_queries(queries_level_3)
- output_level_3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement