python习题
编写一个简单的工资管理程序系统可以管理以下四类人:工人(worker)、销售员(salesman)、经理(manager)、销售经理(salemanger)所有的员工都具有员工号,工资等属性,有设置姓名,获取姓名,获取员工号,计算工资等方法。1)工人:工人具有工作小时数和时薪的属性,工资计算法方法为工作小时数*时薪。2)销售员:具有销售额和提成比例的属性,工资计算方法为销售额*提成比例。3)经理:具有固定月薪的属性,工资计算方法为固定月薪。4)销售经理:工资计算方法为销售额*提成比例+固定月薪。请根据以上要求设计合理的类,完成以下功能:1)添加所有类型的人员2)计算月薪3)显示所有人工资情况
解法一
class Person:def __init__(self,no,name,salary):self.no =noself.name=nameself.salary =salarydef __str__(self):msg = '工号:{}名字:{}薪水:{}'.format(self.no,self.name,self.salary)return msgdef getSalary(self):return self.salary
class Worker(Person):def __init__(self,no,name,salary,hours,per_hour):super().__init__(no,name,salary)self.hours =hoursself.per_hour=per_hourdef getSalary(self):money = self.hours*self.per_hourself.salary+=moneyreturn self.salary
class Salman(Person):def __init__(self,no,name,salary,salemoney,percent):super().__init__(no,name,salary)self.salemoney =salemoneyself.percent =percentdef getSalary(self):money =self.salemoney*self.percentself.salary+=moneyreturn 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_amountself.commission_rate = commission_rateself.fixed_monthly_salary = fixed_monthly_salarydef calculate_salary(self):return self.sales_amount * self.commission_rate + self.fixed_monthly_salaryclass 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_salaryclass Salemanager(Person):def __init__(self, no, name, fixed_monthly_salary, salemoney, percent):super().__init__(no, name, fixed_monthly_salary)self.salemoney = salemoneyself.percent = percentdef getSalary(self):money = self.salemoney * self.percent + self.fixed_monthly_salaryself.salary += moneyreturn 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('月薪')
解法二
class Employee:def __init__(self, employee_number):self.employee_number = employee_numberself.name = ""self.salary = 0def set_name(self, name):self.name = namedef get_name(self):return self.namedef get_employee_number(self):return self.employee_numberdef calculate_salary(self):passclass Worker(Employee):def __init__(self, employee_number, working_hours, hourly_rate):super().__init__(employee_number)self.working_hours = working_hoursself.hourly_rate = hourly_ratedef calculate_salary(self):return self.working_hours * self.hourly_rateclass Salesman(Employee):def __init__(self, employee_number, sales_amount, commission_rate):super().__init__(employee_number)self.sales_amount = sales_amountself.commission_rate = commission_ratedef calculate_salary(self):return self.sales_amount * self.commission_rateclass Manager(Employee):def __init__(self, employee_number, fixed_monthly_salary):super().__init__(employee_number)self.fixed_monthly_salary = fixed_monthly_salarydef calculate_salary(self):return self.fixed_monthly_salaryclass SaleManager(Employee):def __init__(self, employee_number, sales_amount, commission_rate, fixed_monthly_salary):super().__init__(employee_number)self.sales_amount = sales_amountself.commission_rate = commission_rateself.fixed_monthly_salary = fixed_monthly_salarydef 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 = argsself.employees.append(Worker(employee_number, working_hours, hourly_rate))elif employee_type == "salesman":sales_amount, commission_rate = argsself.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 = argsself.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()