Skip to content

Commit e123316

Browse files
Completed both versions of the lab.
1 parent ecbb9a3 commit e123316

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

code/zach/lab10-atm.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
return self.balance - amount >= 0
27+
28+
def withdraw(self, amount):
29+
"""Withdraws the amount from the account and returns it."""
30+
previous_balance = self.balance
31+
self.balance -= amount
32+
self.transactions.append({'Date': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'transaction_type': 'Withdrawal',
33+
'amount': amount, 'previous_balance': previous_balance, 'new_balance': self.balance})
34+
return self.balance
35+
36+
def calc_interest(self):
37+
"""Returns the amount of interest calculated on the account."""
38+
return self.balance * self.interest_rate
39+
40+
41+
atm = ATM() # create an instance of our class
42+
print('Welcome to the ATM')
43+
while True:
44+
command = input('Enter a command: ')
45+
if command == 'balance':
46+
balance = atm.check_balance() # call the check_balance() method
47+
print(f'Your balance is ${balance}')
48+
elif command == 'deposit':
49+
amount = float(input('How much would you like to deposit? '))
50+
atm.deposit(amount) # call the deposit(amount) method
51+
print(f'Deposited ${amount}')
52+
elif command == 'withdraw':
53+
amount = float(input('How much would you like '))
54+
# call the check_withdrawal(amount) method
55+
if atm.check_withdrawal(amount):
56+
atm.withdraw(amount) # call the withdraw(amount) method
57+
print(f'Withdrew ${amount}')
58+
else:
59+
print('Insufficient funds')
60+
elif command == 'interest':
61+
amount = atm.calc_interest() # call the calc_interest() method
62+
atm.deposit(amount)
63+
print(f'Accumulated ${amount} in interest')
64+
elif command == 'transactions':
65+
for transaction in atm.transactions:
66+
print(transaction)
67+
elif command == 'help':
68+
print('Available commands:')
69+
print('balance - get the current balance')
70+
print('deposit - deposit money')
71+
print('withdraw - withdraw money')
72+
print('interest - accumulate interest')
73+
print('transactions - print a list of transactions')
74+
print('exit - exit the program')
75+
elif command == 'exit':
76+
break
77+
else:
78+
print('Command not recognized')

0 commit comments

Comments
 (0)