需求:

  • 可以查询员工所有信息
  • 可以修改员工信息
  • 可以增加新员工

code:

 import sys,json
# yuangong = {
# "1": ["faker", "22", "11111111111", "mid", "2013-01-01"],
# "2": ["huni", "23", "22222222222", "top", "2014-01-01"],
# "3": ["bang", "24", "33333333333", "adc", "2015-01-01"],
# "4": ["wolf", "25", "44444444444", "sup", "2016-01-01"],
# "5": ["blank", "26", "55555555555", "jun", "2017-01-01"]}
# with open('E:/Staff management.txt','w')as w_f:
# json.dump(yuangong,w_f) salary = {} with open('E:/Staff management.txt', 'r') as read_f:
f_line = json.load(read_f)
#read_f.write(json.dump(salary))
salary = f_line
#print(salary) def append_file():
with open('E:/Staff management.txt', 'r+') as append_f:
json.dump(append_f,salary)
return def write_file():
with open('E:/Staff management.txt', 'w') as write_f:
json.dump(salary,write_f)
return def server():
for i in range(1):
print("1.查询员工信息\n2.修改员工信息\n3.增加新员工信息\n4.删除员工\n5.退出")
return def server_choice(a):
while True:
choice = input("Please input your choice:")
if choice == '':
query()
elif choice == '':
modify()
elif choice == '':
increase()
elif choice == '':
delete()
elif choice == '':
sys.exit("程序已退出,欢迎重新运行")
else:
print("你输入的不存在,请重新输入") def query():
position = input("请输入输入员工编号或按b退回上一层:")
if f_line.get(position):
#print(f_line[position])
print("员工信息:\n名字:%s\n年龄:%s\n电话号码:%s\n职位:%s\n入职时间:%s" % (f_line[position][0],f_line[position][1],f_line[position][2],f_line[position][3],f_line[position][4]))
query()
elif position.lower() == 'b':
server()
else:
print('你输入的编号不存在')
query() def modify():
position = input("Please 要修改的员工编号或者按b退到上一层:")
if f_line.get(position):
correct = input(" 1.name:%s\n 2.age:%s\n 3.phone number:%s\n 4.position:%s\n 5.Entry time:%s\n请输入你要修改的信息:" % (f_line[position][0],f_line[position][1],f_line[position][2],f_line[position][3],f_line[position][4]))
if correct == '':
name = input("请输入你要修改的名字:")
salary[position][0] = name
write_file()
modify()
elif correct == '':
age = input("请输入你要修改的年龄:")
if age.isdigit():
salary[position][1] = age
write_file()
modify()
else:
print("你输入的不是数字,请重新输入")
modify()
elif correct == '':
phone = input("请输入你要修改的电话号码:")
salary[position][2] = phone
write_file()
modify()
elif correct == '':
post = input("请输入你要修改的职位:")
salary[position][3] = post
write_file()
modify()
elif correct == '':
Entry = input("请输入你要修改的入职时间:")
salary[position][4] = Entry
write_file()
modify()
else:
print("Input error,Please re-enter")
modify()
elif position.lower() == 'b':
server()
else:
print("你输入的员工编号不存在,请重新输入")
modify() def increase():
post = []
position = input("请输入你要添加的员工编号或者按b返回:")
if salary.get(position):
print("你输入的编号已存在,请重新输入")
increase()
elif position.isdigit():
name = input("请输入员工姓名:")
age = input("请输入员工年龄:")
phone = input("请输入员工电话号码:")
positioN = input("请输入员工职务:")
Entry = input("请输入员工入职时间:")
post.append(name)
post.append(age)
post.append(phone)
post.append(positioN)
post.append(Entry)
salary[position] = post
write_file()
server()
elif position == 'b':
server()
else:
print("你输入的编号格式不正确(必须为数字),请重新输入:")
increase() def delete():
position = input("请输入你要删除的员工编号或者输入b退到上一层:")
if salary.get(position):
salary.pop(position)
write_file()
print("成功删除")
delete()
elif position == 'b':
server()
else:
print("你输入的编号不存在,请重新输入")
delete() server_choice(server())

05-11 17:43