文章目录
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日,则将该雇员的工资增加100元。
做这个题的时候思考了好久还是不会做,于是我又重新看了一遍知识点,再来做的时候思路清晰了点但是还是有没有做出来,最后通过参考别人的做法以及改进自己的代码,写出来了如下:
题目
编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个
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)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wagehour值;
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);
*/
Employee类的编写
package com.wedu.test4;
public abstract class Employee {
private String name;
private int number;
private MyDate birthday;
public Employee() {
}
public Employee(String name, int number, MyDate birthday) {
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;
}
abstract double earnings();
public String toString() {
return "name="+name+"\tnumber="+number+"\tbirthday="+birthday.toDateString();
}
}
MyDate类的编写
package com.wedu.test4;
public class MyDate {
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
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+"日";
}
}
SalaryEmployee类的编写
package com.wedu.test4;
public class SalaryEmployee extends Employee{
private double monthlySalary;
public SalaryEmployee() {
}
public SalaryEmployee(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;
}
public SalaryEmployee(double monthlySalary, String name, int number, MyDate birthday) {
super(name,number,birthday);
this.monthlySalary=monthlySalary;
}
@Override
double earnings() {
return monthlySalary;
}
public String toString() {
return "SalariedEmployee:\t"+super.toString();
}
}
HourEmployee类的编写
package com.wedu.test4;
public class HourEmployee extends Employee{
private double wage;
private double hour;
public HourEmployee() {
}
public HourEmployee(String name, int number, MyDate birthday, double wage, double hour) {
super(name, number, birthday);
this.wage = wage;
this.hour = hour;
}
@Override
double earnings() {
return wage*hour*30;
}//默认每个月都是30天
@Override
public String toString() {
return "HourEmployee:\t"+super.toString();
}
}
PayrollSystem类的编写
package com.wedu.test4;
import java.util.Scanner;
public class PayrollSystem {
public static void main(String[] args) {
MyDate myDate1 = new MyDate(2000, 12, 2);
MyDate myDate2 = new MyDate(2001, 3, 9);
MyDate myDate3 = new MyDate(2002, 5, 8);
MyDate myDate4 = new MyDate(2003, 9, 29);
Employee[] employee = new Employee[4];
Employee p1 = new SalaryEmployee("李强", 1, myDate1, 100000);
Employee p2 = new SalaryEmployee("江山", 2, myDate2, 100000);
Employee h1 = new HourEmployee("张力", 4, myDate3, 24, 12);
Employee h2 = new HourEmployee("蒋悦", 5, myDate4, 26, 10);
employee[0] = p1;
employee[1] = p2;
employee[2] = h1;
employee[3] = h2;
Scanner sc = new Scanner(System.in);
System.out.println("请输入计算工资的月份(1-12):");
int month = sc.nextInt();
PayrollSystem payrollSystem = new PayrollSystem();
for (int i = 0; i < employee.length; i++) {
System.out.println(employee[i]);
double salary = employee[i].earnings();
System.out.println("月工资为:" + salary);
if (month == employee[i].getBirthday().getMonth()) {
double finalSalary = employee[i].earnings() + 100;
System.out.println("本月是你的的生日,奖励你红包100元,最后你的工资是" + finalSalary);
}
}
}
}
运行结果
D:\develop\jdk-12.0.2\bin\java.exe "-javaagent:D:\develop\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar=62764:D:\develop\IntelliJ IDEA 2021.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\develop\ideaprojects\javase-project\out\production\abstractAndInterfaceTest com.wedu.test4.PayrollSystem
请输入计算工资的月份(1-12):
9
SalariedEmployee: name=李强 number=1 birthday=2000年12月2日
月工资为:100000.0
SalariedEmployee: name=江山 number=2 birthday=2001年3月9日
月工资为:100000.0
HourEmployee: name=张力 number=4 birthday=2002年5月8日
月工资为:8640.0
HourEmployee: name=蒋悦 number=5 birthday=2003年9月29日
月工资为:7800.0
本月是你的的生日,奖励你红包100元,最后你的工资是7900.0
Process finished with exit code 0