Skip to content

Commit 65dcb49

Browse files
Merge pull request #114 from PdxCodeGuild/jonpan-lab10-atm
jonpan submitting lab10
2 parents 2d384c8 + dd7e535 commit 65dcb49

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

code/jonpan/lab10_v1.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# lab10 - ATM v1
2+
3+
class ATM:
4+
5+
def __init__(self, balance, interest_rate):
6+
self.balance = balance
7+
self.interest_rate = interest_rate
8+
# balance = 0
9+
interest_rate = 0.001
10+
11+
# returns the account balance
12+
def check_balance(self):
13+
return self.balance
14+
15+
# deposits the given amount in the account
16+
def deposit(self, amount):
17+
self.balance += amount
18+
19+
# returns true if the withdrawn amount won't put the account in the negative
20+
def check_withdrawal(self, amount):
21+
if self.balance < amount:
22+
return False
23+
else:
24+
return True
25+
26+
# withdraws the amount from the account and returns it
27+
def withdraw(self, amount):
28+
self.balance -= amount
29+
30+
# returns the amount of interest calculated on the account
31+
def calc_interest(self):
32+
p = self.balance
33+
t = float(input("Enter the number of years : "))
34+
r = self.interest_rate
35+
return p * (pow((1 + r / 100), t))
36+
37+
atm = ATM(0, 0.001) # create an instance of our class
38+
print('Welcome to the ATM')
39+
while True:
40+
command = input('Enter a command: ')
41+
if command == 'balance':
42+
balance = atm.check_balance() # call the check_balance() method
43+
print(f'Your balance is ${balance}')
44+
elif command == 'deposit':
45+
amount = float(input('How much would you like to deposit? '))
46+
atm.deposit(amount) # call the deposit(amount) method
47+
print(f'Deposited ${amount}')
48+
elif command == 'withdraw':
49+
amount = float(input('How much would you like '))
50+
if atm.check_withdrawal(amount): # call the check_withdrawal(amount) method
51+
atm.withdraw(amount) # call the withdraw(amount) method
52+
print(f'Withdrew ${amount}')
53+
else:
54+
print('Insufficient funds')
55+
elif command == 'interest':
56+
amount = atm.calc_interest() # call the calc_interest() method
57+
atm.deposit(amount)
58+
print(f'Accumulated ${amount:.2f} in interest')
59+
elif command == 'help':
60+
print('Available commands:')
61+
print('balance - get the current balance')
62+
print('deposit - deposit money')
63+
print('withdraw - withdraw money')
64+
print('interest - accumulate interest')
65+
print('exit - exit the program')
66+
elif command == 'exit':
67+
break
68+
else:
69+
print('Command not recognized')
70+
71+
72+

code/jonpan/lab10_v2.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)