Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BankingSystemLevel3:
- def __init__(self):
- self.accounts = {}
- self.transfers = {}
- self.transfer_counter = 1
- self.transfer_expiry = 86400000 # 24 hours in milliseconds
- def create_account(self, timestamp, account_id):
- if account_id in self.accounts:
- return "false"
- self.accounts[account_id] = 0
- return "true"
- def deposit(self, timestamp, account_id, amount):
- if account_id not in self.accounts:
- return ""
- self.accounts[account_id] += int(amount)
- return str(self.accounts[account_id])
- def pay(self, timestamp, account_id, amount):
- if account_id not in self.accounts or self.accounts[account_id] < int(amount):
- return ""
- self.accounts[account_id] -= int(amount)
- return str(self.accounts[account_id])
- def transfer(self, timestamp, source_account_id, target_account_id, amount):
- if source_account_id == target_account_id or \
- source_account_id not in self.accounts or \
- target_account_id not in self.accounts or \
- self.accounts[source_account_id] < int(amount):
- return ""
- transfer_id = f"transfer{self.transfer_counter}"
- self.transfer_counter += 1
- # Withdraw the amount from source account
- self.accounts[source_account_id] -= int(amount)
- # Create a transfer
- self.transfers[transfer_id] = {
- 'source': source_account_id,
- 'target': target_account_id,
- 'amount': int(amount),
- 'timestamp': int(timestamp),
- 'expired': False
- }
- return transfer_id
- def accept_transfer(self, timestamp, account_id, transfer_id):
- if transfer_id not in self.transfers or \
- self.transfers[transfer_id]['target'] != account_id or \
- self.transfers[transfer_id]['expired']:
- return "false"
- # Check for expiry
- if int(timestamp) - self.transfers[transfer_id]['timestamp'] > self.transfer_expiry:
- self.accounts[self.transfers[transfer_id]['source']] += self.transfers[transfer_id]['amount']
- self.transfers[transfer_id]['expired'] = True
- return "false"
- # Complete transfer
- self.accounts[account_id] += self.transfers[transfer_id]['amount']
- del self.transfers[transfer_id] # Remove transfer as it is completed
- return "true"
- def process_queries(self, queries):
- results = []
- for query in queries:
- operation = query[0]
- args = query[1:]
- if operation == "CREATE_ACCOUNT":
- results.append(self.create_account(*args))
- elif operation == "DEPOSIT":
- results.append(self.deposit(*args))
- elif operation == "PAY":
- results.append(self.pay(*args))
- elif operation == "TRANSFER":
- results.append(self.transfer(*args))
- elif operation == "ACCEPT_TRANSFER":
- results.append(self.accept_transfer(*args))
- return results
- # Example usage:
- banking_system = BankingSystemLevel3()
- queries = [
- ["CREATE_ACCOUNT", "1622534400000", "account1"],
- ["CREATE_ACCOUNT", "1622534401000", "account2"],
- ["DEPOSIT", "1622534402000", "account1", "1000"],
- ["DEPOSIT", "1622534403000", "account2", "1000"],
- ["TRANSFER", "1622534404000", "account1", "account2", "500"],
- ["ACCEPT_TRANSFER", "1622620800000", "account2", "transfer1"],
- ["TRANSFER", "1622534405000", "account1", "account2", "100"],
- ["ACCEPT_TRANSFER", "1622620801000", "account2", "transfer2"]
- ]
- # Process the queries and output the results
- output = banking_system.process_queries(queries)
- output
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement