JavaSE進階
2、編寫異常類TeamException
用于封裝整個項目中的異常,例如
添加員工到開發團隊時會遇到如下失敗情況
例如從團隊中刪除員工會遇到如下失敗情況
- 找不到該成員,無法刪除
package com.atguigu.service;
public class TeamException extends Exception {
??? public TeamException() { ??? super(); ??? }
??? public TeamException(String message) { ??????? super(message); ??? } }
|
3、編寫業務類NameListService
service模塊為實體對象(Employee及其子類如程序員等)的管理模塊, NameListService和TeamService類分別用各自的數組來管理公司員工和開發團隊成員對象
package com.atguigu.service;
import com.atguigu.bean.Employee;
public class NameListService { ??? private Employee[] employees;
??? public Employee[] getAllEmployees() { ??????? return employees; ??? }
??? public Employee getEmployee(int id) throws TeamException { ??????? for (Employee e : employees) { ??????????? if (e.getId() == id) ??????????????? return e; ??????? } ??????? throw new TeamException("該員工不存在"); ??? } }
|