|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +class ATM: |
| 4 | + |
| 5 | + def __init__(self, balance=0, interest_rate=0.001): |
| 6 | + self.balance = balance |
| 7 | + self.interest_rate = interest_rate |
| 8 | + self.transactions = [] |
| 9 | + |
| 10 | + def check_balance(self): |
| 11 | + """Returns the account balance.""" |
| 12 | + self.transactions.append({'Date': datetime.now().strftime( |
| 13 | + "%Y-%m-%d %H:%M:%S"), 'transaction_type': 'checked balance', 'balance': self.balance}) |
| 14 | + return self.balance |
| 15 | + |
| 16 | + def deposit(self, amount): |
| 17 | + """Deposits the given amount in the account.""" |
| 18 | + previous_balance = self.balance |
| 19 | + self.balance += amount |
| 20 | + self.transactions.append({'Date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'transaction_type': 'deposit', |
| 21 | + 'amount': amount, 'previous_balance': previous_balance, 'new_balance': self.balance}) |
| 22 | + return self.balance |
| 23 | + |
| 24 | + def check_withdrawal(self, amount): |
| 25 | + """Returns true if the withdrawn amount won't put the account in the negative""" |
| 26 | + if self.balance - amount >= 0: |
| 27 | + return True |
| 28 | + else: |
| 29 | + self.transactions.append({'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'transaction_type': 'Withdrawal', 'result': 'insufficient funds', |
| 30 | + 'amount': amount, 'balance': self.balance}) |
| 31 | + return False |
| 32 | + |
| 33 | + def withdraw(self, amount): |
| 34 | + """Withdraws the amount from the account and returns it.""" |
| 35 | + previous_balance = self.balance |
| 36 | + self.balance -= amount |
| 37 | + self.transactions.append({'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'transaction_type': 'Withdrawal', |
| 38 | + 'amount': amount, 'previous_balance': previous_balance, 'new_balance': self.balance}) |
| 39 | + return self.balance |
| 40 | + |
| 41 | + def calc_interest(self): |
| 42 | + """Returns the amount of interest calculated on the account.""" |
| 43 | + return self.balance * self.interest_rate |
| 44 | + |
| 45 | + |
| 46 | +atm = ATM() # create an instance of our class |
| 47 | +print('Welcome to the ATM') |
| 48 | +while True: |
| 49 | + command = input('Enter a command: ') |
| 50 | + if command == 'balance': |
| 51 | + balance = atm.check_balance() # call the check_balance() method |
| 52 | + print(f'Your balance is ${balance}') |
| 53 | + elif command == 'deposit': |
| 54 | + amount = float(input('How much would you like to deposit? ')) |
| 55 | + atm.deposit(amount) # call the deposit(amount) method |
| 56 | + print(f'Deposited ${amount}') |
| 57 | + elif command == 'withdraw': |
| 58 | + amount = float(input('How much would you like ')) |
| 59 | + # call the check_withdrawal(amount) method |
| 60 | + if atm.check_withdrawal(amount): |
| 61 | + atm.withdraw(amount) # call the withdraw(amount) method |
| 62 | + print(f'Withdrew ${amount}') |
| 63 | + else: |
| 64 | + print('Insufficient funds') |
| 65 | + elif command == 'interest': |
| 66 | + amount = atm.calc_interest() # call the calc_interest() method |
| 67 | + atm.deposit(amount) |
| 68 | + print(f'Accumulated ${amount} in interest') |
| 69 | + elif command == 'transactions': |
| 70 | + for transaction in atm.transactions: |
| 71 | + print(transaction) |
| 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('transactions - print a list of transactions') |
| 79 | + print('exit - exit the program') |
| 80 | + elif command == 'exit': |
| 81 | + break |
| 82 | + else: |
| 83 | + print('Command not recognized') |
0 commit comments