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


原题:
(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("请选择您想办理的业务");
    }
}
  • 8
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的员工信息系统的示例,包括了基本的增删改查操作: 1. 首先,我们需要建立一个名为 `Employee` 的实体,用于表示员工信息: ```java public class Employee { private int id; private String name; private int age; private String gender; public Employee(int id, String name, int age, String gender) { this.id = id; this.name = name; this.age = age; this.gender = gender; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } public String getGender() { return gender; } } ``` 2. 接下来,我们需要建立一个名为 `EmployeeDao` 的 DAO ,用于实现员工信息的增删改查操作。在该中,我们需要建立连接数据库的方法以及对应增删改查方法: ```java public class EmployeeDao { private Connection conn; public EmployeeDao() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employees", "root", "password"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } public void add(Employee employee) { try { PreparedStatement ps = conn.prepareStatement("INSERT INTO employee VALUES (?, ?, ?, ?)"); ps.setInt(1, employee.getId()); ps.setString(2, employee.getName()); ps.setInt(3, employee.getAge()); ps.setString(4, employee.getGender()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void delete(int id) { try { PreparedStatement ps = conn.prepareStatement("DELETE FROM employee WHERE id = ?"); ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void update(Employee employee) { try { PreparedStatement ps = conn.prepareStatement("UPDATE employee SET name = ?, age = ?, gender = ? WHERE id = ?"); ps.setString(1, employee.getName()); ps.setInt(2, employee.getAge()); ps.setString(3, employee.getGender()); ps.setInt(4, employee.getId()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public Employee getById(int id) { try { PreparedStatement ps = conn.prepareStatement("SELECT * FROM employee WHERE id = ?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { return new Employee(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender")); } } catch (SQLException e) { e.printStackTrace(); } return null; } public List<Employee> getAll() { List<Employee> employees = new ArrayList<>(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM employee"); while (rs.next()) { employees.add(new Employee(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("gender"))); } } catch (SQLException e) { e.printStackTrace(); } return employees; } } ``` 3. 最后,我们可以在 `main` 方法中调用 `EmployeeDao` 的方法来执行相应的操作: ```java public static void main(String[] args) { EmployeeDao dao = new EmployeeDao(); // 添加员工信息 dao.add(new Employee(1, "张三", 20, "男")); // 获取指定 ID 的员工信息 Employee employee = dao.getById(1); System.out.println(employee.getName() + " " + employee.getAge() + " " + employee.getGender()); // 修改员工信息 dao.update(new Employee(1, "李四", 22, "女")); // 删除员工信息 dao.delete(1); // 获取所有的员工信息 List<Employee> employees = dao.getAll(); for (Employee e : employees) { System.out.println(e.getName() + " " + e.getAge() + " " + e.getGender()); } } ``` 以上就是一个简单的基于 JDBC 的员工信息系统的示例,您可以根据自己的需求进行相应的修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值