python习题练习

python习题

  编写一个简单的工资管理程序
  系统可以管理以下四类人:工人(worker)、销售员(salesman)、经理(manager)、销售经理(salemanger)
  所有的员工都具有员工号,
  工资等属性,有设置姓名,获取姓名,获取员工号,计算工资等方法。
  1)工人:工人具有工作小时数和时薪的属性,工资计算法方法为工作小时数*时薪。
  2)销售员:具有销售额和提成比例的属性,工资计算方法为销售额*提成比例。
  3)经理:具有固定月薪的属性,工资计算方法为固定月薪。
  4)销售经理:工资计算方法为销售额*提成比例+固定月薪。
  请根据以上要求设计合理的类,完成以下功能:
  1)添加所有类型的人员
  2)计算月薪
  3)显示所有人工资情况

解法一

python 复制代码
class Person:
    def __init__(self,no,name,salary):
        self.no =no
        self.name=name
        self.salary =salary
    def __str__(self):
        msg = '工号:{}名字:{}薪水:{}'.format(self.no,self.name,self.salary)
        return msg
    def getSalary(self):
        return self.salary
class Worker(Person):
    def __init__(self,no,name,salary,hours,per_hour):
        super().__init__(no,name,salary)
        self.hours =hours
        self.per_hour=per_hour
    def getSalary(self):
        money = self.hours*self.per_hour
        self.salary+=money
        return self.salary
class Salman(Person):
    def __init__(self,no,name,salary,salemoney,percent):
        super().__init__(no,name,salary)
        self.salemoney =salemoney
        self.percent =percent
    def getSalary(self):
        money =self.salemoney*self.percent
        self.salary+=money
        return  self.salary
class Manager(Person):
    def __init__(self,no,name,fixed_monthly_salary):
        super().__init__(no,name,fixed_monthly_salary)
        
    def calculate_salary(self):
        return self.fixed_monthly_salary
class SaleManager(Person):
    def __init__(self, no, sales_amount, commission_rate, fixed_monthly_salary):
        super().__init__(no,sales_amount, commission_rate, fixed_monthly_salary)
        self.sales_amount = sales_amount
        self.commission_rate = commission_rate
        self.fixed_monthly_salary = fixed_monthly_salary

    def calculate_salary(self):
        return self.sales_amount * self.commission_rate + self.fixed_monthly_salary
    
class Manager(Person):
    def __init__(self, no, name, fixed_monthly_salary):
        super().__init__(no, name, fixed_monthly_salary)

    def calculate_salary(self):
        return self.fixed_monthly_salary

class Salemanager(Person):
    def __init__(self, no, name, fixed_monthly_salary, salemoney, percent):
        super().__init__(no, name, fixed_monthly_salary)
        self.salemoney = salemoney
        self.percent = percent

    def getSalary(self):
        money = self.salemoney * self.percent + self.fixed_monthly_salary
        self.salary += money
        return self.salary


# 添加所有类型的人员
workers = [
    Worker(1, '工人 1', 0, 160, 50),
    Worker(2, '工人 2', 0, 180, 60)
]

salesmans = [
    Salman(3, '销售员 1', 0, 100000, 0.1),
    Salman(4, '销售员 2', 0, 120000, 0.12)
]

managers = [
    Manager(5, '经理 1', 10000),
    Manager(6, '经理 2', 12000)
]

salemanagers = [
    Salemanager(7, '销售经理 1', 15000, 150000, 0.15),
    Salemanager(8, '销售经理 2', 18000, 180000, 0.18)
]

all_people = workers + salesmans + managers + salemanagers

# 计算月薪并显示所有人工资情况
for person in all_people:
    print(person)
    print('月薪:', person.getSalary())

# 创建子类
w =Worker('001','king',100,160,100)
s =w.getSalary()
print('月薪')

解法二

python 复制代码
class Employee:
    def __init__(self, employee_number):
        self.employee_number = employee_number
        self.name = ""
        self.salary = 0

    def set_name(self, name):
        self.name = name

    def get_name(self):
        return self.name

    def get_employee_number(self):
        return self.employee_number

    def calculate_salary(self):
        pass


class Worker(Employee):
    def __init__(self, employee_number, working_hours, hourly_rate):
        super().__init__(employee_number)
        self.working_hours = working_hours
        self.hourly_rate = hourly_rate

    def calculate_salary(self):
        return self.working_hours * self.hourly_rate


class Salesman(Employee):
    def __init__(self, employee_number, sales_amount, commission_rate):
        super().__init__(employee_number)
        self.sales_amount = sales_amount
        self.commission_rate = commission_rate

    def calculate_salary(self):
        return self.sales_amount * self.commission_rate


class Manager(Employee):
    def __init__(self, employee_number, fixed_monthly_salary):
        super().__init__(employee_number)
        self.fixed_monthly_salary = fixed_monthly_salary

    def calculate_salary(self):
        return self.fixed_monthly_salary


class SaleManager(Employee):
    def __init__(self, employee_number, sales_amount, commission_rate, fixed_monthly_salary):
        super().__init__(employee_number)
        self.sales_amount = sales_amount
        self.commission_rate = commission_rate
        self.fixed_monthly_salary = fixed_monthly_salary

    def calculate_salary(self):
        return self.sales_amount * self.commission_rate + self.fixed_monthly_salary


# 工资管理系统类
class SalaryManagementSystem:
    def __init__(self):
        self.employees = []

    def add_employee(self, employee_type, employee_number, *args):
        if employee_type == "worker":
            working_hours, hourly_rate = args
            self.employees.append(Worker(employee_number, working_hours, hourly_rate))
        elif employee_type == "salesman":
            sales_amount, commission_rate = args
            self.employees.append(Salesman(employee_number, sales_amount, commission_rate))
        elif employee_type == "manager":
            fixed_monthly_salary = args[0]
            self.employees.append(Manager(employee_number, fixed_monthly_salary))
        elif employee_type == "salemanger":
            sales_amount, commission_rate, fixed_monthly_salary = args
            self.employees.append(SaleManager(employee_number, sales_amount, commission_rate, fixed_monthly_salary))

    def calculate_monthly_salary(self):
        for employee in self.employees:
            salary = employee.calculate_salary()
            print(f"{employee.get_name()} 的月薪为: {salary}")


# 测试代码
salary_system = SalaryManagementSystem()

salary_system.add_employee("worker", 1, 160, 50)
salary_system.add_employee("salesman", 2, 10000, 0.1)
salary_system.add_employee("manager", 3, 10000)
salary_system.add_employee("salemanger", 4, 20000, 0.2, 8000)

salary_system.calculate_monthly_salary()
相关推荐
Ws_1 小时前
leetcode LCR 068 搜索插入位置
数据结构·python·算法·leetcode
谈谈叭2 小时前
Javascript中的深浅拷贝以及实现方法
开发语言·javascript·ecmascript
lx学习2 小时前
Python学习26天
开发语言·python·学习
qq_273900232 小时前
pytorch register_buffer介绍
人工智能·pytorch·python
爱编程的鱼3 小时前
javascript用来干嘛的?赋予网站灵魂的语言
开发语言·javascript·ecmascript
q567315233 小时前
用 PHP或Python加密字符串,用iOS解密
java·python·ios·缓存·php·命令模式
捕鲸叉4 小时前
C++设计模式和编程框架两种设计元素的比较与相互关系
开发语言·c++·设计模式
未知陨落5 小时前
数据结构——二叉搜索树
开发语言·数据结构·c++·二叉搜索树
大波V55 小时前
设计模式-参考的雷丰阳老师直播课
java·开发语言·设计模式