public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
TeamService.java
package pers.jsc.dispatch.service;
import pers.jsc.dispatch.domain.Employee;
import pers.jsc.dispatch.domain.domainexte.Architect;
import pers.jsc.dispatch.domain.domainexte.Designer;
import pers.jsc.dispatch.domain.domainexte.Programmer;
import pers.jsc.dispatch.exception.TeamException;
/**
* @author 金聖聰
* @title: TeamService
* @projectName TeamDispatchApp
* @description: 关于开发团队成员的管理:添加、删除等
* @date 2019/5/9 16:34
*/
public class TeamService {
/**
* 静态变量,用来为开发团队新增成员自动生成团队中的唯一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 static int getCounter() {
return counter;
}
public static void setCounter(int counter) {
TeamService.counter = counter;
}
public int getMAX_MEMBER() {
return MAX_MEMBER;
}
public void setTeam(Programmer[] team) {
this.team = team;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
/**
* 返回当前团队的所有对象
* @return 包含所有成员对象的数组,数组大小与成员人数一致
*/
public Programmer[] getTeam() {
if (total != 0){
Programmer[] t = new Programmer[total];
for (int i = 0; i < t.length; i++) {
t[i] = team[i];
}
return t;
}
throw new TeamException("Nobody in Team");
}
/**
* 向团队中添加成员
* @param e 待添加成员的对象
* @throws TeamException 添加失败, TeamException中包含了失败原因
*/
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("该员工已在本开发团队中");
}
// 该员工已是某团队成员
// 该员正在休假,无法添加
String busy = "BUSY";
String vocation = "VOCATION";
if (busy.equals(p.getStatus().getNAME())){
throw new TeamException("该员工已是某团队成员");
}else if (vocation.equals(p.getStatus().getNAME())){
throw new TeamException("该员正在休假,无法添加");
}
int numOfArch = 0;
int numOfDesr = 0;
int numOfPror = 0;
for (int i = 0; i < total; i++) {
if (team[i] instanceof Architect){
numOfArch++;
}else if (team[i] instanceof Designer){
numOfDesr++;
}else if (team[i] instanceof Programmer){
numOfPror++;
}
}
// 团队中至多只能有一名架构师
// 团队中至多只能有两名设计师
// 团队中至多只能有三名程序员
if (p instanceof Architect){
if (numOfArch >= 1){
throw new TeamException("团队中至多只能有一名架构师");
}
}else if (p instanceof Designer){
if (numOfDesr >= 2){
throw new TeamException("团队中至多只能有两名设计师");
}
}else if (p instanceof Programmer){
if (numOfPror >= 3){
throw new TeamException("团队中至多只能有三名程序员");
}
}
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
team[total++] = p;
}
/**
* 从团队中删除成员
* @param memberId 待删除成员的memberId
* @throws TeamException 找不到指定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(&q