1. 程式人生 > >某公司的僱員分為以下若干類。寫一個程式,把若干各種型別的員工放在一個Employee 數組裡,寫一個函式,打印出某月每個員工的工資數額

某公司的僱員分為以下若干類。寫一個程式,把若干各種型別的員工放在一個Employee 數組裡,寫一個函式,打印出某月每個員工的工資數額

某公司的僱員分為以下若干類:
Employee:這是所有員工總的父類,屬性:員工的生日月份。方法:getSalary(int month) 根
據引數月份來確定工資,如果該月員工過生日,則公司會額外獎勵100 元。
SalariedEmployee:Employee 的子類,拿固定工資的員工。屬性:月薪
HourlyEmployee:Employee 的子類,按小時拿工資的員工,每月工作超出160 小時的部分
按照1.5 倍工資發放。屬性:每小時的工資、每月工作的小時數SalesEmployee:Employee
的子類, 銷售人員, 工資由月銷售額和提成率決定。屬性: 月銷售額、提成率
BasePlusSalesEmployee:SalesEmployee 的子類,有固定底薪的銷售人員,工資由底薪加上
銷售提成部分。屬性:底薪。
寫一個程式,把若干各種型別的員工放在一個Employee 數組裡,寫一個函式,打印出某月
每個員工的工資數額。注意:不允許非私有化屬性。

BasePlusSaleEmployee.java

package ex4_2;

public class BasePlusSaleEmployee extends SalesEmployee {
    
    private double baseSalary;
    private double addRate;
    private double sale;
    
    public BasePlusSaleEmployee(double sale,double addRate,double baseSalary) {
        super(sale, addRate);
        
// TODO Auto-generated constructor stub setAddRate(addRate); setBaseSalary(baseSalary); setSale(sale); } @Override public double getSalary() { SalesEmployee a=new SalesEmployee(sale, addRate); // TODO Auto-generated method stub return baseSalary+a.getSalary(); }
public double getBaseSalary() { return baseSalary; } public void setBaseSalary(double baseSalary) { this.baseSalary = baseSalary; } public double getAddRate() { return addRate; } public void setAddRate(double addRate) { this.addRate = addRate; } public double getSale() { return sale; } public void setSale(double sale) { this.sale = sale; } }

Employee.java

 1 package ex4_2;
 2 
 3 public class Employee {
 4     
 5     private int monthN=6;//假設現在六月
 6     private int month;
 7     private int salary=100;
 8     
 9     public Employee() {
10         setMonth(month);
11     }
12     
13     public int getMonth() {
14         return month;
15     }
16 
17     public void setMonth(int month) {
18         this.month = month;
19     }
20     public double getSalary() {
21         if(monthN==month) {
22             return salary;
23         }
24         else return 0;
25     }
26 }

HourlyEmployee.java

package ex4_2;

public class HourlyEmployee extends Employee {
    
    private double hourlySalary;
    private int hour;
    
     public HourlyEmployee(double hourlySalary,int hour) {
         super();
        setHour(hour);
        setHourlySalary(hourlySalary);
    }

    @Override
    public double getSalary() {
        if(hour>160)
            return 160*hourlySalary+(hour-160)*1.5*hourlySalary;
        else
            return hour*hourlySalary;
    }
     
    public double getHourlySalary() {
        return hourlySalary;
    }
    public void setHourlySalary(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }
    public int getHour() {
        return hour;
    }
    public void setHour(int hour) {
        this.hour = hour;
    }





    
    
}

SalariedEmployee.java

package ex4_2;

public class SalariedEmployee extends Employee {

    private double salary;

    public SalariedEmployee(double salary) {
        // TODO Auto-generated constructor stub
        super();
        setSalary(salary);
    }
    
    @Override
    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

SalesEmployee.java

 1 package ex4_2;
 2 
 3 public class SalesEmployee extends Employee {
 4     
 5     private double sale;
 6     private  double addRate;
 7     
 8     public SalesEmployee(double sale,double addRate) {
 9         setAddRate(addRate);
10         setSale(sale);
11     }
12 
13 
14     @Override
15     public double getSalary() {
16         // TODO Auto-generated method stub
17         return sale*addRate;
18     }
19     
20     
21     public double getSale() {
22         return sale;
23     }
24 
25     public void setSale(double sale) {
26         this.sale = sale;
27     }
28 
29     public double getAddRate() {
30         return addRate;
31     }
32 
33     public void setAddRate(double addRate) {
34         this.addRate = addRate;
35     }
36 
37 
38 }

Tester.java

package ex4_2;

import java.util.Calendar;
//import java.util.Scanner;

public class Tester {
    public static void main (String[] args) {
        Employee employee[]= {
                new SalariedEmployee(1000),
                new HourlyEmployee(16,150),
                new SalesEmployee(500,0.2),
                new BasePlusSaleEmployee(500,0.2,1000)
        };
        Calendar cal=Calendar.getInstance();
        cal.set(1994, 6, 18);
        for(int i=0;i<employee.length;i++) {
            int month=cal.get(Calendar.MONTH);
            Employee a=new Employee();
            a.setMonth(month);
            double  welfare=a.getSalary();
            double salary=employee[i].getSalary();
            System.out.println("第"+(i+1)+"個:"+(welfare+salary));
        }
    }
}