编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个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。
(4) 参照 SalariedEmbloyee 类定义 HourlyEmployee 类,实现按小时计算工资的员工处理。该类包括:
private成员变量 wage 和 hour;
实现父类的抽象方法 earnings(),该方法返回 wage*hour 值;
toString()方法输出员工类型信息及员工的 name,number,birthday。
(5) 定义 PayrollSystem 类,创建 Employee 变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
MyDate.java
package com.klvchen.exer2;
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 void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public String toDateString() {
return year + "年" + month + "月" + day + "日";
}
}
Employee.java
package com.klvchen.exer2;
public abstract class Employee {
private String name;
private int number;
private MyDate birthday;
public Employee(String name, int number, MyDate birthday) {
super();
this.name = name;
this.number = number;
this.birthday = 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 abstract double earnings();
@Override
public String toString() {
return "name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() + "]";
}
}
SalariedEmployee.java
package com.klvchen.exer2;
public class SalariedEmployee extends Employee {
private double monthlySalary; //月工资
public SalariedEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {
super(name, number, birthday);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public double earnings() {
return monthlySalary;
}
public String toString() {
return "SalariedEmployee[" + super.toString() + "]";
}
}
HourlyEmployee.java
package com.klvchen.exer2;
public class HourlyEmployee extends Employee {
private int wage; //每小时的工资
private int hour; //月工资的小时数
public HourlyEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public HourlyEmployee(String name, int number, MyDate birthday, int wage, int 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 int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
@Override
public double earnings() {
return wage * hour;
}
public String toString() {
return "HourlyEmployee [" + super.toString() + "]";
}
}
PayrollSystem.java
package com.klvchen.exer2;
import java.util.Calendar;
import java.util.Scanner;
public class PayrollSystem {
public static void main(String[] args) {
//方式一:
// Scanner scanner = new Scanner(System.in);
// System.out.println("请输入当月的月份:");
// int month = scanner.nextInt();
//方式二
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH); //获取当前的月份
Employee[] emps = new Employee[2];
emps[0] = new SalariedEmployee("马森", 1002, new MyDate(1992, 2, 28), 10000);
emps[1] = new HourlyEmployee("潘先生", 2001, new MyDate(1991, 3, 15), 60, 240);
for(int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
double salary = emps[i].earnings();
System.out.println("月工资为: " + salary);
if(month+1 == emps[i].getBirthday().getMonth()) {
System.out.println("生日快乐! 奖励100元");
}
System.out.println("---------------------------");
}
}
}