1. 程式人生 > >用繼承及封裝的特性對員工工資計算的練習程式碼

用繼承及封裝的特性對員工工資計算的練習程式碼

僱員類Employee,這是所有員工總的父類

/**
* 僱員類Employee,這是所有員工總的父類,屬性:員工的姓名,員工的生日月份
* 方法:getSalary(int month) 根據引數月份來確定工資,如果該月
* 員工過生日,則公司會額外獎勵100元。
* @author 張全勝
*
*/
public class Employee {
private String name;//員工姓名
private int month;//員工生日月份
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}

//根據引數月份來確定工資
public double gets(int month){
    if(this.month == month){
        return 100;
    }else{
        return 0;
    }
}

public double getSalary(int month){
    return gets(month);
}

}

====================================

拿固定工資的員工

/**
* Employee的子類,拿固定工資的員工。屬性:月薪
* @author 張全勝
*
*/
public class SalariedEmployee extends Employee{
private double monsalary;//月薪
public double getMonsalary() {
return monsalary;
}
public void setMonsalary(double monsalary) {
this.monsalary = monsalary;
}

//無參構造器
public SalariedEmployee(){
    super();
}

//有參構造器
public SalariedEmployee(String name, int month,double monsalary){
    this.setName(name);
    this.setMonth(month);
    this.setMonsalary(monsalary);
}

//月薪
@Override
public double getSalary(int month) {
    return this.getMonsalary() + gets(month);
}   

}

===============================

銷售人員,工資由月銷售額和提成率決定

/**
* 銷售人員,工資由月銷售額和提成率決定
* 屬性:月銷售額、提成率
* @author 張全勝
*
*/
public class SalesEmployee extends Employee{
private double monsale;//月銷售額
private double deduct;//提成率
public double getMonsale() {
return monsale;
}
public void setMonsale(double monsale) {
this.monsale = monsale;
}
public double getDeduct() {
return deduct;
}
public void setDeduct(double deduct) {
this.deduct = deduct;
}

//無參構造器
public SalesEmployee(){
    super();
}
//有參構造器
public SalesEmployee(String name,int month,double monsale,double deduct){
    this.setName(name);
    this.setMonth(month);
    this.setMonsale(monsale);
    this.setDeduct(deduct);
}

//工資
@Override
public double getSalary(int month) {
    return this.getMonsale()*this.getDeduct() + gets(month);
}

}

================================

按小時拿工資的員工

/**
* 按小時拿工資的員工,每月工作超出160小時
* 的部分按照1.5倍工資發放。屬性:每小時的工資、每月工作的小時數
* @author 張全勝
*
*/
public class HourlyEmployee extends Employee{
private double hsal;//每小時工資
private int hour;//每月工作小時數
public double getHsal() {
return hsal;
}
public void setHsal(double hsal) {
this.hsal = hsal;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}

//無參構造器
public HourlyEmployee(){
    super();
}

//有參構造器
public HourlyEmployee(String name,int month,double hsal,int hour){
    this.setName(name);
    this.setHsal(hsal);
    this.setHour(hour);
    this.setMonth(month);
}

//工資
@Override
public double getSalary(int month) {
    if(this.getHour() <= 160 && this.getHour() >=0){
        return this.getHour()*this.getHsal() + gets(month);
    }else{
        return this.getHsal()*160 + (this.getHour()-160)*this.getHsal()*1.5 + gets(month);
    }
}   

}

===============================

有固定底薪的銷售人員

/**
* SalesEmployee的子類,有固定底薪的銷售人員,
* 工資由底薪加上銷售提成部分。屬性:底薪
* @author 張全勝
*
*/
public class BasePlusSalesEmployee extends SalesEmployee{
private double basicsal;//底薪
public double getBasicsal() {
return basicsal;
}
public void setBasicsal(double basicsal) {
this.basicsal = basicsal;
}
//無參構造器
public BasePlusSalesEmployee(){

}
//有參構造器
public BasePlusSalesEmployee(String name,int month,double monsale,double deduct,double basicsal){
    this.setName(name);
    this.setMonth(month);
    this.setMonsale(monsale);
    this.setDeduct(deduct);
    this.setBasicsal(basicsal);
}

//工資
@Override
public double getSalary(int month) {
    return this.getMonsale()*this.getDeduct() + this.getBasicsal() + gets(month);
}

}

=================================

測試類

/**
* 測試類
* @author 張全勝
*
*/
public class Test {

public static void main(String[] args) {
    int m = 9;//設定當月月份為8

    SalariedEmployee se = new SalariedEmployee("寶寶",8,3500.0);
    System.out.println(se.getName() + "固定工資是" + se.getMonsalary() + ",生日是" + se.getMonth() + "月,這月工資是" + se.getSalary(m));

    HourlyEmployee he = new HourlyEmployee("段譽",5,10.5,300);
    System.out.println(he.getSalary(m));

    SalesEmployee sse = new SalesEmployee("虛竹",8,300000,0.1);
    System.out.println(sse.getSalary(m));

    BasePlusSalesEmployee bpse = new BasePlusSalesEmployee("蕭峰",8,300000,0.1,1500);
    System.out.println(bpse.getSalary(m));
}

}