|
| 1 | +# lab10 - ATM v2 |
| 2 | + |
| 3 | +class ATM: |
| 4 | + |
| 5 | + def __init__(self, balance, interest_rate, transactions=[]): |
| 6 | + self.balance = balance |
| 7 | + self.interest_rate = interest_rate |
| 8 | + self.transactions = [] |
| 9 | + # transactions = [] |
| 10 | + # balance = 0 |
| 11 | + # interest_rate = 0.001 |
| 12 | + |
| 13 | + # returns the account balance |
| 14 | + def check_balance(self): |
| 15 | + return self.balance |
| 16 | + |
| 17 | + # deposits the given amount in the account |
| 18 | + def deposit(self, amount): |
| 19 | + self.balance += amount |
| 20 | + self.transactions.append(f'user deposited ${amount}') |
| 21 | + |
| 22 | + # returns true if the withdrawn amount won't put the account in the negative |
| 23 | + def check_withdrawal(self, amount): |
| 24 | + if self.balance < amount: |
| 25 | + return False |
| 26 | + else: |
| 27 | + return True |
| 28 | + |
| 29 | + # withdraws the amount from the account and returns it |
| 30 | + def withdraw(self, amount): |
| 31 | + self.balance -= amount |
| 32 | + self.transactions.append(f'user withdraw ${amount}') |
| 33 | + |
| 34 | + # returns the amount of interest calculated on the account |
| 35 | + def calc_interest(self): |
| 36 | + p = self.balance |
| 37 | + t = float(input("Enter the number of years : ")) |
| 38 | + r = self.interest_rate |
| 39 | + return p * (pow((1 + r / 100), t)) |
| 40 | + |
| 41 | + # Every time the user makes a deposit or withdrawal, add a string to a list saying 'user deposited $15' or 'user withdrew $15' |
| 42 | + def print_transactions(self): |
| 43 | + for i in self.transactions: |
| 44 | + print(i) |
| 45 | + |
| 46 | + |
| 47 | +atm = ATM(0, 0.001, "") # create an instance of our class |
| 48 | +# print(atm.deposit(100)) |
| 49 | +print('Welcome to the ATM') |
| 50 | +while True: |
| 51 | + command = input('Enter a command: ') |
| 52 | + if command == 'balance': |
| 53 | + balance = atm.check_balance() # call the check_balance() method |
| 54 | + print(f'Your balance is ${balance}') |
| 55 | + elif command == 'deposit': |
| 56 | + amount = float(input('How much would you like to deposit? ')) |
| 57 | + atm.deposit(amount) # call the deposit(amount) method |
| 58 | + print(f'Deposited ${amount}') |
| 59 | + elif command == 'withdraw': |
| 60 | + amount = float(input('How much would you like ')) |
| 61 | + if atm.check_withdrawal(amount): # call the check_withdrawal(amount) method |
| 62 | + atm.withdraw(amount) # call the withdraw(amount) method |
| 63 | + print(f'Withdrew ${amount}') |
| 64 | + else: |
| 65 | + print('Insufficient funds') |
| 66 | + elif command == 'interest': |
| 67 | + amount = atm.calc_interest() # call the calc_interest() method |
| 68 | + atm.deposit(amount) |
| 69 | + print(f'Accumulated ${amount:.2f} in interest') |
| 70 | + elif command == 'transactions': |
| 71 | + atm.print_transactions() |
| 72 | + elif command == 'help': |
| 73 | + print('Available commands:') |
| 74 | + print('balance - get the current balance') |
| 75 | + print('deposit - deposit money') |
| 76 | + print('withdraw - withdraw money') |
| 77 | + print('interest - accumulate interest') |
| 78 | + print('exit - exit the program') |
| 79 | + elif command == 'exit': |
| 80 | + break |
| 81 | + else: |
| 82 | + print('Command not recognized') |
| 83 | + |
| 84 | + |
| 85 | + |
0 commit comments