"""__author__= xxx"""
"""
面向对象的人力资源管理系统:
功能:
1.能存多个员工信息(姓名、年龄、工号、薪资、职位、部门)
2.新员工入职:添加员工
3.员工离职:删除员工
4.查看某个员工信息
5.查询薪资最高的员工
6.查询指定部门中所有员工的平均薪资
7.求整个公司的员工的平均年龄
"""
import json
def read_json():
"""获取json文件"""
try:
with open('./company.json', 'r', encoding='utf-8') as f:
return json.load(f)
except (FileNotFoundError, json.decoder.JSONDecodeError):
with open('./company.json', 'w', encoding='utf-8') as f:
json.dump({}, f)
with open('./company.json', 'r', encoding='utf-8') as f:
return json.load(f)
def write_json(content):
"""写json文件"""
with open('./company.json', 'w', encoding='utf-8') as f:
json.dump(content, f)
class Staff:
"""员工类"""
def __init__(self, name='', age=0, number='', salary=0, position='', sect=''):
self.name = name
self.age = age
self.number = number
self.salary = salary
self.position = position
self.sect = sect
def __str__(self):
return '姓名:%s, 年龄:%d, 工号:%s, 薪资:%d, 职位:%s, 部门:%s' % \
(self.name, self.age, self.number, self.salary, self.position, self.sect)
# class Sect:
# """部门类"""
# def __init__(self, name=''):
# self.name = name
# self.count = 0
# self.staffs = []
#
# def add_staffs(self):
# """添加员工"""
# self.count += 1
# count = self.count
# print('请输入员工的信息')
# add_name = input('姓名:')
# add_age = int(input('年龄:'))
# add_number = self.name + str(count).rjust(3, '0')
# add_salary = int(input('薪资:'))
# add_position = input('职位:')
# sta = Staff(add_name, add_age, add_number, add_salary, add_position, self.name)
# self.staffs.append(sta)
#
# def del_staffs(self):
# """删除学生"""
class Company:
"""公司类"""
def __init__(self, name='', boss=''):
self.com_name = name
self.com_boss = boss
self.sects = []
# self.staffs = []
def add_staff(self):
"""添加员工"""
sect = input('请输入添加员工所属部门:')
count = 0
flag = True
# 判断部门是否存在
for item in self.sects[:]:
if sect in item['sect_name']:
flag = False
count = item['count']
break
if flag:
sect_list = {'sect_name': sect, 'staffs': [], 'count': 0}
self.sects.append(sect_list)
count = 0
print('请输入员工的信息')
add_name = input('姓名:')
add_age = int(input('年龄:'))
count += 1
add_number = sect + str(count).rjust(3, '0')
add_salary = int(input('薪资:'))
add_position = input('职位:')
sta = {'name': add_name, 'age': add_age, 'number': add_number, 'salary': add_salary, 'position': add_position,
'sect': sect}
# sta = Staff(add_name, add_age, add_number, add_salary, add_position, sect)
# 将员工添加进去
for item in self.sects[:]:
if sect == item['sect_name']:
item['staffs'].append(sta)
item['count'] = count
return self.sects
def del_staff(self):
"""删除员工"""
# 根据姓名删除员工
del_name = input('请输入要删除的员工姓名:')
flag = True
del_list = []
index = 0
for index1 in range(0, len(self.sects)):
staffs = self.sects[index1]['staffs']
for index2 in range(0, len(staffs)):
if del_name == staffs[index2]['name']:
flag = False
del_list.append([index1, index2])
print(index, staffs[index2])
index += 1
if flag:
print('本公司没有这个人!')
else:
index_input = int(input('请输入要删除的员工下标:'))
if 0 <= index_input <= index-1:
del self.sects[del_list[index_input][0]]['staffs'][del_list[index_input][1]]
print('删除成功')
else:
print('下标输入错误!')
return self.sects
def find_staff(self):
"""查看员工信息"""
# 根据姓名查看员工信息
find_name = input('请输入要查看的员工姓名:')
flag = True
for index1 in range(0, len(self.sects)):
staffs = self.sects[index1]['staffs']
for index2 in range(0, len(staffs)):
if find_name == staffs[index2]['name']:
print(self.sects[index1]['staffs'][index2])
flag = False
if flag:
print('本公司没有这个人!')
def find_max_salary(self):
"""查看公司薪资最高的员工"""
max_salary = 0
staff = {}
for item in self.sects[:]:
item1 = item['staffs']
for item2 in item1:
if max_salary <= item2['salary']:
max_salary = item2['salary']
staff = item2
print(staff)
def avg_salary(self):
"""查看公司某部门的平均薪资"""
avg1 = None
sum1 = 0
sect_name_input = input('请输入部门的名字:')
for item in self.sects[:]:
if sect_name_input == item['sect_name']:
for item1 in item['staffs']:
sum1 += item1['salary']
avg1 = sum1 / len(item['staffs'])
print('该部门的平均薪资为:%d' % avg1)
def avg_age(self):
"""查看公司所有员工的平均年龄"""
avg2 = None
sum2 = 0
index2 = 0
for item in self.sects[:]:
for item1 in item['staffs']:
sum2 += item1['age']
index2 += 1
avg2 = sum2 / index2
print('该公司的平均年龄为:%d' % avg2)
def print_main():
print('人力资源管理系统')
print('1.添加员工')
print('2.删除员工')
print('3.查看员工信息')
print('4.查看公司薪资最高的员工')
print('5.查看公司某部门的平均薪资')
print('6.查看公司平均年龄')
print('0.退出')
if __name__ == '__main__':
content = read_json()
com1 = Company('qianfeng', 'xxx')
content['com_name'] = com1.com_name
content['com_boss'] = com1.com_boss
write_json(content)
while True:
print_main()
num = input('>>>')
if num == '0':
break
if num == '1':
content = read_json()
if not content.get('sects'):
content['sects'] = []
com1.sects = content['sects']
print(com1.sects)
sects = com1.add_staff()
content['sects'] = sects
write_json(content)
print('添加成功')
continue
if num == '2':
content = read_json()
com1.sects = content['sects']
sects = com1.del_staff()
content['sects'] = sects
write_json(content)
continue
if num == '3':
content = read_json()
com1.sects = content['sects']
com1.find_staff()
continue
if num == '4':
content = read_json()
com1.sects = content['sects']
com1.find_max_salary()
continue
if num == '5':
content = read_json()
com1.sects = content['sects']
com1.avg_salary()
continue
if num == '6':
content = read_json()
com1.sects = content['sects']
com1.avg_age()
continue
网友评论