实战项目助你掌握JavaSE知识,
实战项目助你掌握JavaSE知识,
目标模拟实现一个基于文本界面的《团队人员调度软件》
熟悉Java面向对象的高级特性,进一步掌握编程技巧和调试技巧主要涉及以下知识点:
类的继承和多态对象的关联
static和final修饰符
特殊类的使用
异常处理
需求说明:模拟实现基于文本界面的《团队人员调度软件》。
该软件实现以下功能:软件启动时,根据给定的数据创建公司部分成员列表(数组)
根据菜单提示,基于现有的公司成员,组建一个开发团队以开发一个新的项目
组建过程包括将成员插入到团队中,或从团队中删除某成员,还可以列出团队中现在成员的列表
开发团队成员包括架构师、设计师和程序员
本软件采用单级菜单方式工作。当软件运行时,主界面显示公司成员(部分)的列表,如下:
-------------------------------------开发团队调度软件--------------------------------------
ID 姓名 年龄 工资 职位 状态 奖金 股票 领用设备
1 段誉 22 3000.0
2 令狐冲 32 18000.0 架构师 FREE 15000.0 2000 联想T4(6000.0)
3 任我行 23 7000.0 程序员 FREE 戴尔(NEC17寸)
4 张三丰 24 7300.0 程序员 FREE 戴尔(三星 17寸)
5 周芷若 28 10000.0 设计师 FREE 5000.0 佳能 2900(激光)
……
---------------------------------------------------------------------------------------------------
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4): _
当选择“添加团队成员”菜单时,将执行从列表中添加指定(通过ID)成员到开发团队的功能:
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):2
---------------------添加成员---------------------
请输入要添加的员工ID:2
添加成功
按回车键继续...
添加成功后,按回车键将重新显示主界面。
开发团队人员组成要求:
最多一名架构师
最多两名设计师
最多三名程序员
如果添加操作因某种原因失败,将显示类似以下信息(失败原因视具体原因而不同):
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):2
---------------------添加成员---------------------
请输入要添加的员工ID:2
添加失败,原因:该员已是团队成员
按回车键继续...
失败信息包含以下几种:
成员已满,无法添加该成员不是开发人员,无法添加
该员已是团队成员
该员正在休假,无法添加
团队中只能有一名架构师
团队中只能有两名设计师
团队中只能有三名程序员
当选择“删除团队成员”菜单时,将执行从开发团队中删除指定(通过TeamID)成员的功能:
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):2
---------------------删除成员---------------------
请输入要删除员工的TID:1
确认是否删除成功删除(Y/N):y
按回车键继续...
删除成功后,按回车键将重新显示主界面。
当选择“团队列表”菜单时,将列出开发团队中的现有成员,例如:
--------------------团队成员列表---------------------
TID/ID 姓名 年龄 工资 职位 奖金 股票
2/4 张三丰 24 7300.0 程序员
3/2 令狐冲 32 18000.0 架构师 15000.0 2000
4/6 赵敏 22 6800.0 程序员
5/12 黄蓉 27 9600.0 设计师 4800.0
-----------------------------------------------------
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):
软件设计结构
该软件由以下四个模块组成:
模块说明:
com.atguigu.team.view模块为主控模块,负责菜单的显示和处理用户操作
com.atguigu.team.service模块为实体对象(Employee及其子类如程序员等)的管理模块,
CompanyService和TeamService类分别用各自的数组来管理公司员工和开发团队成员对象
多层继承:①提高代码的复用性②子类继承父类后,子类理解为父类的特殊类型(多态)
com.atguigu.team.domain模块中包含了所有实体类:
其中程序员(Programmer)及其子类,均会领用某种电子设备(Equipment)。
Employee类及其子类的设计:
说明:
memberId 用来记录成员加入开发团队后在团队中的ID
status是项目自定义的枚举类型,表示成员的状态:
FREE-空闲
BUSY-已加入开发团队
VOCATION-正在休假
equipment 表示该成员领用的设备
可根据需要自行为类提供各属性的get/set方法以及重载构造器
Status枚举类
Status枚举类位于com.atguigu.team.service包中.
说明:
bouns为奖金
stock表示公司奖励的股票数量
可根据需要自行为类提供各属性的get/set方法以及重载构造器
Equipment接口及其实现子类的设计
说明:
model表示机器的型号
display表示显示器名称
可根据需要自行为类提供各属性的get/set方法以及重载构造器
CompanyService类的设计
说明:
employees用来保存所有公司员工对象
CompanyService()构造器:
根据项目提供的Data类构建相应大小的employees数组
再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Eqipment子类的对象
将对象存于数组中
Data类位于com.atguigu.team.service包中
说明:
employees用来保存所有公司员工对象
getAllEmployees ()方法:获取当前所有员工。返回:包含所有员工对象的数组
getEmployee(id : int)方法:获取指定ID的员工对象。
参数:指定员工的ID
返回:指定员工对象
异常:找不到指定的员工
另外,可根据需要自行添加其他方法或重载构造器
TeamService类的设计
说明:
counter为静态变量,用来为开发团队新增成员自动生成团队中的唯一ID,即memberId。(提示:应使用增1的方式)
MAX_MEMBER表示开发团队最大成员数
team数组用来保存当前团队中的各成员对象
realCount记录团队成员的实际人数
getTeam()方法:返回当前团队的所有有效对象
返回:包含所有成员对象的数组,数组大小与成员人数一致
addMember(e: Employee)方法:向团队中添加成员
参数:待添加成员的对象
异常:添加失败, TeamException中包含了失败原因
removeMember(memberId: int)方法:从团队中删除成员
参数:待删除成员的memberId
异常:删除失败, TeamException中包含了失败原因
另外,可根据需要自行添加其他方法或重载构造器
TeamView类的设计
说明:
companySvc和teamSvc属性:供类中的方法使用
enterMainMenu ()方法:主界面显示及控制方法。
以下方法仅供enterMainMenu()方法调用:
listAllEmployees ()方法:以表格形式列出公司所有成员
addMember ()方法:实现添加成员操作
deleteMember ()方法:实现删除成员操作
键盘访问的实现
项目中提供了TSUtility.java类,可用来方便地实现键盘访问。该类提供了以下静态方法:
public static char readMenuSelection()
用途:该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
public static void readReturn()
用途:该方法提示并等待,直到用户按回车键后返回。
public static int readInt()
用途:该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
public static char readConfirmSelection() :
用途:从键盘读取‘Y’或’N’,并将其作为方法的返回值。
第1步 — 创建项目基本组件
首先,完成以下工作:创建TSS项目
按照设计要求,创建所有包
按照设计要求,在com.atguigu.team.domain包中,创建所有类和接口的声明
将项目提供的几个类复制到相应的包中
编写Employee类及其各子类代码
编写Equipment接口及其各实现子类代码
检验代码的正确性
第2步 — 实现service包中的类
按照设计要求编写CompanyService类在TeamMain类中的main方法中,写单元测试。
在方法中创建CompanyService对象,然后分别用模拟数据调用该对象的各个方法,以测试是否正确。
注:测试应细化到包含了所有非正常的情况,以确保方法完全正确。
重复1-3步,完成TeamService类的开发
第3步 — 实现view包中类
按照设计要求编写TeamView类,逐一实现各个方法,并编译执行main方法中,测试软件全部功能
项目中提供的Status、Data、TSUtility的具体代码如下:
Status.java
<span >public enum Status {
FREE, BUSY, VOCATION
}</span>
Data.java
<span >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"}
};
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, type, name
public static final String[][] EQUIPMENTS = {
{},
{"22", "联想Y5", "6000"},
{"21", "宏碁 ", "AT7-N52"},
{"21", "戴尔", "3800-R33"},
{"23", "激光", "佳能 2900"},
{"21", "华硕", "K30BD-21寸"},
{"21", "海尔", "18-511X 19"},
{"23", "针式", "爱普生20K"},
{"22", "惠普m6", "5800"},
{"21", "联想", "ThinkCentre"},
{"21", "华硕","KBD-A54M5 "},
{"22", "惠普m6", "5800"}
};
}</span>
TSUtility.java
<span >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;
}
}</span>
参考实现代码:
首先完成各个包以及类的创建,如下图:
其中各个文件内的代码参考如下:
com.atguigu.team.domain的设计
Employee.java
<span >package com.atguigu.team.domain;
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 say() {
return id + "\t" + name + "\t" + age + "\t" + (int)salary;
}
@Override
public String toString() {
return say();
}
}
</span>
Programmer.java
<span >package com.atguigu.team.domain;
import com.atguigu.team.service.Status;
public class Programmer extends Employee {
private int memberid;
private Status status;//状态
private Equipment equipment;//关联的对象,某种设备
public Programmer() {}
public Programmer(int id, String name, int age, double salary,int memberid, Status status,Equipment equipment) {
super(id, name, age, salary);
this.memberid = memberid;
this.status = status;
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 + "\t\t\t" + equipment;
}
public String toString2() {
return memberid + "/" + say() + "\t程序员";
}
}
</span>
Designer.java
<span >package com.atguigu.team.domain;
import com.atguigu.team.service.Status;
public class Designer extends Programmer {
private double bonus;
public Designer() {}
public Designer(int id, String name, int age, double salary,int memberid, Status status,Equipment equipment, double bonus) {
super(id, name, age, salary, memberid, status, equipment);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return say() + "\t" + "设计师" + "\t" + getStatus() + "\t" + (int)bonus + "\t\t" + getEquipment();
}
public String toString2() {
return getMemberid() + "/" + say() + "\t设计师" + "\t" + (int)bonus;
}
}
</span>
Architect.java
<span >package com.atguigu.team.domain;
import com.atguigu.team.service.Status;
public class Architect extends Designer {
private int stock;
public Architect() {}
public Architect(int id, String name, int age, double salary,int memberid, Status status,Equipment equipment, double bonus,int stock) {
super(id, name, age, salary, memberid, status, equipment, bonus);
this.stock = stock;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return super.say() + "\t" + "架构师" + "\t" + getStatus() + "\t" + (int)getBonus() + "\t" + stock + "\t" + getEquipment();
}
public String toString2() {
return getMemberid() + "/" + say() + "\t架构师" + "\t" + (int)getBonus() + "\t" + stock;
}
}
</span>
Equipment.java(接口)
<span >package com.atguigu.team.domain;
public interface Equipment {
public String getDescription();
}
</span>
PC.java
<span >package com.atguigu.team.domain;
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;
}
@Override
public String toString() {
return model + "(" + display + ")";
}
@Override
public String getDescription() {
return toString();
}
}
</span>
NoteBook.java
<span >package com.atguigu.team.domain;
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;
}
@Override
public String toString() {
return model + "(" + price + ")";
}
@Override
public String getDescription() {
return toString();
}
}
</span>
Printer.java
<span >package com.atguigu.team.domain;
public class Printer implements Equipment {
private String type;
private String name;
public Printer() {
}
public Printer(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return type + "(" + name + ")";
}
@Override
public String getDescription() {
return toString();
}
}
</span>
com.atguigu.team.service的设计
Data.java
<span >package com.atguigu.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"}
};
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, type, name
public static final String[][] EQUIPMENTS = {
{},
{"22", "联想Y5", "6000"},
{"21", "宏碁 ", "AT7-N52"},
{"21", "戴尔", "3800-R33"},
{"23", "激光", "佳能 2900"},
{"21", "华硕", "K30BD-21寸"},
{"21", "海尔", "18-511X 19"},
{"23", "针式", "爱普生20K"},
{"22", "惠普m6", "5800"},
{"21", "联想", "ThinkCentre"},
{"21", "华硕","KBD-A54M5 "},
{"22", "惠普m6", "5800"}
};
}
</span>
Status.java
<span >package com.atguigu.team.service;
public enum Status {
FREE, BUSY, VOCATION
}
</span>
TeamExcetion.java
<span >package com.atguigu.team.service;
@SuppressWarnings("serial")
public class TeamException extends Exception{
public TeamException(String message) {
super(message);
}
public TeamException(Exception cause) {
super(cause);
}
}
</span>
CompanyService.java
<span >package com.atguigu.team.service;
import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Equipment;
import com.atguigu.team.domain.NoteBook;
import com.atguigu.team.domain.PC;
import com.atguigu.team.domain.Printer;
import com.atguigu.team.domain.Programmer;
public class CompanyService {
private Employee[] employees;
public CompanyService() {
// 先处理所有的设备对象
// 创建一个设备数组
Equipment[] eqs = new Equipment[Data.EQUIPMENTS.length];
for (int i = 2; i < Data.EQUIPMENTS.length; i++) {
// 获取子数组
String[] child = Data.EQUIPMENTS[i];
// 获取识别码, 并转换成整数
int code = Integer.parseInt(child[0]);
// 样例数据: {"21", "宏碁 ", "AT7-N52"},
// PC :21, model, display
if (code == Data.PC) {
// 当前子数组中的数据就可以整合成一个PC对象
//public PC(String model, String display) {
String model = child[1];
String display = child[2];
// 对象保存在数组中
eqs[i] = new PC(model, display);
} else if (code == Data.NOTEBOOK) {
//样例 : {"22", "联想Y5", "6000"},
//NoteBook:22, model, price
// 当前子数组中的数据就可以整合成一个NoteBook对象
// public NoteBook(String model, double price) {
String model = child[1];
double price = Integer.parseInt(child[2]);
// 对象保存在数组中
eqs[i] = new NoteBook(model, price);
} else if (code == Data.PRINTER) {
//样例 : {"23", "激光", "佳能 2900"},
//Printer :23, type, name
// 当前子数组中的数据就可以整合成一个Printer对象
// public Printer(String type, String name) {
String type = child[1];
String name = child[2];
// 对象保存在数组中
eqs[i] = new Printer(type, name);
}
}
/* 测试用
for (Equipment equipment : eqs) {
System.out.println(equipment);
}*/
this.employees = new Employee[Data.EMPLOYEES.length];
for (int i = 0; i < Data.EMPLOYEES.length; i++) {
// 获取子数组
String[] child = Data.EMPLOYEES[i];
// 获取识别码, 转成整数
int code = Integer.parseInt(child[0]);
int id = Integer.parseInt(child[1]);
String name = child[2];
int age = Integer.parseInt(child[3]);
double salary = Integer.parseInt(child[4]);
//样例{"10", "1", "段誉", "22", "3000"},
//Employee : 10, id, name, age, salary
if (code == Data.EMPLOYEE) {
// 创建Employee对象
//public Employee(int id, String name, int age, double salary) {
//创建好对象后, 保存在数组中
this.employees[i] = new Employee(id, name, age, salary);
} else if (code == Data.PROGRAMMER) {
//{"11", "3", "任我行", "23", "7000"},
//Programmer: 11, id, name, age, salary
/*public Programmer( int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment) {*/
//设备对象从上面的数组中获取, 只需要对应的元素即可
//创建好对象后, 保存在数组中
this.employees[i] = new Programmer(id,
name,
age,
salary,
0,
Status.FREE,
eqs[i]);
} else if (code == Data.DESIGNER) {
//{"12", "5", "周芷若", "28", "10000", "5000"},
//Designer : 12, id, name, age, salary, bonus
/*
public Designer(int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment,
double bonus) {
*/
//设备对象从上面的数组中获取, 只需要对应的元素即可
double bonus = Integer.parseInt(child[5]);
//创建好对象后, 保存在数组中
this.employees[i] = new Designer(id, name, age, salary, 0, Status.FREE, eqs[i], bonus);
} else if (code == Data.ARCHITECT) {
//{"13", "2", "令狐冲", "32", "18000", "15000", "2000"},
//Architect : 13, id, name, age, salary, bonus, stock
/*
public Architect(int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment,
double bonus,
int stock) {
*/
//设备对象从上面的数组中获取, 只需要对应的元素即可
double bonus = Integer.parseInt(child[5]);
int stock = Integer.parseInt(child[6]);
//创建好对象后, 保存在数组中
this.employees[i] = new Architect(id, name, age, salary, 0, Status.FREE, eqs[i], bonus, stock);
}
}
//测试用
/*
for (int j = 0; j < employees.length; j++) {
System.out.println(employees[j]);
}*/
}
/**
* 获取所有员工的存储的数组
* @return
*/
public Employee[] getAllEmployees() {
/*
Employee[] copy = new Employee[employees.length];
for (int i = 0; i < copy.length; i++) {
copy[i] = employees[i];
}
return copy;
*/
return employees;
}
/**
* 方法:获取指定ID的员工对象。
* @param id 指定员工的ID
* @return 指定员工对象
* 异常:找不到指定的员工
*/
public Employee getEmployee(int id) throws TeamException {
// 遍历所有员工的数组
for (int i = 0; i < employees.length; i++) {
// 如果某员工的ID属性和参数id一样,就返回这个员工对象
if (employees[i].getId() == id) {
return employees[i];
}
}
// 如果没有找到对象, 抛出异常 throw
throw new TeamException("找不到指定ID[" + id + "]的员工");
}
}
</span>
TeamService.java
<span >package com.atguigu.team.service;
import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;
public class TeamService {
private static int counter = 1;
public final int MAX_MEMBER = 6;
private Programmer[] team = new Programmer[MAX_MEMBER];
private int realCount = 0;
/**
* 添加一个员工对象到团队中
* @param emp
* @throws TeamException 因某种原因失败
*/
public void addMember(Employee emp) throws TeamException {
if (realCount == team.length) {
throw new TeamException("成员已满,无法添加");
}
if (!(emp instanceof Programmer)) {
throw new TeamException("添加的成员不是开发人员"); // 提前错误返回
}
Programmer programmer = (Programmer)emp;
if (programmer.getMemberId() != 0) {
throw new TeamException("该员已是团队成员");
}
if (programmer.getStatus() == Status.VOCATION) {
throw new TeamException("该员正在休假,无法添加");
}
int architectCount = 0;
int designerCount = 0;
int programmerCount = 0;
for (int i = 0; i < realCount; i++) {
if (team[i] instanceof Architect) {
architectCount++;
} else if (team[i] instanceof Designer) {
designerCount++;
} else {
programmerCount++;
}
}
// 对将要添加的新成员的身份作一个判断
if (programmer instanceof Architect) {
if (architectCount == 1) {
throw new TeamException("团队中只能有一名架构师");
}
} else if (programmer instanceof Designer) {
if (designerCount == 2) {
throw new TeamException("团队中只能有两名设计师");
}
} else {
if (programmerCount == 3) {
throw new TeamException("团队中只能有三名程序员");
}
}
programmer.setMemberId(counter++); // 为要添加的新成员赋予一个团队ID号
programmer.setStatus(Status.BUSY); // 修改对象的状态为busy
team[realCount++] = programmer;
}
/**
* 获取所有团队成员的数组
* @return 完美数组
*/
public Programmer[] getTeam() {
// 创建一个临时数组
Programmer[] tmp = new Programmer[realCount];
// 挨个复制所有有效元素
for (int i = 0; i < tmp.length; i++) {
tmp[i] = team[i];
}
// 返回临时数组
return tmp;
}
/**
* 删除指定TID的团队成员
* @param memberId
* @throws TeamException
*/
public void removeMember(int memberId) throws TeamException {
// 先根据memberId定位要删除的成员的下标
int index = -1; // 先假定要删除的TID成员的下标不存在
for (int i = 0; i < realCount; i++) {
if (team[i].getMemberId() == memberId) {
index = i;
break;
}
}
if (index == -1) { // 检索完成后, 再次检测下标是否有效, 如果无效说明没有找到
throw new TeamException("要删除的TID[" + memberId + "]的团队成员未找到!");
}
// 在覆盖要删除的对象前进行对象的数据修改
team[index].setMemberId(0); // 剥夺ID
team[index].setStatus(Status.FREE); // 还原状态
// 从要删除的下标开始,依次把右边的元素移到左边
for (int j = index; j < realCount - 1; j++) {
team[j] = team[j + 1];
}
// 把最后一个有效元素置为空洞
// 调整计数器
team[--realCount] = null;
}
}
</span>
com.atguigu.team.view的设计
TSUtility.java
<span >package com.atguigu.team.view;
import java.util.*;
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;
}
}
</span>
TeamView.java
<span >package com.atguigu.team.view;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;
import com.atguigu.team.service.CompanyService;
import com.atguigu.team.service.TeamException;
import com.atguigu.team.service.TeamService;
public class TeamView {
/**
* 负责处理和公司员工相关的业务
*/
private CompanyService companyService = new CompanyService();
/**
* 负责处理和团队相关的业务
*/
private TeamService teamService = new TeamService();
/**
* 进入主菜单方法, 相当于入口方法
*/
public void enterMainMenu() {
boolean loopFlag = true;
while (loopFlag) {
listAllEmployees();
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4) : ");
// 获取用户的键盘输入
char choice = TSUtility.readMenuSelection();
// 对用户输入作出分支
switch (choice) {
// 如果是'1' 调用 listTeam()
case '1' : listTeam(); break;
// 如果是'2' 调用 addMember()
case '2' : addMember(); break;
// 如果是'3' 调用 deleteMember()
case '3' : deleteMember(); break;
// 如果是'4' 设置循环变量为false
case '4' : loopFlag = false; break;
}
}
}
/**
* 列出公司所有员工
*/
private void listAllEmployees() {
System.out.println("-------------------------------------开发团队调度软件--------------------------------------");
System.out.println();
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
Employee[] allEmps = companyService.getAllEmployees();
for (int i = 0; i < allEmps.length; i++) {
System.out.println(allEmps[i]);
}
System.out.println("---------------------------------------------------------------------------------------------------");
}
/**
* 添加成员到team中
* @throws
*/
private void addMember() {
System.out.println("---------------------添加成员---------------------");
System.out.print("请输入要添加的员工ID : ");
// 获取用户从键盘输入的ID
int id = TSUtility.readInt();
try {
// 从companyService中获取对象,依据是ID号
Employee employee = companyService.getEmployee(id);
// 把获取到的对象添加到teamService中, 调用方法addMember(Employee emp)
teamService.addMember(employee);
System.out.println("添加成功!");
} catch (TeamException e) {
System.out.println("添加失败, 原因 : " + e.getMessage());
}
// 调用工具类的readReturn()
TSUtility.readReturn();
}
/**
* 从团队中删除成员
*/
private void deleteMember() {
System.out.println("---------------------删除成员---------------------");
System.out.print("请输入要删除员工的TID : ");
int id = TSUtility.readInt();
System.out.print("确认是否删除(Y/N) :");
char confirm = TSUtility.readConfirmSelection();
if (confirm == 'Y') {
try {
teamService.removeMember(id);
System.out.println("删除成功!");
} catch (TeamException e) {
System.out.println("删除失败, 原因 : " + e.getMessage());
}
TSUtility.readReturn();
}
}
/**
* 列出团队成员
*/
private void listTeam() {
System.out.println("--------------------团队成员列表---------------------");
System.out.println();
System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
Programmer[] team = teamService.getTeam();
for (Programmer programmer : team) {
System.out.println(programmer.toString2());
}
System.out.println("-----------------------------------------------------");
}
}
</span>
com.atguigu.team.main的设计
TeamMain.java
<span >package com.atguigu.team.main;
import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Equipment;
import com.atguigu.team.domain.NoteBook;
import com.atguigu.team.domain.PC;
import com.atguigu.team.domain.Printer;
import com.atguigu.team.domain.Programmer;
import com.atguigu.team.service.CompanyService;
import com.atguigu.team.service.Status;
import com.atguigu.team.service.TeamException;
import com.atguigu.team.service.TeamService;
import com.atguigu.team.view.TeamView;
public class TeamMain {
public static void main(String[] args) {
new TeamView().enterMainMenu();
}
public static void main1(String[] args) {
CompanyService companyService = new CompanyService();
Employee[] emps = companyService.getAllEmployees();
for (int i = 0; i < emps.length; i++) {
System.out.println(emps[i]);
}
System.out.println("-------------------------");
Employee emp;
try {
emp = companyService.getEmployee(90);
System.out.println(emp);
} catch (TeamException e) {
e.printStackTrace();
}
}
public static void main0(String[] args) {
Employee emp1 = new Employee(1, "张一", 10, 1000);
//System.out.println(emp1);
/*
public Programmer(int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment) {
*/
Equipment eq1 = new PC("T440", "联想14寸");
Employee emp2 = new Programmer(2, "张二", 20, 2000, 0, Status.FREE, eq1);
//System.out.println(emp2);
/*
public Designer(int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment,
double bonus) {
*/
Equipment eq2 = new NoteBook("MBP13", 10000);
Employee emp3 = new Designer(3, "张三", 30, 3000, 0, Status.FREE, eq2, 30000);
//System.out.println(emp3);
/*
public Architect(int id,
String name,
int age,
double salary,
int memberId,
Status status,
Equipment equipment,
double bonus,
int stock) {
*/
Equipment eq3 = new Printer("激光", "HP1020");
Employee emp4 = new Architect(4, "张四", 40, 4000, 0, Status.VOCATION, eq3, 40000, 400000);
//System.out.println(emp4);
TeamService teamService = new TeamService();
try {
teamService.addMember(emp1);
} catch (TeamException e) {
System.out.println(e);
}
try {
teamService.addMember(emp2);
} catch (TeamException e) {
System.out.println(e);
}
try {
teamService.addMember(emp3);
} catch (TeamException e) {
System.out.println(e);
}
try {
teamService.addMember(emp4);
} catch (TeamException e) {
System.out.println(e);
}
Programmer[] tmp = teamService.getTeam();
for (int i = 0; i < tmp.length; i++) {
System.out.println(tmp[i]);
}
System.out.println("------------------------------");
try {
teamService.removeMember(2);// 张二, 张三, 张四
} catch (TeamException e) {
System.out.println(e);
}
// 删除完成后,再次遍历
tmp = teamService.getTeam();
for (int i = 0; i < tmp.length; i++) {
System.out.println(tmp[i]);
}
System.out.println("------------------------------");
}
}
</span>
执行结果如下:
按照以上的方法添加3、4、6、10号员工,然而添加到10号员工时出现如下界面
此时团队中的成员如图:
此时添加8号员工,出现如下界面:
接着在添加5、9、12号员工,出现如下界面:
接着删除3号员工
此时团队成员如下:
此时我们插入7号员工,出现如下界面:
到这里所有功能均已实现,小项目完成。
相关文章
- 暂无相关文章
用户点评