Skip to content

Commit f7732fc

Browse files
committed
adding lab 12 part 1 and part 2
1 parent 334fa46 commit f7732fc

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

Code/matthew/python/Labs/lab12.py

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

0 commit comments

Comments
 (0)