Java初学——项目三之开发团队调度软件
程序员文章站
2022-06-11 21:35:41
...
三、目标
二、需求说明
三、软件结构设计
四、具体实现
1. 创建项目基本组件
(1)键盘访问的实现
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
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;
}
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
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;
}
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;
}
}
(2)所需数据的实现
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"}
};
//如下的EQIPMENTS数组与上面的EMPLOYEES数组元素一一对应
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, type, name
public static final String[][] EQIPMENTS = {
{},
{"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"}
};
}
(3)Equipment及其子类的实现
//Equipment接口很简单,只定义了一个抽象方法,其实现类也很简单,在此只列出PC的代码
public interface Equipment {
public abstract String getDescription();
}
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+")";
}
}
(4)Employee类及其子类的实现
//employee类及其子类实现比较简单,这了只给出Status类用于封装员工的状态
public class Status {
private final String NAME;
private Status(String nAME) {
NAME = nAME;
}
public static final Status FREE=new Status("FREE");
public static final Status VOCATION=new Status("VOCATION");
public static final Status BUSY=new Status("BUSY");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
2. 实现service包中的类
(1)NameListService类的设计
/*
* 构造器:
* 根据项目提供的Data类构建相应大小的employees数组
* 再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,
* 以及相关联的Equipment子类的对象. 将对象存于数组中 Data类位于com.atguigu.team.service包中
*/
public NameListService() {
employees=new Employee[EMPLOYEES.length];
for(int i=0;i<employees.length;i++) {
int type=Integer.parseInt(EMPLOYEES[i][0]);
//
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:
employees[i]=new Employee(id,name, age, salary);
break;
case PROGRAMMER:
equipment = createEquipment(i);
employees[i]=new Programmer(id, name, age, salary, equipment);
break;
case DESIGNER:
equipment = createEquipment(i);
bonus=Double.parseDouble(EMPLOYEES[i][5]);
employees[i]=new Designer(id, name, age, salary, equipment, bonus);
break;
case ARCHITECT:
equipment = createEquipment(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;
}
}
}
/**
* 根据传入的索引,获取指定Data中设备数组中的对象。
* @param index
*/
private Equipment createEquipment(int index) {
int key=Integer.parseInt(EQIPMENTS[index][0]);
String model = EQIPMENTS[index][1];
String info = EQIPMENTS[index][2];
switch (key) {
case PC:
return new PC(model, info);
case NOTEBOOK:
double price=Double.parseDouble(info);
return new NoteBook(model, price);
case PRINTER:
return new Printer(model, info);
}
return null;
}
/**
* @param id 指定员工的ID
* @return 指定员工对象
* @throws TeamException 找不到指定的员工
*/
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("该员工不存在");
}
(2)TeamService类的设计
// 返回team中所有程序员构成的数组
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for (int i = 0; i < team.length; i++) {
team[i] = this.team[i];
}
return team;
}
public void addMember(Employee e) throws TeamException {
// 成员已满,无法添加
if (total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
// 该成员不是开发人员,无法添加
if (!(e instanceof Programmer)) {
throw new TeamException("该成员不是开发人员,无法添加");
}
// 该员工已在本团队中
Programmer p = (Programmer) e;
if (isExit(p)) {
throw new TeamException("该员工已在本团队中");
}
// 该员工已是某团队成员
// 该员正在休假,无法添加
if ("BUSY".equalsIgnoreCase(p.getStatus().getNAME())) {
throw new TeamException("该员工已是某团队成员");
} else if ("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())) {
throw new TeamException("该员工正在休假,无法添加");
}
// 团队中至多只能有一名架构师
// 团队中至多只能有两名设计师
// 团队中至多只能有两名设计师
int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0;
for (int i = 0; i < total; i++) {
if (team[i] instanceof Architect)
numOfArch++;
else if (team[i] instanceof Designer)
numOfDsgn++;
else if (team[i] instanceof Programmer)
numOfPrg++;
}
if (p instanceof Architect) {
if (numOfArch >= 1)
throw new TeamException("团队中至多只能有一名架构师");
} else if (p instanceof Designer) {
if (numOfDsgn >= 2)
throw new TeamException("团队中至多只能有两名设计师");
} else if (p instanceof Programmer) {
if (numOfPrg >= 3)
throw new TeamException("团队中至多只能有三名程序员");
}
team[total++] = p;
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
// 删除指定memberId的程序员
public void removeMember(int memberId) throws TeamException {
int i = 0;
for (; i < total; i++) {
if (team[i].getMemberId() == memberId) {
team[i].setStatus(Status.FREE);
break;
}
}
if (i == total) {
throw new TeamException("找不到该成员,无法删除");
}
for (int j = i + 1; j < total; i++) {
team[j - 1] = team[j];
}
team[--total] = null;
}
3. 实现view包中的类
注意:为了使信息正确显示,需要在Employee及其子类中重写toString()方法
public void enterMainMenu() {
Boolean loopFlag = true;
char key = 0;
while (loopFlag) {
if (key != '1') {
listAllEmployee();
}
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
key = TSUtility.readMenuSelection();
switch (key) {
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') {
loopFlag = false;
}
break;
}
}
}
private void listAllEmployee() {
System.out.println("-------------------------------开发团队调度软件--------------------------------\n");
Employee[] emps = listService.getAllEmployees();
if (emps.length == 0) {
System.out.println("没有客户记录!");
} else {
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
}
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
System.out.println("-------------------------------------------------------------------------------");
}
private void getTeam() {
System.out.println("\n--------------------团队成员列表---------------------\n");
Programmer[] team = teamService.getTeam();
if (team == null || team.length == 0) {
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("-----------------------------------------------------");
}
private void addMember() {
System.out.println("---------------------添加成员---------------------");
System.out.print("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try {
Employee employee = listService.getEmployee(id);
teamService.addMember(employee);
System.out.println("添加成功");
} catch (TeamException e) {
System.out.println("添加失败,原因:" + e.getMessage());
}
// 按回车键继续...
TSUtility.readReturn();
}
private void deleteMember() {
System.out.println("---------------------删除成员---------------------");
System.out.print("请输入要删除员工的TID:");
int id = TSUtility.readInt();
System.out.print("确认是否删除(Y/N):");
char yn = TSUtility.readConfirmSelection();
if (yn == 'N')
return;
try {
teamService.removeMember(id);
System.out.println("删除成功");
} catch (TeamException e) {
System.out.println("删除失败,原因:" + e.getMessage());
}
// 按回车键继续...
TSUtility.readReturn();
}
全部工程代码链接如下:
https://download.csdn.net/download/qq_42044213/12229696
下一篇: Flask学习日志(三)