|
| 1 | +# Lab 12: ATM |
| 2 | + |
| 3 | +Let's represent an ATM with a class containing two attributes: a balance and an interest rate. A newly created account will default to a balance of 0 and an interest rate of 0.1%. Implement the initializer, as well as the following functions: |
| 4 | + |
| 5 | +- `check_balance()` returns the account balance |
| 6 | +- `deposit(amount)` deposits the given amount in the account |
| 7 | +- `check_withdrawal(amount)` returns true if the withdrawn amount won't put the account in the negative |
| 8 | +- `withdraw(amount)` withdraws the amount from the account and returns it |
| 9 | +- `calc_interest()` returns the amount of interest calculated on the account |
| 10 | + |
| 11 | + |
| 12 | +```python |
| 13 | +atm = ATM() # create an instance of our class |
| 14 | +print('Welcome to the ATM') |
| 15 | +while True: |
| 16 | + command = input('Enter a command: ') |
| 17 | + if command == 'balance': |
| 18 | + balance = atm.check_balance() # call the check_balance() method |
| 19 | + print(f'Your balance is ${balance}') |
| 20 | + elif command == 'deposit': |
| 21 | + amount = float(input('How much would you like to deposit? ')) |
| 22 | + atm.deposit(amount) # call the deposit(amount) method |
| 23 | + print(f'Deposited ${amount}') |
| 24 | + elif command == 'withdraw': |
| 25 | + amount = float(input('How much would you like ')) |
| 26 | + if atm.check_withdrawal(amount): # call the check_withdrawal(amount) method |
| 27 | + atm.withdraw(amount) # call the withdraw(amount) method |
| 28 | + print(f'Withdrew ${amount}') |
| 29 | + else: |
| 30 | + print('Insufficient funds') |
| 31 | + elif command == 'interest': |
| 32 | + amount = atm.calc_interest() # call the calc_interest() method |
| 33 | + atm.deposit(amount) |
| 34 | + print(f'Accumulated ${amount} in interest') |
| 35 | + elif command == 'help': |
| 36 | + print('Available commands:') |
| 37 | + print('balance - get the current balance') |
| 38 | + print('deposit - deposit money') |
| 39 | + print('withdraw - withdraw money') |
| 40 | + print('interest - accumulate interest') |
| 41 | + print('exit - exit the program') |
| 42 | + elif command == 'exit': |
| 43 | + break |
| 44 | + else: |
| 45 | + print('Command not recognized') |
| 46 | +``` |
| 47 | + |
| 48 | +## Version 2 |
| 49 | + |
| 50 | +Have the ATM maintain a list of transactions. Every time the user makes a deposit or withdrawal, add a string to a list saying 'user deposited $15' or 'user withdrew $15'. Add a new method `print_transactions()` to your class for printing out the list of transactions, and add a `transactions` option to your REPL loop. |
| 51 | + |
0 commit comments