作业:
面向对象人力资源管理系统:
- 能存多个员工信息
(每个员工的信息有:姓名、年龄、工号、薪资、职位、部门) - 新员工入职(添加员工)
- 员工离职(删除员工)
- 查看某个员工的信息
- 查询薪资最高的员工
- 查询指定部门中所有员工的平均薪资
- 求整个公司的员工的平均年龄
(可以根据能力改善和添加功能,做到尽量贴近生活)
class Employee:
"""员工类"""
def __init__(self):
self.name = ''
self.age = ''
self.work_id = ''
self.money = ''
self.position = ''
self.department = ''
class Manager:
"""公司类"""
def __init__(self):
self.c_name = ''
self.employees = []
#添加员工
def add_employee(self,employee):
self.employees.append(employee)
print('新员工入职成功')
#删除员工
def del_employee(self,employee):
self.employees.remove(employee)
print('删除成功')
#查看某个员工信息
def find_employee(self):
message = input('输入你要查找的员工名字:')
for employee in self.employees[:]:
if message == employee['name']:
print(employee['age'],employee['work_id'],employee['money'],employee['position'],employee['department'])
# print('查无此人')
#查询薪资最高的员工
def query_money(self):
for epployee in self.employees:
for self.money in epployee:
money_list = []
money_list.append(self.money)
result = sum(money_list)
print('薪资最高为%s'%result)
#查询指定部门中所有员工的平均薪资
def query_pos_money(self,query_pos):
# query_pos = input('请输入要查询的部门')
sum = 0
count = 0
for epployee in self.employees:
employee = Employee()
#把每个员工的信息遍历出来以键值对的形式存入message
message =employee.__dict__
if query_pos == message['department']:
sum += message['money']
count += 1
print('该部门平均薪资为%s'%(sum/count))
#求整个公司的员工的平均年龄
def avg_age(self):
sum = 0
for employee in self.employees:
employee = Employee()
message = employee.__dict__
sum += message['age']
print('平均年龄为%s'%(sum/len(self.employees)))
comp1 = Manager()
company = input('请输入公司名:')
comp1.c_name = company
while True:
print('======================')
print(' 1.新员工入职 ')
print(' 2.员工离职 ')
print(' 3.查看信息 ')
print(' 4.查询薪资最高员工 ')
print(' 5.查询部门平均薪资 ')
print(' 6.员工平均年龄')
print('======================')
value = input('请选择:')
if value == '1':
while True:
name = input('请输入员工姓名:')
age = input('请输入员工年龄:')
work_id = input('请输入员工工号:')
money = input('请输入员工薪资:')
position = input('请输入员工职位:')
department = input('请输入员工部门:')
emp = Employee()
emp.name = name
emp.age = age
emp.work_id = work_id
emp.money = money
emp.position = position
emp.department = department
comp1.add_employee(emp)
emp = emp.__dict__
print(' 1.继续添加')
print('2.返会上一层')
value2 = input('请选择:')
if value2 == '1':
continue
else:
break
if value == '2':
while True:
del_name = input('请输入离职员工姓名:')
comp1.del_employee(del_name)
print('1.继续删除')
print('2.返会上一层')
value3 = input('请选择:')
if value3 == '1':
continue
else:
break
if value == '3':
comp1.find_employee()
if value =='4':
comp1.query_money()
if value == '5':
query_pos = input('请输入要查询的部门')
if emp.department == query_pos:
comp1.query_money(query_pos)
老师讲解版
"""__author__zhangdong__"""
class Staff:
"""员工类"""
def __init__(self,name,age,salary,job,department):
self.name = name
self.age = age
self.id = ''
self.salary = salary
self.job = job
self.department = department
def show_info(self):
print('姓名:%s 工号:%s 部门:%s 职位:%s'%(self.name,self.id,self.department,self.job))
def __add__(self, other):
return self.name+other.name
class HrManager:
"""人力资源管理系统"""
#整个公司的所有的员工
all_staff = []
#目前公司已经入职的人数
__numbers = 0
_all_department = ['财务部','行政部','研发部','总经办','后勤部']
@classmethod
def add_staff(cls):
name = input('名字:')
age = input('年龄:')
salary = input('薪资:')
while True:
print('公司部门:',*cls._all_department)
department = input('部门:')
if department in cls._all_department:
break
else:
print('部门输入有误')
job = input('职位:')
#生成工号
cls.__numbers += 1
id = str(cls.__numbers).rjust(4,'0')
#创建员工对象
staff = Staff(name,age,salary,job,department)
staff.id = id
#添加员工
cls.all_staff.append(staff)
@classmethod
def del_staff(cls):
"""删除员工"""
del_name = input('请输入要删除的员姓名')
flag = False
for staff in cls.all_staff[:]:
if staff.name == del_name:
flag = True
staff.show_info()
value = input('是否删除(Y/N):')
if value == 'Y':
cls.all_staff.remove(staff)
print('删除成功')
if not flag:
print('公司没有该员工')
@classmethod
def find_staff(cls):
find_name = input('请输入要查找的员工姓名:')
flag = False
for staff in cls.all_staff[:]:
if staff.name == find_name:
flag = True
staff.show_info()
if not flag:
print('公司没有该员工')
@classmethod
def get_most_rich(cls):
if len(cls.all_staff) == 0:
print('公司还没有员工')
return
max_staff = cls.all_staff[0]
for staff in cls.all_staff:
if staff.salary>max_staff.salary:
max_staff = staff
print('薪资最高是:%s %d'%(max_staff.name,max_staff.salary))
@classmethod
def get_aver_rich(cls):
if len(cls.all_staff) == 0:
print('公司还没有员工')
return
find_department = input('请输入要查找的部门:')
sum = 0
count = 1
for staff in cls.all_staff:
if staff.department == find_department:
sum += staff.salary
count += 1
print('该部门平均薪资为%s'%(sum/count))
@classmethod
def average_age(cls):
if len(cls.all_staff) == 0:
print('公司还没有员工')
return sum(cls.all_staff)/(len(cls.all_staff))
if __name__ == '__main__':
HrManager.add_staff()
HrManager.find_staff()
HrManager.del_staff()
HrManager.get_most_rich()
HrManager.get_aver_rich()
HrManager.average_age()