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;
private final int MAX_MEMBER = 5;
private Programmer[] team = new Programmer[MAX_MEMBER];
private int total;
public TeamService() {
super();
}
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(isExist(e)) {
throw new TeamException("该员工已在本开发团队中");
}
Programmer p = (Programmer)e;
if("BUSY".equals(p.getStatus().getNAME())) {
throw new TeamException("该员工已是某团队成员");
}else if("VOCATION".equals(p.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] instanceof Programmer) {
numOfPro++;
}
}
if(p instanceof Architect) {
if(numOfArch >= 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.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
private boolean isExist(Employee e) {
for(int i = 0;i < total;i++) {
if(team[i].getId() == e.getId()) {
return true;
}
}
return false;
}
public void removeMeber(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("找不到指定的memberId的员工,删除失败");
}
for(int j = i + 1;j < total;j++) {
team[j - 1] = team[j];
}
team[--total] = null;
}
}
package com.atguigu.team.view;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;
import com.atguigu.team.service.NameListService;
import com.atguigu.team.service.TeamException;
import com.atguigu.team.service.TeamService;
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public void enterMainMenu() {
boolean loopFlag = true;
char menu = 0;
while(loopFlag) {
if(menu != '1') {
listAllEmployees();
}
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
menu = TSUtility.readMenuSelection();
switch (menu) {
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 listAllEmployees() {
System.out.println("------------------------------开发团队调度软件------------------------------\n");
Employee[] employees = listSvc.getAllEmployees();
if(employees == null || employees.length == 0) {
System.out.println("公司中没有如何员工的信息!");
}else {
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
for(int i = 0;i < employees.length;i++) {
System.out.println(employees[i]);
}
}
System.out.println("-----------------------------------------------------------------------------------");
}
private void getTeam() {
System.out.println("--------------------团队成员列表--------------------\n");
Programmer[] team = teamSvc.getTeam();
if(team == null || team.length == 0) {
System.out.println("开发团队目前没有成员!");
}else {
System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票\n");
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.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();
}
private void deleteMember() {
System.out.println("--------------------删除成员--------------------");
System.out.println("请输入要删除员工的TID:");
int memberId = TSUtility.readInt();
System.out.print("确认是否删除(Y/N):");
char isDelete = TSUtility.readConfirmSelection();
if(isDelete == 'N') {
return;
}
try {
teamSvc.removeMeber(memberId);
System.out.println("删除成功");
}catch (TeamException e) {
System.out.println("删除失败,原因:" + e.getMessage());
}
TSUtility.readReturn();
}
public static void main(String[] args) {
TeamView view = new TeamView();
view.enterMainMenu();
}
}
项目目录:
总结:今天完成了TeamService中属性和getTeam()、addMember()、TeamService中三个小问题、removeMember()的实现、TeamView整体框架的搭建、TeamView退出功能的实现、TeamView显示所有团队和员工、TeamView添加与删除功能的实现。
明日计划:明天开始学习IDEA和多线程,等把多线程学完就可以开始其他内容了。