美文网首页
day13-作业

day13-作业

作者: 七一欧 | 来源:发表于2018-08-01 20:57 被阅读0次

    面向对象人力资源管理系统:

    • 能存多个员工信息
      (每个员工的信息有:姓名、年龄、工号、薪资、职位、部门)
    • 新员工入职(添加员工)
    • 员工离职(删除员工)
    • 查看某个员工的信息
    • 查询薪资最高的员工
    • 查询指定部门中所有员工的平均薪资
    • 求整个公司的员工的平均年龄
    import math
    import random
    class Clerk:
        def __init__(self,name='',age=0,pay=0,position='',branch=''):
            self.name = name
            self.age = age
            self.clerkid = str(random.randint(1,100)).rjust(5,'0')
            self.pay = pay
            self.position = position
            self.branch = branch
        def __str__(self):
            return
        # def create(self):
        #     clerk = {
        #         'name':self.name,
        #         'age':self.age,
        #         'clerkid':self.clerkid,
        #         'pay':self.pay,
        #         'position':self.position,
        #         'branch':self.branch
        #     }
            # print(clerk)
    class Company:
        def __init__(self,name):
            self.company_name = name
            self.clerks = []
        #  新员工入职
        def add_clerk(self,clerk=None):
            self.clerks.append(clerk)
    
    
    
        #  员工离职
        def del_clerk(self,name):
            for clerk in self.clerks[:]:
                if clerk.name == name:
                    self.clerks.remove(clerk)
    
        # 查看某个员工的信息
        def find_clerk(self,name):
            for clerk in self.clerks[:]:
                if clerk.name == name:
                    print(clerk.name,clerk.age,clerk.clerkid,clerk.pay,clerk.position,clerk.branch)
                    continue
        # 查询薪资最高的员工
        def find_maxpay(self):
            maxpay = []
            for clerk in self.clerks[:]:
                maxpay.append(clerk.pay)
            return print(max(maxpay))
    
        # 查询指定部门中所有员工的平均薪资
        def find_branch_ave(self,branch):
            branch_pay = []
            for clerk in self.clerks[:]:
                if clerk.branch == branch:
                    branch_pay.append(clerk.branch)
            return print(sum(branch_pay)/len(branch_pay))
    
        # 求整个公司的平均年龄
        def company_average_age(self):
            ages = []
            for clerk in self.clerks[:]:
                ages.append(clerk.age)
            return print(sum(ages)/len(ages))
    

    老师讲解代码

    class Staff:
        def __init__(self,name,age,salary,job,department):
            self.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))
    
    
    class HrManger:
        """人力资源管理系统"""
        # 整个公司的所有员工
        all_staff = []
    
        # 目前公司已经入职的人数
        numbers = 0
    
    
        __all_department = ['财务部','行政部门','研发部','总经办','后勤部']
    
    
        @classmethod
        def add_staff(cls):
            name = input('名字:')
            age = input('年龄:')
            salary = int(input('薪资:'))
            while True:
                print('公司部门:',*cls.__all_department)
                department = input('部门:')
                if department in cls.__all_department:
                    break
                else:
                    print('部门输入有误')
            job = input('职位:')
    
            # 生成工号
            cls.__number += 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,name):
            """删除员工"""
            name = input('请输入要删除的员工姓名:')
            flag = False
            for staff in cls.all_staff:
                if staff.name == 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,name):
            name = input('请输入要查找的员工姓名:')
            flag = False
            for staff in cls.all_staff:
                if staff.name == 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 average_age(cls):
            if len(cls.all_staff) == 0:
                print('公司还没有员工!')
                return
            return sum(cls.all_staff)/len(cls.all_staff)
    
    

    相关文章

      网友评论

          本文标题:day13-作业

          本文链接:https://www.haomeiwen.com/subject/wuurvftx.html