-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUS.py
326 lines (228 loc) · 8.91 KB
/
BUS.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
BANKING USER SYSTEM
An intermediate python project that helps strengthen python skills by tying together various
python and object-oriented programming concepts.
This program allows a user to create a banking account, View/Edit User Profile and Deposit/Withdraw Money
"""
#PROGRAM START
#Import necessary packages.
#stdiomask helps mask your pasword while typing and must be installed using pip
import hashlib
import _pickle as pickle
from stdiomask import getpass
from datetime import datetime
#Customer class stores the User Profile and allows the User to perform Account Operations
class Customer:
total_customers = 0
#Initializes Instance variables through a dictionary
def __init__(self, account_dict):
for key in account_dict:
setattr(self, key, account_dict[key])
self.account_dict = account_dict
#Allows User to View and Modify Profile
def profile(self):
print("\n_____PROFILE_____\n")
print(f"\n1. Name: {self.name}\n2. Account Number: {self.accnum}\n3. Date of Birth: {self.dob}\n4. Mobile Number: {self.mobnum}\n5. Last Login: {self.last_login}")
while True:
profile_input = input("\nTo EDIT your profile type 'E' else type 'M' to go back to MAIN MENU: ").strip().upper()
if (profile_input == "E"):
account_dict = update_profile()
self.name = account_dict['name']
self.mobnum = account_dict['mobnum']
self.dob = account_dict['dob']
print("\nYour Profile has been Updated Successfuly!\n")
with open(f"{self.uname}.pkl", "wb") as cdeets:
pickle.dump(self, cdeets, -1)
return "goto_main_menu"
if (profile_input == "M"):
return "goto_main_menu"
else:
print("\nError! Please Enter a Valid Response.")
#Prints Account Summary and Transaction History (If Available)
def account_summary(self):
print("\n_____ACCOUNT SUMMARY_____\n")
print(f"\n Name: {self.name} | Account Number: {self.accnum} | Account Balance: {self.accbalance}\n")
print("\n*** Transaction History ***\n")
if(len(self.acchistory.keys()) == 0):
print("No Transactions Found!")
else:
print(f"No. Date Time Type Amount\n")
for i, key in enumerate(self.acchistory, start=1):
if(self.acchistory[key][0] == "Deposit"):
print(f"{i}. {key} {self.acchistory[key][0]} {self.acchistory[key][1]}")
else:
print(f"{i}. {key} {self.acchistory[key][0]} {self.acchistory[key][1]}")
while True:
response = input("\nEnter 'M' to go back to MAIN MENU: ").strip().upper()
if(response == 'M'):
return "goto_main_menu"
else:
print("Please Enter a Valid Response.")
continue
#Allows User to Deposit and Withdraw Money to/from Account.
#Appends data to 'acchistory' to View Transaction History in Account Summary.
def dw(self):
print("\n_____DEPOSIT / WITHDRAWAL_____")
while True:
print(f"\n\n Current Account Balance: {self.accbalance}")
dorw = input("\nEnter 'D' to DEPOSIT or 'W' to WITHDRAW money (Enter 'M' to go back to MAIN MENU): ").strip().upper()
if (dorw == 'M'):
return "goto_main_menu"
elif (dorw == "D"):
try:
d_amount = int(input("\nEnter Amount to Deposit: ").strip())
except:
print("\n Error! Enter a Valid Number.")
d_datetime = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
self.acchistory[d_datetime] = ['Deposit', d_amount]
self.accbalance += d_amount
with open(f"{self.uname}.pkl", "wb") as cdeets:
pickle.dump(self, cdeets, -1)
print("\n\nDeposit Successful!")
elif (dorw == "W"):
try:
w_amount = int(input("\nEnter Amount to Withdraw: ").strip())
except:
print("\n Error! Enter a Valid Number.")
continue
if(w_amount > self.accbalance):
print("\n Insufficient Balance in Account!")
else:
d_datetime = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
date_key = d_date +" "+ d_time
self.acchistory[date_key] = ['Withdrawal', w_amount]
self.accbalance -= w_amount
with open(f"{self.uname}.pkl", "wb") as cdeets:
pickle.dump(self, cdeets, -1)
print("\n\nWithdrawal Successful!")
else:
print("\nPlease Enter a Valid Response. \n\n")
#Create account with Useername and Password and Add Profile details.
#Creates and Stores data in pickle file named '<username>'
#Appends username to "UnameList.pkl" to check if Username exists while creating New Account.
def create_account():
print("\n_____CREATE YOUR ACCOUNT_____\n")
try:
with open("UnameList.pkl", 'rb') as UnameList:
temp_uname_list = pickle.load(UnameList)
except:
with open("UnameList.pkl", 'wb') as UnameList:
temp_uname_list = []
while True:
uname = input("Enter a Username: ").strip()
if (uname in temp_uname_list):
print("Username already exists! Please choose another Username.\n")
continue
else:
passw = input("Enter a Password: ").strip()
passw_conf = input("Enter your Password again: ").strip()
#passw = getpass(prompt="Enter a Password: ").strip()
#passw_conf = getpass(prompt="Enter your Password again: ").strip()
if(passw != passw_conf):
print("Error! Passwords do not match. \n\n")
continue
elif(len(uname) == 0 or len(passw) == 0):
print("Please Enter a Valid Response. \n\n")
continue
else:
temp_uname_list.append(uname)
passw = hashlib.sha256(passw.encode()).hexdigest()
account_dict = update_profile()
accnum = datetime.now().strftime("%d%m%Y%H%M%S%f")[0:-5]
accbalance = 0
acchistory = {}
last_login = "Now"
account_dict.update({"accnum":accnum, "accbalance":accbalance, "acchistory":acchistory, "last_login":last_login,"uname":uname, "passw":passw})
customer1 = Customer(account_dict)
with open(f"{uname}.pkl", "wb") as cdeets:
pickle.dump(customer1, cdeets, -1)
with open("UnameList.pkl", "wb") as UnameList:
pickle.dump(temp_uname_list, UnameList, -1)
print("\nUser Created Successfuly! Login to your account via Existing Customer.\n\n")
return "goto_intro"
#Add/Modify User Profile Details
def update_profile():
account_dict = {}
print("\n_____PROFILE UPDATE_____\n")
while True:
date_error = False
name = input("Enter your Name: ").strip().upper()
mobnum = input("Enter your 10-digit Mobile Number: ").strip()
dob = input("Enter your Date of Birth (DD/MM/YYYY): ").strip()
try:
dob = datetime.strptime(dob, "%d/%m/%Y").strftime("%d/%m/%Y")
except:
date_error = True
if(len(name)==0 or not mobnum.isdigit() or len(mobnum)!=10 or date_error):
print("\nERROR! Please Check your Input and Try Again!\n")
else:
account_dict.update({"name":name, "mobnum":mobnum, "dob":dob})
return account_dict
#Login mechanism into User Account.
#If Invalid Credentials are provided more than 3 times, EXIT Program.
def login_account():
global customer
global login_datetime
trys_left = 3
print("\n_____ACCOUNT LOGIN_____\n")
while True and trys_left > 0:
uname = input("Enter a Username: ").strip()
passw = input("Enter a Password: ").strip()
passw = hashlib.sha256(passw.encode()).hexdigest()
try:
with open(f"{uname}.pkl", "rb") as unamepkl:
customer = pickle.load(unamepkl)
except:
trys_left -= 1
print(f"Invalid Credentials! You have {trys_left} attempts left.\n")
continue
if(passw != customer.passw):
trys_left -= 1
print(f"Invalid Credentials! You have {trys_left} attempts left.\n")
else:
global is_login
is_login = True
login_datetime = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
print(f"\nLogin Successful! | Last Login: {customer.last_login}")
return "goto_main_menu"
print("Error! Restart Program.\n\n\n")
return "goto_intro"
#Intro Page when program is first run. Connects to other functions
def intro():
print("*** x-x-x- WELCOME TO THE FUTURE BANK -x-x-x ***\n")
c_type = input("Enter E for 'Existing Customer' or N for 'New Customer': ").strip().upper()
if(c_type == 'N'):
return create_account()
if(c_type == 'E'):
return login_account()
else:
print("\nError! Please Enter a Valid Response.\n\n")
return intro()
#Main Menu Page when the User Logs in Successfully
def main_menu():
print("\n_____MAIN MENU_____\n")
print("1. Profile \n2. Account Summary \n3. Deposit/Withdrawal \n4. Logout")
while True:
response = input("Enter your Option Number (1-4): ").strip()
if (response == '1'):
return customer.profile()
elif (response == '2'):
return customer.account_summary()
elif (response == '3'):
return customer.dw()
elif(response == '4'):
customer.last_login = login_datetime
with open(f"{customer.uname}.pkl", "wb") as cdeets:
pickle.dump(customer, cdeets, -1)
print("\nLogout Successful!\n\n")
return "goto_intro"
else:
print("\nError! Please Enter a Valid Response.\n")
#Main Program that loops, till User closes program manually
intro_result = intro()
while True:
if (intro_result == "goto_intro"):
intro_result = intro()
elif (intro_result == "goto_main_menu"):
intro_result = main_menu()
#PROGRAM END