面向对象实例化练习——继承

作业内容:

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

  • Employee:这是所有员工总的父类。

    • 属性:员工的姓名,员工的生日月份。

    • 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

  • SalariedEmployee: Employee 的子类,拿固定工资的员工。

    • 属性:月薪。

  • HourlyEmployee Employee 的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5 倍工资发放。

    • 属性:每小时的工资、每月工作的小时数。

  • SalesEmployee: Employee 的子类,销售,工资由月销售额和提成率决定。

    • 属性:月销售额、提成率。

  • BasePlusSalesEmployee : SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。

    • 属性:底薪。

要求:

  1. 创建一个Employee 数组,分别创建若干不同的Employee对象,根据并打印月工资。(输入输出形式可自由发挥,例如输入员工姓名和月份,打印相应的工资数)

  2. 把每个类都做成完全封装,不允许非私有化属性。


答案:

  • Employee类

public abstract class Employee {
    private String name;
    private int birthdayMonth;

    public Employee(String name, int birthdayMonth) {
        this.name = name;
        this.birthdayMonth = birthdayMonth;
    }

    public abstract double getSalary(int month);

    public String getName() {
        return name;
    }

    public int getBirthdayMonth() {
        return birthdayMonth;
    }
}
  • SalariedEmployee类
public class SalariedEmployee extends Employee{
    private double salary;

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

    @Override
    public double getSalary(int month) {
        if (month == getBirthdayMonth()) {
            return salary + 100;
        } else {
            return salary;
        }
    }
}
  • HourlyEmployee类

public class HourlyEmployee extends Employee{
    private double hourlySalary;
    private int hoursWorked;

    public HourlyEmployee(String name, int birthdayMonth, double hourlySalary, int hoursWorked) {
        super(name, birthdayMonth);
        this.hourlySalary = hourlySalary;
        this.hoursWorked = hoursWorked;
    }

    @Override
    public double getSalary(int month) {
        double salary = 0;
        if (hoursWorked >160)
            salary = (hoursWorked * 1.5 - 80) * hourlySalary;
        else
            salary = hoursWorked * hourlySalary;
        if (month == getBirthdayMonth()) {
            return salary + 100;
        } else {
            return salary;
        }
    }
}
  • SalesEmployee类

public class SalesEmployee extends Employee{
    private double monthlySales;
    private double commissionRate;

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

    @Override
    public double getSalary(int month) {
        double salary = monthlySales * commissionRate;
        if (month == getBirthdayMonth()) {
            return salary + 100;
        } else {
            return salary;
        }
    }
}
  • BasePlusSalesEmployee类

public class BasePlusSalesEmployee extends SalesEmployee{
    private double baseSalary;
    public BasePlusSalesEmployee(String name, int birthdayMonth, double monthlySales, double commissionRate, double baseSalary) {
        super(name, birthdayMonth, monthlySales, commissionRate);
        this.baseSalary = baseSalary;
    }

    @Override
    public double getSalary(int month) {
        double salesEmployeeSalary = super.getSalary(month); // 计算销售员工的基本工资和提成。
        return salesEmployeeSalary + baseSalary; // 加上底薪。
    }
}
  • Main类

import java.util.Scanner;

public class Main {
    static Scanner scan = new Scanner(System.in);

    // 测试SalariedEmployee类
    public static void testSalariedEmployee() {
        System.out.println("当前测试固定工资用例 \n请输入用例个数(建议至少2人):");
        int x = scan.nextInt();
        for (int i=1; i<=x; i++)
        {
            System.out.println("请输入员工姓名、出生月份、月薪:");
            String name = scan.next();
            int birthdayMonth = scan.nextInt();
            double salary = scan.nextDouble();
            Employee employee1 = new SalariedEmployee(name, birthdayMonth, salary);
            for (int j=1; j<=12; j++)
            {
                double salary1 = employee1.getSalary(j);
                System.out.println(name + "在" + j + "月的工资:" + salary1);
            }
            System.out.println("\n");
        }
    }

    // 测试HourlyEmployee类
    public static void testHourlyEmployee() {
        System.out.println("当前测试小时工资用例 \n请输入用例个数(建议至少2人):");
        int x = scan.nextInt();
        for (int i=1; i<=x; i++)
        {
            System.out.println("请输入员工姓名、出生月份、时薪、每月工时(国家规定标准的一个月工时不得超过176小时):");
            String name = scan.next();
            int birthdayMonth = scan.nextInt();
            double hourlySalary = scan.nextDouble();
            int hoursWorked = scan.nextInt();
            Employee employee2 = new HourlyEmployee(name, birthdayMonth, hourlySalary, hoursWorked);
            if (hoursWorked > 176)
            {
                System.out.println("请遵守劳动法!");
                System.out.println("\n");
            }
            else
            {
                for (int j=1; j<=12; j++)
                {
                    double salary2 = employee2.getSalary(j);
                    System.out.println(name + "在" + j + "月的工资:" + salary2);
                }
                System.out.println("\n");
            }
        }
    }

    // 测试SalesEmployee类
    public static void testSalesEmployee() {
        System.out.println("当前测试销售提成用例 \n请输入用例个数(建议至少2人):");
        int x = scan.nextInt();
        for (int i=1; i<=x; i++)
        {
            System.out.println("请输入员工姓名、出生月份、月均销售额、提成率:");
            String name = scan.next();
            int birthdayMonth = scan.nextInt();
            double monthlySales = scan.nextDouble();
            double commissionRate = scan.nextDouble();
            Employee employee3 = new SalesEmployee(name, birthdayMonth, monthlySales, commissionRate);
            for (int j=1; j<=12; j++)
            {
                double salary3 = employee3.getSalary(j);
                System.out.println(name + "在" + j + "月的工资:" + salary3);
            }
            System.out.println("\n");
        }
    }

    // 测试BasePlusSalesEmployee类
    public static void testBasePlusSalesEmployee() {
        System.out.println("当前测试底薪提成用例 \n请输入用例个数(建议至少2人):");
        int x = scan.nextInt();
        for (int i=1; i<=x; i++)
        {
            System.out.println("请输入员工姓名、出生月份、月均销售额、提成率、底薪:");
            String name = scan.next();
            int birthdayMonth = scan.nextInt();
            double monthlySales = scan.nextDouble();
            double commissionRate = scan.nextDouble();
            double baseSalary = scan.nextDouble();
            Employee employee4 = new BasePlusSalesEmployee(name, birthdayMonth, monthlySales, commissionRate, baseSalary);
            for (int j=1; j<=12; j++)
            {
                double salary4 = employee4.getSalary(j);
                System.out.println(name + "在" + j + "月的工资:" + salary4);
            }
            System.out.println("\n");
        }
    }


    public static void main(String[ ] args) {

        while (true)
        {
            System.out.println("请选择测试用例: \n 1.固定工资用例 \n 2.小时工资用例 \n 3.销售提成用例 \n 4.底薪提成用例 \n 5.退出测试");
            int x = scan.nextInt();

            switch (x) {
                case 1: testSalariedEmployee();break;
                case 2: testHourlyEmployee();break;
                case 3: testSalesEmployee();break;
                case 4: testBasePlusSalesEmployee();break;
                case 5: System.out.println("程序已退出。");return;
                default: System.out.println("无效输入,请重新选择!");break;
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值