思路型代码
"""new python"""
class Employee:
"""员工类"""
def __init__(self, name, age, sex, staff_id, salary, position, department):
self.name = name
self.age = age
self.staff_id = staff_id
self.salary = salary
self.position = position
self.department = department
self.sex = sex
class Management:
"""员工管理器"""
employee_number = 0 # 用于实现自动生成的员工号,永久存储后将由读取文件替代
def __init__(self):
self.name = '人力资源管理器1.0' # 自定义名字
self.employees = [] # 自定义初始员工数组,永久存储后将由读取文件替代
@classmethod
def __create_id(cls): # id自动生成 永久存储读+写
cls.employee_number += 1
return '2018' + str(cls.employee_number).rjust(4, '0')
def add_employee(self): # 添加学生 永久存储读+写
print('the place of adding employee:')
name = input('new employee\'s name:')
age = int(input('new employee\'s age:'))
sex = input('new employee\'s sex:')
salary = int(input('new employee\'s salary:'))
position = input('new employee\'s position:')
department = input('new employee\'s department:')
employee = Employee(name, age, sex, Management.__create_id(), salary, position, department)
self.employees.append(employee)
print('添加成功')
def del_employee(self): # 删除学生(按照id)永久存储读+写
print('the place of deleting employee:')
staff_id = input('input the deleted employee\'s staff_id:')
for employee in self.employees[:]:
if employee.staff_id == staff_id:
self.employees.remove(employee)
break
print('删除成功')
def info_employee(self): # 单个员工信息查看(按照姓名)永久存储读
print('the place of looking up a employee:')
name = input('input the looking employee\'s name:')
for employee in self.employees:
if employee.name == name:
print('name:%s age:%d sex:%s staff_id:%s salary:%d position:%s department:%s' %
(employee.name, employee.age, employee.sex,
employee.staff_id, employee.salary,
employee.position, employee.department))
def max_salary_employee(self): # 最高工资员工信息提取 永久存储读
print('the employee of having max salary:')
max_employee = Employee('', 0, '', '', 0, '', '')
for employee in self.employees:
if max_employee.salary <= employee.salary:
max_employee = employee
print('name:%s age:%d sex:%s staff_id:%s salary:%d position:%s department:%s' %
(max_employee.name, max_employee.age, employee.sex,
max_employee.staff_id, max_employee.salary,
max_employee.position, max_employee.department))
def avg_department_salary(self): # 指定部门平均工资提取 永久存储读
print('the avg_salary of a department:')
department = input('the department\'s name:')
sum1 = 0
count = 0
for employee in self.employees:
if employee.department == department:
sum1 += employee.salary
count += 1
print(sum1/count)
def avg_age_allemployee(self): # 所有员工平均年龄 永久存储读
print('the avg_age of the company:')
sum1 = 0
for employee in self.employees:
sum1 += employee.age
print(sum1/len(self.employees))
def all_employee_info(self): # 显示所有员工信息 永久存储读
print('all information of employees:')
for employee in self.employees:
print('name:%s age:%d sex:%s staff_id:%s salary:%d position:%s department:%s' %
(employee.name, employee.age, employee.sex,
employee.staff_id, employee.salary,
employee.position, employee.department))
# @staticmethod
# def json_read() 永久存储方法入口(未写)
# @staticmethod
# def json_write() 永久存储方法入口 (未写)
if __name__ == '__main__':
# 测试代码
management = Management()
while True:
management.add_employee()
order = input('contiue? y/n >>>>')
if order == 'n':
break
management.all_employee_info()
management.del_employee()
management.info_employee()
management.all_employee_info()
management.max_salary_employee()
management.avg_department_salary()
management.avg_age_allemployee()
测试代码可以自己设计