-
Notifications
You must be signed in to change notification settings - Fork 4
/
BankingSystem.py
215 lines (201 loc) · 9.55 KB
/
BankingSystem.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
import mysql.connector
# Establish a connection to the MySQL database
connection = mysql.connector.connect(
host="localhost",
user="root",
password="<Your Password>",
database="<Your Database>"
)
# Create a cursor object to interact with the database
cursor = connection.cursor()
# Define the table structure for the customers
cursor.execute("""
CREATE TABLE IF NOT EXISTS customers (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
pin VARCHAR(255),
balance DECIMAL(10, 2)
)
""")
# Commit changes to the database
connection.commit()
i = 0
counter_1 = 1
counter_2 = 1
# Function to add a new customer to the database
def add_customer(name, pin, balance):
cursor.execute("INSERT INTO customers (name, pin, balance) VALUES (%s, %s, %s)", (name.upper(), pin, balance))
connection.commit()
# Function to retrieve customer data by name and pin
def get_customer_data(name, pin):
cursor.execute("SELECT id, balance FROM customers WHERE name = %s AND pin = %s", (name.upper(), pin))
result = cursor.fetchone()
return result # Returns (id, balance) or None if not found
# Function to update customer balance by id
def update_customer_balance(customer_id, new_balance):
cursor.execute("UPDATE customers SET balance = %s WHERE id = %s", (new_balance, customer_id))
connection.commit()
def displaystud():
sql = "SELECT * FROM customers"
cursor.execute(sql)
customers = cursor.fetchall()
for customer in customers:
print(f"Customer ID: {customer[0]}")
print(f"Name: {customer[1]}")
print(f"Balance: {customer[3]} -/Rs")
print("\n")
return customers
while True:
print("=====================================")
print(" ----Welcome to Times Bank---- ")
print("*************************************")
print("=<< 1. Open a new account >>=")
print("=<< 2. Withdraw Money >>=")
print("=<< 3. Deposit Money >>=")
print("=<< 4. Check Customers & Balance >>=")
print("=<< 5. Exit/Quit >>=")
print("*************************************")
# The below statement takes the choice number from the user.
choiceNumber = input("Select your choice number from the above menu : ")
if choiceNumber == "1":
print("Choice number 1 is selected by the customer")
NOC = eval(input("Number of Customers : "))
i = i + NOC
customerNames = []
if i > 5:
print("\n")
print("Customer registration exceed reached or Customer registration too low")
i = i - NOC
else:
while counter_1 <= i:
name = input("Input Fullname : ")
pin = str(input("Please input a pin of your choice : "))
balance = 0
deposition = eval(input("Please input a value to deposit to start an account : "))
balance = balance + deposition
# Add the new customer to the database
add_customer(name, pin, balance)
print("\nName=", end=" ")
print(name)
print("Pin=", end=" ")
print(pin)
print("Balance=", end=" ")
print(balance, end=" ")
print("-/Rs")
counter_1 = counter_1 + 1
counter_2 = counter_2 + 1
print("\nYour name is added to the customer system")
print("Your pin is added to the customer system")
print("Your balance is added to the customer system")
print("----New account created successfully !----")
print("\n")
print("Your name is available on the customer's list now : ")
customerNames.append(name)
print(customerNames)
print("\n")
print("Note! Please remember the Name and Pin")
print("========================================")
mainMenu = input("Please press enter key to go back to the main menu to perform another function or exit ...")
elif choiceNumber == "2":
print("Choice number 2 is selected by the customer")
n = 0
# The while loop below would work when the pin or the username is wrong.
while n < 1:
k = -1
name = input("Please input name : ")
pin = input("Please input pin : ")
# The while loop below will keep the function running to find the correct user.
if get_customer_data(name, pin):
n = n + 1
id, balance = get_customer_data(name, pin)
# These statements below would show the customer balance and update list values according to
# the withdrawl made.
print("Your Current Balance: ", end=" ")
print(balance, end=" ")
print("-/Rs")
# This statement below takes the withdrawl from the customer.
deposition = eval(input("Enter the value you want to withdraw : "))
temp = balance - deposition
if(temp<0):
print("Balance too low..")
break
else:
balance = temp
update_customer_balance(id, balance)
print("\n")
print("----Withdrawl successful!----")
print("Your New Balance: ", balance, end=" ")
print("-/Rs\n\n")
if n < 1:
print("Your name and pin does not match!\n")
break
# This statement below helps the user to go back to the start of the program (main menu).
mainMenu = input("Please press enter key to go back to main menu to perform another function or exit ...")
elif choiceNumber == "3":
print("Choice number 3 is selected by the customer")
n = 0
# The while loop below would work when the pin or the username is wrong.
while n < 1:
k = -1
name = input("Please input name : ")
pin = input("Please input pin : ")
# The while loop below will keep the function running to find the correct user.
if get_customer_data(name, pin):
n = n + 1
id, balance = get_customer_data(name, pin)
# These statements below would show the customer balance and update list values according to
# the deposition made.
print("Your Current Balance: ", end=" ")
print(balance, end=" ")
print("-/Rs")
#balance = (customerBalances[k])
# This statement below takes the depositionn from the customer.
deposition = eval(input("Enter the value you want to deposit : "))
balance = balance + deposition
update_customer_balance(id, balance)
print("\n")
print("----Deposition successful!----")
print("Your New Balance: ", balance, end=" ")
print("-/Rs\n\n")
if n < 1:
print("Your name and pin does not match!\n")
break
# This statement below helps the user to go back to the start of the program (main menu).
mainMenu = input("Please press enter key to go back to main menu to perform another function or exit ...")
elif choiceNumber == "4":
j = 0
print("Choice number 4 is selected by the customer")
# This while loop will prevent the user using the account if the username or pin is wrong.
while j < 1:
name = input("Please input name : ")
pin = input("Please input pin : ")
# This while loop will keep the function running when variable k is smaller than length of the
if get_customer_data(name, pin):
j = j + 1
id, balance = get_customer_data(name, pin)
# These few statement would show the balance taken from the list.
print("Your Current Balance:", end=" ")
print(balance, end=" ")
print("-/Rs\n")
if j < 1:
# The if condition above would work when the pin or the name is wrong.
print("Your name and pin does not match!\n")
# This statement below helps the user to go back to the start of the program (main menu).
mainMenu = input("Please press enter key to go back to main menu to perform another function or exit ...")
elif choiceNumber == "5":
# These statements would be just showed to the customer.
print("Choice number 5 is selected by the customer")
print("Thank you for using our banking system!")
print("\n")
print("Come again")
print("Bye bye")
break
else:
# This else function above would work when a wrong function is chosen.
print("Invalid option selected by the customer")
print("Please Try again!")
# This statement below helps the user to go back to the start of the program (main menu).
mainMenu = input("Please press enter key to go back to main menu to perform another function or exit ...")
# Close the cursor and connection when done
cursor.close()
connection.close()