public class TestEmployee {
public static void main(String[] args) {
Employee[] em = new Employee[4];//建立员工数组,将员工存入数组中
SalariedEmployee work1 = new SalariedEmployee("tom" , 5 , 3000.0);
HourlyEmployee work2 = new HourlyEmployee("jack" , 3 , 20.0 , 200);
SalesEmployee work3 = new SalesEmployee("marry" , 6 , 0.1 , 50000);
BasePlusSalesEmployee work4 = new BasePlusSalesEmployee("anni" , 10 , 40000 , 0.05 , 1000.0 );
Months mon = new Months();
mon.months(work1);
mon.months(work2);
mon.months(work3);
mon.months(work4);
em[0] = work1;
em[1] = work2;
em[2] = work3;
em[3] = work4;
for(int i = 0;i < em.length;i++){
System.out.println("第三月的工资为:" + em[i].getName() +" " + em[i].getSalary(3));
}
printOvaetimeSalary();
}
public static void printOvaetimeSalary(){
double sums = SalariedEmployee.value * 2000 + HourlyEmployee.nums * 1000;
System.out.println("加班费总和为:" + sums);
}
}
class Months{//计算工资
public double months(Employee em){
double salary = em.getSalary(3);
return salary;
}
}
abstract class Employee{//-------------------------------父类
private String name;
private int birthdayMonth;
public abstract double getSalary(int month);
public double giveMonth(int month){
if(month == this.birthdayMonth){
return 100D;
}
return 0D;
}
public Employee() {}
public Employee(String name, int birthdayMonth) {
super();
this.name = name;
this.birthdayMonth = birthdayMonth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthdayMonth() {
return birthdayMonth;
}
public void setBirthdayMonth(int birthdayMonth) {
this.birthdayMonth = birthdayMonth;
}
}
class SalariedEmployee extends Employee implements OvertimePay{//---------------------固定工资
private double salary;
public static int value = 0;
public double getSalary(int month){
return salary + giveMonth(month) + salesEmployee;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public SalariedEmployee() {
value++;
}
public SalariedEmployee(String name, int birthdayMonth , double salary) {
super();
value++;
this.salary = salary;
super.setName(name);
super.setBirthdayMonth(birthdayMonth);
}
}
class HourlyEmployee extends Employee implements OvertimePay{//------------------------------小时工资
private double hourlySalary;
private int hours;
public static int nums = 0;
public double getSalary(int month){
if(this.hours > 160){
return (this.hourlySalary * 160) + ((this.hours - 160) * 1.5 * this.hourlySalary) + giveMonth(month) + basePlusSalesEmployee;
}
return hours * hourlySalary + giveMonth(month) + basePlusSalesEmployee;
}
public HourlyEmployee(){
nums++;
}
public HourlyEmployee(String name, int birthdayMonth , double hourlySalary, int hours) {
super();
nums++;
this.hourlySalary = hourlySalary;
this.hours = hours;
super.setName(name);
super.setBirthdayMonth(birthdayMonth);
}
public double getHourlySalary() {
return hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
}
class SalesEmployee extends Employee{//--------------------------------提成工资
private double sales;
private double rate;
public SalesEmployee(){}
public double getSalary(int month){
return sales * rate + giveMonth(month);
}
public SalesEmployee(String name, int birthdayMonth , double sales, double rate) {
super();
this.sales = sales;
this.rate = rate;
super.setName(name);
super.setBirthdayMonth(birthdayMonth);
}
public double getSales() {
return sales;
}
public void setSales(double sales) {
this.sales = sales;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
class BasePlusSalesEmployee extends SalesEmployee{//-----------------------------固定工资加提成工资
private double baseSalary;
public double getSalary(int month){
return this.getRate() * this.getSales() + baseSalary + giveMonth(month);
}
public BasePlusSalesEmployee(){}
public BasePlusSalesEmployee(String name, int birthdayMonth,double sales, double rate, double baseSalary) {
this.baseSalary = baseSalary;
super.setRate(rate);
super.setSales(sales);
super.setName(name);
super.setBirthdayMonth(birthdayMonth);
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
}
interface OvertimePay {
double salesEmployee = 2000.0;
double basePlusSalesEmployee = 1000;
}