Similar Problems
Similar Problems not available
Simple Bank System - Leetcode Solution
Companies:
LeetCode: Simple Bank System Leetcode Solution
Difficulty: Medium
Topics: design array hash-table simulation
As an AI language model program, I don't really have the ability to access external sources such as Leetcode. However, I can explain what a typical solution to the Simple Bank System problem might look like:
Problem statement: Design a simple banking system that includes the ability to add accounts, deposit or withdraw money from those accounts, and view a history of transactions.
Solution: One approach for implementing this system would be to use the object-oriented programming paradigm. We can define three classes: Account, Transaction, and Bank.
The Account class would have attributes such as account number, account holder name, and balance. It would also have methods for depositing and withdrawing money from the account, updating the account balance, and getting the account balance.
The Transaction class would have attributes such as transaction ID, account number, transaction type (debit or credit), transaction amount, and transaction date. It would also have methods for creating a new transaction, retrieving transaction details, and displaying the transaction history.
The Bank class would have a list of accounts and transactions as attributes. It would also have methods for creating a new account, depositing or withdrawing money from an account, retrieving account details and transaction history, and displaying the bank's summary.
Here's a sample code implementation in Python:
class Account:
def __init__(self, account_number, account_holder):
self.account_number = account_number
self.account_holder = account_holder
self.balance = 0
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
else:
print("Insufficient balance")
def get_balance(self):
return self.balance
class Transaction:
def __init__(self, transaction_id, account_number, transaction_type, amount, date):
self.transaction_id = transaction_id
self.account_number = account_number
self.transaction_type = transaction_type
self.amount = amount
self.date = date
def get_transaction_details(self):
return (self.transaction_id, self.account_number, self.transaction_type, self.amount, self.date)
class Bank:
def __init__(self):
self.accounts = {}
self.transactions = {}
self.transaction_id = 0
def create_account(self, account_number, account_holder):
new_account = Account(account_number, account_holder)
self.accounts[account_number] = new_account
def deposit(self, account_number, amount):
if account_number in self.accounts:
self.accounts[account_number].deposit(amount)
self.transaction_id += 1
new_transaction = Transaction(self.transaction_id, account_number, "Credit", amount, date.today())
self.transactions[self.transaction_id] = new_transaction
else:
print("Account not found")
def withdraw(self, account_number, amount):
if account_number in self.accounts:
self.accounts[account_number].withdraw(amount)
self.transaction_id += 1
new_transaction = Transaction(self.transaction_id, account_number, "Debit", amount, date.today())
self.transactions[self.transaction_id] = new_transaction
else:
print("Account not found")
def get_account_details(self, account_number):
if account_number in self.accounts:
return (account_number, self.accounts[account_number].account_holder, self.accounts[account_number].balance)
else:
print("Account not found")
def get_transaction_history(self, account_number):
if account_number in self.accounts:
transactions = []
for transaction_id in self.transactions:
if self.transactions[transaction_id].account_number == account_number:
transactions.append(self.transactions[transaction_id].get_transaction_details())
return transactions
else:
print("Account not found")
def display_summary(self):
print("Bank summary:")
for account_number in self.accounts:
print("Account number:", account_number)
print("Account holder:", self.accounts[account_number].account_holder)
print("Account balance:", self.accounts[account_number].balance)
print("------------")
This implementation provides the basic functionality required for a simple banking system. However, additional features such as interest calculation, account deletion, and user authentication could also be added to make it more robust.
Simple Bank System Solution Code
1