描述说明
基于课本题目说明,实现一个简单的员工信息管理程序,可以实现基本的增删改查功能。
原题
(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)每个类都完全封装,不允许非私有化属性
加强版
核心主要使用了泛型,以及上下转型的运用。
在数据存储上采用了Set接口的TreeSet实现类,内部采用二叉树存储,可以保证集合中没有重复元素,并且可以对元素进行排序。
代码实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
abstract class Employee implements Comparable {
private String name; //定义姓名name并私有化属性
private int month; //定义生日月份month并私有化属性
private int employeeNo;
private String telephone; //定义员工电话号码
public Employee() {
} //无参构造器
public Employee(int employeeNo, String name, int month, String telephone) { //有参构造方法
this.employeeNo = employeeNo; //给属性employeeNo初始化赋值
this.name = name; //给属性name初始化赋值
this.month = month; //给属性month初始化赋值、
this.telephone = telephone; //给属性telephone初始化赋值
}
//获取属性telephone的方法
public String getTelephone() {
return telephone;
}
//获取属性name的方法
public String getName() {
return name; //返回name属性
}
//获取属性month的方法
public int getMonth() {
return month; //返回month属性
}
public int getEmployeeNo() {
return employeeNo;
}
//给属性name赋初始值
public void setName(String name) {
this.name = name; //本类中的属性name
}
//给属性month赋初始值
public void setMonth(int month) {
this.month = month; //本类中的属性month
}
//给属性telephone赋初始值
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public void setEmployeeNo(int employeeNo) {
this.employeeNo = employeeNo;
}
//创建一个方法getSalary()用来计算工资,参数month是月份,如果当月是员工生日,奖 励100元
public double getSalary(int month) {
double salary = 0; //定义工资变量
//判断当前月份是否是员 工的生日月份,如果是奖励100元
if (this.month == month) {
salary = salary + 100;
return salary; //返回工资salary
} else {
return salary;
}
}
@Override
public int compareTo(Object obj) {
Employee e = (Employee) obj;
if (this.employeeNo - e.getEmployeeNo() > 0) {
return 1;
}
if (this.employeeNo - e.getEmployeeNo() == 0) {
return this.name.compareTo(e.getName());
}
return -1;
}
}
interface EmployeeInfOperation {
int getCounts(); // 获取记录个数
void add(Employee employee); // 添加一个员工信息记录
void listAll(); // 遍历所有员工信息记录
void search(int employeeNo); // 根据员工编号查询记录
void search(String name); // 根据员工姓名查询记录
void delete(int employeeNo); // 根据员工编号删除记录
}
class EmployeeInf implements EmployeeInfOperation {
TreeSet<Employee> employees = new TreeSet<>();
Date date = new Date();
@Override
public int getCounts() {
return employees.size();
}
@Override
public void add(Employee employee) {
employees.add(employee);
System.out.println("添加成功!");
}
@Override
public void listAll() {
if (employees.size() == 0) {
System.out.println("没有记录!");
} else {
for (Iterator it = employees.iterator(); it.hasNext(); ) {
Employee e = (Employee) it.next();
System.out.println("编号:" + e.getEmployeeNo() + ",姓名:" + e.getName() + ",电话:" + e.getTelephone() + ",工资:" + e.getSalary(date.getMonth() + 1));
}
}
}
@Override
public void search(int employeeNo) {
int flag = 0;
for (Iterator it = employees.iterator(); it.hasNext(); ) {
Employee e = (Employee) it.next();
if (e.getEmployeeNo() == employeeNo) {
flag = 1;
System.out.println("编号:" + e.getEmployeeNo() + ",姓名:" + e.getName() + ",电话:" + e.getTelephone() + ",工资:" + e.getSalary(date.getMonth() + 1));
break;
}
}
if (flag == 0)
System.out.println("输入的编号无效!");
}
@Override
public void search(String name) {
int flag = 0;
for (Iterator it = employees.iterator(); it.hasNext(); ) {
Employee e = (Employee) it.next();
if (e.getName().equals(name)) {
flag = 1;
System.out.println("编号:" + e.getEmployeeNo() + ",姓名:" + e.getName() + ",电话:" + e.getTelephone() + ",工资:" + e.getSalary(date.getMonth() + 1));
break;
}
}
if (flag == 0)
System.out.println("查无此人!");
}
@Override
public void delete(int employeeNo) {
int flag = 0;
for (Iterator it = employees.iterator(); it.hasNext(); ) {
Employee e = (Employee) it.next();
if (e.getEmployeeNo() == employeeNo) {
flag = 1;
employees.remove(e);
System.out.println("删除成功!");
break;
}
}
if (flag == 0)
System.out.println("查无此人!");
}
}
class SalariedEmployee extends Employee {
private double monthSalary; //封装monthSalary属性
public SalariedEmployee() {
} //无参构造方法
//有参构造方法 参数 姓名 生日月份 月薪
public SalariedEmployee(int employeeNo, String name, int month, String telephone, double monthSalary) {
super(employeeNo, name, month, telephone); //调用父类有参构造方法
this.monthSalary = monthSalary; //为属性monthSalary初始化赋值
}
//获取monthSalary的值
public double getMonthSalary() {
return monthSalary;
}
//给monthSalary赋值
public void setMonthSalary(double monthSalary) {
this.monthSalary = monthSalary;
}
//覆盖父类中的方法
public double getSalary(int month) {
double salary = monthSalary + super.getSalary(month); //定义工资变量
return salary;
}
}
class HourlyEmployee extends Employee {
private double hourlySalary; //定义属性hourlySalary每小时的工资
private int hours; //定义属性hours每月工作的小时数
public HourlyEmployee() {
} //无参构造方法
//有参构造方法 参数 姓名 生日月份 每小时的工资 每月工作的小时数
public HourlyEmployee(int employeeNo, String name, int month, String telephone, double hourlySalary, int hours) {
super(employeeNo, name, month, telephone); //调用父类有参构造方法
this.hourlySalary = hourlySalary; //为属性hourlySalary初始化赋值
this.hours = hours; //为属性hours 初始化赋值
}
public double getHourlySalary() { //获取hourlySalary的值
return hourlySalary;
}
public int getHours() { //获取hours的值
return hours;
}
//定义set方法设置hourlySalary hours的值
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public void setHourly(int hours) {
this.hours = hours;
}
//覆盖父类方法
public double getSalary(int month) {
if (hours < 0) { //如果工作小时数小于0 输出数据错误
System.out.println("数据错误");
return 0;
}
//小于160个小时的 按照每个月的工作小时数乘以每小时的工资
else if (hours <= 160)
return hourlySalary * hours + super.getSalary(month);
//超出160个小时的小时数 按照1.5倍计算
else return hourlySalary * 160 + hourlySalary * 1.5 * (hours - 160) + super.getSalary(month);
}
}
class SalesEmployee extends Employee {
private double sales; //定义销售额sales
private double rate; //定义提成率rate
public SalesEmployee() {
}
public SalesEmployee(int employeeNo, String name, int month, String telephone, double sales, double rate) {
super(employeeNo, name, month, telephone);
this.sales = sales;
this.rate = rate;
}
public double getSales() {
return sales;
}
public double getRate() {
return rate;
}
public void setSales(double sales) {
this.sales = sales;
}
public void setRate(double rate) {
this.rate = rate;
}
public double getSalary(int month) {
return this.getSales() * (1 + this.getRate()) + super.getSalary(month);
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary; //定义基础工资baseSalary
//无参构造方法
public BasePlusSalesEmployee() {
}
//有参构造方法
public BasePlusSalesEmployee(int employeeNo, String name, int month, String telephone, double sales, double rate, double baseSalary) {
super(employeeNo, name, month, telephone, sales, rate);
this.baseSalary = baseSalary;
}
//get/set方法对私有属性的调用和设置
public double gatBaseSalary() {
return baseSalary;
}
public void setBaseSalary() {
this.baseSalary = baseSalary;
}
public double getSalary(int month) {
return baseSalary + super.getSalary(month);
}
}
//定义一个测试类
public class Test {
public static void main(String[] args) throws IOException {
EmployeeInf employeeInf = new EmployeeInf();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("********员工信息管理系统**********\n");
System.out.print("1 增加记录\n");
System.out.print("2 显示所有员工信息\n");
System.out.print("3 根据员工编号查找记录\n");
System.out.print("4 根据员工姓名查找记录\n");
System.out.print("5 获取员工信息记录个数\n");
System.out.print("6 根据员工编号删除记录\n");
System.out.print("0 退出\n\n");
System.out.print("请输入你的选择:");
int choice = Integer.parseInt(br.readLine());
switch (choice) {
case 0:
System.exit(0);
case 1:
System.out.print("********员工类型**********\n");
System.out.print("1 拿固定工资\n");
System.out.print("2 按小时拿工资\n");
System.out.print("3 工资由月销售额和提成率决定\n");
System.out.print("4 有固定底薪的销售人员\n");
System.out.print("请输入员工类型:");
int typeChoice = Integer.parseInt(br.readLine());
switch (typeChoice) {
case 1:
System.out.print("\n请输入员工编号:");
int EmployNum = Integer.parseInt(br.readLine());
System.out.print("请输入姓名:");
String EmployName = br.readLine();
System.out.print("请输入生日月份:");
int EmployMonth = Integer.parseInt(br.readLine());
System.out.print("请输入电话号码:");
String EmployTelephone = br.readLine();
System.out.print("请输入月薪:");
double monthSalary = Double.parseDouble(br.readLine());
SalariedEmployee employee = new SalariedEmployee(EmployNum, EmployName,
EmployMonth, EmployTelephone, monthSalary);
employeeInf.add(employee);
System.out.println();
break;
case 2:
System.out.print("\n请输入员工编号:");
int EmployNum2 = Integer.parseInt(br.readLine());
System.out.print("请输入姓名:");
String EmployName2 = br.readLine();
System.out.print("请输入生日月份:");
int EmployMonth2 = Integer.parseInt(br.readLine());
System.out.print("请输入电话号码:");
String EmployTelephone2 = br.readLine();
System.out.print("请输入每小时的工资:");
double hourlySalary = Double.parseDouble(br.readLine());
System.out.print("请输入每月工作的小时数:");
int hours = Integer.parseInt(br.readLine());
HourlyEmployee employee2 = new HourlyEmployee(EmployNum2, EmployName2,
EmployMonth2, EmployTelephone2, hourlySalary, hours);
employeeInf.add(employee2);
System.out.println();
break;
case 3:
System.out.print("\n请输入员工编号:");
int EmployNum3 = Integer.parseInt(br.readLine());
System.out.print("请输入姓名:");
String EmployName3 = br.readLine();
System.out.print("请输入生日月份:");
int EmployMonth3 = Integer.parseInt(br.readLine());
System.out.print("请输入电话号码:");
String EmployTelephone3 = br.readLine();
System.out.print("请输入月销售额:");
double sales = Double.parseDouble(br.readLine());
System.out.print("请输入提成率:");
double rate = Double.parseDouble(br.readLine());
SalesEmployee employee3 = new SalesEmployee(EmployNum3, EmployName3,
EmployMonth3, EmployTelephone3, sales, rate);
employeeInf.add(employee3);
System.out.println();
break;
case 4:
System.out.print("\n请输入员工编号:");
int EmployNum4 = Integer.parseInt(br.readLine());
System.out.print("请输入姓名:");
String EmployName4 = br.readLine();
System.out.print("请输入生日月份:");
int EmployMonth4 = Integer.parseInt(br.readLine());
System.out.print("请输入电话号码:");
String EmployTelephone4 = br.readLine();
System.out.print("请输入月销售额:");
double sales4 = Double.parseDouble(br.readLine());
System.out.print("请输入提成率:");
double rate4 = Double.parseDouble(br.readLine());
System.out.print("请输入底薪:");
double baseSalary = Double.parseDouble(br.readLine());
BasePlusSalesEmployee employee4 = new BasePlusSalesEmployee(EmployNum4, EmployName4,
EmployMonth4, EmployTelephone4, sales4, rate4, baseSalary);
employeeInf.add(employee4);
System.out.println();
break;
default:
System.out.println("请输入正确选项!");
System.out.println();
break;
}
break;
case 2:
employeeInf.listAll();
System.out.println();
break;
case 3:
System.out.print("\n请输入员工编号:");
int employeeNo = Integer.parseInt(br.readLine());
employeeInf.search(employeeNo);
System.out.println();
break;
case 4:
System.out.println("请输入要查询记录的员工姓名");
String name1 = br.readLine();
employeeInf.search(name1);
System.out.println();
break;
case 5:
System.out.println("员工信息记录个数为:" + employeeInf.getCounts());
break;
case 6:
System.out.println("请输入要删除记录的员工编号");
int employeeId = Integer.parseInt(br.readLine());
employeeInf.delete(employeeId);
System.out.println();
break;
default:
System.out.println("请输入正确选项!");
System.out.println();
break;
}
}
}
}