import java.time.Month;
//父类:Employee
class Employee{
private String name;
private Month birthmonth;
protected Employee(String name, Month birthmonth){
this.name = name;
this.birthmonth = birthmonth;
}
public String getName() {
return name;
}
public Month getMonth() {
return birthmonth;
}
public int calculateSalary(int month){
int bonus = birthmonth.getValue() == month ? 100:0;
return bonus;
}
}
//子类:拿固定工资的员工(SalariedEmployee)
class SalariedEmployee extends Employee{
private int monthlySalary;
public SalariedEmployee(String name, Month birthmonth, int monthlySalary) {
super(name, birthmonth);
this.monthlySalary = monthlySalary;
}
public int getMonthlySalary() {
return monthlySalary;
}
@Override
public int calculateSalary(int month) {
return super.calculateSalary(month)+monthlySalary;
}
}
//子类:按小时拿工资的员工(HourlyEmployee)
class HourlyEmployee extends Employee{
private double hourlyRate;
private int hoursWorked;
public HourlyEmployee(String name, Month birthmonth, double hourlyRate, int hoursWorked) {
super(name, birthmonth);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
public double getHourlyRate() {
return hourlyRate;
}
public int getHoursWorked() {
return hoursWorked;
}
@Override
public int calculateSalary(int month) {
int baseSalary = (int)(hourlyRate*Math.min(hoursWorked,160));
int overtimeSalary = (int)(hourlyRate*1.5*Math.max(0,hoursWorked - 160));
return super.calculateSalary(month);
}
}
// 子类:销售人员(SalesEmployee)
class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
public SalesEmployee(String name, Month birthMonth, double monthlySales, double commissionRate) {
super(name, birthMonth);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
public double getMonthlySales() {
return monthlySales;
}
public double getCommissionRate() {
return commissionRate;
}
@Override
public int calculateSalary(int month) {
int commission = (int) (monthlySales * commissionRate);
return super.calculateSalary(month) + commission;
}
}
// 子类:有固定底薪的销售人员(BasePlusSalesEmployee)
class BasePlusSalesEmployee extends SalesEmployee {
private int baseSalary;
public BasePlusSalesEmployee(String name, Month birthMonth, double monthlySales, double commissionRate, int baseSalary) {
super(name, birthMonth, monthlySales, commissionRate);
this.baseSalary = baseSalary;
}
public int getBaseSalary() {
return baseSalary;
}
@Override
public int calculateSalary(int month) {
return super.calculateSalary(month) + baseSalary;
}
}
public class Main {
public static void main(String[] args) {
// 创建几个不同类型的员工对象
Employee employee1 = new SalariedEmployee("Alice", Month.JANUARY, 3000);
Employee employee2 = new HourlyEmployee("Bob", Month.FEBRUARY, 20, 200);
Employee employee3 = new SalesEmployee("Charlie", Month.MARCH, 10000, 0.05);
Employee employee4 = new BasePlusSalesEmployee("David", Month.APRIL, 15000, 0.04, 2500);
// 打印某个员工的信息
printEmployeeInfo(employee1);
System.out.println();
printEmployeeInfo(employee2);
System.out.println();
printEmployeeInfo(employee3);
System.out.println();
// 打印某个员工在某个月的工资
System.out.println(employee1.getName() + ": " + employee1.calculateSalary(3));
System.out.println(employee2.getName() + ": " + employee2.calculateSalary(4));
System.out.println(employee3.getName() + ": " + employee3.calculateSalary(5));
System.out.println(employee4.getName() + ": " + employee4.calculateSalary(6));
}
private static void printEmployeeInfo(Employee employee) {
System.out.println("Name: " + employee.getName());
System.out.println("Birth Month: " + employee.getMonth());
System.out.println("Salary for current month (assuming current month is 7): " + employee.calculateSalary(7));
System.out.println("--------------------");
}
}