python习题
编写一个简单的工资管理程序
系统可以管理以下四类人:工人(worker)、销售员(salesman)、经理(manager)、销售经理(salemanger)
所有的员工都具有员工号,
工资等属性,有设置姓名,获取姓名,获取员工号,计算工资等方法。
1)工人:工人具有工作小时数和时薪的属性,工资计算法方法为工作小时数*时薪。
2)销售员:具有销售额和提成比例的属性,工资计算方法为销售额*提成比例。
3)经理:具有固定月薪的属性,工资计算方法为固定月薪。
4)销售经理:工资计算方法为销售额*提成比例+固定月薪。
请根据以上要求设计合理的类,完成以下功能:
1)添加所有类型的人员
2)计算月薪
3)显示所有人工资情况
解法一
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('月薪')
解法二
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()
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » python习题练习
发表评论 取消回复