-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_ATM.py
More file actions
99 lines (81 loc) · 3.41 KB
/
my_ATM.py
File metadata and controls
99 lines (81 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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%.
# The REPL below calls the methods of the class to simulate an ATM.
class ATM:
def __init__(self, balance=0, message1=[], message2=[]):
# initialize our class with a balance of 0 and an interest rate of 0.1
self._balance = balance
self.__interest = 0.1
self.message1 = message1
self.message2 = message2
def deposit(self, amount):
# add the given amount to the balance
self._balance += amount
self.message1.append(f'User deposit {amount}')
return self._balance
def check_withdrawal(self, amount):
# returns true if the withdrawn amount won't put the account in the negative, false otherwise
if self._balance >= amount:
return True
else:
return False
def withdraw(self, amount):
# removes the given amount from the balance and returns it
self._balance -= amount
self.message2.append(f'User withdraw {amount}')
return self._balance
def balance(self):
# return the account balance
return self._balance
def deposit_interest(self):
# calculate the amount of interest accumulated and add it to our balance
# return the amount of interest added
return self._balance * self.__interest
# Part 2: Have the ATM maintain a list of transactions
# Add a new method
def print_transactions(self):
if transactions == 'deposit':
return self.message1
elif transactions == 'withdraw':
return self.message2
atm = ATM() # create an instance of our class
print('Welcome to the ATM')
while True:
command = input('Enter a command: ')
if command == 'balance':
balance = atm.balance() # call the balance() method
print(f'Your balance is ${balance}')
elif command == 'deposit':
amount = float(input('How much would you like to deposit? '))
atm.deposit(amount) # call the deposit(amount) method
print(f'Deposited ${amount}')
elif command == 'withdraw':
amount = float(input('How much would you like '))
if atm.check_withdrawal(amount): # call the check_withdrawal(amount) method
atm.withdraw(amount) # call the withdraw(amount) method
print(f'Withdrew ${amount}')
else:
print('Insufficient funds')
elif command == 'interest':
amount = atm.deposit_interest() # call the calc_interest() method
atm.deposit(amount)
print(f'Deposited ${amount} in interest')
elif command == 'transactions':
transactions = input("Which transaction would you like to print (deposit or withdraw)?: ")
if transactions == 'deposit':
msg = atm.print_transactions()
print(msg)
elif transactions == 'withdraw':
msg = atm.print_transactions()
print(msg)
elif command == 'help':
print('Available commands:')
print('balance - get the current balance')
print('deposit - deposit money')
print('withdraw - withdraw money')
print('interest - accumulate interest')
print('exit - exit the program')
elif command == 'exit':
break
else:
print('Command not recognized')