Untitled
unknown
python
2 years ago
1.4 kB
6
Indexable
class Bank: def __init__(self, balance): self.balance = balance def transfer(self, account1, account2, money): # Check if both accounts exist and there's enough balance in the first account if account1 > 0 and account1 <= len(self.balance) and account2 > 0 and account2 <= len(self.balance) and self.balance[account1 - 1] >= money: self.balance[account1 - 1] -= money self.balance[account2 - 1] += money return True else: return False def deposit(self, account, money): # Check if account exists if account > 0 and account <= len(self.balance): self.balance[account - 1] += money return True else: return False def withdraw(self, account, money): # Check if account exists and there's enough balance if account > 0 and account <= len(self.balance) and self.balance[account - 1] >= money: self.balance[account - 1] -= money return True else: return False # Testing the Bank class as per the example given bank = Bank([10, 100, 20, 50, 30]) # List to store the results of operations results = [None] # Executing the operations results.append(bank.withdraw(3, 10)) results.append(bank.transfer(5, 1, 20)) results.append(bank.deposit(5, 20)) results.append(bank.transfer(3, 4, 15)) results.append(bank.withdraw(10, 50)) results
Editor is loading...
Leave a Comment