面向对象练习(初稿,学习用)

Col公司的雇员分为以下若干类:(知识点:多态)
  (1)ColEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。
       方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
  (2)SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
  (3)HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
       属性:每小时的工资、每月工作的小时数
  (4)SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
  (5)定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,
  写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并遍历出数组中每个员工当月的工资。

package test1;
/**
 * ColEmployee类:所有员工总的父类;属性有员工姓名和生日月份
 * 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
 */
class ColEmployee {

    private final String name;//员工姓名
    private final int birthMonth;//员工生日月份

    public String getName() {
        return name;
    }

    public ColEmployee(String name, int birthMonth) {
        this.name = name;
        this.birthMonth = birthMonth;
    }

    public double getSalary(int month) {
        //根据各种类型员工工资结构进行计算salary
        return 0.0;
    }

    public boolean isBirthday(int month) {
        return month == birthMonth;
    }
}


/**
 * SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
 */
class SalariedEmployee extends ColEmployee {
    private final double monthlySalary;//固定月薪

    public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
        super(name, birthMonth);
        this.monthlySalary = monthlySalary;
    }

    public double getSalary(int month) {
        double salary = monthlySalary;
        if (isBirthday(month)) {
            salary += 100.0; //当前月员工过生日,工资+100
        }
        return salary;
    }
}

/**
 * HourlyEmployee :ColaEmployee 的子类
 * 按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
 * 属性:每小时的工资、每月工作的小时数
 */
class HourlyEmployee extends ColEmployee {
    private final double hourlyRate;//时薪
    private final int hoursWorked;//工时

    public HourlyEmployee(String name, int birthMonth, double hourlyRate, int hoursWorked) {
        super(name, birthMonth);
        this.hourlyRate = hourlyRate;
        this.hoursWorked = hoursWorked;//每个月工作的小时数
    }

    public double getSalary(int month) {
        double salary = hourlyRate * hoursWorked;
        if (hoursWorked > 160) {
            int overtimeHours = hoursWorked - 160;
            double overtimePay = overtimeHours * hourlyRate * 0.5; //超出部分,在固定时薪的基础上增加0.5倍时薪
            salary += overtimePay;
        }

        if (isBirthday(month)) {
            salary += 100.0; //当前月员工过生日,工资+100
        }
        return salary;
    }
}

/**
 * SalesEmployee :ColaEmployee 的子类,销售人员
 * 工资由月销售额和提成率决定。属性:月销售额、提成率
 */
class SalesEmployee extends ColEmployee {
    private final double monthlySales;//月销售额
    private final double commissionRate;//提成率

    public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
        super(name, birthMonth);
        this.monthlySales = monthlySales;
        this.commissionRate = commissionRate;
    }

    public double getSalary(int month) {
        double salary = monthlySales * commissionRate;
        if (isBirthday(month)) {
            salary += 100.0; //当前月员工过生日,工资+100
        }
        return salary;
    }
}

/**
 * Company类
 * 实现打印某个月某个员工的工资
 */
class Company {
    public void printSalary(ColEmployee employee, int month) {
        double salary = employee.getSalary(month);
        System.out.println("Employee Name: " + employee.getName());//员工姓名
        System.out.println("Month: " + month);//目标月
        System.out.println("Salary: ¥" + salary);//工资
        System.out.println("****************************************");
    }
}

/**
 * 测试类
 */
public class TestCompany {
    public static void main(String[] args) {

        ColEmployee[] employees = new ColEmployee[6];//定义员工数组
        employees[0] = new SalariedEmployee("张三", 5, 3000.0);
        employees[1] = new SalariedEmployee("李四", 6, 3000.0);
        employees[2] = new HourlyEmployee("李华", 5, 15.0, 120);
        employees[3] = new HourlyEmployee("李明", 5, 15.0, 200);
        employees[4] = new SalesEmployee("小明", 7, 60000.0, 0.05);
        employees[5] = new SalesEmployee("小红", 7, 60000.0, 0.08);

        Company company = new Company();
        int month = 6; //目标月份工资对应的月份

        //循环遍历目标月份各个员工工资
        for (ColEmployee employee : employees) {
            company.printSalary(employee, month);
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值