'''
需求如下:
1、员工信息:编号、姓名、性别、工资
2、编号不允许修改,不允许重复
3、功能实现:
新增员工信息
根据员工编号删除员工信息
根据编号修改员工信息
显示所有员工信息
退出员工管理系统
'''
#思路如下:
'''1\【数据如何存储】确定员工信息使用哪种容器来存储[根据业务特点选择]-------------------------------------'''
#需要增删改,所以元组不行;并且是根据编号进行增删改,所以使编号为key的字典容器比较适合【字典嵌套字典】
#即:员工编号:员工信息(姓名、性别、工资)例如 1101:{'name': obaa,'gender': male,'salary': 10000}
'''2\搭建系统业务框架---------------------------------------------'''
def menu():
"""显示系统菜单"""
pass
while True:
#1、显示系统菜单
menu()
#2、获得用户输入的命令
user_input=int(input('请输入您的命令:'))
#3、根据用户输入判断做什么事情
if user_input==1:
print('添加员工信息')
elif user_input==2:
print('修改员工信息')
elif user_input==3:
print('删除员工信息')
elif user_input==4:
print('查看员工信息')
elif user_input==5:
print('退出系统')
break
else:
print('输入的数据不合法')
'''3\具体实现每个业务功能----------------------------------------------------------'''
#存储所有的员工信息-----------------------
employees = {}
#定义菜单函数-----------------------------
def menu():
"""显示系统菜单"""
print('*' * 18)
print('欢迎使用员工管理系统!')
print('*' * 6, '菜单', '*' * 6)
print('新增一个员工信息,请输入:1')
print('修改一个员工信息,请输入:2')
print('删除一个员工信息,请输入:3')
print('查看所有员工信息,请输入:4')
print('退出员工管理系统,请输入:5')
#定义新增员工函数----------------------------------------------------------
def new_employees():
"""添加员工信息"""
#1、获得员工编号、姓名、性别、工资;因为员工编号key不能重复,需要判断key
em_id=input('请输入编号:')
all_id=list(employees.keys())#将编号key转换成列表,才可进行遍历查询重复值
if em_id in all_id: #判断编号是否存在
print('编号重复,添加失败!')
return #不符合return的直接进行下一步,因此不用在下面再加else了
em_name=input('请输入姓名:')
em_gender = input('请输入性别:')
em_salary = input('请输入工资:')
#2、info作为值,编号作为key,保存到employees字典中
employees_info={'name':em_name,'gender':em_gender,'salary':em_salary}
employees[em_id]=employees_info#将编号作为info的key,新建到info字典中
#输出为:1001:{'name':em_name,'gender':em_gender,'salary':em_salary}
print('员工编号为%s的员工信息添加成功!'%em_id)
#定义修改信息的函数-------------------------------------------------------------
def edit_employees():
"""修改员工信息"""
input_id = input('请输入要修改的员工编号:')
all_id = list(employees.keys())
if input_id not in all_id:
print('该编号不存在,修改失败!')
return
#列出原来的值和需要输入的新值
new_name=input('姓名是:%s,您要修改为:'% employees[input_id]['name'])
new_gender = input('性别是:%s,您要修改为:' % employees[input_id]['gender'])
new_salary=input('工资是:%s,您要修改为:'% employees[input_id]['salary'])
#判断输入值是否为空
if new_name=='' or new_gender=='' or new_salary=='':
print('输入的值不能为空')
else:
employees[input_id]['name']=new_name
employees[input_id]['gender'] = new_gender
employees[input_id]['salary'] = new_salary
print('员工编号%s的信息修改成功'%input_id)
#定义删除信息的函数-------------------------------------------------------------
def del_employees():
"""删除员工信息"""
input_id=input('请输入要删除的员工编号:')
all_id=list(employees.keys())
if input_id not in all_id:
print('该编号不存在,删除失败!')
return
del employees[input_id]
print('员工编号为%s的员工信息删除成功!'%input_id)
#定义查看信息的函数-------------------------------------------------------------
def show_employees():
"""显示员工信息"""
for info in employees.items(): #返回结果:(1001, {'name': 38, 'gender': '男', 'salary': 99})
print('%s\t\t%s\t\t%s\t\t%s'%(info[0],#此处显示编号key值
info[1]['name'],#info[1]为编号key后面的values值,包含姓名、性别、工资三个值
info[1]['gender'],
info[1]['salary']))
#结果示例:(编号、姓名、性别、工资)
# 1 1 1 1
# 2 2 2 2
# 3 3 3 3
#写出主体部分-------------------------------------------------------------
while True:
#1、显示系统菜单
menu()
#2、获得用户输入的命令
user_input=int(input('请输入您的命令:'))
#3、根据用户输入判断做什么事情
employees_dict={}
if user_input==1:
#直接在此写添加的代码结构不清晰,可以定义一个函数,直接调用【下方同理】
new_employees()
print(employees) #把添加的值显示出来
elif user_input==2:
edit_employees()
print(employees)
elif user_input==3:
del_employees()
print(employees)
elif user_input==4:
show_employees()
print(employees)
elif user_input==5:
print('退出系统成功!')
break
else:
print('输入的数据不合法')
python-综合案例
最新推荐文章于 2024-04-08 09:15:54 发布