
TSUtility.java
package com.klvchen.team.view;
import java.util.*;
import org.junit.jupiter.api.Test;
//项目中提供了TSUtility.java类,可用来方便地实现键盘访问。
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
//该方法读取键盘,如果用户键入'1’-'4’中的任意字符,则方法返回。返回值为用户键入的字符
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);
}
//该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
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;
}
}
TeamView.java
package com.klvchen.team.view;
import com.klvchen.team.domain.*;
import com.klvchen.team.service.*;
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.println("确认是否退出(Y/N): ");
char isExit = TSUtility.readConfirmSelection();
if(isExit == 'Y') {
loopFlag = false;
}
break;
}
}
}
//显示所有的员工信息
private void listAllEmployees() {
System.out.println("------------------------------开发团队调度软件------------------------------");
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股票\t领用设备");
for(int i = 0; i < employees.length; i++) {
System.out.println(employees[i]);
}
}
System.out.println("------------------------------------------------------------");
}
private void getTeam() {
System.out.println("------------------------------团队成员列表------------------------------");
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股票");
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.println("确认是否删除(Y/N)");
char isDelete = TSUtility.readConfirmSelection();
if(isDelete == 'N') {
return;
}
try {
teamSvc.removeMember(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();
}
}
运行测试
