23,405
社区成员
发帖
与我相关
我的任务
分享
import java.util.Arrays;
public class TestCompany {
static class ColaEmployee {
protected String name;
protected int birthday;
public ColaEmployee(String name, int birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return this.name;
}
public double getSalary(int month) {
return birthday == month ? 100 : 0;
}
}
static class SalariedEmployee extends ColaEmployee {
private double salary;
SalariedEmployee(String name, int birthday, double salary) {
super(name, birthday);
this.salary = salary;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + salary;
}
}
static class HourlyEmployee extends ColaEmployee {
private double workHours;
private double hourlySalary;
HourlyEmployee(String name, int birthday, int workHours, double hourlySalary) {
super(name, birthday);
this.workHours = workHours;
this.hourlySalary = hourlySalary;
}
@Override
public double getSalary(int month) {
double more = workHours - 160;
return super.getSalary(month) + (more > 0 ? (160 * hourlySalary) + (more * hourlySalary * 1.5) : workHours * hourlySalary);
}
}
static class SalesEmployee extends ColaEmployee {
private double sales;
private double bonus;
SalesEmployee(String name, int birthday, double sales, double bonus) {
super(name, birthday);
this.sales = sales;
this.bonus = bonus;
}
@Override
public double getSalary(int month) {
return super.getSalary(month) + sales * bonus;
}
}
static class Company {
public void print(int month, ColaEmployee[] employees) {
Arrays.stream(employees).forEach(employee -> System.out.println(String.format("%s月份,%s员工工资%s", month, employee.getName(), employee.getSalary(month))));
}
}
public static void main(String[] args) {
ColaEmployee[] colaEmployees = new ColaEmployee[3];
colaEmployees[0] = new SalariedEmployee("salary employee", 2, 1800);
colaEmployees[1] = new HourlyEmployee("hourly employee", 5, 100, 10);
colaEmployees[2] = new SalesEmployee("sales employee", 6, 800, 1.5);
Company company = new Company();
company.print(6, colaEmployees);
}
}