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("该员工不存在");

    }

}