Advertisement
rajeshinternshala

Untitled

Dec 22nd, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. class BankingSystem:
  2.     def __init__(self):
  3.         # Initialize a dictionary to store account balance
  4.         self.accounts = {}
  5.    
  6.     def create_account(self, timestamp, account_id):
  7.         # Check if account already exists
  8.         if account_id in self.accounts:
  9.             return "false"
  10.         else:
  11.             # Create account with a balance of 0
  12.             self.accounts[account_id] = 0
  13.             return "true"
  14.    
  15.     def deposit(self, timestamp, account_id, amount):
  16.         # Check if account exists
  17.         if account_id not in self.accounts:
  18.             return ""
  19.         else:
  20.             # Add amount to account balance
  21.             self.accounts[account_id] += amount
  22.             return str(self.accounts[account_id])
  23.    
  24.     def pay(self, timestamp, account_id, amount):
  25.         # Check if account exists and has enough balance
  26.         if account_id not in self.accounts or self.accounts[account_id] < amount:
  27.             return ""
  28.         else:
  29.             # Subtract amount from account balance
  30.             self.accounts[account_id] -= amount
  31.             return str(self.accounts[account_id])
  32.    
  33.     def process_queries(self, queries):
  34.         # Process each query and store the results
  35.         results = []
  36.         for query in queries:
  37.             operation = query[0]
  38.             if operation == "CREATE_ACCOUNT":
  39.                 result = self.create_account(query[1], query[2])
  40.             elif operation == "DEPOSIT":
  41.                 result = self deposit(query[1], query[2], int(query[3]))
  42.             elif operation == "PAY":
  43.                 result = self.pay(query[1], query[2], int(query[3]))
  44.             results.append(result)
  45.         return results
  46.  
  47. # Example queries, this should be replaced with the actual input
  48. example_queries = [
  49.     ["CREATE_ACCOUNT", "1", "account1"],
  50.     ["CREATE_ACCOUNT", "2", "account1"],
  51.     ["DEPOSIT", "3", "account1", "2700"],
  52.     ["DEPOSIT", "4", "non-existing", "2700"],
  53.     ["PAY", "5", "account1", "700"],
  54.     ["PAY", "6", "non-existing", "2700"],
  55.     ["PAY", "7", "account1", "2701"],
  56.     ["PAY", "8", "account1", "2000"]
  57. ]
  58.  
  59. # Create a banking
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement