Skip to content

Instantly share code, notes, and snippets.

@yogesh-aggarwal
Last active October 21, 2019 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yogesh-aggarwal/27a3097944aafe858187a3b665675977 to your computer and use it in GitHub Desktop.
Save yogesh-aggarwal/27a3097944aafe858187a3b665675977 to your computer and use it in GitHub Desktop.
"""
Employee Management System
Name: Nishika | Saloni
Class: XII-B
Roll no: "I don't know"
"""
filename = "emp.txt"
seperator = "="*60
def add():
# For name
while True:
name = input("Name: ").capitalize()
if name:
name += "\n"
break
# For experience
while True:
exp = input("Experience: ")
if exp:
exp += "\n"
break
# For lastCompany
while True:
lastCompany = input("Last company worked (nill): ")
if lastCompany:
lastCompany += "\n"
break
attrib = [f"{seperator}\n", name, exp, lastCompany]
# import pdb;pdb.set_trace()
file = open(filename, "a+")
# Writing objects
file.writelines(attrib)
file.close()
_name = name.replace("\n", "")
print(f"Successfully added employee with name as {_name}.")
def delete():
data = read(show=True)
if data:
while True:
try:
poss = int(input("\nPossition: "))
except Exception:
continue
break
del data[poss-1]
final = []
for i in data:
final.append(f"{seperator}\n")
for j in i:
final.append(f"{j}\n")
file = open(filename, "w+")
file.writelines(final)
file.close()
print(f"Successfully deleted employee at {poss} possition.")
def update():
data = read(show=True)
if data:
while True:
try:
poss = int(input("\nPossition: ")) - 1
emp = data[poss]
# For name
while True:
name = input("Name: ").capitalize()
if name:
break
# For experience
while True:
exp = input("Experience: ")
if exp:
break
# For lastCompany
while True:
lastCompany = input("Last company worked (nill): ")
if lastCompany:
break
data[poss] = [name, exp, lastCompany]
except Exception:
print("Invalid possition")
continue
break
final = []
for i in data:
final.append(f"{seperator}\n")
for j in i:
final.append(f"{j}\n")
file = open(filename, "w+")
file.writelines(final)
file.close()
print(f"Successfully updated employee at {poss+1} possition.")
def read(show=False):
# Retrieving the data from file
file = open(filename)
data = file.read()
file.close()
data = data.split(seperator)
data.remove("")
final = []
for emp in data:
mid = emp.split("\n")
final.append(mid[1:len(mid)-1])
if show:
if final:
for i in range(len(final)):
print(f"Employee {i+1}: \n\tName: {final[i][0]}\n\tExperience: {final[i][1]}\n\tPreviously worked: {final[i][2]}")
else:
print("No employees to show!")
return final
else:
return False
def takeCommand(greet=False):
if greet:
print("Welcome to Employee Management System")
commands = [
"a: Add employee",
"d: Delete employee",
"u: Update employee",
"s: Show employees",
"ab: About program",
"h: Help",
"q: Quit"
]
def printCommands():
for i in commands:
print(i)
printCommands()
while True:
command = input("\n> ").lower()
if command == "a":
add()
elif command == "d":
delete()
elif command == "u":
update()
elif command == "s":
read(show=True)
elif command == "h":
printCommands()
elif command == "ab":
print("\n\tEmployee Management System program helping us to manage the employees of our company easily.")
elif command == "q":
print("Bye!")
exit()
else:
print("Invalid choice!")
def main():
file = open(filename, "a+")
file.close()
takeCommand(greet=True)
try:
main()
except Exception as e:
print(e)
print("\nBye!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment