2021-11-11小练习

本文介绍了一个Java程序,通过多态实现不同类型的员工(如固定月薪的SalariedEmployee和按小时计酬的HourlyEmployee),在计算工资时,如果员工生日当月,工资额外增加100元。通过Employee、SalariedEmployee和HourlyEmployee类的定义,以及PayrollSystem类的实例化和操作,展示了面向对象编程和继承的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习3 编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个 Employee对象的生日,则将该雇员的工资增加100元。 实验说明: (1)定义一个Employee类,该类包含: private成员变量name,number,birthday,其中birthday 为MyDate类的对象; abstract方法earnings(); toString()方法输出对象的name,number和birthday。 (2)MyDate类包含: private成员变量year,month,day ; toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处 理。该类包括:private成员变量monthlySalary; 实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输 出员工类型信息及员工的name,number,birthday。 练习3 (4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的 员工处理。该类包括: private成员变量wage和hour; 实现父类的抽象方法earnings(),该方法返回wage*hour值; toString()方法输出员工类型信息及员工的name,number,birthday。 (5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各 类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类 型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本 月是某个Employee对象的生日,还要输出增加工资信息。 提示: //定义People类型的数组People c1[]=new People[10]; //数组元素赋值 c1[0]=new People("John","0001",20); c1[1]=new People("Bob","0002",19); //若People有两个子类Student和Officer,则数组元素赋值时,可以使父类类型的数组元素指向子类。 c1[0]=new Student("John","0001",20,85.0); c1[1]=new Officer("Bob","0002",19,90.5)

abstract public class Employee {
    private String name;
    private int number;
    private MyDate birthday= new MyDate();
    public abstract int earnings();//抽象方法
    public String toString(){
        return "姓名:"+name+" 编号:"+number+" 生日:"+birthday.toDateString();
    }

    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
}
public class MyDate {
    private int year;
    private int month;
    private int day;
public MyDate(int year,int month,int day){
    this.year=year;
    this.month=month;
    this.day=day;
}
public MyDate(){

}

    public int getMonth() {
        return month;
    }

    public String toDateString() {
        if(month<0||month>12){
            System.out.println("输入错误");
        }
        if(month==1){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==2&&year%400==0){
            if(day>29||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==2&&year%400!=0){
            if(day>28||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==3){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==4){
            if(day>30||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==5){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==6){
            if(day>30||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==7){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==8){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==9){
            if(day>30||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==10){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==11){
            if(day>30||day<0){
                System.out.println("输入错误");
            }
        }
        if(month==12){
            if(day>31||day<0){
                System.out.println("输入错误");
            }
        }
        return year + "年" + month + "月" + day + "日";
    }
}
public class SalariedEmployee extends Employee {
    private int monthlySalary;

    public SalariedEmployee(String name,int number,MyDate m,int monthlySalary){
        setName(name);
        setNumber(number);
        setBirthday(m);
        this.monthlySalary=monthlySalary;
    }
    public int earnings(){
        return monthlySalary;
    }
}
public class HourlyEmployee extends Employee {
    private int hour;
    private int wage;
    public HourlyEmployee(String name,int number,MyDate m,int hour,int wage){
        setName(name);
        setNumber(number);
        setBirthday(m);
        this.hour=hour;
        this.wage=wage;
    }
    public int earnings(){
        return hour*wage;
    }
}
import java.util.Scanner;
public class PayrollSystem {

    Employee[] arr = new Employee[10];
    Scanner scan=new Scanner(System.in);
    int total=0;
    public void addArr(Employee e){//(懒得写)

        arr[total++]=e;
    }
    public void getArr() {//获取数据方法(暂定)
        int inputMonth=0;
        boolean flag=true;
        while(flag) {
            System.out.println("请输入当前月份: ");
             inputMonth = scan.nextInt();
            if (inputMonth > 12 || inputMonth < 0) {
                System.out.println("输入错误,请重新输入");
            }
        else{
                flag=false;
            }
        }
        for (int i = 0; i < total; i++){
                System.out.println(arr[i]);
            if(arr[i].getBirthday().getMonth()==inputMonth){
                     int wages  =  arr[i].earnings()+100;

                    System.out.print("本月工资是:"+wages+",今天是你生日,公司给你100元去买房吧年轻人!\n");
            }

        }
    }
}
public class test111 {
    public static void main(String[]args){
        PayrollSystem p1=new PayrollSystem();
        p1.addArr(new SalariedEmployee("zx",1,new MyDate(2001,12,29),2000));
        p1.addArr(new HourlyEmployee("rr",2,new MyDate(2001,3,7),240,17));
        p1.addArr(new HourlyEmployee("ww",3,new MyDate(2001,6,12),240,17));

        p1.getArr();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值