编写程序:5类员工有对应封装类,创建Employee数组,若干不同的Employee对象,并实现增删改查功能(《黑马程序员》P144编程题加强版)

2023-11-09


原题:
(1) Employee:这是所有员工总的父类。

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

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

(2) SalariedEmployee:Employee 的子类,拿固定工资的员工。

① 属性:月薪。

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

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

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

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

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

① 属性:底薪。

本题要求根据上述雇员分类,编写一个程序,实现以下功能:

(1)创建一个 Employee 数组,分别创建若干不同的 Employee 对象,并打印某个月的工资。

(2)每个类都完全封装,不允许非私有化属性

加强版:实现增删改查的功能

Employee类

public class Employee {
    private String name;
    private int month;

    public Employee() {
    }

    public Employee(String name, int month) {
        this.name = name;
        this.month = 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 getSalary(int month){
        double salary=0;
        if(this.month==month){
            salary=salary+100;
        }
        return salary;
    }

}

SalariedEmployee类

public class SalariedEmployee extends Employee {
    private double monthSalary;

    public SalariedEmployee() {
    }

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

    public double getMonthSalary() {
        return monthSalary;
    }

    public void setMonthSalary(double monthSalary) {
        this.monthSalary = monthSalary;
    }

    @Override
    public double getSalary(int month){
        double salary = monthSalary+super.getSalary(month);
        return salary;
    }

}

HourlyEmployee类

public class HourlyEmployee extends Employee{
    private double hourSalary;
    private int hours;

    public HourlyEmployee() {
    }

    public HourlyEmployee(String name, int month, double hourSalary, int hours) {
        super(name, month);
        this.hourSalary = hourSalary;
        this.hours = hours;
    }

    public double getHourSalary() {
        return hourSalary;
    }

    public void setHourSalary(double hourSalary) {
        this.hourSalary = hourSalary;
    }

    public int getHours() {
        return hours;
    }

    public void setHours(int hours) {
        this.hours = hours;
    }
    @Override
    public double getSalary(int month){
        if(hours < 0){      //如果工作小时数小于0  输出数据错误
            System.out.println("数据错误");
            return 0;
        }
        //小于160个小时的 按照每个月的工作小时数乘以每小时的工资
        else if(hours <= 160)
            return hourSalary*hours+super.getSalary(month);
            //超出160个小时的小时数 按照1.5倍计算
        else return hourSalary*160+hourSalary*1.5*(hours-160)+super.getSalary(month);
    }

}

SalesEmployee类

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

    public SalesEmployee() {
    }

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

    public double getMonthlySales() {
        return monthlySales;
    }

    public void setMonthlySales(double monthlySales) {
        this.monthlySales = monthlySales;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }
    @Override
    public double getSalary(int month){
        return this.getMonthlySales()*(1+this.getRate())+super.getSalary(month);
    }

}

BasePlusSalesEmployee类

public class BasePlusSalesEmployee extends SalesEmployee{
    private double basePay;

    public BasePlusSalesEmployee() {
    }

    public BasePlusSalesEmployee(String name, int month, double monthlySales, double rate, double basePay) {
        super(name, month, monthlySales, rate);
        this.basePay = basePay;
    }

    public double getBasePay() {
        return basePay;
    }

    public void setBasePay(double basePay) {
        this.basePay = basePay;
    }
    @Override
    public double getSalary(int month){
        return basePay+super.getSalary(month);
    }


}

Test类(实现增删改查)

import java.util.Scanner;


public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a;
        Employee[] employee;
        employee = new Employee[100];
        employee[0]=new SalariedEmployee("张三", 1, 6000);
        employee[1]=new HourlyEmployee("李四", 2, 50, 180);
        employee[2]=new SalesEmployee("王五", 3, 6500, 0.15);
        employee[3]=new BasePlusSalesEmployee("赵六", 4, 5000, 0.15, 2000);
        employee[4]=new BasePlusSalesEmployee("qz",5,6000,0.1,3000);

        System.out.println("--------------欢迎来到员工管理系统---------------");
        System.out.println("1.办理员工入职");
        System.out.println("2.辞退员工");
        System.out.println("3.修改员工信息");
        System.out.println("4.查询员工信息");
        System.out.println("0.退出");
        System.out.println("-----------------------------");
        System.out.println("请选择您想办理的业务");

        while(true){
            a = sc.nextInt();
            switch(a){
                case 1:
                    addEmployee(employee);
                    break;
                case 2:
                    deleteEmployee(employee);
                    break;
                case 3:
                    updateEmployee(employee);
                    break;
                case 4:
                    searchEmployee(employee);
                    break;
                case 0:
                    System.exit(0);
                    break;
                default:
                    System.out.println("您的输入有误,请重新输入!");
                    break;
            }
        }

    }
    private static void searchEmployee(Employee[] employee){
        int i,m,k;
        for(k=0;k<employee.length;k++){
            if(employee[k]!=null)
                System.out.println(employee[k].getName());
        }
        System.out.println("请输入您想要查询的员工的姓名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        for(i=0;i<employee.length;i++){
            if(name.equals(employee[i].getName())){
                System.out.println("姓名:"+employee[i].getName()+"\n出生月份:"+employee[i].getMonth());
                System.out.println("请随机输入一个月份:");
                m=sc.nextInt();
                System.out.println(employee[i].getName()+"在"+m+"月的工资为:"+employee[i].getSalary(m));
                break;
            }else{
                if(i==employee.length){
                    System.out.println("查无此人");
                    break;
                }else{
                    continue;
                }
            }
        }
        show();
    }
    private static void deleteEmployee(Employee[] employee){
        int k;
        for(k=0;k<employee.length;k++){
            if(employee[k]!=null)
                System.out.println(employee[k].getName());
        }
        System.out.println("请输入您想要辞退的员工的姓名");
        Scanner sc=new Scanner(System.in);
        String name=sc.next();
        int i;
        for(i=0;i<employee.length;i++){
            if(name.equals(employee[i].getName())){
                break;
            }
        }
        for(int j=i;j<employee.length-1;j++){
            employee[j]=employee[j+1];
        }
        show();
    }
    private static void addEmployee(Employee[] employee){
        Scanner sc=new Scanner(System.in);
        int n,month;
        String name;
        System.out.println("请选择员工类型:");
        System.out.println("1) 拿固定工资的员工\n2) 按小时拿工资的员工\n3) 销售人员\n4) 有固定底薪的销售人员\n0) 退出");
        int flag=1;
        int i;
        for(i=0;i<employee.length;i++){
            if(employee[i]==null){
                break;
            }
        }
        while(flag==1){
            n=sc.nextInt();
            switch (n){
                case 1:

                    double monthSalary;
                    employee[i]=new SalariedEmployee();
                    SalariedEmployee se=(SalariedEmployee)employee[i];
                    System.out.println("请输入员工姓名");
                    name=sc.next();
                    System.out.println("出生月份");
                    month=sc.nextInt();
                    System.out.println("月薪");
                    monthSalary=sc.nextDouble();
                    se.setName(name);
                    se.setMonth(month);
                    se.setMonthSalary(monthSalary);
                    System.out.println("新入职员工姓名:"+name+"\n出生月份:"+month+"\n月薪:"+monthSalary);
                    System.out.println("--------------------------=");
                    System.out.println("若还要添加,请选择:\n否则摁0退出\n1) 拿固定工资的员工\n2) 按小时拿工资的员工\n3) 销售人员\n4) 有固定底薪的销售人员\n0) 退出");
                    break;
                case 2:
                    double hourSalary;
                    int hours;
                    System.out.println("请输入员工姓名");
                    name=sc.next();
                    System.out.println("出生月份");
                    month=sc.nextInt();
                    System.out.println("时薪");
                    hourSalary=sc.nextDouble();
                    System.out.println("每月工作小时数");
                    hours=sc.nextInt();
                    employee[i]=new HourlyEmployee(name,month,hourSalary,hours);
                    HourlyEmployee he = (HourlyEmployee)employee[i];
                    System.out.println("新入职员工姓名:"+name+"\n出生月份:"+month+"\n时薪:"+hourSalary+"\n每月工作小时数:"+hours);
                    System.out.println("--------------------------=");
                    System.out.println("若还要添加,请选择:\n否则摁0退出\n1) 拿固定工资的员工\n2) 按小时拿工资的员工\n3) 销售人员\n4) 有固定底薪的销售人员\n0) 退出");
                    break;
                case 3:
                    double monthlySales;
                    double rate;
                    System.out.println("请输入员工姓名");
                    name=sc.next();
                    System.out.println("出生月份");
                    month=sc.nextInt();
                    System.out.println("月销售额");
                    monthlySales=sc.nextDouble();
                    System.out.println("提成率");
                    rate=sc.nextDouble();
                    employee[i]=new SalesEmployee(name,month,monthlySales,rate);
                    SalesEmployee see=(SalesEmployee)employee[i];
                    System.out.println("新入职员工姓名:"+name+"\n出生月份:"+month+"\n月销售额:"+monthlySales+"\n提成率:"+rate);
                    System.out.println("--------------------------=");
                    System.out.println("若还要添加,请选择:\n否则摁0退出\n1) 拿固定工资的员工\n2) 按小时拿工资的员工\n3) 销售人员\n4) 有固定底薪的销售人员\n0) 退出");
                    break;
                case 4:
                    double basePay;
                    double monthlyS;
                    double royalty;
                    System.out.println("请输入员工姓名");
                    name=sc.next();
                    System.out.println("出生月份");
                    month=sc.nextInt();
                    System.out.println("月销售额");
                    monthlyS=sc.nextDouble();
                    System.out.println("提成率");
                    royalty=sc.nextDouble();
                    System.out.println("底薪");
                    basePay=sc.nextDouble();
                    employee[i]=new BasePlusSalesEmployee(name,month,monthlyS,royalty,basePay);
                    BasePlusSalesEmployee bpse=(BasePlusSalesEmployee)employee[i];
                    System.out.println("新入职员工姓名:"+name+"\n出生月份:"+month+"\n月销售额:"+monthlyS+"\n提成率:"+royalty+"\n底薪:"+basePay);
                    System.out.println("----------------------------");
                    System.out.println("若还要添加,请选择:\n否则摁0退出\n1) 拿固定工资的员工\n2) 按小时拿工资的员工\n3) 销售人员\n4) 有固定底薪的销售人员\n0) 退出");
                    break;
                case 0:
                    flag=0;
                    continue;
                default:
                    System.out.println("没有此选项,请重新输入!");
                    break;
            }
        }
        show();
    }
    private static void updateEmployee(Employee[] employee){
        int k,i,n,flag;
        String name;
        Scanner sc=new Scanner(System.in);
        System.out.println("员工名单:");
        for(k=0;k<employee.length;k++){
            if(employee[k]!=null)
                System.out.println(employee[k].getName());
        }
        System.out.println("请输入你想要操作的员工姓名");
        name=sc.next();
        //1.根据名字找到员工
        for(i=0;i<employee.length;i++){
            if(name.equals(employee[i].getName())){
                break;
            }
        }
        //2.确定员工类型并打印员工信息,,,3.选择并更改员工信息,不同员工所要更改的信息不同
        if(employee[i] instanceof SalariedEmployee){
            double tempMonthSalary;
            SalariedEmployee se=(SalariedEmployee)employee[i];
            System.out.println("-------------------------");
            System.out.println("员工姓名:"+se.getName()+"\n出生月份:"+se.getMonth()+"\n月薪:"+se.getMonthSalary());
            System.out.println("-------------------------");
            System.out.println("请输入更改后的月薪:");
            tempMonthSalary=sc.nextDouble();
            se.setMonthSalary(tempMonthSalary);
            System.out.println("更改后员工信息:");
            System.out.println("员工姓名:"+se.getName()+"\n出生月份:"+se.getMonth()+"\n月薪:"+se.getMonthSalary());
            show();
        }else if(employee[i] instanceof HourlyEmployee){
            double tempHourSalary;
            int tempHours;
            HourlyEmployee he=(HourlyEmployee)employee[i];
            System.out.println("-------------------------");
            System.out.println("员工姓名:"+he.getName()+"\n出生月份:"+he.getMonth()+"\n时薪:"+he.getHourSalary()+"\n每月工作小时数:"+he.getHours());
            System.out.println("-------------------------");
            flag=1;
            while(flag==1){
                System.out.println("请选择您想要更改的信息:\n1.时薪\n2.每月工作小时数\n0.退出");
                n=sc.nextInt();
                switch(n){
                    case 1:
                        System.out.println("请输入更改后的时薪:");
                        tempHourSalary=sc.nextDouble();
                        he.setHourSalary(tempHourSalary);
                        System.out.println("更改后员工信息:");
                        System.out.println("员工姓名:"+he.getName()+"\n出生月份:"+he.getMonth()+"\n时薪:"+he.getHourSalary()+"\n每月工作小时数:"+he.getHours());
                        System.out.println("-------------");
                        break;
                    case 2:
                        System.out.println("请输入更改后的每月工作小时数:");
                        tempHours=sc.nextInt();
                        he.setHours(tempHours);
                        System.out.println("更改后员工信息:");
                        System.out.println("员工姓名:"+he.getName()+"\n出生月份:"+he.getMonth()+"\n时薪:"+he.getHourSalary()+"\n每月工作小时数:"+he.getHours());
                        System.out.println("-------------");
                        break;
                    case 0:
                        flag=0;
                        break;
                    default:
                        System.out.println("抱歉,您的输入有误,请重新输入");
                        break;

                }
            }
            show();
        }else if(employee[i] instanceof SalesEmployee){
            //因为BasePlusSalesEmployee extends SalesEmployee,所以要再判断一下
            if(employee[i] instanceof BasePlusSalesEmployee){
                double tempRate,tempBasePay;
                BasePlusSalesEmployee bpse=(BasePlusSalesEmployee)employee[i];
                System.out.println("-------------------------");
                System.out.println("员工姓名:"+bpse.getName()+"\n出生月份:"+bpse.getMonth()+"\n月销售额:"+bpse.getMonthlySales()+"\n提成率:"+bpse.getRate()+"\n底薪:"+bpse.getBasePay());
                System.out.println("-------------------------");
                flag=1;
                while(flag==1){
                    System.out.println("请选择您想要更改的信息:\n1. 提成率\n2. 底薪\n0. 退出");
                    n=sc.nextInt();
                    switch(n){
                        case 1:
                            System.out.println("请输入更改后的提成率:");
                            tempRate=sc.nextDouble();
                            bpse.setRate(tempRate);
                            System.out.println("更改后员工信息:");
                            System.out.println("员工姓名:"+bpse.getName()+"\n出生月份:"+bpse.getMonth()+"\n月销售额:"+bpse.getMonthlySales()+"\n提成率:"+bpse.getRate()+"\n底薪:"+bpse.getBasePay());
                            System.out.println("-------------");
                            break;
                        case 2:
                            System.out.println("请输入更改后的底薪:");
                            tempBasePay=sc.nextInt();
                            bpse.setBasePay(tempBasePay);
                            System.out.println("更改后员工信息:");
                            System.out.println("员工姓名:"+bpse.getName()+"\n出生月份:"+bpse.getMonth()+"\n月销售额:"+bpse.getMonthlySales()+"\n提成率:"+bpse.getRate()+"\n底薪:"+bpse.getBasePay());
                            System.out.println("-------------");
                            break;
                        case 0:
                            flag=0;
                            break;
                        default:
                            System.out.println("抱歉,您的输入有误,请重新输入");

                    }
                }
                show();
            }else{
                double tempRate;
                SalesEmployee se=(SalesEmployee)employee[i];
                System.out.println("-------------------------");
                System.out.println("员工姓名:"+se.getName()+"\n出生月份:"+se.getMonth()+"\n月销售额:"+se.getMonthlySales()+"\n提成率:"+se.getRate());
                System.out.println("-------------------------");
                System.out.println("请输入更改后的提成率:");
                tempRate=sc.nextDouble();
                se.setRate(tempRate);
                System.out.println("更改后员工信息:");
                System.out.println("员工姓名:"+se.getName()+"\n出生月份:"+se.getMonth()+"\n月销售额:"+se.getMonthlySales()+"\n提成率:"+se.getRate());
                show();
            }

        }
    }
    private static void show(){
        System.out.println("                               ");
        System.out.println("                               ");
        System.out.println("-----------------------------");
        System.out.println("1.办理员工入职");
        System.out.println("2.辞退员工");
        System.out.println("3.修改员工信息");
        System.out.println("4.查询员工信息");
        System.out.println("0.退出");
        System.out.println("-----------------------------");
        System.out.println("请选择您想办理的业务");
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

编写程序:5类员工有对应封装类,创建Employee数组,若干不同的Employee对象,并实现增删改查功能(《黑马程序员》P144编程题加强版) 的相关文章

随机推荐

  • 可中断睡眠 sleep

    可中断睡眠 可中断睡眠的执行情况 进程开始时处于可中断睡眠状态 那么如果进程接收到信号后 进程将被唤醒而不在阻塞 当执行完信号处理函数后 就不再睡眠了 直接向下执行代码 sleep 函数 unsigned int sleep unsigne
  • bugku各种绕过

    题目要求uname passwd 但是他们的SHA1值要相同 且id值为margin 利用PHP的sha1漏洞 当参数为数组时返回false 判断成立
  • 嵌入式入门基础知识有哪些?

    嵌入式系统是指在特定应用领域内为满足特定要求而设计的计算机系统 通常被嵌入到设备中 具有实时性 可靠性 低功耗等特点 嵌入式系统应用广泛 例如 智能家居 智能手表 汽车控制系统 医疗设备等 在本篇博客中 我们将讨论嵌入式入门基础知识 包括嵌
  • 狂神说Mybatis笔记(全网最全)

    Mybatis 环境说明 jdk 8 MySQL 5 7 19 maven 3 6 0 IDEA 学习前需要掌握 JDBC MySQL Java 基础 Maven Junit 1 Mybatis简介 1 1 什么是MyBatis MyBat
  • 认识区块链,认知区块链— —数据上链

    上周末参加一次长沙本地胡子互联网俱乐部举办的区块链分享会 颇受启发 同时感谢俱乐部提供的这个交流平台 祝好 好吧 还是先把前些天对区块链的一点理解简单整理下 再回顾下上周末的参会纪要比较好 下篇给大家分享出来 个人区块链思考第一篇 认识区块
  • Yolov3计算准确率、误报率、漏检率等

    思想很简单 将标注的yolo数据转下格式 转为 类别 xmin ymin xmax ymax 转换valid后的信息 两个信息进行对比 完事 具体的 在终端执行 darknet detector valid cfg voc data cfg
  • 【SSM框架】之Spring

    SSM框架笔记 自用 Spring Spring Framework系统架构 Spring程序开发步骤 核心概念 IoC Inversion of Control 控制反转 使用对象时 由主动new产生对象转换为由外部提供对象 此过程中对象
  • 计算机毕业设计看这篇就够了(二)毕设流程

    本篇将为大家介绍计算机专业毕业设计流程 提前了解毕设流程可以让同学们从宏观角度去看毕设要做些什么样的事情 大概知道每个阶段要去做哪些工作 为后续毕设任务的真正开展打下心理预期 也不至于一脸懵 计算机毕设分为以下主流程 选题 确定导师 完成前
  • 【Proteus仿真】【STM32单片机】基于stm32的智能书桌设计

    文章目录 一 功能简介 二 软件设计 三 实验现象 联系作者 一 功能简介 系统运行后 默认为手动模式 当检测有人 可通过K2键开关灯 如果姿势不对 警示灯亮 否则灭 可通过K3和K4键调节桌子高度 按下K1键切换为自动模式 此时有人 且光
  • Sentinel原理与Demo

    Sentinel 是什么 随着微服务的流行 服务和服务之间的稳定性变得越来越重要 Sentinel 以流量为切入点 从流量控制 熔断降级 系统负载保护等多个维度保护服务的稳定性 Sentinel 具有以下特征 丰富的应用场景 Sentine
  • 【FreeRTOS(三)】任务状态

    文章目录 任务状态 任务挂起 vTaskSuspend 取消任务挂起 vTaskResume 挂起任务调度器 vTaskSuspendAll 取消挂起任务调度器 xTaskResumeAll 代码示例 任务挂起 取消任务挂起 代码示例 挂起
  • Docker help帮助文档

    1 查看 docker help 帮助 docker help 2 用法 docker 选项 命令 3 选项 客户端配置文件的配置字符串位置 默认为 root docker D 启用调试模式 H 要连接的主机列表守护进程套接字 l 设置日志
  • Centos7.3安装和配置Mysql5.7

    第一步 获取mysql YUM源 进入mysql官网获取RPM包下载地址 https dev mysql com downloads repo yum 点击 下载 右击 复制链接地址 https dev mysql com get mysq
  • 源码剖析transformer、self-attention

    原文链接 首先给大家引入一个github博客 这份代码是我在看了4份transformer的源码后选出来的 这位作者的写法非常易懂 代码质量比较高 GitHub Separius BERT keras Keras implementatio
  • 一步一步教你用idea上交代码到gitee(图文解释)

    一步一步教你用idea上交代码到gitee 图文解释 文章目录 一步一步教你用idea上交代码到gitee 图文解释 工具准备 具体操作 结语 工具准备 首先 我们进行代码的提交需要两个工具包 在我的上一篇中有讲 大家可以自行去提取 2条消
  • .net 批量注册服务

    假设我们需要注册xxxQuery服务 例如下图中的BarQuery和FooQuery 传统的做法是 services TryAddScoped
  • vscode 运行和调试 javascript 代码

    安装node 安装vscode 扩展包 code runer 配置vs code下有关F5的操作的文件 参考地址
  • 【Zabbix实战之运维篇】Zabbix监控Docker容器配置方法

    Zabbix实战之运维篇 Zabbix监控Docker容器配置方法 一 检查Zabbix监控平台状态 1 检查Zabbix各组件容器状态 2 奸诈Zabbix server状态 二 下载监控模板 1 进入Zabbix官网下载页面 2 查看下
  • 微信小程序中识别html标签的方法

    rich text组件 在微信小程序中有一个组件rich text可以识别文本节点或是元素节点 具体入下 需要识别的数据放在data中 然后放在nodes属性中即可
  • 编写程序:5类员工有对应封装类,创建Employee数组,若干不同的Employee对象,并实现增删改查功能(《黑马程序员》P144编程题加强版)

    文章目录 Employee类 SalariedEmployee类 HourlyEmployee类 SalesEmployee类 BasePlusSalesEmployee类 Test类 实现增删改查 原题 1 Employee 这是所有员工