-
Notifications
You must be signed in to change notification settings - Fork 0
/
atm.py
245 lines (231 loc) · 7.92 KB
/
atm.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from getpass import getpass
#pre-defined dictionary with user-names, passwords and balance
accounts = {'admin' : ['2901'],
'user1' : ['1234', 3000],
'user2' : ['7008', 4000],
'user3' : ['9876', 5000]}
#function that formats and prints a message given as param
def print_message(message):
border = '=' * 50
print(border)
print('{:*^50s}'.format(message))
print(border)
print()
#takes care of logging the user into his account by entering correct username
#and password
def login(acc):
users = list(acc.keys())
while True:
print("Please enter username and password.")
user = input("Enter Username: ").lower()
if user in users:
count = 0
actual_password = acc[user][0]
while count < 3:
password = getpass("Enter Pin: ")
count += 1
if password == actual_password:
print_message(' SUCCESSFULLY LOGGED IN. ')
return user
else:
print_message(' INCORRECT PIN. TRY AGAIN! ')
else:
print_message(' INCORRECT PIN ENTERED 3 TIMES. EXITING... ')
return ''
else:
print_message(' INVALID USERNAME ')
#checks the balance of the account given as a parameter
def check_balance(user, acc):
print()
print(user, 'Account Balance:', acc[user][1])
#increases the balance of the given account by an amount given from input
def deposit_funds(user, acc):
while True:
funds = input('Enter funds to deposit: ')
try:
funds = int(funds)
break
except ValueError:
print_message(' Invalid Input. Try Again! ')
balance = acc[user][1] + funds
acc[user][1] = balance
print(funds, 'deposited.')
print('New balance:', balance)
return acc
#decreases the balance of the given account by an amount given from input
def withdraw_funds(user, acc):
while True:
funds = input('Enter funds to withdraw: ')
try:
funds = int(funds)
break
except ValueError:
print_message(' Invalid Input. Try Again! ')
balance = acc[user][1] - funds
acc[user][1] = balance
print(funds, 'withdrawn.')
print('New balance:', balance)
return acc
#changes the pin of the given account
def change_pin(user, acc):
old_password = acc[user][0]
count = 0
while count < 3:
temp = getpass('Enter old pin: ')
count += 1
if temp == old_password:
while True:
new_password = getpass('Enter new pin: ')
if len(new_password) != 4:
print_message(' MAKE SURE PIN IS 4 DIGITS! ')
continue
confirmation = getpass('Confirm new pin: ')
if new_password == confirmation:
print_message(' YOUR PIN IS CHANGED. ')
acc[user][0] = new_password
return acc
else:
print_message(" PINS DON'T MATCH. TRY AGAIN! ")
else:
print_message(' OLD PIN IS INCORRECT. TRY AGAIN! ')
else:
print_message(' INCORRECT PIN ENTERED 3 TIMES. EXITING... ')
return acc
#shows all the users in the system
def view_users(acc):
print('=' * 20)
print('|| USERNAME ||')
print('=' * 20)
users = list(acc.keys())
for user in users:
print('|| {:<14s} ||'.format(user))
print('=' * 20)
print()
#adds a user to the system
def add_user(acc):
print('Please enter username and password of the new user.')
username = input('Enter username: ')
while True:
password = getpass('Enter pin: ')
if len(password) != 4:
print_message(' MAKE SURE PIN IS 4 DIGITS! ')
continue
confirmation = getpass('Confirm pin: ')
if password == confirmation:
while True:
balance = input('Enter balance: ')
try:
balance = int(balance)
break
except ValueError:
print_message(' Invalid Input. Try Again! ')
acc[username] = [password, balance]
print_message(' NEW USER CREATED. ')
return acc
else:
print_message(" PINS DON'T MATCH. TRY AGAIN! ")
#removes a user from the system
def remove_user(acc):
view_users(acc)
users = list(acc.keys())
while True:
user = input("Enter user to remove: ").lower()
if user == 'admin':
print_message(' ADMIN CANNOT BE REMOVED ')
return acc
if user in users:
count = 0
actual_password = acc[user][0]
while count < 3:
password = getpass("Enter Pin: ")
count += 1
if password == actual_password:
while True:
temp = input('Are you sure you want to remove {}? (Y/N) '.format(user)).lower()
if temp == 'y':
del acc[user]
print_message(' {} DELETED '.format(user))
return acc
elif temp == 'n':
return acc
else:
print_message(' PLEASE ENTER (Y) OR (N) ')
else:
print_message(' INCORRECT PIN. TRY AGAIN! ')
else:
print_message(' INCORRECT PIN ENTERED 3 TIMES. EXITING... ')
return acc
else:
print_message(' INVALID USERNAME ')
#called when a guest logs in
def guest():
global accounts
while True:
print()
print('1. Check account balance.')
print('2. Deposit funds.')
print('3. Withdraw funds.')
print('4. Change ATM pin.')
print('5. Log out')
print('6. Exit')
choice = input('Enter choice: ')
if choice not in '123456':
print_message(' INVALID CHOICE ENTERED. TRY AGAIN! ')
continue
else:
if choice == '1':
check_balance(user, accounts)
inp = input('Press enter to continue.')
elif choice == '2':
accounts = deposit_funds(user, accounts)
inp = input('Press enter to continue.')
elif choice == '3':
accounts = withdraw_funds(user, accounts)
inp = input('Press enter to continue.')
elif choice == '4':
accounts = change_pin(user, accounts)
inp = input('Press enter to continue.')
elif choice == '5':
start(accounts)
break
elif choice == '6':
break
#called when an admin logs in
def admin():
global accounts
while True:
print()
print('1. View users.')
print('2. Add user.')
print('3. Remove user.')
print('4. Log out')
print('5. Exit')
choice = input('Enter choice: ')
if choice not in '12345':
print_message(' INVALID CHOICE ENTERED. TRY AGAIN! ')
continue
else:
if choice == '1':
view_users(accounts)
inp = input('Press enter to continue.')
elif choice == '2':
accounts = add_user(accounts)
view_users(accounts)
inp = input('Press enter to continue.')
elif choice == '3':
accounts = remove_user(accounts)
inp = input('Press enter to continue.')
elif choice == '4':
start(accounts)
break
elif choice == '5':
break
def start(acc):
global user
user = login(acc)
if user != '':
if user == 'admin':
admin()
else:
guest()
start(accounts)