package com.it.team.service;
import com.it.team.domain.*;
import com.it.team.domain.PC;
import static com.it.team.service.Data.*;
public class NameListService {
private Employee[] employees;
public NameListService() {
employees = new Employee[Data.EMPLOYEES.length];
for (int i1 = 0; i1 < Data.EMPLOYEES.length; i1++) {
int type = Integer.parseInt(EMPLOYEES[i1][0]);
int id = Integer.parseInt(EMPLOYEES[i1][1]);
String name = EMPLOYEES[i1][2];
int age = Integer.parseInt(EMPLOYEES[i1][3]);
double salayr = Double.parseDouble(EMPLOYEES[i1][4]);
Equiment equiment;
double bonus;
switch (type) {
case EMPLOYEE:
Employee employee = new Employee(id, name, age, salayr);
employees[i1] =employee;
break;
case PROGRAMMER://int id, String name, int age, double salary, Equiment equiment
equiment =createEquiment(i1);
Programmer programmer = new Programmer(id, name, age, salayr,equiment );
employees[i1] =programmer;
break;
case DESIGNER:
equiment =createEquiment(i1);
bonus = Double.parseDouble(EMPLOYEES[i1][5]);
Designer designer =new Designer(id, name, age,salayr,equiment,bonus);
employees[i1] =designer;
break;
case ARCHITECT:
equiment =createEquiment(i1);
bonus = Double.parseDouble(EMPLOYEES[i1][5]);
int stock =Integer.parseInt(EMPLOYEES[i1][6]);
Architect architect =new Architect(id,name,age,salayr,equiment,bonus, stock);
employees[i1] =architect;
break;
}
}
}
private Equiment createEquiment(int index) {
int type = Integer.parseInt(EQUIPMENTS[index][0]);
switch (type) {
case PC:
return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
case NOTEBOOK:
return new NoteBook(EQUIPMENTS[index][1], Integer.parseInt(EQUIPMENTS[index][2]));
case PRINTER:
return new Printer(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);
}
return null;
}
public Employee[] getAllEmployees() {
return employees;
}
int i = 0;
public Employee getEmployee(int id) throws TeamException {
for (i = 0; i < employees.length; i++) {
if (id == employees[i].getId()) {
return employees[i];
}
}
throw new TeamException("未找到该员工");
}
}
package com.it.team.service;
public class Status {
private String NAME;
public static final Status FREE = new Status("FREE");
public static final Status BUSY = new Status("BUSY");
public static final Status VOCATION = new Status("VOCATION");
private Status(String NAME) {
this.NAME=NAME;
}
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME = NAME;
}
@Override
public String toString() {
return NAME ;
}
}
开发团队组建实现类
```package com.it.team.service;
public class TeamException extends Exception {
static final long serialVersionUID = -33875169931249948L;
public TeamException() {
}
public TeamException(String message) {
super(message);
}
}
package com.it.team.service;
import com.it.team.domain.Architect;
import com.it.team.domain.Designer;
import com.it.team.domain.Employee;
import com.it.team.domain.Programmer;
public class TeamService {
private static int counter = 1;
private final int MAX_MEMBER = 5;
private Programmer[] team = new Programmer[MAX_MEMBER];
private int total = 0;
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("该成员不是开发人员,无法添加");
}
if (isExit(e)) {
throw new TeamException("该员工已在本开发团队中");
}
if ("BUSY".equals(((Programmer) e).getStatus().getNAME())) {
throw new TeamException("该员工已是某团队成员");
}
if ("VOCATION".equals(((Programmer) e).getStatus().getNAME())) {
throw new TeamException("该员工正在休假,无法添加");
}
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] != null) {
numOfPro++;
}
}
if (e instanceof Architect) {
if (numOfArch >= 1) {
throw new TeamException("团队中之多只能有一名架构师");
}
}
else if (e instanceof Designer) {
if (numOfDes >= 2) {
throw new TeamException("团队中至多只能有两名设计师");
}
}
else if (e instanceof Programmer) {
if (numOfPro >= 3) {
throw new TeamException("团队中之多只能有三名程序员");
}
}
team[total++] = (Programmer) e;
((Programmer) e).setStatus(Status.BUSY);
((Programmer) e).setMemberId(counter++);
}
private boolean isExit(Employee e) throws TeamException {
for (int i = 0; i < total; i++) {
if (team[i].getId() == e.getId()) {
return true;
}
}
return false;
}
public void removeMember(int memberId) throws TeamException {
for (int i = 0; i < total; i++) {
if (memberId == team[i].getMemberId()) {
team[i].setStatus(Status.FREE);
for (int j = i; j <total-1 ; j++) {
team[j]= team[j+1];
}
team[--total]=null;
return;
}
}
throw new TeamException("找不到指定memberId的员工,删除失败");
}
}