公司的雇员

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

package com.hp.demo12;

abstract class Employee{
    private String name;
    private int month;

    //constructor
    public Employee() {
    }

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

    //setter and getter
    public String getName() {
        return name;
    }

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

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public abstract double getSalary(int month);
}

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

package com.hp.demo12;


class SalariedEmployee extends Employee{
    //月薪
    private int monthlySalary;

    @Override
    public double getSalary(int month) {
        if (this.getMonth() == month){
            return this.getMonthlySalary() + 100;
        }else{
            return this.getMonthlySalary();
        }
    }

    //constructor
    public SalariedEmployee() {
    }

    public SalariedEmployee(String name, int month, int monthlySalary) {
        super(name, month);
        this.monthlySalary = monthlySalary;
    }

    //setter and getter

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
}

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

package com.hp.demo12;

class HourlyEmployee extends Employee{
    //小时工资
    int hourlyPayment;
    //每月工作小时数
    int workHour;

    @Override
    public double getSalary(int month) {
        if (this.getWorkHour() > 160){
            if (this.getMonth() == month){
                return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5 + 100;
            }
            return getHourlyPayment()*160 + getHourlyPayment()*(getWorkHour()-160)*1.5;
        }
        else{
            if (this.getMonth() == month){
                return getWorkHour()*getHourlyPayment() + 100;
            }
            return getHourlyPayment()*getWorkHour();
        }
    }

    //constructor

    public HourlyEmployee() {
    }

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

    //setter and getter

    public int getHourlyPayment() {
        return hourlyPayment;
    }

    public void setHourlyPayment(int hourlyPayment) {
        this.hourlyPayment = hourlyPayment;
    }

    public int getWorkHour() {
        return workHour;
    }

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

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

package com.hp.demo12;

class SalesEmployee extends Employee{
    private double monthlySale;
    //提成率应在0~1之间
    private double commissionRate;

    //constructor

    public SalesEmployee() {
    }

    public SalesEmployee(String name, int month, double monthlySale, double commissionRate) {
        super(name, month);
        this.monthlySale = monthlySale;
        this.commissionRate = commissionRate;
    }

    //setter and getter


    public double getMonthlySale() {
        return monthlySale;
    }

    public void setMonthlySale(double monthlySale) {
        this.monthlySale = monthlySale;
    }

    public double getCommissionRate() {
        return commissionRate;
    }

    public void setCommissionRate(double commissionRate) {
        this.commissionRate = commissionRate;
    }

    @Override
    public double getSalary(int month) {
        if (this.getMonth() == month){
            return this.getMonthlySale()*this.getCommissionRate() + 100;
        }else{
            return this.getMonthlySale()*this.getCommissionRate();
        }
    }
}

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

package com.hp.demo12;

class BasePlusSalesEmployee extends SalesEmployee{
    private double baseSalary;

    //constructor

    public BasePlusSalesEmployee() {
    }

    public BasePlusSalesEmployee(String name, int month, double monthlySale, double commissionRate, double baseSalary) {
        super(name, month, monthlySale, commissionRate);
        this.baseSalary = baseSalary;
    }

    //setter and getter

    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    //重写父类获得薪水的方法

    @Override
    public double getSalary(int month) {
        if (this.getMonth() == month){
            return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate() + 100;
        }else{
            return this.getBaseSalary() + this.getMonthlySale()*this.getCommissionRate();
        }
    }
}

测试类

package com.hp.demo12;

public class Homework1 {
    public static void main(String[] args) {
        //创建对象
        Employee e1 = new SalariedEmployee("张三", 1, 2500);
        Employee e2 = new HourlyEmployee("李四", 2, 100, 200);
        Employee e3 = new SalesEmployee("赵六", 3, 1000000, 0.01);
        Employee e4 = new BasePlusSalesEmployee("钱七",4,100000, 0.02, 500);

        //获得薪水
        System.out.println(e1.getName() + "的工资是:" + e1.getSalary(4));
        System.out.println(e2.getName() + "的工资是:" + e2.getSalary(4));
        System.out.println(e3.getName() + "的工资是:" + e3.getSalary(4));
        System.out.println(e4.getName() + "的工资是:" + e4.getSalary(4));

    }
}

运行结果

张三的工资是:2500.0
李四的工资是:22000.0
赵六的工资是:10000.0
钱七的工资是:2600.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值