题目要求
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个
Employee对象的生日,则将该雇员的工资增加100元。
定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();
toString()方法输出对象的name,number和birthday。
Employee.java
public abstract class Employee {
private String name;
private int number;
private MyDate birthday;
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 Employee(String name, int number, MyDate birthday) {
super();
this.name = name;
this.number = number;
this.birthday = birthday;
}
public abstract double earnings();
@Override
public String toString() {
return " [name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() + "]";
}
}
MyDate类包含:
private成员变量year,month,day ;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
MyDate.java
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String toDateString() {
return year + "年" + month + "月" + day + "日";
}
}
定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处
理。该类包括:private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输
出员工类型信息及员工的name,number,birthday。
SalariedEmployee.java
public class SalariedEmployee extends Employee {
private int monthlySalary;//月工资
public SalariedEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
// TODO Auto-generated constructor stub
}
public SalariedEmployee(String name, int number, MyDate birthday, int monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
@Override
public String toString() {
return "SalariedEmployee [" + super.toString() + "]";
}
}
参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的
员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;
toString()方法输出员工类型信息及员工的name,number,birthday。
HourlyEmployee.java
public class HourlyEmployee extends Employee {
private int wage;//每小时的工资
private double hour;//月工作的小时
public HourlyEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
// TODO Auto-generated constructor stub
}
public HourlyEmployee(String name, int number, MyDate birthday, int wage, double hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
@Override
public double earnings() {
return wage * hour;
}
@Override
public String toString() {
return "HourlyEmployee [" + super.toString() + "]";
}
}
定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各
类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类
型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本
月是某个Employee对象的生日,还要输出增加工资信息。
PayrollSystem.java
public class PayrollSystem {
public static void main(String[] args) {
Employee[] c1=new Employee[2];
//方式一
// Scanner scan = new Scanner(System.in);
// System.out.println("请输入月份");
// int m = scan.nextInt();
//方式二
Calendar calendar = Calendar.getInstance();
int m = calendar.get(Calendar.MONTH);//获取当前月份
// System.out.println(m);//一月份:0
c1[0] = new SalariedEmployee("库克", 1002, new MyDate(1992,11,28),10000);
c1[1] = new HourlyEmployee("马斯克", 1006, new MyDate(1980,6,3),60,240);
for(int i=0;i<c1.length;i++) {
System.out.println(c1[i]);
double salary = c1[i].earnings();
if(c1[i].getBirthday().getMonth() == m+1)
System.out.println("生日快乐,奖励100元");
System.out.println("月工资为:" + salary);
}
}
}