要点:注重逻辑非业务分析,推荐将页面代码和功能代码分开
难点:团队分配项目,及相关逻辑的实现
主要代码:
public class LoginView {
private String userName="";//定义存放用户名的空字符串
private String passWord="";//定义存放密码的空字符串
//注册
public void register(){//定义注册方法
//注册页面
System.out.println("-----------------");
System.out.println("-----注册界面-----");
System.out.println("--请输入用户名:");
String userName=TSUtility.readKeyBoard(10,false);//接收键盘输入的用户名
this.userName=userName;//将接收的用户名赋值给初始定义的用户名空字符串
System.out.println("--请输入密码:");
String passWord=TSUtility.readKeyBoard(18,false);//接收键盘输入的密码
this.passWord=passWord;//将接收的密码赋值给初始定义的密码空字符串
System.out.println("注册成功!请登录!");
}
//登录
public void login() throws TeamException, InterruptedException {//定义登录方法
int count=5;//限制登录错误次数为5次
boolean flag=true;//定义循环条件初始值为true
while (flag){//初始值为true循环开始
//登录界面
System.out.println("-----------------");
System.out.println("-----登录界面-----");
System.out.println("--请输入用户名:");
String userName=TSUtility.readKeyBoard(10,false);
System.out.println("--请输入密码:");
String passWord=TSUtility.readKeyBoard(18,false);//定义String类型的密码接收键盘输入的内容
//如果未注册
if (this.userName.length()==0||this.passWord.length()==0){//判断条件为初始用户名和初始密码是否为空
System.out.println("未检测到账号,请注册!");
flag=false;//循环条件改变,跳出循环
register();//调用注册方法进行注册
}
//如果已注册
else if (this.userName.equals(userName)&&this.passWord.equals(passWord)){//判断条件为初始密码和用户名是否与上面键盘接收的密码和用户名相同
System.out.println("登录成功!!欢迎您!!!");
// count++;//登录成功,登录次数加一
flag=false;//循环条件改变,跳出循环
nextInterface();//调用功能选择界面方法
}else {
//若密码或用户名错误,则判断登录次数,若为0则不能登录,返回初始界面,否则输出错误信息,并返回剩余登录次数
if (count<=0){
System.out.println("登录次数不足!");
initialInterface();
}else {
System.out.println("用户名或密码有误,请重新输入!");
System.out.println("登录次数剩余"+count+"次!");
count--;
}
}
}
}
//修改信息
public void change() throws TeamException, InterruptedException {//定义用户信息修改界面方法
boolean flag8=true;//定义循环条件
while (flag8){
//用户信息修改界面
System.out.println("--------------------");
System.out.println("--用户名、密码修改界面--");
System.out.println("--1、修改用户名");
System.out.println("--2、修改密码");
System.out.println("--3、修改用户名和密码");
System.out.println("--4、退出本界面");
System.out.println("请选择:");
char choice=TSUtility.readMenuSelection();
//不同原则进入不同的分支语句块,执行相关功能
switch (choice){
case '1':System.out.println("--请输入新用户名:");
String userName=TSUtility.readKeyBoard(10,false);
this.userName=userName;
System.out.println("修改成功!新用户名为:"+this.userName);
break;
case '2':System.out.println("--请输入新密码:");
String passWord=TSUtility.readKeyBoard(18,false);
this.passWord=passWord;
System.out.println("修改成功!新密码为:"+this.passWord);
break;
case '3': System.out.println("--请输入新用户名:");
String userName1=TSUtility.readKeyBoard(10,false);
this.userName=userName1;
System.out.println("--请输入新密码:");
String passWord1=TSUtility.readKeyBoard(18,false);
this.passWord=passWord1;
System.out.println("修改成功!新用户名为:"+this.userName+"新密码为:"+this.passWord);
break;
case '4':flag8=false;break;
default:
System.out.println("输入有误,请重新输入!");
break;
}}
}
//初始界面
public void initialInterface() throws TeamException, InterruptedException {//定义初始界面方法
while (true){
//初始界面
System.out.println("================================");
System.out.println("|------项目开发团队分配管理软件-----|");
System.out.println("================================");
System.out.println("---1、用户注册功能");
System.out.println("---2、用户登录功能");
System.out.println("---3、退出系统");
System.out.println("请选择:");
char choice=TSUtility.readMenuSelection();
//不同选择执行不同功能
switch (choice){
case '1': register();break;
case '2': login();break;
case '3':
System.out.println("是否退出系统?(y/n)");
char res=TSUtility. readConfirmSelection();
if (res=='Y'||res=='y'){
System.out.println("已成功退出!");
System.exit(0);
}else if (res=='n'||res=='N'){
TSUtility.readReturn();
}
default:
System.out.println("输入有误,请重新输入!");break;
}
}
}
public void nextInterface() throws TeamException, InterruptedException {//定义功能选择界面方法
//实例化会用到的类,根据后续switch分支语句功能添加进行添加
TeamService teamService = new TeamService();
NameListService nls = new NameListService();
ArrayList<ArrayList<Programmer>> manyTeam = null;
ProjectService prs = new ProjectService();
boolean flag=true;
while (flag) {
//功能选择界面
System.out.println("===============================");
System.out.println("|----------功能选择界面----------|");
System.out.println("===============================");
System.out.println("---1、用户修改功能");
System.out.println("---2、开发人员管理");
System.out.println("---3、开发团队调度管理");
System.out.println("---4、开发项目管理");
System.out.println("---5、退出系统");
Scanner sc3 = new Scanner(System.in);
String choice=sc3.next();
//不同选择进入不同功能板块的界面
switch (choice){
//用户修改功能,调用用户信息修改方法
case "1":change();break;
//开发人员管理,调用开发人员管理的界面方法
case "2":nls.nameViewInterface();
break;
//开发团队调度管理,调用开发团队管理的界面方法
case "3":
manyTeam = teamService.getManyTeam(nls);
break;
//开发项目管理界面(也可以像上面几个一样写界面方法后直接调用界面方法)
case "4":
boolean flag1=true;
while (flag1) {
System.out.println("===========================");
System.out.println("|--------开发项目管理--------|");
System.out.println("===========================");
System.out.println("--1、项目的添加");
System.out.println("--2、项目分配开发团队");
System.out.println("--3、项目的查看");
System.out.println("--4、项目的删除");
System.out.println("--5、退出当前菜单");
System.out.println("请选择: ");
System.out.println("请选择(1-4):");
Scanner sc4 = new Scanner(System.in);
String choice1=sc3.next();
switch (choice1) {
case "1":
try {
prs.addProject();
} catch (InterruptedException i) {
i.printStackTrace();
}
break;
case "2":
if (prs.getAllPro().size() == 0) {
System.out.println("当前没有项目,请先添加!");
} else {
for (ArrayList<Programmer> pro : manyTeam) {
prs.distributionPro(pro);
}
}
break;
case "3":
try {
prs.showPro();
} catch (InterruptedException i) {
i.printStackTrace();
}
break;
case "4":
System.out.println("请输入你要删除的项目的ID:");
int id = sc4.nextInt();
try {
prs.delPro(id);
} catch (InterruptedException | TeamException i) {
System.out.println("输入有误,请重新输入!!");
}
break;
case "5":
flag1=false;
break;
default:
System.out.println("输入错误,请重新输入!");
break;
}}
break;
//退出系统
case "5": flag=false;break;
default:
System.out.println("输入有误,请重新输入!");break;
}
}
}
项目管理的逻辑实现主要包含,添加项目,给项目添加管理团队,修改和删除功能
public class ProjectService {
int first = -1;
int second = -1;
int third = -1;
int forth = -1;
private ArrayList<Project> pro = new ArrayList<Project>();
private int count = 1;
public void addProject() throws InterruptedException {
System.out.println("项目参考:--------------------------------------------------");
System.out.println("1.小米官网:开发完成类似于小米官网的web项目.");
System.out.println("2.公益在线商城:猫宁Morning公益商城是中国公益性在线电子商城.");
System.out.println("3.博客系统:Java博客系统,让每一个有故事的人更好的表达想法!");
System.out.println("4.在线协作文档编辑系统:一个很常用的功能,适合小组内的文档编辑。");
System.out.println("------------------------------------------------------------");
System.out.println("请输入你想添加的项目号: ");
Scanner sc2=new Scanner(System.in);
String c=sc2.next();
switch (c) {
case "1":
Project p1 = new Project();
p1.setProjectName("小米官网");
p1.setDesName("开发完成类似于小米官网的web项目.");
if (first == -1) {
//这段代码放进来是防止我们的count在我们重复添加的是否依然自增,导致我们编号出现问题
p1.setProId(count++);
pro.add(p1);
//加载语句 TSUtility.loadSpecialEffects();
System.out.println("项目:" + p1.getProjectName() + "添加成功!");
first++;
} else {
System.out.println("项目:" + p1.getProjectName() + "已添加,请勿重复添加!");
}
break;
case "2":
Project p2 = new Project();
p2.setProjectName("公益在线商城");
p2.setDesName("猫宁Morning公益商城是中国公益性在线电子商城.");
if (second == -1) {
p2.setProId(count++);
pro.add(p2);
//加载语句
System.out.println("项目:" + p2.getProjectName() + "添加成功!");
second++;
} else {
System.out.println("项目:" + p2.getProjectName() + "已添加,请勿重复添加!");
}
break;
case "3":
Project p3 = new Project();
p3.setProjectName("博客系统");
p3.setDesName("Java博客系统,让每一个有故事的人更好的表达想法!");
if (third == -1) {
p3.setProId(count++);
pro.add(p3);
//加载语句
System.out.println("项目:" + p3.getProjectName() + "添加成功!");
third++;
} else {
System.out.println("项目:" + p3.getProjectName() + "已添加,请勿重复添加!");
}
break;
case "4":
Project p4 = new Project();
p4.setProjectName("在线协作文档编辑系统");
p4.setDesName("一个很常用的功能,适合小组内的文档编辑。");
if (forth == -1) {
p4.setProId(count++);
pro.add(p4);
//加载语句
forth++;
System.out.println("项目:" + p4.getProjectName() + "添加成功!");
} else {
System.out.println("项目:" + p4.getProjectName() + "已添加,请勿重复添加!");
}
break;
default:
System.out.println("项目不存在!");
break;
}
}
public void distributionPro(ArrayList<Programmer> team) throws TeamException {
System.out.println("当前团队有人员:");
System.out.println("编号" + "\t\t姓名" + "\t\t年龄" + "\t工资" + "\t\t职位" + "\t\t状态" + "\t\t奖金" + "\t\t股票" + "\t\t\t设备");
for (int j = 0; j < team.size(); j++) {
System.out.println(team.get(j));
}
System.out.println("请为当前团队给定一个名称:");
Scanner sc2 = new Scanner(System.in);
String teamName=sc2.next();
//用随机数获取项目,随机分配
Random r = new Random();
int rNum = r.nextInt(pro.size());
Project project = this.pro.get(rNum);
//设置开发项目团队名
project.setTeamName(teamName);
//设置开发项目的团队
project.setTeam(team);
//设置开发状态,true为开发中
project.setStatus(true);
//将对象放入项目集合当中
pro.set(rNum, project);
System.out.println("项目:" + pro.get(rNum) + "分配给团队:" + teamName);
}
/**
* + showPro() 查看项目当前状态、
*/
public void showPro() throws InterruptedException {
//加载语句
if (pro.size() == 0) {
System.out.println("当前没有项目,请先添加");
} else {
for (int i = 0; i < pro.size(); i++) {
System.out.println(pro.get(i));
}
}
}
// 删除选择的项目
public void delPro(int id) throws InterruptedException, TeamException {
boolean flag = false;
for (int i = 0; i < pro.size(); i++) {
if (pro.get(i).getProId() == id ) {
if(pro.get(i).isStatus() == false) {
pro.get(i).setStatus(true);
//不要忘了我们的自增变量要减
if(pro.get(i).getProjectName().equals("小米官网")) {
first = -1;
}else if(pro.get(i).getProjectName().equals("公益在线商城")){
second = -1;
}else if(pro.get(i).getProjectName().equals("博客系统")) {
third = -1;
}else if(pro.get(i).getProjectName().equals("在线协作文档编辑系统")) {
forth = -1;
}
pro.remove(i);
//集合中第i个元素的前一位I-1位元素的ID应该减一
for (i = id; i < pro.size(); i++) {
pro.get(i - 1).setProId(pro.get(i - 1).getProId() - 1);
}
flag = true;
}else if(pro.get(i).isStatus() == true) {
try{
throw new TeamException(pro.get(i) + "项目正在被开发,不能删除");
}catch(TeamException t) {
System.out.println("删除失败,原因:" + t.getMessage());
}
return;
}
}
}
if (flag) {
System.out.println("项目删除成功");
count--;
} else {
try {
throw new TeamException("项目不存在,删除失败");
} catch (TeamException t) {
System.out.println("删除失败,原因:" + t.getMessage());
}
}
}
/**
* 返回所有项目的集合
*/
public ArrayList<Project> getAllPro() {
return pro;
}
}
开发人员管理
public class NameListService {
protected ArrayList<Employee> employees =new ArrayList<Employee>();//初始化动态数组
//添加员工的id
protected int count = 1;//1
//初始化员工信息板块
{
employees.add(new Employee(count, "马云 ", 22, 3000));
employees.add(new Architect(++count, "马化腾", 32, 18000, new NoteBook("联想T4", 6000), 60000, 5000));
employees.add(new Programmer(++count, "李彦宏", 23, 7000, new PC("戴尔", "NEC 17寸")));
employees.add(new Programmer(++count, "刘强东", 24, 7300, new PC("戴尔", "三星 17寸")));
employees.add(new Architect(++count, "雷军 ", 50, 10000, new Printer("激光", "佳能2900"), 5000, 2000));
employees.add(new Programmer(++count, "任志强", 30, 16800, new PC("华硕", "三星 17寸")));
employees.add(new Designer(++count, "柳传志", 45, 35500, new PC("华硕", "三星 17寸"), 8000));
employees.add(new Architect(++count, "杨元庆", 35, 6500, new Printer("针式", "爱普生20k"), 15500, 1200));
employees.add(new Designer(++count, "史玉柱", 27, 7800, new NoteBook("惠普m6", 5800), 1500));
employees.add(new Programmer(++count, "丁磊 ", 26, 6600, new PC("戴尔", "NEC17寸")));
employees.add(new Programmer(++count, "张朝阳 ", 35, 7100, new PC("华硕", "三星 17寸")));
employees.add(new Designer(++count, "杨致远", 38, 9600, new NoteBook("惠普m6", 5800), 3000));
}
//初始化默认值
public void clearEmployees() {
this.employees=new ArrayList<Employee>();
count = 1;
}
//得到所有员工数据集合
public ArrayList<Employee> getAllEmployees() {
return employees;
}
//自定义异常
public Employee getEmployee(int id) throws TeamException {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == id) {
return employees.get(i);
}
}
throw new TeamException("该员工不存在");
}
//开发人员的添加
public void addEmployee() throws TeamException, InterruptedException {
boolean flag2=true;
while (flag2){
System.out.println("-------------------");
System.out.println("---开发人员添加界面---");
System.out.println("请输入想要添加的员工职位");
System.out.println("--1、无职位");
System.out.println("--2、程序员");
System.out.println("--3、设计师");
System.out.println("--4、架构师");
System.out.println("--5、退出本页面");
System.out.println("请选择:");
Scanner sc4 = new Scanner(System.in);
String choice = sc4.next();
switch (choice){
case "1":
//无职位
System.out.println("当前职位分配为:无");
System.out.println("请输入当前员工姓名:");
String name = sc4.next();
System.out.println("请输入当前员工年龄:");
int age =-1;
try{
age = sc4.nextInt();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
age=sc4.nextInt();
}
System.out.println("请输入当前员工工资:");
Double salary = -1.0;
try{
salary= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
salary=sc4.nextDouble();
}
Employee employee = new Employee(++count, name, age, salary);
employees.add(employee);
System.out.println("人员添加成功!");
break;
case "2":
// 程序员
System.out.println("当前职位分配为:程序员");
System.out.println("请输入当前员工姓名:");
String name0 = sc4.next();
System.out.println("请输入当前员工年龄:");
int age0 =-1;
try{
age0 = sc4.nextInt();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
}
System.out.println("请输入当前员工工资:");
Double salary0 = -1.0;
try{
salary0= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
salary0=sc4.nextDouble();
}
System.out.println("请为当前程序员配一台好的台式电脑:");
PC pc = new PC().addPC();
Programmer programmer = new Programmer(++count, name0, age0, salary0, pc);
employees.add(programmer);
System.out.println("人员添加成功!");
break;
case "3":
//设计师
System.out.println("当前职位分配为:设计师");
System.out.println("请输入当前员工姓名:");
String name1 = sc4.next();
System.out.println("请输入当前员工年龄:");
int age1 =-1;
try{
age1 = sc4.nextInt();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
age1=sc4.nextInt();
}
System.out.println("请输入当前员工工资:");
Scanner sc1=new Scanner(System.in);
Double salary1 = -1.0;
try{
salary1= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
salary1=sc4.nextDouble();
}
NoteBook noteBook = new NoteBook().addNoteBook();
System.out.println("请输入当前设计师的奖金:");
Double bonus = -1.0;
try{
bonus= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
bonus=sc4.nextDouble();
}
Designer designer = new Designer(++count, name1, age1, salary1, noteBook, bonus);
employees.add(designer);
System.out.println("人员添加成功!");
break;
case "4":
//架构师
System.out.println("当前职位分配为:架构师");
System.out.println("请输入当前员工姓名:");
String name2 = sc4.next();
System.out.println("请输入当前员工年龄:");
int age2 =-1;
try{
age2 = sc4.nextInt();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
age2=sc4.nextInt();
}
System.out.println("请输入当前员工工资:");
Double salary2= -1.0;
try{
salary2= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
salary2=sc4.nextDouble();
}
System.out.println("请输入当前架构师的奖金:");
Double bonus2 = -1.0;
try{
bonus2= sc4.nextDouble();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
bonus2=sc4.nextDouble();
}
System.out.println("请为当前架构师配一台好的打印机:");
Printer pt = new Printer().addPrinter();
System.out.println("请输入当前架构师的股票:");
int stock = -1;
try{
stock= sc4.nextInt();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新输入!");
String a = sc4.next();
stock=sc4.nextInt();
}
Architect architect = new Architect(++count, name2, age2, salary2, pt, bonus2, stock);
System.out.println(employees.add(architect));
System.out.println("架构师"+employees.hashCode());
System.out.println("人员添加成功!");
break;
case "5":
flag2 = false;
break;
default:
System.out.println("输入有误,请重新输入!");break;
}}
}
//开发人员查看
public void showEmployee() throws TeamException, InterruptedException {
boolean flag3 = true;
while (flag3) {
System.out.println("---------------------");
System.out.println("----开发人员查询界面----");
System.out.println("1、通过id查询");
System.out.println("2、查看全部人员");
System.out.println("3、退出本页面");
System.out.println("请选择:");
Scanner sc5 = new Scanner(System.in);
String choice=sc5.next();
switch (choice){
case "1":
System.out.println("请输入员工id");
int id=sc5.nextInt();
if (employees.size()>=id) {
System.out.println("ID\t 姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
System.out.println(employees.get(id - 1));
}else {
try{
throw new TeamException( "该id不存在,请重新输入!");
}catch(TeamException t){
System.out.println(t.getMessage());
}
}
break;
case "2":
System.out.println("ID\t 姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
for (int i = 0; i < employees.size(); i++) {
System.out.println(" " +employees.get(i));
}
break;
case "3":
flag3=false;break;
default:
System.out.println("输入有误,请重新输入!");break;
}
}}
//开发人员的修改
public void modifyEmployee() throws InterruptedException, TeamException {
boolean flag0=true;
while (flag0) {
System.out.println("---------------------");
System.out.println("----开发人员修改界面----");
System.out.println("---1、信息修改");
System.out.println("---2、退出本页面");
System.out.println("请选择:");
Scanner sc5 = new Scanner(System.in);
String choice=sc5.next();
switch (choice){
case "1":
boolean flag = false;
System.out.println("请输入员工id");
int id = sc5.nextInt();
for (int i = 0; i < employees.size(); i++) {
Employee emp = employees.get(i);
if (employees.get(i).getId() == id) {
System.out.print("姓名(" + emp.getName() + "):");
System.out.println("请输入修改后的员工姓名");
String name =sc5.next();
emp.setName(name);
System.out.print("年龄(" + emp.getAge() + "):");
System.out.println("请输入修改后员工年龄");
int age = sc5.nextInt();
emp.setAge(age);
System.out.print("工资(" + emp.getSalary() + "):");
System.out.println("请输入修改后员工工资");
double salary =sc5.nextDouble();
emp.setSalary(salary);
employees.set(i,emp);
flag = true;
}
}
if (flag) {
System.out.println("修改成功!");
} else {
try {
throw new TeamException("该员工不存在");
} catch (TeamException e) {
e.printStackTrace();
};break;
}
case "2":
flag0=false;break;
default:
System.out.println("输入有误,请重新输入!");break;
}}}
//开发人员的删除
public void deleteEmploee() throws TeamException, InterruptedException {
boolean flag4 = true;
while (flag4) {
System.out.println("---------------------");
System.out.println("----开发人员删除界面----");
System.out.println("---1、人员删除");
System.out.println("---2、退出本页面");
System.out.println("请选择:");
Scanner sc5 = new Scanner(System.in);
String choice = sc5.next();
switch (choice) {
case "1":
System.out.println("请输入员工id");
int id = sc5.nextInt();
System.out.println(employees.size());
for (int i = 0; i < employees.size(); i++) {
if (employees.size()>=id) {
System.out.println( "员工:"+ employees.get(i).getName() + "删除成功!");
employees.remove(i);
for (i = id; i <= employees.size(); i++) {
//这个代码表示,将我们第i个对象从集合中删除之后,我们后一个元素的ID就应该相应的减一。
employees.get(i - 1).setId(employees.get(i - 1).getId() - 1);
}
}else {
System.out.println("输入人员不存在,请重新输入!");
}}
break;
case "2":
flag4 = false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
//对应功能下的一级界面界面视图
public void nameViewInterface() throws TeamException, InterruptedException {
boolean flag5=true;
while (flag5) {
System.out.println("---------------------");
System.out.println("----开发人员管理界面----");
System.out.println("---1、开发人员的添加");
System.out.println("---2、开发人员的修改");
System.out.println("---3、开发人员的查询");
System.out.println("---4、开发人员的删除");
System.out.println("---5、退出本界面");
System.out.println("请选择:");
Scanner sc4 = new Scanner(System.in);
String choice2 = sc4.next();
switch (choice2) {
case "1":
try {
addEmployee();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新添加!");
}
break;
case "2":
try {
modifyEmployee();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新添加!");
}
break;
case "3":
try {
showEmployee();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新添加!");
}
break;
case "4":
try {
deleteEmploee();
}catch (InputMismatchException e){
System.out.println("输入错误,请重新添加!");
}
break;
case "5":
flag5=false;
break;
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
}
团队管理
public class TeamService {
int counter = 1;
//定义集合最大值,即我们团队最多只能放入多少个成员
final int MAX_MEMBER = 5;
ArrayList<Programmer> team = new ArrayList<Programmer>();
//团队的实际人数
int total = 0;
//创建新对象放数据
public void clearTeam() {
this.team=new ArrayList<Programmer>();
total = 0;
counter = 1;
}
//查看所有团队
public ArrayList<Programmer> getTeam() throws TeamException {
for (int i = 0; i < team.size(); i++) {
team.set(i, this.team.get(i));
}
return team;
}
//增加团队成员
public void addMember(Employee e) throws TeamException {
//判断成员是否满5个
if (total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
//判断添加的成员是否为开发人员,即他是否是我们程序员类的实例对象
if (!(e instanceof Programmer)) {
throw new TeamException("该成员不是开发人员,无法添加");
}
//向下转型,利用父类引用转为子类对象,调用子类特有方法
Programmer p = (Programmer) e;
//如果我们的集合当中已经有了这个成员,抛出异常。
if (team.contains(p)) {
throw new TeamException("该员工已在本团队中");
}
//如果这个成员被另一个团队录取,
if (!p.isStatus()) {
throw new TeamException("该员工已是某团队成员");
}
//定义工程师,架构师,和程序员的数量,初始化为0;
int numOfArch = 0, numOfDesi = 0, numOfPrg = 0;
for (int i = 0; i < total; i++) {
if (team.get(i) instanceof Architect) {
numOfArch++;
} else if (team.get(i) instanceof Designer) {
numOfDesi++;
} else if (team.get(i) instanceof Programmer) {
numOfPrg++;
}
}
//判断如果我们的成员类型超过了规定的数量,则程序抛出异常
if (p instanceof Architect) {
if (numOfArch >= 1) {
throw new TeamException("团队中至多只能有一名架构师");
}
} else if (p instanceof Designer) {
if (numOfDesi >= 2) {
throw new TeamException("团队中至多只能有两名设计师");
}
} else if (p instanceof Programmer) {
if (numOfPrg >= 3) {
throw new TeamException("团队中至多只能有三名程序员");
}
}
//完成以上条件判断以后,即可将对象添加到集合当中。
//改状态
p.setStatus(false);
//改团队ID
p.setMemberId(counter++);
//放入我们的对象进集合
team.add(total++, p);
}
//删除指定memberId的程序员
public void removeMember(int MemberId) throws TeamException {
int n = 0;
boolean flag = false;
for (; n < total; n++) {
if (team.get(n).getMemberId() == MemberId) {
team.get(n).setStatus(true);
team.remove(n);
for (n = MemberId; n < total; n++) {
//将第i个对象从集合中删除之后,前一个对象的ID就应该相应的减一。
team.get(n - 1).setMemberId(team.get(n - 1).getMemberId() - 1);
}
flag = true;
break;
}
}
if (flag) {
System.out.println("成员删除成功!");
total--;
counter--;
} else {
try{
throw new TeamException("删除失败,该员工不存在");
}catch(TeamException t) {
System.out.println("原因:" + t.getMessage());
}
}
}
public ArrayList<Programmer> getTeamX() {
return team;
}
NameListService nls = new NameListService();
ArrayList<ArrayList<Programmer>> team1 = new ArrayList<ArrayList<Programmer>>();
//对应功能下的二级界面界面视图
public void teamInterface(NameListService nls) throws TeamException, InterruptedException {
boolean flag = true;
while (flag) {
System.out.println("-----------------------------开发团队调度软件---------------------------");
listAllEmployees(nls);
System.out.println(" 1-查看团队列表 2-添加团队成员 3-删除团队成员 4-退出本界面 请选择(1-4):");
Scanner sc4 = new Scanner(System.in);
String choice = sc4.next();
switch (choice) {
case "1":
showTeam();
break;
case "2":
addMember(nls);
break;
case "3":
deleteMember();
break;
case "4":
ArrayList<Programmer> teamX = getTeamX();
if(teamX.size() == 0) {
clearTeam();
}else{
team1.add(teamX);
clearTeam();}
flag=false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
//查看团队列表
public void listAllEmployees(NameListService nls) {
//将我的员工集合给新的集合
ArrayList<Employee> emp =nls.employees;
if (emp.size() == 0) {
System.out.println("没有成员记录!");
} else {
System.out.println("编号" + "\t\t姓名" + "\t\t年龄" + "\t工资" + "\t\t职位" + "\t\t状态" + "\t\t奖金" + "\t\t股票" + "\t\t\t设备");
for (int i = 0; i < emp.size(); i++) {
System.out.println(emp.get(i));
}
}
System.out.println("---------------------------------------------------------------------------");
}
public void showTeam() throws TeamException {
System.out.println("\n--------------------团队成员列表---------------------\n");
//把团队成员给新集合Pro
ArrayList<Programmer> pro =getTeam();
if (pro.size() == 0) {
System.out.println("目前没有开发团队!");
} else {
System.out.println("TID/ID" + "\t姓名" + "\t\t年龄" + "\t工资" + "\t\t职位" + "\t\t状态" + "\t\t奖金" + "\t\t股票" + "\t\t\t设备");
for (int i = 0; i < pro.size(); i++) {
System.out.println(pro.get(i));
}
}
System.out.println("-------------------------------------------------------------------------------");
}
//添加成员
public void addMember(NameListService nls) {
System.out.println("---------------------添加成员---------------------");
System.out.println("请输入要添加的员工ID:");
Scanner sc4 = new Scanner(System.in);
int id = sc4.nextInt();
try {
//找到指定员工存入我们的团队集合中
Employee e = nls.getEmployee(id);
addMember(e);
System.out.println("添加成功");
} catch (TeamException t) {
System.out.println("添加失败,原因:" + t.getMessage());
}
}
//删除成员
public void deleteMember() throws TeamException {
System.out.println("---------------------删除成员---------------------");
System.out.println("当前团队有成员:");
getTeam();
System.out.println("请输入你要删除的员工的团队TID:");
Scanner sc4 = new Scanner(System.in);
int id = sc4.nextInt();
if (nls.employees.size() >= id) {
removeMember(id);
} else {
System.out.println("输入人员不存在,请重新输入!");
}
}
//对应功能下的一级界面界面视图
public ArrayList<ArrayList<Programmer>> getManyTeam(NameListService nls) throws TeamException, InterruptedException {
boolean flag = true;
while (flag) {
System.out.println("=============================");
System.out.println("|---------团队调度界面---------|");
System.out.println("=============================");
System.out.println("1-添加团队 2-查看团队 3-删除团队 4-退出 请选择(1-4):");
Scanner sc4 = new Scanner(System.in);
String choice = sc4.next();
switch (choice) {
case "1":
//进入添加团队方法
teamInterface(nls);
break;
case "2":
if(team1.size() == 0) {
System.out.println("当前没有团队,请先添加");
}else{
System.out.println("-------团队列表--------");
//表头
for (int i = 0; i < team1.size(); i++) {
//表头
System.out.println("TID/ID" + "\t姓名" + "\t\t年龄" + "\t工资" + "\t\t职位" + "\t\t状态" + "\t\t奖金" + "\t\t股票" + "\t\t\t设备");
//team1.add(teamSvc.getTeamX());
System.out.println("第" + (i + 1) + "个团队:");
for (int j = 0; j < team1.get(i).size(); j++) {
System.out.println(team1.get(i).get(j).toString());
}
System.out.println("------------------");
}
}
break;
case "3":
System.out.println("请输入想要删除第几个团队");
int num = sc4.nextInt();
if(num <= 0||sc4.hasNextInt()==false) {
try{
throw new TeamException("团队号输入不正确,请重新输入!");
}catch(TeamException t){
System.out.println(t.getMessage());
}
}
else if (num <= team1.size()) {
//当删除整个团队时,状态应该随之修改这个bug要及其注意,防止索引越界问题
ArrayList<Employee> t = nls.getAllEmployees();
for (int n = 0; n < t.size(); n++) {
for (int i = num; i <= team1.size(); i++) {
for (int j = 0; j < team1.get(i-1).size(); j++) {
//这里的team1.get(num -1)之所以为num-1,是因为集合中这个元素不能改变,
if(team1.get(num-1).get(j).getId() == t.get(n).getId()) {
//使用eProgrammer类转换集合对象的类型,已达到设置状态的目的。
Programmer employee = (Programmer)t.get(n);
employee.setStatus(true);
}
}
}
}
team1.remove(num - 1);
System.out.println("删除成功");
} else {
System.out.println("团队中没有这个团队,请正常输入,目前只有" + team1.size() + "个团队");
}
break;
case "4":
flag = false;
break;
default:
break;
}
}
return team1;
}
public ArrayList<ArrayList<Programmer>> getTeam1() {
return team1;
}
}