-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_cmdline.py
349 lines (293 loc) · 10.1 KB
/
main_cmdline.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Employee Management System Using Python – copyassignment.com
from os import system
import re
# importing mysql connector
import mysql.connector
import platform
# making Connection
# TODO Implement Generated Configuration File
con = mysql.connector.connect(
host="localhost", user="root", password="halohalo")
my_os = platform.system()
print(my_os)
clear_cmd = 'cls' if my_os == 'Windows' else 'clear'
mycursor = con.cursor()
try:
# if DB not exist, create one
mycursor.execute("CREATE DATABASE Employee")
mycursor.execute("USE Employee")
except:
mycursor.execute("USE Employee")
# Create Table
try:
mycursor.execute("CREATE TABLE empdata ( Id INT(11) PRIMARY KEY , Name VARCHAR(1800), Emial_Id TEXT(1800),Phone_no BIGINT( 11 ), Address TEXT(1000), Post TEXT(1000), Salary BIGINT(20))")
except:
# Table is created, do nothing
pass
# make a regular expression
# for validating an Email
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
# for validating an Phone Number
Pattern = re.compile("(0|91)?[7-9][0-9]{9}")
# Function to Add_Employ
def Add_Employ():
print("{:>60}".format("-->>Add Employee Record<<--"))
Id = input("Enter Employee Id: ")
# checking If Employee Id is Exit Or Not
if (check_employee(Id) == True):
print("Employee ID Already Exists\nTry Again..")
press = input("Press Any Key To Continue..")
Add_Employ()
Name = input("Enter Employee Name: ")
# checking If Employee Name is Exit Or Not
if (check_employee_name(Name) == True):
print("Employee Name Already Exists\nTry Again..")
press = input("Press Any Key To Continue..")
Add_Employ
Email_Id = input("Enter Employee Email ID: ")
if(re.fullmatch(regex, Email_Id)):
print("Valid Email")
else:
print("Invalid Email")
press = input("Press Any Key To Continue..")
Add_Employ()
Phone_no = input("Enter Employee Phone No.: ")
if(Pattern.match(Phone_no)):
print("Valid Phone Number")
else:
print("Invalid Phone Number")
press = input("Press Any Key To Continue..")
Add_Employ()
Address = input("Enter Employee Address: ")
Post = input("Enter Employee Post: ")
Salary = input("Enter Employee Salary: ")
data = (Id, Name, Email_Id, Phone_no, Address, Post, Salary)
# Instering Employee Details in
# the Employee (empdata) Table
sql = 'insert into empdata values(%s,%s,%s,%s,%s,%s,%s)'
c = con.cursor()
# Executing the sql Query
c.execute(sql, data)
# Commit() method to make changes in the table
con.commit()
print("Successfully Added Employee Record")
press = input("Press Any Key To Continue..")
menu()
# Function To Check if Employee With
# given Name Exist or not
def check_employee_name(employee_name):
# query to select all Rows from
# employee(empdata) table
sql = 'select * from empdata where Name=%s'
# making cursor buffered to make
# rowcount method work properly
c = con.cursor(buffered=True)
data = (employee_name,)
# Execute the sql query
c.execute(sql, data)
# rowcount method to find number
# of rowa with given values
r = c.rowcount
if r == 1:
return True
else:
return False
# Function To Check if Employee With
# given Id Exist or not
def check_employee(employee_id):
# query to select all Rows from
# employee(empdata) table
sql = 'select * from empdata where Id=%s'
# making cursor buffered to make
# rowcount method work properly
c = con.cursor(buffered=True)
data = (employee_id,)
# Execute the sql query
c.execute(sql, data)
# rowcount method to find number
# of rowa with given values
r = c.rowcount
if r == 1:
return True
else:
return False
# Function to Display_Employ
def Display_Employ():
print("{:>60}".format("-->> Display Employee Record <<--"))
# query to select all rows from Employee (empdata) Table
sql = 'select * from empdata'
c = con.cursor()
# Executing the sql query
c.execute(sql)
# Fetching all details of all the Employees
r = c.fetchall()
for i in r:
print("Employee Id: ", i[0])
print("Employee Name: ", i[1])
print("Employee Email Id: ", i[2])
print("Employee Phone No.: ", i[3])
print("Employee Address: ", i[4])
print("Employee Post: ", i[5])
print("Employee Salary: ", i[6])
print("\n")
press = input("Press Any key To Continue..")
menu()
# Function to Update_Employ
def Update_Employ():
print("{:>60}".format("-->> Update Employee Record <<--\n"))
Id = input("Enter Employee Id: ")
# checking If Employee Id is Exit Or Not
if(check_employee(Id) == False):
print("Employee Record Not exists\nTry Again")
press = input("Press Any Key To Continue..")
menu()
else:
Email_Id = input("Enter Employee Email ID: ")
if(re.fullmatch(regex, Email_Id)):
print("Valid Email")
else:
print("Invalid Email")
press = input("Press Any Key To Continue..")
Update_Employ()
Phone_no = input("Enter Employee Phone No.: ")
if(Pattern.match(Phone_no)):
print("Valid Phone Number")
else:
print("Invalid Phone Number")
press = input("Press Any Key To Continue..")
Update_Employ()
Address = input("Enter Employee Address: ")
# Updating Employee details in empdata Table
sql = 'UPDATE empdata set Email_Id = %s, Phone_no = %s, Address = %s where Id = %s'
data = (Email_Id, Phone_no, Address, Id)
c = con.cursor()
# Executing the sql query
c.execute(sql, data)
# commit() method to make changes in the table
con.commit()
print("Updated Employee Record")
press = input("Press Any Key To Continue..")
menu()
# Function to Promote_Employ
def Promote_Employ():
print("{:>60}".format("-->> Promote Employee Record <<--\n"))
Id = input("Enter Employee Id: ")
# checking If Employee Id is Exit Or Not
if(check_employee(Id) == False):
print("Employee Record Not exists\nTry Again")
press = input("Press Any Key To Continue..")
menu()
else:
Amount = int(input("Enter Increase Salary: "))
# query to fetch salary of Employee with given data
sql = 'select Salary from empdata where Id=%s'
data = (Id,)
c = con.cursor()
# executing the sql query
c.execute(sql, data)
# fetching salary of Employee with given Id
r = c.fetchone()
t = r[0]+Amount
# query to update salary of Employee with given id
sql = 'update empdata set Salary = %s where Id = %s'
d = (t, Id)
# executing the sql query
c.execute(sql, d)
# commit() method to make changes in the table
con.commit()
print("Employee Promoted")
press = input("Press Any key To Continue..")
menu()
# Function to Remove_Employ
def Remove_Employ():
print("{:>60}".format("-->> Remove Employee Record <<--\n"))
Id = input("Enter Employee Id: ")
# checking If Employee Id is Exit Or Not
if(check_employee(Id) == False):
print("Employee Record Not exists\nTry Again")
press = input("Press Any Key To Continue..")
menu()
else:
# query to delete Employee from empdata table
sql = 'delete from empdata where Id = %s'
data = (Id,)
c = con.cursor()
# executing the sql query
c.execute(sql, data)
# commit() method to make changes in the empdata table
con.commit()
print("Employee Removed")
press = input("Press Any key To Continue..")
menu()
# Function to Search_Employ
def Search_Employ():
print("{:>60}".format("-->> Search Employee Record <<--\n"))
Id = input("Enter Employee Id: ")
# checking If Employee Id is Exit Or Not
if(check_employee(Id) == False):
print("Employee Record Not exists\nTry Again")
press = input("Press Any Key To Continue..")
menu()
else:
# query to search Employee from empdata table
sql = 'select * from empdata where Id = %s'
data = (Id,)
c = con.cursor()
# executing the sql query
c.execute(sql, data)
# fetching all details of all the employee
r = c.fetchall()
for i in r:
print("Employee Id: ", i[0])
print("Employee Name: ", i[1])
print("Employee Email Id: ", i[2])
print("Employee Phone No.: ", i[3])
print("Employee Address: ", i[4])
print("Employee Post: ", i[5])
print("Employee Salary: ", i[6])
print("\n")
press = input("Press Any key To Continue..")
menu()
# Menu function to display menu
def menu():
system(clear_cmd)
print("{:>60}".format("************************************"))
print("{:>60}".format("-->> Employee Management System <<--"))
print("{:>60}".format("************************************"))
print("1. Add Employee")
print("2. Display Employee Record")
print("3. Update Employee Record")
print("4. Promote Employee Record")
print("5. Remove Employee Record")
print("6. Search Employee Record")
print("7. Exit\n")
print("{:>60}".format("-->> Choice Options: [1/2/3/4/5/6/7] <<--"))
ch = int(input("Enter your Choice: "))
if ch == 1:
system(clear_cmd)
Add_Employ()
elif ch == 2:
system(clear_cmd)
Display_Employ()
elif ch == 3:
system(clear_cmd)
Update_Employ()
elif ch == 4:
system(clear_cmd)
Promote_Employ()
elif ch == 5:
system(clear_cmd)
Remove_Employ()
elif ch == 6:
system(clear_cmd)
Search_Employ()
elif ch == 7:
system(clear_cmd)
print("{:>60}7".format("Have A NIce Day :)"))
exit(0)
else:
print("Invalid Choice!")
press = input("Press Any key To Continue..")
menu()
# Calling menu function
menu()