Java项目案例之---计算公司员工的工资(面向对象复习)

时间:2024-10-12 12:07:19

计算公司员工的工资(面向对象复习)

某公司的雇员分为以下若干类:

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