该软件实现以下功能:
package com.gaorui.team.service;
public class Data {
public static final int EMPLOYEE = 10;
public static final int PROGRAMMER = 11;
public static final int DESIGNER = 12;
public static final int ARCHITECT = 13;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
//Employee : 10, id, name, age, salary
//Programmer: 11, id, name, age, salary
//Designer : 12, id, name, age, salary, bonus
//Architect : 13, id, name, age, salary, bonus, stock
public static final String[][] EMPLOYEES = {
{"10", "1", "马 云", "22", "3000"},
{"13", "2", "马化腾", "32", "18000", "15000", "2000"},
{"11", "3", "李彦宏", "23", "7000"},
{"11", "4", "刘强东", "24", "7300"},
{"12", "5", "雷 军", "28", "10000", "5000"},
{"11", "6", "任志强", "22", "6800"},
{"12", "7", "柳传志", "29", "10800","5200"},
{"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
{"12", "9", "史玉柱", "26", "9800", "5500"},
{"11", "10", "丁 磊", "21", "6600"},
{"11", "11", "张朝阳", "25", "7100"},
{"12", "12", "杨致远", "27", "9600", "4800"}
};
//如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, name, type
public static final String[][] EQUIPMENTS = {
{},
{"22", "联想T4", "6000"},
{"21", "戴尔", "NEC17寸"},
{"21", "戴尔", "三星 17寸"},
{"23", "佳能 2900", "激光"},
{"21", "华硕", "三星 17寸"},
{"21", "华硕", "三星 17寸"},
{"23", "爱普生20K", "针式"},
{"22", "惠普m6", "5800"},
{"21", "戴尔", "NEC 17寸"},
{"21", "华硕","三星 17寸"},
{"22", "惠普m6", "5800"}
};
}
package com.gaorui.team.view;
import java.util.*;
/**
*
* @Description 项目中提供了TSUtility.java类,可用来方便地实现键盘访问。
* @author Gaorui
* @version
* @date 2022年3月28日13:10:29
*
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
*
* @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
* @author Gaorui
* @date 2022年3月28日13:10:29
* @return
*/
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
/**
*
* @Description 该方法提示并等待,直到用户按回车键后返回。
* @author Gaorui
* @date 2022年3月28日13:10:29
*/
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
/**
*
* @Description 该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
* @author Gaorui
* @date 2022年3月28日13:10:29
* @return
*/
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
*
* @Description 从键盘读取‘Y’或’N’,并将其作为方法的返回值。
* @author Gaorui
* @date 2022年3月28日13:10:29
* @return
*/
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
package com.gaorui.team.domain;
/**
* @InterfaceName Equipment
* @Description Equipment接口,声明了抽象方法getDescription()
* @Author 86150
* @Date 2022/3/28 13:25
* @Version 1.0
*/
public interface Equipment {
//抽象方法getDescription()
public abstract String getDescription();
}
package com.gaorui.team.domain;
/**
* @ClassName PC
* @Description Equipment接口的实现类
* @Author 86150
* @Date 2022/3/28 13:25
* @Version 1.0
*/
public class PC implements Equipment {
private String model; //机器的型号
private String display; //显示器的名称
public PC() {
}
public PC(String model, String display) {
this.model = model;
this.display = display;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
//实现类重写接口中的抽象方法getDescription()
@Override
public String getDescription() {
return model + "(" + display + ")";
}
}
package com.gaorui.team.domain;
/**
* @ClassName NoteBook
* @Description Equipment接口的实现类
* @Author 86150
* @Date 2022/3/28 13:34
* @Version 1.0
*/
public class NoteBook implements Equipment {
private String model; //机器的型号
private double price; //机器的价格
public NoteBook() {
}
public NoteBook(String model, double price) {
this.model = model;
this.price = price;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
//实现类重写接口中的抽象方法getDescription()
@Override
public String getDescription() {
return model + "(" + price + ")";
}
}
package com.gaorui.team.domain;
/**
* @ClassName Printer
* @Description Equipment接口的实现类
* @Author 86150
* @Date 2022/3/28 13:34
* @Version 1.0
*/
public class Printer implements Equipment {
private String name; //机器的型号
private String type; //机器的类型
public Printer() {
}
public Printer(String name, String type) {
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//实现类重写接口中的抽象方法getDescription()
@Override
public String getDescription() {
return name + "(" + type + ")";
}
}
package com.gaorui.team.domain;
/**
* @ClassName Employee
* @Description TODO
* @Author 86150
* @Date 2022/3/28 14:53
* @Version 1.0
*/
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDetails() {
return id + "\t" + name + "\t" + age + "\t" + salary;
}
@Override
public String toString() {
return getDetails();
}
}
package com.gaorui.team.domain;
import com.gaorui.team.service.Status;
import static com.gaorui.team.service.Status.*;
/**
* @ClassName Programmer
* @Description Employee的子类
* @Author 86150
* @Date 2022/3/28 14:55
* @Version 1.0
*/
public class Programmer extends Employee {
private int memberId; //记录成员加入开发团队后在团队中的ID
private Status status = FREE; //表示成员的状态
private Equipment equipment; //表示成员领用的设备。接口不能实例化对象,故该引用类型变量指向的是该接口的实现类(多态)
public Programmer() {
}
public Programmer(int id, String name, int age, double salary, Equipment equipment) {
super(id, name, age, salary);
this.equipment = equipment;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
@Override
public String toString() {
return super.toString() + "\t程序员\t" + status.getNAME() + "\t\t\t\t\t" + equipment.getDescription();
}
}
package com.gaorui.team.domain;
/**
* @ClassName Designer
* @Description Programmer的子类
* @Author 86150
* @Date 2022/3/28 15:42
* @Version 1.0
*/
public class Designer extends Programmer {
private double bonus; //奖金
public Designer() {
}
public Designer(int id, String name, int age, double salary, Equipment equipment, double bonus) {
super(id, name, age, salary, equipment);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return getDetails() + "\t设计师\t" + getStatus().getNAME() + "\t" + bonus + "\t\t\t" + getEquipment().getDescription();
}
}
package com.gaorui.team.domain;
/**
* @ClassName Architect
* @Description Designer的子类
* @Author 86150
* @Date 2022/3/28 15:45
* @Version 1.0
*/
public class Architect extends Designer{
private int stock; //公司奖励的股票数量
public Architect() {
}
public Architect(int id, String name, int age, double salary, Equipment equipment, double bonus, int stock) {
super(id, name, age, salary, equipment, bonus);
this.stock = stock;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return getDetails() + "\t架构师\t" + getStatus().getNAME() + "\t" + getBonus() + "\t" + stock + "\t" + getEquipment().getDescription();
}
}
package com.gaorui.team.service;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
/**
* @ClassName Status
* @Description 枚举类Status是项目service包下自定义的类,在类内声明三个Status类型的对象属性,
* 分别表示成员的状态:① FREE-空闲;② BUSY-已加入开发团队;③ VACATION-正在休假
* @Author 86150
* @Date 2022/3/28 14:56
* @Version 1.0
*/
public class Status {
//私有常量属性,表示成员的状态
private final String NAME;
//枚举类,该类只能有三个对象,因此需要将构造器私有化
private Status(String NAME) {
this.NAME = NAME;
}
//声明三个对象属性:全局常量(public static final)
public static final Status FREE = new Status("FREE"); //FREE-空闲
public static final Status BUSY = new Status("BUSY"); //BUSY-已加入开发团队
public static final Status VACATION = new Status("VACATION"); //VACATION-正在休假
//提供NAME属性的get方法,供该类的对象属性调用
public String getNAME() {
return NAME;
}
//也可以直接重写toString方法,
//直接System.out.println(),自动执行重写后的toString方法,返回对象的属性NAME
@Override
public String toString() {
return NAME;
}
}
package com.gaorui.team.service;
import com.gaorui.team.domain.*;
//导入Data类的所有静态结构,在此文件下调用Data类的静态结构无需再在前面加Data.
import static com.gaorui.team.service.Data.*;
/**
* @ClassName NameListService
* @Description 负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法
* @Author 86150
* @Date 2022/3/28 16:19
* @Version 1.0
*/
public class NameListService {
private Employee[] employees; //employees对象数组用来保存公司所有员工对象
/*
NameListService()构造器:对employees对象数组及其数组元素进行初始化
*/
public NameListService() {
//1. 对象数组的初始化:根据Data类的静态二维数组EMPLOYEES的形状构建相应大小的employees数组
employees = new Employee[EMPLOYEES.length]; //二维数组.length 返回的是外层数组的长度
//2. 根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Equipment实现类的对象,并将对象存在数组中
for (int i = 0; i < EMPLOYEES.length; i++) {
//确定员工的类型,并将字符串转换成整型
int type = Integer.parseInt(EMPLOYEES[i][0]);
//所有类型的公共属性:Employee类的4个基本属性值
int id = Integer.parseInt(EMPLOYEES[i][1]);
String name = EMPLOYEES[i][2];
int age = Integer.parseInt(EMPLOYEES[i][3]);
double salary = Double.parseDouble(EMPLOYEES[i][4]);
//非公共属性
Equipment equipment;
double bonus;
int stock;
switch (type) {
case EMPLOYEE: //10
employees[i] = new Employee(id, name, age, salary);
break;
case PROGRAMMER: //11
equipment = creatEquipment(i);
employees[i] = new Programmer(id, name, age, salary, equipment);
break;
case DESIGNER: //12
equipment = creatEquipment(i);
bonus = Double.parseDouble(EMPLOYEES[i][5]);
employees[i] = new Designer(id, name, age, salary, equipment, bonus);
break;
case ARCHITECT: //13
equipment = creatEquipment(i);
bonus = Double.parseDouble(EMPLOYEES[i][5]);
stock = Integer.parseInt(EMPLOYEES[i][6]);
employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
break;
}
}
}
/*
creatEquipment(int index)方法:获取employees对象数组指定索引的员工的设备
*/
private Equipment creatEquipment(int index) {
int type = Integer.parseInt(EQUIPMENTS[index][0]);
switch (type) {
case PC: //21
return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
case NOTEBOOK: //22
return new NoteBook(EQUIPMENTS[index][1], Double.parseDouble(EQUIPMENTS[index][2]));
case PRINTER: //23
return new Printer(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
}
return null;
}
/*
getAllEmployees()方法:获取当前所有员工
返回:包含所有员工对象的数组
*/
public Employee[] getAllEmployees() {
return employees;
}
/*
getEmployee(int id)方法:获取指定ID的员工对象
参数:指定员工的ID
返回:对象数组中指定id的Employee(员工)对象
异常:找不到指定的员工
*/
public Employee getEmployee(int id) throws TeamException {
for (int i = 0; i < employees.length; i++) {
if (employees[i].getId() == id) {
return employees[i];
}
}
throw new TeamException("找不到指定的员工");
}
}
package com.gaorui.team.service;
/**
* @ClassName TeamException
* @Description 自定义异常类
* @Author 86150
* @Date 2022/3/28 21:05
* @Version 1.0
*/
//1.自定义的异常类需要继承Java现有的异常类,一般为:RuntimeException 或 Exception
public class TeamException extends Exception {
//2.提供全局常量(static final):serialVersionUID
static final long serialVersionUID = -703745766939L;
//3.提供重载的构造器
public TeamException() {
}
public TeamException(String message) {
super(message);
}
}
package com.gaorui.team.service;
import com.gaorui.team.domain.*;
/**
* @ClassName TeamService
* @Description 关于开发团队成员的管理:添加、删除等
* @Author 86150
* @Date 2022/3/29 1:28
* @Version 1.0
*/
public class TeamService {
//counter为静态变量,用来为开发团队新增成员自动生成团队中的唯一ID,即memberId(提示:应使用增1的方式)
private static int counter = 1;
//表示开发团队最大成员数
private final int MAX_MEMBER = 5;
//对象数组:用来保存当前团队中的各成员对象
private Programmer[] team = new Programmer[MAX_MEMBER];
//记录团队成员的实际人数
private int total = 0;
//构造器
public TeamService() {
}
/*
getTeam()方法:返回当前团队的所有对象
返回:包含所有成员对象的数组,数组大小与当前团队总的成员人数一致
*/
public Programmer[] getTeam() {
Programmer[] currentTeam = new Programmer[total];
for (int i = 0; i < total; i++) {
currentTeam[i] = team[i];
}
return currentTeam;
}
/*
addMember(e: Employee)方法:向团队中添加成员
参数:待添加成员的对象
异常:添加失败,TeamException中包含了失败原因,包含以下几种:
1. 成员已满,无法添加
2. 该成员不是开发人员,无法添加
3. 该员工已在本开发团队中
4. 该员工已是某团队成员
5. 该员正在休假,无法添加
6. 团队中至多只能有一名架构师
7. 团队中至多只能有两名设计师
8. 团队中至多只能有三名程序员
注意:一般先把可能出现的异常情况写在前
*/
public void addMember(Employee e) throws TeamException {
if (total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
if (!(e instanceof Programmer)) {
throw new TeamException("该成员不是开发人员,无法添加");
}
if (isExist(e)) {
throw new TeamException("该员工已在本开发团队中");
}
//getStatus()是Programmer类的方法,这里需要向下转型(强转)才能调用子类中的方法
Programmer p = (Programmer) e;
//两个String类型变量使用equals比较值时,通常将确定的字符串写在前面,避免空指针异常
if ("BUSY".equals(p.getStatus().getNAME())) {
throw new TeamException("该员工已是某团队成员");
}
if ("VACATION".equals(p.getStatus().getNAME())) {
throw new TeamException("该员正在休假,无法添加");
}
//获取team已有成员中架构师、设计师、程序员的人数
int numOfArch = 0, numOfDes = 0, numOfPro = 0;
for (int i = 0; i < total; i++) {
if (team[i] instanceof Architect) {
numOfArch++;
} else if (team[i] instanceof Designer) {
numOfDes++;
} else if (team[i] instanceof Programmer) {
numOfPro++;
}
}
//再判断待加入成员是什么类型
if (p instanceof Architect) {
if (numOfArch >=1) {
throw new TeamException("团队中至多只能有一名架构师");
}
} else if (p instanceof Designer) {
if (numOfDes >= 2) {
throw new TeamException("团队中至多只能有两名设计师");
}
} else if (p instanceof Programmer) {
if (numOfPro >= 3) {
throw new TeamException("团队中至多只能有三名程序员");
}
}
//若以上异常情况均排除,则将待添加对象添加到team数组中,total加1
team[total++] = p;
//更改待添加对象的status属性
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
/*
判断指定的员工是否已经存在于现有开发团队中
*/
private boolean isExist(Employee e) {
for (int i = 0; i < total; i++) {
if (team[i].getId() == e.getId()) {
return true;
}
}
return false;
}
/*
removeMember(memberId: int)方法:从团队中删除成员
参数:待删除成员的memberId
异常:找不到指定memberId的员工,删除失败
*/
public void removeMember(int memberId) throws TeamException {
for (int i = 0; i < total; i++) {
if (team[i].getMemberId() == memberId) {
//将该员工的status置为FREE
team[i].setStatus(Status.FREE);
//删除元素,后面的元素覆盖前面的元素
for (int j = i; j < total - 1; j++) {
team[j] = team[j + 1];
}
//最后一个元素置成null
team[total - 1] = null;
//当前团队成员数减1
total--;
return;
}
}
throw new TeamException("找不到指定memberId的员工,删除失败");
}
}
package com.gaorui.team.view;
import com.gaorui.team.domain.*;
import com.gaorui.team.service.*;
/**
* @ClassName TeamView
* @Description UI界面
* @Author 86150
* @Date 2022/3/29 15:40
* @Version 1.0
*/
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
/*
enterMainMenu()方法:主界面显示及控制方法
*/
public void enterMainMenu() {
char menu = '0';
while (true) {
if (menu != '1') {
listAllEmployees();
}
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
menu = TSUtility.readMenuSelection();
switch (menu) {
case '1':
getTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.print("是否确认退出(Y/N):");
char isExit = TSUtility.readConfirmSelection();
if (isExit == 'Y') {
return;
}
}
}
}
/*
listAllEmployees()方法:以表格形式列出公司所有成员
*/
private void listAllEmployees() {
System.out.println("----------------------- 开发团队调度软件 -------------------------------\n");
Employee[] employees = listSvc.getAllEmployees();
if (employees == null || employees.length == 0) {
System.out.println("公司没有任何员工信息!");
} else {
System.out.println("ID\t姓名\t\t年龄\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
for (int i = 0; i < employees.length; i++) {
System.out.println(employees[i]);
}
}
System.out.println("---------------------------------------------------------------------");
}
/*
getTeam()方法:显示团队成员列表操作
*/
private void getTeam() {
System.out.println("-------------------------- 团队成员列表 -------------------------------\n");
Programmer[] team = teamSvc.getTeam();
if (team == null || team.length == 0) {
System.out.println("开发团队目前没有成员!");
} else {
System.out.println("TID/ID\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票");
for (int i = 0; i < team.length; i++) {
System.out.println(team[i].getDetailsForTeam());
}
}
System.out.println("---------------------------------------------------------------------");
}
/*
addMember()方法:实现添加成员操作
*/
private void addMember() {
System.out.println("-------------------------- 添加成员 -------------------------------\n");
System.out.print("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try {
Employee employee = listSvc.getEmployee(id);
teamSvc.addMember(employee);
System.out.println("添加成功!");
} catch (TeamException e) {
System.out.println("添加失败,原因:"+ e.getMessage());
}
TSUtility.readReturn();
}
/*
deleteMember()方法:实现删除成员操作
*/
private void deleteMember() {
System.out.println("-------------------------- 删除成员 -------------------------------\n");
System.out.print("请输入要添加的员工TID:");
int memberId = TSUtility.readInt();
System.out.print("是否确认删除(Y/N):");
char isDelete = TSUtility.readConfirmSelection();
if (isDelete == 'Y') {
try {
teamSvc.removeMember(memberId);
System.out.println("删除成功!");
} catch (TeamException e) {
System.out.println("删除失败,原因:" + e.getMessage());
}
TSUtility.readReturn();
}
}
public static void main(String[] args) {
TeamView view = new TeamView();
view.enterMainMenu();
}
}
System.out.println("添加成功!");
} catch (TeamException e) {
System.out.println("添加失败,原因:"+ e.getMessage());
}
TSUtility.readReturn();
}