import java.util.Calendar;
public class Employee {
private String name;
private int month;
public int getSalary(int month){
Calendar cal = Calendar.getInstance();
int m = cal.get(Calendar.MONTH) + 1;
return m==month?100:0;
}
}
public class SalariedEmployee extends Employee{
private int monthlyPay;
public int getMonthlyPay() {
return monthlyPay;
}
public void setMonthlyPay(int monthlyPay) {
this.monthlyPay = monthlyPay;
}
@Override
public int getSalary(int month) {
return super.getSalary(month)+monthlyPay;
}
}
public class HourlyEmployee extends Employee {
private int peerHourPay;
private int peerSumHour;
public int getPeerHourPay() {
return peerHourPay;
}
public void setPeerHourPay(int peerHourPay) {
this.peerHourPay = peerHourPay;
}
public int getPeerSumHour() {
return peerSumHour;
}
public void setPeerSumHour(int peerSumHour) {
this.peerSumHour = peerSumHour;
}
@Override
public int getSalary(int month) {
return super.getSalary(month)+peerSumHour*peerHourPay+(int)(peerSumHour>160?(peerSumHour-160)*peerHourPay*0.5:0);
}
}
package com.example.demo;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
public class Employee {
private String name;
private int month;
public BigDecimal getSalary(int month){
Calendar cal = Calendar.getInstance();
int m = cal.get(Calendar.MONTH) + 1;
return m == month ? new BigDecimal(100) : BigDecimal.ZERO;
}
public static class SalariedEmployee extends Employee {
private BigDecimal salary = new BigDecimal(5000);
@Override
public BigDecimal getSalary(int month) {
return this.salary.add(super.getSalary(month));
}
}
public static class HourlyEmployee extends Employee {
// 每小时工资
private BigDecimal salaryOfHour = new BigDecimal(5);
// 工作时间
private BigDecimal workHours = new BigDecimal(180);
@Override
public BigDecimal getSalary(int month) {
if (workHours.compareTo(new BigDecimal(160)) > 0){
return workHours.subtract(new BigDecimal(160)).multiply(new BigDecimal(1.5).multiply(salaryOfHour))
.add(new BigDecimal(160).multiply(salaryOfHour)).add(super.getSalary(month));
} else {
return salaryOfHour.multiply(workHours).add(super.getSalary(month));
}
}
}
public static void main(String[] args) {
Employee[] employees = {new HourlyEmployee(),new SalariedEmployee()};
Arrays.stream(employees).forEach(employee -> System.out.println(employee.getSalary(11)));
}
}
最后两个你自己写写吧