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()
相关推荐
木向几秒前
leetcode22:括号问题
开发语言·c++·leetcode
comli_cn2 分钟前
使用清华源安装python包
开发语言·python
筑基.8 分钟前
basic_ios及其衍生库(附 GCC libstdc++源代码)
开发语言·c++
赵谨言12 分钟前
基于python 微信小程序的医院就诊小程序
经验分享·python·毕业设计
雨颜纸伞(hzs)22 分钟前
C语言介绍
c语言·开发语言·软件工程
J总裁的小芒果24 分钟前
THREE.js 入门(六) 纹理、uv坐标
开发语言·javascript·uv
1.01^100026 分钟前
[1111].集成开发工具Pycharm安装与使用
python·pycharm
HEX9CF27 分钟前
【Django】测试带有 CSRF 验证的 POST 表单 API 报错:Forbidden (CSRF cookie not set.)
python·django·csrf
坊钰1 小时前
【Java 数据结构】移除链表元素
java·开发语言·数据结构·学习·链表
chenziang11 小时前
leetcode hot100 LRU缓存
java·开发语言