根据要求创建 SalariedEmployee 、 HourlyEmployees 、SaleEmployee 和 BasePlusSalesEmployee四个类的对象各一个, 并计算某个月这四个

某公司的雇员分为以下若干类:
    Employee:这是所有员工总的父类,
        属性:
            员工的姓名,员工的生日月份。
        方法:getSalary(intmonth) 
            根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

    SalariedEmployee:
        Employee 的子类,拿固定工资的员工。
        属性:月薪

    HourlyEmployee: 
        Employee 的子类, 按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
        属性:每小时的工资、每月工作的小时数

    SalesEmployee:
        Employee 的子类,销售人员,工资由月销售额和提成率决定。 
        属性:月销售额、提成率

    BasePlusSalesEmployee:
        SalesEmployee 的子类,有固定底薪的销售人员,工资 由底薪加上销售提成部分。
        属性:底薪。

    根据要求创建 SalariedEmployee 、 HourlyEmployees 、SaleEmployee 和 BasePlusSalesEmployee四个类的对象各一个,
    并计算某个月这四个对象的工资。

    注意:要求把每个类都做成完全封装,不允许非私有化属性。

public class Employee extends Test{    //Employee类
    private String name;
    private int BirthMonth;
    private double salary;

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee() {
    }

    public Employee(String name, int birthMonth) {
        this.name = name;
        BirthMonth = birthMonth;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBirthMonth() {
        return BirthMonth;
    }

    public void setBirthMonth(int birthMonth) {
        BirthMonth = birthMonth;
    }
    public void getSalary(int month){
        if(getBirthMonth() == month){
            System.out.println("生日快乐,工资加100");
            setSalary(getSalary() + 100);
        }
    }
}
public class SalariedEmployee extends Employee{    //SalariedEmployee 类 
    private double salary;

    public SalariedEmployee() {
    }

    public SalariedEmployee(String name, int birthMonth, double salary) {
        super(name, birthMonth);
        this.salary = salary;
    }

    @Override
    public double getSalary() {
        return salary;
    }

    @Override
    public void setSalary(double salary) {
        this.salary = salary;
    }
}
public class HourlyEmployee extends Employee{    //HourlyEmployee 类
    private int workHour;
    private int hourSalary;

    public HourlyEmployee() {
    }

    public HourlyEmployee(String name, int birthMonth, int workHour, int hourSalary) {
        super(name, birthMonth);
        this.workHour = workHour;
        this.hourSalary = hourSalary;
    }

    public HourlyEmployee(int workHour, int hourSalary) {
        this.workHour = workHour;
        this.hourSalary = hourSalary;
    }

    public HourlyEmployee(String name, int birthMonth) {
        super(name, birthMonth);
    }

    public int getWorkHour() {
        return workHour;
    }

    public void setWorkHour(int workHour) {
        this.workHour = workHour;
    }

    public int getHourMoney() {
        return hourSalary;
    }

    public void setHourMoney(int hourMoney) {
        hourSalary = hourMoney;
    }

    public void setMoney(){
        if(getWorkHour() > 160){
        double extra = (getWorkHour() - 160) * 1.5 * getHourMoney();
        double salary = 160 * getHourMoney();
        setSalary(salary + extra);
        }else{
            double salary = getWorkHour()  * getHourMoney();
            setSalary(salary);
            System.out.println("工资:" + getSalary());
        }
    }
}
public class SalesEmployee extends Employee{    //SalesEmployee 类
    private double monthlySales;
    private double commission;

    public SalesEmployee() {
    }

    public SalesEmployee(String name, int birthMonth, double monthlySales, double commission) {
        super(name, birthMonth);
        this.monthlySales = monthlySales;
        this.commission = commission;
    }

    public SalesEmployee(double monthlySales, double commission) {
        this.monthlySales = monthlySales;
        this.commission = commission;
    }

    public double getMonthlySales() {
        return monthlySales;
    }

    public void setMonthlySales(double monthlySales) {
        this.monthlySales = monthlySales;
    }

    public double getCommission() {
        return commission;
    }

    public void setCommission(double commission) {
        this.commission = commission;
    }

    public void setMoney(){
        setSalary(getMonthlySales() * getCommission());
    }
}
public class BasePlusSalesEmployee extends SalesEmployee{    //BasePlusSalesEmployee 类
    private double Base;

    public BasePlusSalesEmployee() {
    }

    public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commission, double base) {
        super(name, birthMonth, monthlySales, commission);
        Base = base;
    }

    public BasePlusSalesEmployee(double base) {
        Base = base;
    }

    public double getBase() {
        return Base;
    }

    public void setBase(double base) {
        Base = base;
    }

    public void setMoney(){
        setSalary((getMonthlySales() * getCommission()) + Base);
    }
}
public class Test {    //Test 类
    public static void main(String[] args) {
        int month = 10;
        Employee a = new HourlyEmployee("test1",10,160,15);

        HourlyEmployee b = (HourlyEmployee) a;
        b.setMoney();
        b.getSalary(month);
        System.out.println("姓名:" + b.getName() + "生日" + b.getBirthMonth() + "工作时长" + b.getWorkHour() + "时薪" + b.getHourMoney() + "此月工资" + b.getSalary());

        Employee c = new SalariedEmployee("test2",10,4000);
        SalariedEmployee d = (SalariedEmployee) c;
        d.getSalary(month);
        System.out.println("姓名:" + d.getName() + "生日" + d.getBirthMonth() + "此月工资" + d.getSalary() );

        Employee e = new SalesEmployee("test3",8,100000,0.005);
        SalesEmployee f = (SalesEmployee) e;
        f.getSalary(month);
        f.setMoney();
        System.out.println("姓名:" + f.getName() + "生日" + f.getBirthMonth() + "销售额" + f.getMonthlySales() + "提成率" + f.getCommission() * 100 + "%" + "此月工资" + f.getSalary() );

        Employee g = new BasePlusSalesEmployee("test4",10,100000,0.005,4000);
        BasePlusSalesEmployee h = (BasePlusSalesEmployee) g;
        h.getSalary(month);
        h.setMoney();
        System.out.println("姓名:" + h.getName() + "生日" + +h.getBirthMonth() + "固定工资" + h.getBase()+ "销售额" + h.getMonthlySales() + "提成率" + h.getCommission() * 100 + "%" + "此月工资" + h.getSalary() );
    }
}

2021年11月6日01:39:16

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在此回答中,我将使用 Python 语言来给出示例代码。 首先,我们需要定义一个名为 `Employee` 的父,该具有两个属性:姓名和生日份,以及一个方法 `getSalary()`。 ``` class Employee: def __init__(self, name, birthday_month): self.__name = name self.__birthday_month = birthday_month def getSalary(self, month): salary = 0 if month == self.__birthday_month: salary += 100 return salary ``` 然后,我们可以定义四个来继承 `Employee` :`SalariedEmployee`、`HourlyEmployee`、`SalesEmployee` 和 `BasePlusSalesEmployee`。 ``` class SalariedEmployee(Employee): def __init__(self, name, birthday_month, salary): super().__init__(name, birthday_month) self.__salary = salary def getSalary(self, month): return super().getSalary(month) + self.__salary class HourlyEmployee(Employee): def __init__(self, name, birthday_month, hourly_wage, hours_worked): super().__init__(name, birthday_month) self.__hourly_wage = hourly_wage self.__hours_worked = hours_worked def getSalary(self, month): salary = super().getSalary(month) overtime_hours = max(0, self.__hours_worked - 160) salary += overtime_hours * self.__hourly_wage * 1.5 salary += (self.__hours_worked - overtime_hours) * self.__hourly_wage return salary class SalesEmployee(Employee): def __init__(self, name, birthday_month, sales, commission_rate): super().__init__(name, birthday_month) self.__sales = sales self.__commission_rate = commission_rate def getSalary(self, month): return super().getSalary(month) + self.__sales * self.__commission_rate class BasePlusSalesEmployee(SalesEmployee): def __init__(self, name, birthday_month, sales, commission_rate, base_salary): super().__init__(name, birthday_month, sales, commission_rate) self.__base_salary = base_salary def getSalary(self, month): return super().getSalary(month) + self.__base_salary
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值