文章目錄
全部源碼:https://github.com/name365/JavaSE-30Day
轉載自atguigu.com
項目三 開發團隊調度軟件
目標
- 模擬實現一個基於文本界面的《開發團隊調度軟件》
- 熟悉Java面向對象的高級特性,進一步掌握編程技巧和調試技巧
- 主要涉及以下知識點:
- 類的繼承性和多態性
- 對象的值傳遞、接口
- static和final修飾符
- 特殊類的使用:包裝類、抽象類、內部類
- 異常處理
需求說明
-
該軟件實現以下功能:
- 軟件啓動時,根據給定的數據創建公司部分成員列表(數組)
- 根據菜單提示,基於現有的公司成員,組建一個開發團隊以開發一個新的項目
- 組建過程包括將成員插入到團隊中,或從團隊中刪除某成員,還可以列出團隊中現有成員的列表
- 開發團隊成員包括架構師、設計師和程序員
-
本軟件採用單級菜單方式工作。當軟件運行時,主界面顯示公司成員的列表,如下:
-
當選擇“添加團隊成員”菜單時,將執行從列表中添加指定(通過ID)成員到開發團隊的功能:
-
添加成功後,按回車鍵將重新顯示主界面。
-
開發團隊人員組成要求:
- 最多一名架構師
- 最多兩名設計師
- 最多三名程序員
-
如果添加操作因某種原因失敗,將顯示類似以下信息(失敗原因視具體原因而不同):
-
失敗信息包含以下幾種:
- 成員已滿,無法添加
- 該成員不是開發人員,無法添加
- 該員工已在本開發團隊中
- 該員工已是某團隊成員–該員正在休假,無法添加
- 團隊中至多隻能有一名架構師
- 團隊中至多隻能有兩名設計師
- 團隊中至多隻能有三名程序員
-
當選擇“刪除團隊成員”菜單時,將執行從開發團隊中刪除指定(通過TeamID)成員的功能:
-
刪除成功後,按回車鍵將重新顯示主界面。
-
當選擇“團隊列表”菜單時,將列出開發團隊中的現有成員,例如:
軟件設計結構
- 該軟件由以下三個模塊組成:
-
github.view模塊爲主控模塊,負責菜單的顯示和處理用戶操作
-
github.service模塊爲實體對象(Employee及其子類如程序員等)的管理模塊,NameListService和TeamService類分別用各自的數組來管理公司員工和開發團隊成員對象
-
domain模塊爲Employee及其子類等JavaBean類所在的包
-
github.domain模塊中包含了所有實體類:
- 其中程序員(Programmer)及其子類,均會領用某種電子設備(Equipment)。
第1步—創建項目基本組件
- 完成以下工作:
-
創建TeamSchedule項目
-
按照設計要求,創建所有包
-
將項目提供的幾個類複製到相應的包中(view包中:TSUtility.java; service包中:Data.java)
-
- TSUtility.java
import java.util.*;
/**
*
* @Description 項目中提供了TSUtility.java類,可用來方便地實現鍵盤訪問。
* @author subei Email:[email protected]
* @version
* @date 2020年5月6日下午3:10:30
*
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
*
* @Description 該方法讀取鍵盤,如果用戶鍵入’1’-’4’中的任意字符,則方法返回。返回值爲用戶鍵入字符。
* @author subei
* @date 2020年5月6日下午3:10:48
* @return
*/
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;
}
/**
*
* @Description 該方法提示並等待,直到用戶按回車鍵後返回。
* @author subei
* @date 2020年5月6日下午3:11:04
*/
public static void readReturn() {
System.out.print("按回車鍵繼續...");
readKeyBoard(100, true);
}
/**
*
* @Description 該方法從鍵盤讀取一個長度不超過2位的整數,並將其作爲方法的返回值。
* @author subei
* @date 2020年5月6日下午3:11:17
* @return
*/
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;
}
/**
*
* @Description 從鍵盤讀取‘Y’或’N’,並將其作爲方法的返回值。
* @author subei
* @date 2020年5月6日下午3:11:33
* @return
*/
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;
}
}
- Data.java
public class Data {
public static final int EMPLOYEE = 10;
public static final int PROGRAMMER = 11;
public static final int DESIGNER = 12;
public static final int ARCHITECT = 13;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
//Employee : 10, id, name, age, salary
//Programmer: 11, id, name, age, salary
//Designer : 12, id, name, age, salary, bonus
//Architect : 13, id, name, age, salary, bonus, stock
public static final String[][] EMPLOYEES = {
{"10", "1", "馬雲", "22", "3000"},
{"13", "2", "馬化騰", "32", "18000", "15000", "2000"},
{"11", "3", "李彥宏", "23", "7000"},
{"11", "4", "劉強東", "24", "7300"},
{"12", "5", "雷軍", "28", "10000", "5000"},
{"11", "6", "任志強", "22", "6800"},
{"12", "7", "柳傳志", "29", "10800","5200"},
{"13", "8", "楊元慶", "30", "19800", "15000", "2500"},
{"12", "9", "史玉柱", "26", "9800", "5500"},
{"11", "10", "丁磊", "21", "6600"},
{"11", "11", "張朝陽", "25", "7100"},
{"12", "12", "楊致遠", "27", "9600", "4800"}
};
//如下的EQIPMENTS數組與上面的EMPLOYEES數組元素一一對應
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, type, name
public static final String[][] EQIPMENTS = {
{},
{"22", "聯想T4", "6000"},
{"21", "戴爾", "NEC17寸"},
{"21", "戴爾", "三星 17寸"},
{"23", "激光", "佳能 2900"},
{"21", "華碩", "三星 17寸"},
{"21", "華碩", "三星 17寸"},
{"23", "針式", "愛普生20K"},
{"22", "惠普m6", "5800"},
{"21", "戴爾", "NEC 17寸"},
{"21", "華碩","三星 17寸"},
{"22", "惠普m6", "5800"}
};
}
- 按照設計要求,在github.domain包中,創建Equipment接口及其各實現子類代碼
- 按照設計要求,在github.domain包中,創建Employee類及其各子類代碼
- 檢驗代碼的正確性
Equipment接口及其實現子類的設計
-
說明:
- model 表示機器的型號
- display 表示顯示器名稱
- type 表示機器的類型
-
根據需要提供各屬性的get/set方法以及重載構造器
-
實現類實現接口的方法,返回各自屬性的信息
-
Equipment類
public interface Equipment {
String getDescription();
}
- PC類
public class PC implements Equipment{
private String model; //表示機器的型號
private String display; //表示顯示器名稱
@Override
public String getDescription() {
return model + "(" + display + ")";
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
public PC() {
super();
}
public PC(String model, String display) {
super();
this.model = model;
this.display = display;
}
}
- Printer類
public class Printer implements Equipment{
private String name; //表示機器的名稱
private String type; //表示機器的類型
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String getDescription() {
return name + "(" + type + ")";
}
public Printer() {
super();
}
public Printer(String name, String type) {
super();
this.name = name;
this.type = type;
}
}
- NoteBook類
public class NoteBook implements Equipment{
private String model;//表示機器的型號
private double price;//表示價格
@Override
public String getDescription() {
return model + "(" + price + ")";
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public NoteBook() {
super();
}
public NoteBook(String model, double price) {
super();
this.model = model;
this.price = price;
}
}
Employee類及其子類的設計
說明
-
memberId用來記錄成員加入開發團隊後在團隊中的ID
-
Status是項目service包下自定義的類,聲明三個對象屬性,分別表示成員的狀態。
- FREE-空閒
- BUSY-已加入開發團隊
- VOCATION-正在休假
-
equipment 表示該成員領用的設備
-
可根據需要爲類提供各屬性的get/set方法以及重載構造器
-
Employee類
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
protected String getDetails() {
return id + "\t" + name + "\t" + age+ "\t" +salary;
}
@Override
public String toString() {
return getDetails();
}
}
- Programer類
import github.service.Status;
public class Programer extends Employee {
private int memberId;
private Status status = Status.FREE;
private Equipment equipment;
public Programer() {
}
public Programer(int id, String name, int age, double salary, Equipment equipment) {
super(id, name, age, salary);
this.equipment = equipment;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
protected String getMemberDetails() {
return getMemberId() + "/" + getDetails();
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t程序員";
}
@Override
public String toString() {
return getDetails() + "\t程序員\t" + status + "\t\t\t" + equipment.getDescription();
}
}
- Status類
/**
*
* @Description 表示員工的狀態
* @author subei Email:[email protected]
* @version
* @date 2020年5月6日下午5:09:18
*
*/
public class Status {
private final String NAME;
private Status(String name) {
this.NAME = name;
}
public static final Status FREE = new Status("FREE");
public static final Status VOCATION = new Status("VOCATION");
public static final Status BUSY = new Status("BUSY");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
Employee類及其子類的設計
-
說明:
- bonus表示獎金
- stock 表示公司獎勵的股票數量
-
可根據需要爲類提供各屬性的get/set方法以及重載構造器
-
Designer類
public class Designer extends Programer {
private double bonus; // 表示獎金
public Designer() {
super();
}
public Designer(int id, String name, int age, double salary, Equipment equipment, double bonus) {
super(id, name, age, salary, equipment);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t設計師\t" + getBonus();
}
@Override
public String toString() {
return getDetails() + "\t設計師\t" + getStatus() + "\t" +
getBonus() +"\t\t" + getEquipment().getDescription();
}
}
- Architect類
public class Architect extends Designer {
private int stock; // 表示公司獎勵的股票數量
public Architect() {
super();
}
public Architect(int id, String name, int age, double salary, Equipment equipment, double bonus, int stock) {
super(id, name, age, salary, equipment, bonus);
this.stock = stock;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t架構師\t" +
getBonus() + "\t" + getStock();
}
@Override
public String toString() {
return getDetails() + "\t架構師\t" + getStatus() + "\t" +
getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
}
}
第2步—實現service包中的類
- 按照設計要求編寫NameListService類
- 在NameListService類中臨時添加一個main方法中,作爲單元測試方法。
- 在方法中創建NameListService對象,然後分別用模擬數據調用該對象的各個方法,以測試是否正確。注:測試應細化到包含了所有非正常的情況,以確保方法完全正確。
- 重複1-3步,完成TeamService類的開發
NameListService類的設計
-
功能:負責將Data中的數據封裝到Employee[]數組中,同時提供相關操作Employee[]的方法。
-
說明:
- employees用來保存公司所有員工對象
- NameListService()構造器:
- 根據項目提供的Data類構建相應大小的employees數組
- 再根據Data類中的數據構建不同的對象,包括Employee、Programmer、Designer和Architect對象,以及相關聯的Equipment子類的對象
- 將對象存於數組中
- Data類位於com.atguigu.team.service包中
- getAllEmployees ()方法:獲取當前所有員工。
- 返回:包含所有員工對象的數組
- getEmployee(id : int)方法:獲取指定ID的員工對象。
- 參數:指定員工的ID
- 返回:指定員工對象
- 異常:找不到指定的員工
- 在service子包下提供自定義異常類:TeamException
- 另外,可根據需要自行添加其他方法或重載構造器
-
NameListService類
import github.domain.*;
import static github.service.Data.*;
/**
*
* @Description 負責將Data中的數據封裝到Employee[]數組中,同時提供相關操作Employee[]的方法。
* @author subei Email:[email protected]
* @version v1.0
* @date 2020年5月6日下午5:42:19
*
*/
public class NameListService {
private Employee[] employees;
/**
* 給employees及數組元素進行初始化
*/
public NameListService() {
/*
* 1.根據項目提供的Data類構建相應大小的employees數組
* 2.再根據Data類中的數據構建不同的對象,包括Employee、Programmer、Designer
* 和Architect對象,以及相關聯的Equipment子類的對象 3.將對象存於數組中
*/
employees = new Employee[EMPLOYEES.length];
for (int i = 0; i < employees.length; i++) {
// 獲取員工的類型
int type = Integer.parseInt(EMPLOYEES[i][0]);
//獲取employee的4個基本信息
int id = Integer.parseInt(EMPLOYEES[i][1]);
String name = EMPLOYEES[i][2];
int age = Integer.parseInt(EMPLOYEES[i][3]);
double salary = Double.parseDouble(EMPLOYEES[i][4]);
//
Equipment eq;
double bonus;
int stock;
switch(type){
case EMPLOYEE:
employees[i] = new Employee(id, name, age, salary);
break;
case PROGRAMMER:
eq = createEquipment(i);
employees[i] = new Programer(id, name, age, salary, eq);
break;
case DESIGNER:
eq = createEquipment(i);
bonus = Integer.parseInt(EMPLOYEES[i][5]);
employees[i] = new Designer(id, name, age, salary, eq, bonus);
break;
case ARCHITECT:
eq = createEquipment(i);
bonus = Integer.parseInt(EMPLOYEES[i][5]);
stock = Integer.parseInt(EMPLOYEES[i][6]);
employees[i] = new Architect(id, name, age, salary, eq, bonus,
stock);
break;
}
}
}
/**
*
* @Description 獲取指定index上的員工的設備
* @author subei
* @date 2020年5月6日下午6:15:23
* @param i
* @return
*/
private Equipment createEquipment(int index) {
int type = Integer.parseInt(EQIPMENTS[index][0]);
switch (type) {
case PC:
return new PC(EQIPMENTS[index][1], EQIPMENTS[index][2]);
case NOTEBOOK:
int price = Integer.parseInt(EQIPMENTS[index][2]);
return new NoteBook(EQIPMENTS[index][1], price);
case PRINTER:
return new Printer(EQIPMENTS[index][1], EQIPMENTS[index][2]);
}
return null;
}
/**
*
* @Description 獲取當前所有員工。
* @author subei
* @date 2020年5月6日下午8:22:47
* @return
*/
public Employee[] getAllEmployees() {
return employees;
}
/**
*
* @Description 獲取指定ID的員工對象。
* @author subei
* @date 2020年5月6日下午8:26:12
* @param id
* @return
* @throws TeamException
*/
public Employee getEmployee(int id) throws TeamException{
for(Employee e : employees){
if(e.getId() == id){
return e;
}
}
throw new TeamException("找不到指定的員工");
}
}
- TeamException類
/**
*
* @Description 自定義異常類
* @author subei Email:[email protected]
* @version
* @date 2020年5月6日下午8:29:54
*
*/
public class TeamException extends Exception{
static final long serialVersionUID = -33875169124229948L;
public TeamException(){
super();
}
public TeamException(String img){
super(img);
}
}
- NameListServiceTest類——測試NameListService類
import org.junit.Test;
import github.domain.Employee;
import github.service.NameListService;
import github.service.TeamException;
/**
*
* @Description 對NameListService類的測試
* @author subei Email:[email protected]
* @version
* @date 2020年5月6日下午8:37:51
*
*/
public class NameListServiceTest {
@Test
public void testGetAllEmployees(){
NameListService service = new NameListService();
Employee[] employees = service.getAllEmployees();
for(int i = 0;i < employees.length;i++){
System.out.println(employees[i]);
}
}
@Test
public void testGetEmployee(){
int id = 101;
NameListService listService = new NameListService();
try {
Employee emp = listService.getEmployee(id);
System.out.println(emp);
} catch (TeamException e) {
System.out.println(e.getMessage());
}
}
}
TeamService類的設計
- 功能:關於開發團隊成員的管理:添加、刪除等。
- 說明:
- counter爲靜態變量,用來爲開發團隊新增成員自動生成團隊中的唯一ID,即memberId。(提示:應使用增1的方式)
- MAX_MEMBER:表示開發團隊最大成員數
- team數組:用來保存當前團隊中的各成員對象
- total:記錄團隊成員的實際人數
- getTeam()方法:返回當前團隊的所有對象
- 返回:包含所有成員對象的數組,數組大小與成員人數一致
- addMember(e: Employee)方法:向團隊中添加成員
- 參數:待添加成員的對象
- 異常:添加失敗,TeamException中包含了失敗原因
- removeMember(memberId: int)方法:從團隊中刪除成員
- 參數:待刪除成員的memberId
- 異常:找不到指定memberId的員工,刪除失敗
- 另外,可根據需要自行添加其他方法或重載構造器
import github.domain.Architect;
import github.domain.Designer;
import github.domain.Employee;
import github.domain.Programer;
/**
*
* @Description 關於開發團隊成員的管理:添加、刪除等。
* @author subei Email:[email protected]
* @version
* @date 2020年5月6日下午9:14:19
*
*/
public class TeamService {
private static int counter = 1; //爲開發團隊新增成員自動生成團隊中的唯一ID
private final int MAX_MEMBER = 5; //限定開發團隊的人數
private Programer[] team = new Programer[MAX_MEMBER]; //用來保存當前團隊中的各成員對象
private static int total = 0; //記錄團隊成員的實際人數
public TeamService() {
super();
}
/**
*
* @Description 獲取當前團隊的所有對象,即返回team中所有程序員構成的數組
* @author subei
* @date 2020年5月6日下午9:19:18
* @return
*/
public Programer[] getTeam() {
Programer[] team = new Programer[total];
for (int i = 0; i < total; i++) {
team[i] = this.team[i];
}
return team;
}
/**
*
* @Description 向團隊中添加成員,即將指定員工添加到開發團隊中
* @author subei
* @date 2020年5月6日下午9:23:06
* @param e
* @throws TeamException
*/
public void addMember(Employee e) throws TeamException{
// 失敗信息包含以下幾種:
// – 成員已滿,無法添加
if(total >= MAX_MEMBER){
throw new TeamException("成員已滿,無法添加");
}
// – 該成員不是開發人員,無法添加
if(!(e instanceof Programer)){
throw new TeamException("該成員不是開發人員,無法添加");
}
// – 該員工已在本開發團隊中
if(isExist(e)){
throw new TeamException("該員工已在本開發團隊中");
}
// – 該員工已是某團隊成員
// – 該員正在休假,無法添加
Programer p = (Programer)e; //一定不會出現類型轉化異常:ClassCaseException
if("BUSY".equalsIgnoreCase(p.getStatus().getNAME())){
throw new TeamException("該員工已是某團隊成員");
}else if("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())){
throw new TeamException("該員正在休假,無法添加");
}
// – 團隊中至多隻能有一名架構師
// – 團隊中至多隻能有兩名設計師
// – 團隊中至多隻能有三名程序員
//獲取team已有成員中架構師,設計師,程序員的人數
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 Programer){
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 Programer){
if(numOfPro >= 3){
throw new TeamException("團隊中至多隻能有三名程序員");
}
}
//錯誤的寫法
// if(p instanceof Architect && numOfArch >= 1){
// throw new TeamException("團隊中至多隻能有一名架構師");
// }else if(p instanceof Designer && numOfDes >= 2){
// throw new TeamException("團隊中至多隻能有兩名設計師");
// }else if(p instanceof Programer && numOfPro >= 3){
// throw new TeamException("團隊中至多隻能有三名程序員");
// }
//p的屬性賦值
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
//將p添加到現有的team中
team[total++] = p;
}
private boolean isExist(Employee e) {
for(int i = 0;i < total;i++){
return team[i].getId() == e.getId();
}
return false;
}
/**
*
* @Description 從團隊中刪除成員,即刪除指定memberId的程序員
* @author subei
* @date 2020年5月6日下午9:23:44
* @param memberId
* @throws TeamException
*/
public void removeMember(int memberId) throws TeamException{
int n = 0;
//找到指定memberId的員工,並刪除
for (n = 0; n < total; n++) {
if (team[n].getMemberId() == memberId) {
team[n].setStatus(Status.FREE);
break;
}
}
//如果遍歷一遍,都找不到,則報異常
if (n == total)
throw new TeamException("找不到該成員,無法刪除");
//後面的元素覆蓋前面的元素
for (int i = n + 1; i < total; i++) {
team[i - 1] = team[i];
}
//寫法一
// team[total - 1] = null;
// total --;
//寫法二
team[--total] = null;
}
}
第3步—實現view包中類
- 按照設計要求編寫TeamView類,逐一實現各個方法,並編譯
- 執行main方法中,測試軟件全部功能
- 說明:
- listSvc和teamSvc屬性:供類中的方法使用
- enterMainMenu()方法:主界面顯示及控制方法。
- 以下方法僅供enterMainMenu()方法調用:
- listAllEmployees()方法:以表格形式列出公司所有成員
- getTeam()方法:顯示團隊成員列表操作
- addMember()方法:實現添加成員操作
- deleteMember()方法:實現刪除成員操作
import github.domain.Employee;
import github.domain.Programer;
import github.service.NameListService;
import github.service.TeamException;
import github.service.TeamService;
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public void enterMainMenu() {
boolean loopFlag = true;
char num = 0;
while (loopFlag) {
//小細節
if (num != '1') {
listAllEmployees();
}
System.out.print("1-團隊列表 2-添加團隊成員 3-刪除團隊成員 4-退出 請選擇(1-4):");
num = TSUtility.readMenuSelection();
switch (num) {
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;
}
}
}
/**
*
* @Description 以表格形式列出公司所有成員
* @author subei
* @date 2020年5月6日下午10:36:51
*/
private void listAllEmployees() {
System.out.println("\n-------------------------------開發團隊調度軟件--------------------------------\n");
Employee[] emps = listSvc.getAllEmployees();
if (emps == null || emps.length == 0) {
System.out.println("沒有客戶記錄!");
} else {
System.out.println("ID\t姓名\t年齡\t工資\t職位\t狀態\t獎金\t股票\t領用設備");
}
for (int i = 0;i < emps.length;i++) {
System.out.println(emps[i]);
}
System.out.println("-------------------------------------------------------------------------------");
}
/**
*
* @Description 顯示團隊成員列表操作
* @author subei
* @date 2020年5月6日下午10:37:45
*/
private void getTeam() {
// System.out.println("查看團隊開發情況");
System.out.println("\n--------------------團隊成員列表---------------------\n");
Programer[] 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("-----------------------------------------------------");
}
/**
*
* @Description 實現添加成員操作
* @author subei
* @date 2020年5月6日下午10:38:43
*/
private void addMember() {
// System.out.println("添加團隊成員");
System.out.println("---------------------添加成員---------------------");
System.out.print("請輸入要添加的員工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();
}
/**
*
* @Description 實現刪除成員操作
* @author subei
* @date 2020年5月6日下午10:38:28
*/
private void deleteMember() {
// System.out.println("刪除團隊成員");
System.out.println("---------------------刪除成員---------------------");
System.out.print("請輸入要刪除員工的TID:");
int id = TSUtility.readInt();
System.out.print("確認是否刪除(Y/N):");
char dn = TSUtility.readConfirmSelection();
if (dn == 'N'){
return;
}
try {
teamSvc.removeMember(id);
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();
}
}
- 整體結構
結束
這個項目,先是自己寫了一半,就寫不下去了。之後看源碼,看的似懂非懂,最後又去看視頻,看到11:00,然後改bug改了1小時。最終,大體上清楚了,可還是有些地方比較懵。這裏記錄的是最後的源碼,可以運行!!!
整個Java全棧系列都是筆者自己敲的筆記。寫作不易,如果可以,點個讚唄!✌