当前位置: 首页 > article >正文

python习题练习

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()

http://www.kler.cn/a/393602.html

相关文章:

  • 三维测量与建模笔记 - 特征提取与匹配 - 4.2 梯度算子、Canny边缘检测、霍夫变换直线检测
  • 学法减分交管12123模拟练习小程序源码前端和后端和搭建教程
  • Could not initialize class sun.awt.X11FontManager
  • TortoiseSVN提示服务器凭证检核错误:站点名称不符
  • Spring框架之观察者模式 (Observer Pattern)
  • VMware虚拟机安装Win7专业版保姆级教程(附镜像包)
  • WebGIS四大地图框架:Leaflet、OpenLayers、Mapbox、Cesium
  • 【插件】多断言 插件pytest-assume
  • 2024年8个最佳在线websocket调试工具选择
  • 30.超市管理系统(基于springboot和Vue的Java项目)
  • Android 13.0 framework系统修改安兔兔等显示的屏幕尺寸大小功能实现
  • 集群架构中Lua脚本的限制以及出现的报错
  • N种方式解决
  • 计算机图形学论文 | 木工设计与制造计划的共同优化
  • 计算机毕业设计——ssm基于JAVA的求职招聘网站的设计与实现演示录像 2021
  • LeetCode【0009】回文数
  • 微信小程序=》基础=》常见问题=》性能总结
  • 期货配资系统行情源对接通讯协议范本
  • 如何选择适合小团队的项目管理工具?免费与开源软件推荐
  • cache中命中率和平均访问时间
  • odoo 17 后端路由接口认证自定义
  • 前端常用布局模板39套,纯CSS实现布局
  • Python 虚拟环境创建
  • Linux解决 -bash: nc: command not found-bash: nc: 未找到命令
  • hive的tblproperties支持修改的属性
  • QT自定义控件封装