This repository was archived by the owner on Sep 3, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMiniBankSystem.py
More file actions
323 lines (306 loc) · 13.5 KB
/
Copy pathMiniBankSystem.py
File metadata and controls
323 lines (306 loc) · 13.5 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
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
import mysql.connector
import datetime
import time
class BankSystem:
#constructor
def __init__(self,server,user,pwd,db) -> None:
self.server = server
self.user = user
self.pwd = pwd
self.db = db
self.con = mysql.connector.connect(host = server, username = user, password = pwd, database = db)
#Current Date and Time (timestamp)
def time_stamp(self):
timeStamp = datetime.datetime.now()
return timeStamp
#Wait for user input to return
def go_back(self):
back = input("Press ENTER to return")
print()
#Fxn to generate an account number for user
def generate_ac_no(self,name,country):
name_char_vals = [ord(i) for i in name]
sum_name_char_vals = sum(name_char_vals)
base_num = 90036900100000
ac_no = country[0:2].upper() + str(base_num + sum_name_char_vals)
return ac_no
#Fxn for creating new account
def sign_up(self):
name = input("Enter your name: ")
country = input("Enter country name: ")
contact = input("Enter your contact no.: ")
email = input("Enter your email: ")
password = input("Enter your password: ")
ac_num = self.generate_ac_no(name,country)
query1 = f"select * from users where Email='{email}'"
c = self.con.cursor()
c.execute(query1)
found = [i for i in c]
if len(found) == 0:
query2 = f"insert into users(Name,Country,Contact,Email,Password,AccountNo,CurrentBalance) values('{name}','{country}','{contact}','{email}','{password}','{ac_num}',0);"
c.execute(query2)
self.con.commit()
#create table for handling transaction details for an account
query3 = f"create table {ac_num}(SNo int auto_increment,Credit float default 0,Debit float default 0,Balance float not null,Remarks varchar(30) default 'None', TransactionTime datetime,primary key(SNo));"
c.execute(query3)
self.con.commit()
c.close()
print("Account created successfully.")
self.go_back()
else:
print("Email already used!")
self.go_back()
#Fxn for checking current account balance
def check_bal(self, ac_no):
query1 = f"select CurrentBalance from users where AccountNo='{ac_no}';"
c = self.con.cursor()
c.execute(query1)
for i in c:
balance = float(i[0])
c.close()
return (balance)
#Fxn for adding money to the account
def add_money(self, ac_no, balance, amount):
try:
query1 = f"insert into {ac_no}(Credit,Balance,TransactionTime) values({amount},{balance}+{amount},'{self.time_stamp()}');"
query2 = f"update users set CurrentBalance={balance}+{amount} where AccountNo='{ac_no}';"
c = self.con.cursor()
c.execute(query1)
self.con.commit()
c.execute(query2)
self.con.commit()
c.close()
return True
except:
print("Invalid value!")
#Fxn for withdrawing money from account
def withdraw(self, ac_no, balance,amount):
try:
query1 = f"insert into {ac_no}(Debit,Balance,TransactionTime) values({amount},{balance}-{amount},'{self.time_stamp()}');"
query2 = f"update users set CurrentBalance={balance}-{amount} where AccountNo='{ac_no}';"
if balance > amount:
c = self.con.cursor()
c.execute(query1)
self.con.commit()
c.execute(query2)
self.con.commit()
c.close()
return True
else:
return False
except:
print("Invalid value!")
#Fxn for viewing transaction details
def view_transactions(self, ac_no):
query1 = f"describe {ac_no}"
query2 = f"select * from {ac_no} order by TransactionTime desc;"
c = self.con.cursor()
c.execute(query1)
for i in c:
print(i[0],end=" ")
print()
c.execute(query2)
for i in c:
print(i[0],i[1],i[2],i[3],i[4],i[5], sep=" | ")
#Fxn for resetting password of the user account
def reset_pwd(self,ac_no):
current_pwd = input("Enter your current password: ")
query1 = f"select * from users where AccountNo='{ac_no}';"
c = self.con.cursor()
c.execute(query1)
found = [i for i in c]
if current_pwd == found[0][4]:
new_pwd = input("Enter new password: ")
confirm_pwd = input("Confirm password: ")
if new_pwd == confirm_pwd:
query2 = f"update users set Password='{new_pwd}' where Password='{current_pwd}';"
c.execute(query2)
self.con.commit()
print("Password Changed Successfully!")
else:
print("Oops! you've entered incorrect password. Try Again.")
#Fxn for sending meny to another account
def send_money(self, ac_no, balance,amount):
if amount <= balance:
print("1. Via Contact Number\n2. Via Account Number")
try:
inp = int(input("Enter your choice: "))
if inp == 1:
receiver_contact = input("Enter receiver's contact number: ")
query1 = f"select * from users where Contact='{receiver_contact}';"
c = self.con.cursor()
c.execute(query1)
found = [i for i in c]
receiver_ac = found[0][5]
receiver_bal = float(found[0][6])
if len(found) == 1:
confirm = input(f"Type 'CONFIRM' to send {amount} to {found[0][0]}: ")
if confirm.upper() == "CONFIRM":
self.withdraw(ac_no, balance,amount)
query2 = f"update {ac_no} set Remarks='TFR to {found[0][0]}' where Balance={self.check_bal(ac_no)};"
c.execute(query2)
self.con.commit()
self.add_money(receiver_ac,receiver_bal,amount)
query3 = f"update {receiver_ac} set Remarks='Recv From {ac_no}' where Balance={self.check_bal(receiver_ac)};"
c.execute(query3)
self.con.commit()
print("Transaction Successful.")
else:
print("Transaction Failed!")
elif inp == 2:
receiver_ac_no = input("Enter receiver's account number: ")
query1 = f"select * from users where AccountNo='{receiver_ac_no}';"
c = self.con.cursor()
c.execute(query1)
found = [i for i in c]
receiver_ac = found[0][5]
receiver_bal = float(found[0][6])
if len(found) == 1:
confirm = input(f"Type 'CONFIRM' to send {amount} to {found[0][0]}: ")
if confirm.upper() == "CONFIRM":
self.withdraw(ac_no, balance,amount)
query2 = f"update {ac_no} set Remarks='TFR to {found[0][0]}' where Balance={self.check_bal(ac_no)};"
c.execute(query2)
self.con.commit()
self.add_money(receiver_ac,receiver_bal,amount)
query3 = f"update {receiver_ac} set Remarks='Recv From {ac_no}' where Balance={self.check_bal(receiver_ac)};"
c.execute(query3)
self.con.commit()
print("Transaction Successful.")
else:
print("Transaction Failed!")
else:
print("Invalid input!")
except:
print("Invalid value!")
else:
print("Oops! insufficient balace.")
#Fxn for getting access to the user account
def sign_in(self):
c = self.con.cursor()
email = input("Enter your email: ")
#kinda easter egg ^_^
if email.lower() == "iamaqib":
query = f"select * from users;"
c.execute(query)
for data in c:
print(data)
self.go_back()
else:
password = input("Enter your password: ")
query1 = f"select * from users where Email='{email}' and Password='{password}';"
c.execute(query1)
found = [i for i in c]
if len(found) != 0:
while True:
print(f"Welcome Mr. {found[0][0].upper()}")
ac_no = found[0][5]
print(f"A/C No.: {ac_no}")
print("="*30)
print("1. Check Balance\n2. Add money\n3. Withdraw money\n4. Send money\n5. View Transaction Details\n6. Reset Password\n7. Sign Out")
try:
op = int(input("Enter your choice: "))
balance = self.check_bal(ac_no)
if op == 1:
print(f"Available balance: {balance}")
self.go_back()
elif op == 2:
try:
amount = float(input("Enter amount to add: "))
if self.add_money(ac_no,balance,amount):
print("Transaction Successful.")
self.go_back()
except:
print("Invalid value!")
self.go_back()
elif op == 3:
try:
amount = float(input("Enter amount to withdraw: "))
if self.withdraw(ac_no,balance,amount):
print("Transaction Successful.")
self.go_back()
else:
print("Oops! insuficient balance.")
self.go_back()
except:
print("Invalid value!")
self.go_back()
elif op == 4:
try:
amount = float(input("Enter amount: "))
self.send_money(ac_no,balance,amount)
self.go_back()
except:
print("Invalid value!")
self.go_back()
elif op == 5:
self.view_transactions(ac_no)
self.go_back()
elif op == 6:
self.reset_pwd(ac_no)
self.go_back()
elif op == 7:
print("Signed out.")
self.go_back()
break
else:
print("Invalid value!")
self.go_back()
except:
print("Invalid value!")
self.go_back()
else:
print("Account doesn't exist!")
print("\n1. Create new account\n2. Main menu")
new_acc = int(input("Enter your choice: "))
if new_acc == 1:
self.sign_up()
elif new_acc == 2:
pass
else:
print("Invalid value!")
self.go_back()
#close connection
def close_connection(self):
self.con.close()
#MAIN
heading = "========== Mini Bank System =========="
for i in heading:
print(i,end="",flush=True)
time.sleep(0.05)
time.sleep(1)
print("\nUSER LOGIN")
username = input("\tEnter username: ")
password = input("\tEnter password: ")
print("Please Wait! Connecting to DATABASE", end="")
for i in "...":
print(i,end="",flush=True)
time.sleep(0.5)
time.sleep(0.5)
print()
try:
BankSys = BankSystem("localhost",username,password, "bank")
print("Successfully Connected!")
while 1:
print("="*10 + " Welcome to Mini Bank " + "="*10)
try:
print("1. Sign in\n2. Create an account\n3. Exit")
ch = int(input("Enter your choice: "))
if ch == 1:
BankSys.sign_in()
elif ch == 2:
BankSys.sign_up()
elif ch == 3:
BankSys.close_connection()
break
else:
print("Invalid choice.")
BankSys.go_back()
except:
print("Invalid value.")
BankSys.go_back()
print("Thanks for using this program.")
close = input("Press ENTER ")
except:
print("Oops! An error occured. Check login credentials.")
close = input("Press ENTER ")