-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank Management.py
279 lines (206 loc) · 8.56 KB
/
Bank Management.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
import mysql.connector
import time
import getpass
import sys
login_count = 0
for i in range(0, 3):
try:
print("\n")
passwd = getpass.getpass("Enter the MySQL Client Password: ")
conobj = mysql.connector.connect(host = 'localhost', user = 'root', password = passwd)
if conobj.is_connected:
print("Connected Successfully")
break
else:
print("Password Entered is Incorrect\n")
login_count = login_count + 1
if login_count > 3:
print("\nPassword Entered Incorrectly 3 times")
time.sleep(5)
print('\nExiting Program')
sys.exit(1)
break
print(login_count)
except mysql.connector.errors.ProgrammingError:
print("\nConnection failed, incorrect password entered\n")
if login_count > 3:
sys.exit(1)
break
try:
cur = conobj.cursor()
except NameError:
print("This program requires Authorisation / DataBase Admin Password")
admin_name = input("Enter the administrator name: ")
print("Welcome", admin_name.capitalize())
print("Table Sample:")
print("""
_______________________________________________________
|Sr. No| Customer ID | Account Number | Customer Name |
|------+-------------+----------------+---------------|
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
|------+-------------+----------------+---------------|
\n""")
def db_creation():
'''
This function creates the MySQL Bank Management DataBase
'''
try:
cur.execute('show databases')
pre_existing_db = cur.fetchall()
req_db = 'banking_db'
if (req_db, ) in pre_existing_db:
print("The required DataBase exists in the Client")
else:
print("Creating the Bank DataBase")
cur.execute("create database banking_db")
cur.execute('commit')
cur.execute('use banking_db')
except NameError:
quit()
db_creation()
def table_creation():
'''
This function creates a table within the Banking DataBase
'''
cur.execute('show tables')
pre_existing_tables = cur.fetchall()
req_tables = ('customer_details', )
if req_tables in pre_existing_tables:
print("Requirement already Satisfied")
else:
print("Creating the Customer Table")
cur.execute("create table customer_details(Customer_ID int, Customer_Name varchar(255), Account_Number int, Account_Balance int)")
cur.execute('commit')
table_creation()
def account_balance_checker():
'''
This function checks for the balance in the Customer's account
'''
customer_account_no = int(input("Enter the Account Number of the Customer: "))
cur.execute('select * from customer_details where Account_Number = %i;' %(customer_account_no))
record = cur.fetchall()
for i in record:
print("Customer Name: ", i[1])
print("BALANCE: ", i[3])
def account_creation():
'''
This function creates a new Bank account for a Customer
'''
cus_name = input("Enter the First Name of the customer: ")
name = cus_name.capitalize()
cus_id = int(input('Enter the customer ID: '))
acc_no = id(name)
acc_bal = int(input('Enter the current account balance: '))
cur.execute('insert into customer_details(Customer_ID, Customer_Name, Account_Number, Account_Balance) values ({}, "{}", {}, {});'.format(cus_id, name, acc_no, acc_bal))
cur.execute('commit')
def del_account():
'''
This function deletes an existing Bank Account from the database
'''
account_no = int(input("Enter the Account Number: "))
confirmation = input("Are you sure?(Y/n): ")
if confirmation.lower() == 'y':
del_account_cmd = 'delete from customer_details where Account_Number = %s' %(account_no)
cur.execute(del_account_cmd)
cur.execute('commit')
else:
print("Request Cancelled")
def display_details():
'''
This function prints/displays the details of a bank account
'''
account_no = int(input("Enter Account Number: "))
retrieve_record = 'select * from customer_details where Account_Number = %s' %(account_no)
cur.execute(retrieve_record)
details = cur.fetchall()
for i in details:
print("CUSTOMER DETAILS FOR ACCOUNT NUMBER:", account_no," \n")
print("CUSTOMER ID:", i[0])
print("NAME:", i[1])
print("ACCOUNT NUMBER:", i[2])
print("ACCOUNT BALANCE:", i[3])
min_balance = 2500
def transfer_function():
'''
This function transfers an amount 'x' from account "A" to account "B"
'''
remitter_account_no = int(input("Enter the Remitter Account Number: "))
beneficiary_account_no = int(input("Enter the Beneficiary Account Number: "))
amount_x = int(input("Enter the amount to be transfered: "))
cur.execute('select Account_Balance from customer_details where Account_Number = %d' %(remitter_account_no))
remitter = cur.fetchall()
bal = remitter[0][0]
if (bal - amount_x) > min_balance:
cmd_upd_rem = 'update customer_details set Account_Balance = Account_Balance - %s' %(amount_x) + ' where Account_Number = %s' %(remitter_account_no)
cmd_upd_ben = 'update customer_details set Account_Balance = Account_Balance + %s' %(amount_x) + ' where Account_Number = %s' %(beneficiary_account_no)
cur.execute(cmd_upd_rem)
cur.execute(cmd_upd_ben)
cur.execute('commit')
time.sleep(5)
print("\nTransaction Completed Successfully")
else:
print("Transaction cannot continue. Account Balance is less than minimum balance.")
def deposit_function():
'''
This function deposits an amount 'x' into an Account
'''
dep_amt = int(input("Enter the amount to be deposited: "))
account = int(input("Enter the Account to deposit the amount: "))
cmd_upd_bal = 'update customer_details set Account_Balance = Account_Balance + %s' %(dep_amt) + ' where Account_Number = %s' %(account)
cur.execute(cmd_upd_bal)
cur.execute('commit')
time.sleep(5)
print("Money Deposited Successfully")
def withdraw_function():
'''
This function withdraws an amount 'x' from an Account
'''
try:
withdraw_amt = int(input("Enter the amount to be withdrawn: "))
account = int(input("Enter the Account Number to withdraw the amount: "))
cmd_upd_bal = 'update customer_details set Account_Balance = Account_Balance - %d' %(withdraw_amt) + ' where Account_number = %s' %(account)
cur.execute(cmd_upd_bal)
cur.execute('commit')
print("Money Withdrawn Successfully")
except AttributeError:
print("Un Unknown Error has caused the program from executing properly")
ans = 'y'
while ans.lower() == 'y':
print("\n\t\t\t\t\t\tMAIN MENU\n")
print('1. Account Management \n2. Transactions\n')
main_menu_choice = int(input("Enter your choice: "))
print()
if main_menu_choice == 1:
print('\t\t\tAccount Management\n\n1. Check for Balance in an account \n2. Create a new account \n3. Delete an account \n4. Display Account Details\n')
acc_choice = int(input("Enter your choice: "))
if acc_choice == 1:
account_balance_checker()
elif acc_choice == 2:
account_creation()
elif acc_choice == 3:
del_account()
elif acc_choice == 4:
display_details()
else:
print("Invalid Option Entered")
elif main_menu_choice == 2:
print('\t\t\tTransactions\n1. Transfer Money \n2. Depositing Money \n3. Withdrawing Money')
tra_choice = int(input("Enter your choice: "))
if tra_choice == 1:
transfer_function()
elif tra_choice == 2:
deposit_function()
elif tra_choice == 3:
withdraw_function()
else:
print("Invalid Option Entered")
else:
print("Invalid Main-Menu Option Entered")
print()
ans = input("Do you wish to continue?(Y/n): ")
print()
conobj.close()