一、 team.uti包的设计
此包只包含一些工具类TSUtility类。
TSUtility类的设计
此类为一个工具类,用来进行键盘的交互。
package team.uti;
import java.util.Scanner;
public class TSUtility {
private static Scanner sc = new Scanner(System.in);
/**
*
* @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
* @author XiaoQ
* @date 2022年9月6日上午10:08:04
* @return
*/
public static char readMenuSelection() {
char c;
for(; ; ) {
String s = readKeyBorad(1, false);
c = s.charAt(0);
if(c != '1' && c != '2' && c != '3' && c != '4')
System.out.println("输入有误,请重新输入:");
else break;
}
return c;
}
/**
*
* @Description 该方法提示并等待,直到用户按回车键后返回。
* @author XiaoQ
* @date 2022年9月6日上午10:21:55
*/
public static void readReturn() {
System.out.println("按回车继续...");
readKeyBorad(100, true);
}
/**
*
* @Description 从键盘中读取一个不超过2位数的整数,并将其作为返回值
* @author XiaoQ
* @date 2022年9月6日上午10:22:56
* @return
*/
public static int readInt() {
int n;
for(; ; ) {
String s = readKeyBorad(2, false);
try{
n = Integer.parseInt(s);
break;
}catch(NumberFormatException e) {
System.out.println("数字输入有误,请重新输入:");
}
}
return n;
}
/**
*
* @Description 从键盘读取‘Y’或’N’,并将其作为方法的返回值。
* @author XiaoQ
* @date 2022年9月6日上午10:24:56
* @return
*/
public static char readConfirmSelection() {
char c;
for(; ; ) {
String s = readKeyBorad(1, false).toUpperCase();
c = s.charAt(0);
if(c == 'Y' || c == 'N') {
break;
}else {
System.out.println("输入有误,请重新输入:");
}
}
return c;
}
public static String readKeyBorad(int limit, boolean blankReturn) {
String line = "";
while(sc.hasNextLine()) { //检查输入,如果有另一个非空格字符则返回true。
line = sc.nextLine(); //读取一整行,以一个回车符作为结束标记停止扫描
if(line.length() == 0) {
if(blankReturn) return line;
else continue;
}
if(line.length() > limit) {
System.out.println("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
二、team.main包的设计
此包包含一些设备类(如:PC、Printer、NoteBook等)和一些员工类型的类(Employee、Programmer等)
1. Employee类的设计
用来记录员工的一些基本信息
package team.main;
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
super();
}
public Employee(int id, String name, int age, double salary) {
super();
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();
}
}
2. Programmer类的设计(程序员)
package team.main;
import team.service.Status;
public class Programmer extends Employee{
private int memberId; //开发团队中的id
private Status status = Status.FREE;
private Equipment equipment;
public Programmer() {
super();
}
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 getDetails() + "\t程序员\t" + status + "\t\t\t" + equipment.getDescription();
}
public String getDetailsForTeam() {
return memberId + "/" + this.getId() + "\t" + this.getName() + "\t" + this.getAge() + "\t" + this.getSalary() + "\t程序员";
}
}
3. Designer类的设计(设计师)
package team.main;
public class Designer extends Programmer{
private double bonus;
public Designer() {
super();
}
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" + this.getStatus() + "\t" + bonus + "\t\t" + this.getEquipment().getDescription();
}
public String getDetailsForTeam() {
return this.getMemberId() + "/" + this.getId() + "\t" + this.getName() + "\t" + this.getAge() + "\t" + this.getSalary() + "\t设计师\t" + bonus;
}
}
4. Architect类的设计(架构师)
package team.main;
public class Architect extends Designer{
private int stock;
public Architect() {
super();
}
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" + this.getStatus() + "\t" + this.getBonus() + "\t" + stock + "\t" + this.getEquipment().getDescription();
}
public String getDetailsForTeam() {
return this.getMemberId() + "/" + this.getId() + "\t" + this.getName() + "\t" + this.getAge() + "\t" + this.getSalary() + "\t架构师\t" + this.getBonus() + "\t" + stock;
}
}
5. Equipment接口的设计(设备)
package team.main;
public interface Equipment {
String getDescription();
}
6. PC类的设计(电脑)
package team.main;
public class PC implements Equipment{
private String model;
private String display; //机器的名称
public PC() {
super();
}
public PC(String model, String display) {
super();
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;
}
@Override
public String getDescription() {
return model + "(" + display + ")";
}
}
7. Printer类的设计(打印机)
package team.main;
public class Printer implements Equipment{
private String name; //机器型号
private String type; //机器的类型
public Printer() {
super();
}
public Printer(String name, String type) {
super();
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;
}
@Override
public String getDescription() {
return name + "(" + type + ")";
}
}
8. NoteBook类的设计(笔记本电脑)
package team.main;
public class NoteBook implements Equipment{
private String model; //机器的型号
private double price;
public NoteBook() {
super();
}
public NoteBook(String model, double price) {
super();
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;
}
@Override
public String getDescription() {
return model + "(" + price + ")";
}
}
三、team.service包的设计
1. Data类的设计
用来存储开发人员的相关信息
package 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"}
};
}
2. Status类的设计
package team.service;
/**
*
* @Description 表示员工的状态
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月6日下午1:57:00
*
*/
public class Status {
private final String NAME;
private Status(String NAME) {
this.NAME = NAME;
}
public static final Status FREE = new Status("FREE");
public static final Status BUSY = new Status("BUSY");
public static final Status VACATION = new Status("VACATION");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
3. TeamException类的设计
package team.service;
/**
*
* @Description 自定义异常类
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月12日上午9:45:02
*
*/
public class TeamException extends Exception{
static final long serialVersionUID = -33875164229948L;
public TeamException() {
super();
}
public TeamException(String msg) {
super(msg);
}
}
4. NameListService类的设计
package team.service;
/**
*
* @Description 读取Data中的数据并保存
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月6日下午1:57:00
*
*/
import team.main.Architect;
import team.main.Designer;
import team.main.Employee;
import team.main.Equipment;
import team.main.NoteBook;
import team.main.PC;
import team.main.Printer;
import team.main.Programmer;
import static team.service.Data.*;
public class NameListService {
private Employee[] employees;
public NameListService() {
employees = new Employee[EMPLOYEES.length];
for(int i = 0; i < employees.length; i++) {
int t = Integer.parseInt(EMPLOYEES[i][0]); //获取员工的类型
//获取Employess的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 bouns;
int stock;
switch(t) {
case EMPLOYEE:
employees[i] = new Employee(id, name, age, salary);
break;
case PROGRAMMER:
equipment = creatEquipment(i);
employees[i] = new Programmer(id, name, age, salary, equipment);
break;
case DESIGNER:
equipment = creatEquipment(i);
bouns = Double.parseDouble(EMPLOYEES[i][5]);
employees[i] = new Designer(id, name, age, salary, equipment, bouns);
break;
case ARCHITECT:
equipment = creatEquipment(i);
bouns = Double.parseDouble(EMPLOYEES[i][5]);
stock = Integer.parseInt(EMPLOYEES[i][6]);
employees[i] = new Architect(id, name, age, salary, equipment, bouns, stock);
break;
}
}
}
/**
*
* @Description 获取员工的设备信息
* @author XiaoQ
* @date 2022年9月7日上午12:33:25
* @param index
* @return
*/
private Equipment creatEquipment(int index) {
int t = Integer.parseInt(EQUIPMENTS[index][0]); //获取设备类型
// System.out.println(t);
String modelOrname = EQUIPMENTS[index][1];
switch(t) {
case PC:
String display = EQUIPMENTS[index][2];
return new PC(modelOrname, display);
case NOTEBOOK:
double price = Double.parseDouble(EQUIPMENTS[index][2]);
return new NoteBook(modelOrname, price);
case PRINTER:
String type = EQUIPMENTS[index][2];
return new Printer(modelOrname, type);
}
return null;
}
/**
*
* @Description 获取当前所有员工
* @author XiaoQ
* @date 2022年9月12日上午9:36:37
* @return
*/
public Employee[] getAllEmployees() {
return employees;
}
/**
*
* @Description 获取指定ID的员工
* @author XiaoQ
* @date 2022年9月12日上午9:39:11
* @param id
* @return
* @throws TeamException
*/
public Employee getEmployee(int id) throws TeamException {
for(int i = 0; i < employees.length; i++) {
if(id == employees[i].getId()) {
return employees[i];
}
}
throw new TeamException("找不到指定员工");
}
}
5. TeamService类的设计
package team.service;
import team.main.Architect;
import team.main.Designer;
import team.main.Employee;
import team.main.Programmer;
/**
*
* @Description 关于开发团队成员管理:添加、删除等
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月13日上午11:14:19
*
*/
public class TeamService {
private static int counter = 1; //计数器,给memberId赋值用
private final int MAX_MEMBER = 5; //限制开发团队的人数
private Programmer[] team = new Programmer[MAX_MEMBER]; //保存当前团队中个成员的对象
private int total = 0; //记录开发团队中的实际人数
/**
*
* @Description 获取开发团队中的成员
* @author XiaoQ
* @date 2022年9月13日上午11:24:05
* @return
*/
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for(int i = 0; i < total; i++) {
team[i] = this.team[i];
}
return team;
}
/**
*
* @Description 添加团队成员
* @author XiaoQ
* @date 2022年9月13日上午11:17:09
* @param e
* @throws TeamException
*/
public void addMember(Employee e) throws TeamException {
int numOfArc = 0, numOfDes = 0, numOfPro = 0;
for(int i = 0; i < total; i++) {
if(team[i] instanceof Architect) {
numOfArc++;
}else if(team[i] instanceof Designer) {
numOfDes++;
}else {
numOfPro++;
}
if(team[i].getId() == e.getId()) {
throw new TeamException("该成员已在本开发团队中");
}
}
if(total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
if(!(e instanceof Programmer)) {
throw new TeamException("该成员不是开发人员,无法添加");
}
Programmer p = (Programmer) e;
if(p.getStatus() == Status.BUSY) {
throw new TeamException("该成员已是某团队成员");
}else if(p.getStatus() == Status.VACATION) {
throw new TeamException("该成员正在休假,无法添加");
}
// 错误的写法
// if((p instanceof Architect) && numOfArc >= 1) {
// throw new TeamException("团队中至多只能有一名架构师");
// }else if((p instanceof Designer) && numOfDes >= 2) {
// throw new TeamException("团队中至多只能有两名设计师");
// }else if((p instanceof Programmer) && numOfPro >= 3) {
// throw new TeamException("团队中至多只能有三名程序员");
// }
if(p instanceof Architect) {
if(numOfArc >= 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++] = p;
p.setMemberId(counter++);
p.setStatus(Status.BUSY);
}
/**
*
* @Description 删除团队成员
* @author XiaoQ
* @date 2022年9月13日上午11:17:27
* @param memberId
* @throws TeamException
*/
public void removeMember(int memberId) throws TeamException{
for(int i = 0; i < total; i++) {
team[i].setStatus(Status.FREE);
if(team[i].getMemberId() == memberId) {
for(int j = i; j < total - 1; j++) {
team[j] = team[j + 1];
}
team[--total] = null;
return ;
}
}
throw new TeamException("找不到该成员,无法删除");
}
}
四、team.view包的设计
TeamView类的设计
package team.view;
import team.main.Employee;
import team.main.Programmer;
import team.service.NameListService;
import team.service.TeamException;
import team.service.TeamService;
import team.uti.TSUtility;
/**
*
* @Description 用户交互界面
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月14日下午9:35:39
*
*/
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public void enterMainMenu() {
boolean loopFlag = true;
char menu;
while(loopFlag) {
System.out.println("1-团队列表 2-添加团队成员 3-删除成员 4-退出 请选择(1-4):");
menu = TSUtility.readMenuSelection();
if(menu != '1') {
listAllEmployees();
}
switch(menu) {
case '1':
getTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.println("是否要退出(Y/N):");
char isExit = TSUtility.readConfirmSelection();
if(isExit == 'Y') loopFlag = false;
break;
}
}
}
/**
*
* @Description 显示所有员工信息
* @author XiaoQ
* @date 2022年9月13日下午9:27:39
*/
private void listAllEmployees() {
System.out.println("--------------------------开发团队调度软件--------------------------");
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
Employee[] employees = listSvc.getAllEmployees();
for(int i = 0; i < employees.length; i++) {
System.out.println(employees[i]);
}
System.out.println("------------------------------------------------------------------");
}
/**
*
* @Description 显示成员信息
* @author XiaoQ
* @date 2022年9月13日下午10:15:31
*/
private void getTeam() {
System.out.println("----------------------团队成员列表----------------------");
Programmer[] team = teamSvc.getTeam();
if(team.length == 0 || team == null) {
System.out.println("开发团队目前没有成员");
}else {
System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
for(int i = 0; i < team.length; i++) {
System.out.println(team[i].getDetailsForTeam());
}
}
System.out.println("------------------------------------------------------");
}
/**
*
* @Description 添加成员
* @author XiaoQ
* @date 2022年9月13日下午10:45:52
*/
private void addMember() {
System.out.println("----------------------添加成员----------------------");
System.out.println("请输入要添加成员的ID:");
int id = TSUtility.readInt();
try {
Employee emp = listSvc.getEmployee(id);
teamSvc.addMember(emp);
System.out.println("添加成功");
} catch (TeamException e) {
System.out.println("添加失败,原因:" + e.getMessage());
}
TSUtility.readReturn();
System.out.println("----------------------------------------------------");
}
private void deleteMember() {
System.out.println("----------------------删除成员----------------------");
System.out.println("请输入删除成员的TID:");
int memberId = TSUtility.readInt();
System.out.println("确认是否删除(Y/N):");
char isDelete = TSUtility.readConfirmSelection();
if(isDelete == 'N') {
return ;
}
try {
teamSvc.removeMember(memberId);
System.out.println("删除成功");
} catch (TeamException e) {
System.out.println("删除失败,原因:" + e.getMessage());
}
TSUtility.readReturn();
System.out.println("---------------------------------------------------");
}
public static void main(String[] args) {
TeamView view = new TeamView();
view.enterMainMenu();
}
}
五、team.junit包的设计
NameListServiceTest类的设计
package team.junit;
import org.junit.Test;
import team.main.Employee;
import team.service.Data;
import team.service.NameListService;
import team.service.TeamException;
/**
*
* @Description 对NameListService的测试
* @author XiaoQ Email:1954247937@qq.com
* @version
* @date 2022年9月12日上午11:52:35
*
*/
public class NameListServiceTest {
@Test
public void testGetAllEmployees() {
NameListService t = new NameListService();
Employee[] employees = t.getAllEmployees();
for(int i = 0; i < employees.length; i++) {
System.out.println(employees[i]);
}
}
@Test
public void testGetEmployee() {
NameListService t = new NameListService();
try {
Employee employee = t.getEmployee(10);
System.out.println(employee);
} catch (TeamException e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
总结
该项目总的来所不是很难 ,但是这个设计和架构还是挺让人头痛的!!!