Java項目案例之---計算公司員工的工資(面向對象復習)


計算公司員工的工資(面向對象復習)

某公司的雇員分為以下若干類:

         Employee:這是所有員工總的父類,屬性:員工的姓名,員工的生日月份。方法:double getSalary(int month) 根據參數月份來確定工資,如果該月員工過生日,則公司會額外獎勵100元。

         SalariedEmployee:Employee的子類,拿固定工資的員工。屬性:月薪

         HourlyEmployee:Employee的子類,按小時拿工資的員工,每月工作超出160小時的部分按照1.5倍工資發放。屬性:每小時的工資、每月工作的小時數

         SalesEmployee:Employee的子類,銷售人員,工資由月銷售額和提成率決定。屬性:月銷售額、提成率

         BasePlusSalesEmployee:SalesEmployee的子類,有固定底薪的銷售人員,工資由底薪加上銷售提成部分。屬性:底薪。

         寫一個程序,把若干各種類型的員工放在一個Employee數組里,寫一個方法,打印出某月每個員工的工資數額。

         注意:要求把每個類都做成完全封裝,不允許非私有化屬性。

import java.util.Calendar;

public class Employee {
    private String name;//員工姓名
    private int month;//生日月份
    public Employee(){

    }
    public Employee(String name,int month){
        this.setName(name);
        this.setMonth(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;
    }

    /**
     * 判斷員工是否當月過生日
     * @param month
     * @return 是的話返回100,不是返回0
     */
    public static double getSalary(int month){
        //獲取當前月份,屬於工具類中的內容
        Calendar ca=Calendar.getInstance();
        int m=ca.get(Calendar.MONTH)+1;
        if(month==m){
            return 100;
        }else{
            return 0;
        }
    }
}
//固定月薪

public class SalariedEmployee extends Employee {

    private double monthsalary;//月薪

    public SalariedEmployee(){



    }

    public SalariedEmployee(String name,int month){

        super(name,month);

    }

    public double getMonthsalary() {

        return monthsalary;

    }



    public void setMonthsalary(double monthsalary) {

        this.monthsalary = monthsalary;

    }



    /**

     * 計算固定月薪員工的工資

     * @param monthsalary

     * @param month

     * @return 固定月薪員工的工資

     */

    public double getmonth(double monthsalary,int month){

        double n=monthsalary+Employee.getSalary( month);

        return n;

    }
}

 

//時薪*小時   超過160小時,超過部分1.5工資

public class HourlyEmployee extends Employee{

    private double hourlyWage;//時薪

    private int hour;//總時長

    public HourlyEmployee(){



    }

    public HourlyEmployee(String name,int month){

        super(name,month);

    }

    public double getHourlyWage() {

        return hourlyWage;

    }



    public void setHourlyWage(double hourlyWage) {

        this.hourlyWage = hourlyWage;

    }



    public int getHour() {

        return hour;

    }



    public void setHour(int hour) {

        this.hour = hour;

    }



    /**

     * 計算時薪員工的工資

     * @param hour

     * @param hourlyWage

     * @param month

     * @return  時薪員工的工資

     */

    public double gethour(int hour,double hourlyWage,int month){

        double n;

        if(hour>160){

           n=160*hourlyWage+(hour-160)*hourlyWage*1.5+Employee.getSalary(month);

        }else{

            n=hour*hourlyWage+Employee.getSalary(month);

        }

        return n;

    }

}

 

//銷售額*提成率

public class SalesEmployee extends Employee{

    private int salesVolume;//銷售額

    private double royalty;//提成率

    public SalesEmployee(){



    }

    public SalesEmployee(String name,int month){

        super(name,month);

    }

    public int getSalesVolume() {

        return salesVolume;

    }



    public void setSalesVolume(int salesVolume) {

        this.salesVolume = salesVolume;

    }



    public double getRoyalty() {

        return royalty;

    }



    public void setRoyalty(double royalty) {

        this.royalty = royalty;

    }



    /**

     * 計算提成

     * @param salesVolume

     * @param royalty

     * @return 提成

     */

    public double getroyalty(int salesVolume,double royalty){

       return salesVolume*royalty;

    }

}

 

//銷售額*提成+底薪

public class BasePlusSalesEmployee extends SalesEmployee{

   private double baseSalary;//底薪

    public BasePlusSalesEmployee(){



    }

    public BasePlusSalesEmployee(String name,int month){

        super(name,month);

    }

    public double getBaseSalary() {

        return baseSalary;

    }



    public void setBaseSalary(double baseSalary) {

        this.baseSalary = baseSalary;

    }



    /**

     * 計算銷售人員的工資

     * @param salesVolume

     * @param royalty

     * @param baseSalary

     * @param month

     * @return 銷售人員月薪

     */

    public double getroyalty(int salesVolume,double royalty,double baseSalary,int month){

        return salesVolume*royalty+baseSalary+Employee.getSalary(month);

    }

}

 

public class EmployeeTest {
    public static void main(String[] args){
        SalariedEmployee a=new SalariedEmployee("月薪人",7);
        HourlyEmployee b=new HourlyEmployee("時薪人",10);
        BasePlusSalesEmployee c=new BasePlusSalesEmployee("提成人",1);
        System.out.println( a.getName()+":\n月薪"+a.getmonth(10000,a.getMonth()));
        System.out.println(b.getName()+":\n月薪"+b.gethour(170,20,b.getMonth()));
        System.out.println(c.getName()+":\n月薪"+c.getroyalty(100,50,1000,c.getMonth()));

    }
}

 

運行結果:

月薪人:

月薪10100.0

時薪人:

月薪3500.0

提成人:

月薪6000.0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM