├── README.md └── Bank.py /README.md: -------------------------------------------------------------------------------- 1 | # Hazrat Ali 2 | 3 | # Software Engineer & MERN Stack Developer 4 | 5 | -------------------------------------------------------------------------------- /Bank.py: -------------------------------------------------------------------------------- 1 | class Transaction: 2 | def __init__(self, sender, receiver, amount): 3 | self.sender = sender 4 | self.receiver = receiver 5 | self.amount = amount 6 | 7 | 8 | class Bank: 9 | def __init__(self): 10 | self.total_balance = 0 11 | self.total_loan_amount = 0 12 | self.loan_enabled = True 13 | self.transaction_history = [] 14 | 15 | def get_total_balance(self): 16 | return self.total_balance 17 | 18 | def get_total_loan_amount(self): 19 | return self.total_loan_amount 20 | 21 | def enable_loan(self): 22 | self.loan_enabled = True 23 | 24 | def disable_loan(self): 25 | self.loan_enabled = False 26 | 27 | def add_transaction(self, transaction): 28 | self.transaction_history.append(transaction) 29 | 30 | 31 | class Admin: 32 | def __init__(self, bank): 33 | self.bank = bank 34 | 35 | def create_account(self, name, initial_deposit): 36 | user = User(name) 37 | if user.create_account(initial_deposit): 38 | self.bank.total_balance += initial_deposit 39 | return user 40 | return None 41 | 42 | def get_total_balance(self): 43 | return self.bank.get_total_balance() 44 | 45 | def get_total_loan_amount(self): 46 | return self.bank.get_total_loan_amount() 47 | 48 | def enable_loan_feature(self): 49 | self.bank.enable_loan() 50 | 51 | def disable_loan_feature(self): 52 | self.bank.disable_loan() 53 | 54 | def get_transaction_history(self): 55 | return self.bank.transaction_history 56 | 57 | 58 | class User: 59 | def __init__(self, name): 60 | self.name = name 61 | self.balance = 0 62 | self.loan_limit = 0 63 | self.transaction_history = [] 64 | 65 | def create_account(self, initial_deposit): 66 | if initial_deposit > 0: 67 | self.balance = initial_deposit 68 | self.loan_limit = initial_deposit * 2 69 | return True 70 | return False 71 | 72 | def deposit(self, bank, amount): 73 | if amount > 0: 74 | self.balance += amount 75 | bank.total_balance += amount 76 | self.loan_limit = self.balance * 2 77 | transaction = Transaction(self.name, self.name, amount) 78 | self.transaction_history.append(transaction) 79 | return True 80 | return False 81 | 82 | def withdraw(self, bank, amount): 83 | if amount > 0 and amount <= self.balance and amount <= bank.total_balance: 84 | self.balance -= amount 85 | bank.total_balance -= amount 86 | transaction = Transaction(self.name, self.name, -amount) 87 | self.transaction_history.append(transaction) 88 | return True 89 | elif amount > bank.total_balance: 90 | print("Bank is bankrupt. Unable to withdraw.") 91 | return False 92 | 93 | def transfer(self, recipient, amount): 94 | if amount > 0 and amount <= self.balance: 95 | self.balance -= amount 96 | recipient.balance += amount 97 | transaction = Transaction(self.name, recipient.name, amount) 98 | self.transaction_history.append(transaction) 99 | recipient.transaction_history.append(transaction) 100 | return True 101 | return False 102 | 103 | def check_balance(self): 104 | return self.balance 105 | 106 | def take_loan(self, bank, amount): 107 | if bank.loan_enabled and amount <= self.loan_limit and amount > 0 and amount <= bank.total_balance: 108 | self.balance += amount 109 | bank.total_loan_amount += amount 110 | bank.total_balance -= amount 111 | transaction = Transaction(self.name, self.name, amount) 112 | self.transaction_history.append(transaction) 113 | bank.add_transaction(transaction) 114 | return True 115 | return False 116 | 117 | def get_transaction_history(self): 118 | return self.transaction_history 119 | 120 | 121 | bank = Bank() 122 | 123 | admin = Admin(bank) 124 | 125 | Hazrat = admin.create_account('Hazrat', 500) 126 | 127 | Minhaz = admin.create_account('Minhaz', 100) 128 | 129 | Hazrat.deposit(bank, 10000) 130 | 131 | print(Hazrat.check_balance()) 132 | print(Minhaz.check_balance()) 133 | 134 | Hazrat.withdraw(bank, 1000) 135 | Hazrat.transfer(Minhaz, 500) 136 | 137 | admin.enable_loan_feature() 138 | 139 | Hazrat.take_loan(bank, 100) 140 | 141 | print(Hazrat.check_balance()) 142 | 143 | print(admin.get_total_loan_amount()) 144 | print(admin.get_total_balance()) 145 | 146 | transaction_history = Hazrat.get_transaction_history() 147 | for transaction in transaction_history: 148 | print( 149 | f"Sender: {transaction.sender}, Receiver: {transaction.receiver}, Amount: {transaction.amount}") 150 | --------------------------------------------------------------------------------