某公司的员工分为5类,每类员工都有相应的封装类。(黑马第三版Java基础入门 第四章编程题)

(1) Employee:这是所有员工总的父类。

① 属性:员工的姓名,员工的生日月份

② 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励 100 元。

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

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

① 属性:每小时的工资、每月工作的小时数。

(4) SalesEmployee:Employee 的子类,销售,工资由月销售额和提成率决定。

① 属性:月销售额、提成率。

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

① 属性:底薪。

本题要求根据上述雇员分类,编写一个程序,实现以下功能:

(1)创建一个 Employee 数组,分别创建若干不同的 Employee 对象,并打印某个月的工资。

(2)每个类都完全封装,不允许非私有化属性

代码如下:

父类:

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

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

    public String getName() {
        return name;
    }

    public int getMonth() {
        return month;
    }

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

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

    public double getSalary(int month) {
        double salary = 0;
        if (this.month == month) {
            salary += 100;
        }
        return salary;
    }
}

固定工资的员工:

class SalariedEmployee extends Employee  {
    private double monthsalary;//月薪
    public SalariedEmployee (String name,int month,double monthsalary){
        super(name,month);
        this.monthsalary =monthsalary ;
    }

    public double getMonthsalary() {
        return monthsalary;
    }

    public void setMonthsalary(double monthsalary) {
        this.monthsalary = monthsalary;
    }

    public double getSalary(int month){
       return monthsalary+super.getSalary(month);
    }
}

按小时拿工资的员工:

public class HourlyEmployee extends Employee {
    private double hourlysalary;//每小时工资
    private double monthhourly;//每月小时数
    public HourlyEmployee (String name,int month,double monthhourly ,double hourlysalary ){
        super(name,month);
        this.hourlysalary =hourlysalary ;
        this.monthhourly =monthhourly ;
    }

    public double getHourlysalary() {
        return hourlysalary;
    }

    public void setHourlySalary(double hourlysalary) {
        this.hourlysalary = hourlysalary;
    }

    public double getMonthhourly() {
        return monthhourly;
    }

    public void setMonthhourly(double monthhourly) {
        this.monthhourly = monthhourly;
    }
    public double getSalary(int month) {
        double salary = 0;
        if (monthhourly  > 160) {
            salary = (160 + (monthhourly  - 160) * 3/2) * hourlysalary ;
        } else {
            salary = month  * hourlysalary ;
        }
        return salary + super.getSalary(month);
    }
}

销售人员:

public class SalesEmployee extends Employee {
    private double salasmonth;//月销售额
    private double commmissionrate;//提成率
    public SalesEmployee (String name,int month,double salasmonth ,double commmissionrate ){
        super(name,month);
        this.commmissionrate =commmissionrate ;
        this.salasmonth =salasmonth ;
    }

    public double getSalasmonth() {
        return salasmonth;
    }

    public double getCommmissionrate() {
        return commmissionrate;
    }

    public void setSalasmonth(double salasmonth) {
        this.salasmonth = salasmonth;
    }

    public void setCommmissionrate(double commmissionrate) {
        this.commmissionrate = commmissionrate;
    }

    @Override
    public double getSalary(int month) {
        return commmissionrate *salasmonth +super.getSalary(month);
    }
}

固定底薪的销售员(销售人员的子类):

public class BasePlusSalesEmployee extends SalesEmployee {
    private double basesalary;//底薪
    public BasePlusSalesEmployee (String name,int month,double basesalary,double salasmonth ,double commmissionrate ){
        super(name,month,salasmonth ,commmissionrate );
        this.basesalary =basesalary ;
    }

    public double getBasesalary() {
        return basesalary;
    }

    public void setBasesalary(double basesalary) {
        this.basesalary = basesalary;
    }

    @Override
    public double getSalary(int month) {
        return basesalary +super .getSalary(month);
    }
}

测试类:

public class Texe {
    public static void main(String[] args) {
        Employee stu[]={
                new SalariedEmployee("李四",7,5000),
                new HourlyEmployee("张三",8,20,260),
                new SalesEmployee("王二",7,8000,0.2),
                new BasePlusSalesEmployee("赵六",9,2000,6000,0.1)
        };
        System .out .println("七月份工资:");
        for(int i=0;i< stu.length ;i++){
            System .out.println("姓名:"+stu[i].getName()+"   生日月份:"+stu[i].getMonth()+"     工资为:"+  stu[i].getSalary(7));

        }
    }

调试结果图:

 

  • 53
    点赞
  • 259
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值