开发团队组建项目_准备三_Service核心

package com.it.team.service;
/*名单列表类*/

import com.it.team.domain.*;
import com.it.team.domain.PC;

import static com.it.team.service.Data.*;

public class NameListService {
    private Employee[] employees;//用来保存公司所有员工对象

    public NameListService() {

        employees = new Employee[Data.EMPLOYEES.length];

        for (int i1 = 0; i1 < Data.EMPLOYEES.length; i1++) {
            int type = Integer.parseInt(EMPLOYEES[i1][0]);//将存储雇员信息的二维数组的i1位上的id取出并转换为int类型,便于进行匹配
            //从而进行创建不同的对象
            //由于以下四种变量在三个对象中都存在, 因此这这里可以统一进行使用
            int id = Integer.parseInt(EMPLOYEES[i1][1]);
            String name = EMPLOYEES[i1][2];
            int age = Integer.parseInt(EMPLOYEES[i1][3]);
            double salayr = Double.parseDouble(EMPLOYEES[i1][4]);
            Equiment  equiment;//只声明不处理
            double bonus;
            switch (type) {

                case EMPLOYEE:
                    //创建雇员对象
                    Employee employee = new Employee(id, name, age, salayr);
                    employees[i1] =employee;
                    break;
                case PROGRAMMER://int id, String name, int age, double salary, Equiment equiment
                    //根据传入类型参数创建程序员对象
                    equiment =createEquiment(i1);
                    Programmer programmer = new Programmer(id, name, age, salayr,equiment );
                    employees[i1] =programmer;
                    break;
                case DESIGNER:
                    //根据传入类型参数创建设计师对象
                    equiment =createEquiment(i1);
                    bonus = Double.parseDouble(EMPLOYEES[i1][5]);
                    Designer designer =new Designer(id, name, age,salayr,equiment,bonus);
                    employees[i1] =designer;
                    break;
                case ARCHITECT:
                    //根据传入类型参数创建架构师对象
                    equiment =createEquiment(i1);//获取设备对象
                    bonus = Double.parseDouble(EMPLOYEES[i1][5]);//获取奖金参数
                    int stock  =Integer.parseInt(EMPLOYEES[i1][6]);//获取股票参数
                    Architect  architect =new Architect(id,name,age,salayr,equiment,bonus, stock);
                    employees[i1] =architect;
                    break;
            }


        }
    }
//定义一个获取不同类型员工设备的方法,根据传入的索引值,获取设备数组的0号索引从而判断是什么类型的设备.然后进行创建对应类型的设备对象
    //并返回
    private Equiment createEquiment(int index) {
        int type = Integer.parseInt(EQUIPMENTS[index][0]);
        switch (type) {
            case PC:
                return new PC(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);

            case NOTEBOOK:
                return new NoteBook(EQUIPMENTS[index][1], Integer.parseInt(EQUIPMENTS[index][2]));

            case PRINTER:
                return new Printer(EQUIPMENTS[index][1], EQUIPMENTS[index][2]);

        }
        return  null;
    }


    public Employee[] getAllEmployees() {
        return employees;
    }

    int i = 0;

    public Employee getEmployee(int id) throws TeamException {
        for (i = 0; i < employees.length; i++) {
            if (id == employees[i].getId()) {
                return employees[i];
            }
        }
        throw new TeamException("未找到该员工");
    }


}

package com.it.team.service;
/*表示员工状态的类,存储着静态常量*/
public class Status {
    private String NAME;//表示员工的状态
    public static final Status  FREE = new Status("FREE");//表示空闲
    public static final Status  BUSY = new Status("BUSY");//已加入团队
    public static final Status  VOCATION = new Status("VOCATION");//正在度假



    private Status(String NAME) {//私有构造器, 外界无法再创建对象,只能使用三种对象常量
        this.NAME=NAME;
    }

    public String getNAME() {
        return NAME;
    }

    public void setNAME(String NAME) {
        this.NAME = NAME;
    }

    @Override
    public String toString() {
        return  NAME ;
    }
}

开发团队组建实现类
```package com.it.team.service;
/*自定义异常类*/
public class TeamException extends Exception {

    //1,标识id
    static final long serialVersionUID = -33875169931249948L;
        //2.无参构造器
    public TeamException() {
    }
    //3.带参构造器
    public TeamException(String message) {
        super(message);
    }
}
package com.it.team.service;

import com.it.team.domain.Architect;
import com.it.team.domain.Designer;
import com.it.team.domain.Employee;
import com.it.team.domain.Programmer;

public class TeamService {
    private static int counter = 1;
    private final int MAX_MEMBER = 5;
    private Programmer[] team = new Programmer[MAX_MEMBER];
    private int total = 0;//统计开发团队目前总人数

    /*获取开发团队成员的方法,不能直接返回team,因为团队中可能没有5个人, 那么遍历数组的时候就可能出现空指针
     * 因此需要再造一个数组,将已经存在的元素赋值过来*/
    public Programmer[] getTeam() {
        Programmer[] team = new Programmer[total];
        for (int i = 0; i < team.length; i++) {
            team[i] = this.team[i];
        }
        return team;
    }

    /*添加成员的方法*/
    public void addMember(Employee e) throws TeamException {
        if (total == MAX_MEMBER) {
            throw new TeamException("成员已满,无法添加");
        }
        if (!(e instanceof Programmer)) {
            throw new TeamException("该成员不是开发人员,无法添加");
        }
        if (isExit(e)) {
            throw new TeamException("该员工已在本开发团队中");
        }
        if ("BUSY".equals(((Programmer) e).getStatus().getNAME())) {
            throw new TeamException("该员工已是某团队成员");
        }
        if ("VOCATION".equals(((Programmer) e).getStatus().getNAME())) {
            throw new TeamException("该员工正在休假,无法添加");
        }
        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] != null) {
                numOfPro++;
            }
        }
        if (e instanceof Architect) {
            if (numOfArch >= 1) {
                throw new TeamException("团队中之多只能有一名架构师");
            }
        }
        else if (e instanceof Designer) {
            if (numOfDes >= 2) {
                throw new TeamException("团队中至多只能有两名设计师");
            }
        }
        else if (e instanceof Programmer) {
            if (numOfPro >= 3) {
                throw new TeamException("团队中之多只能有三名程序员");
            }
        }
        // 以上步骤都通过后, 则可以将该成员添加进团队中
        team[total++] = (Programmer) e;
        //将成员状态设置为BUSY
        ((Programmer) e).setStatus(Status.BUSY);
        //给成员设置团队memberID
        ((Programmer) e).setMemberId(counter++);


    }

    private boolean isExit(Employee e) throws TeamException {
        for (int i = 0; i < total; i++) {
            if (team[i].getId() == e.getId()) {
                return true;
            }
        }
        return false;
    }

    //删除成员的方法
    public void removeMember(int memberId) throws TeamException {
        for (int i = 0; i < total; i++) {
            if (memberId == team[i].getMemberId()) {
                //找到要删除的元素了, 将其状态置为FREE
                team[i].setStatus(Status.FREE);
                //循环将该对象移动到数组最后一个位置
                for (int j = i; j <total-1 ; j++) {
                    team[j]= team[j+1];
                }
                //并置为null //并将成员数量减一
              team[--total]=null;

                return;
            }

        }
        throw new TeamException("找不到指定memberId的员工,删除失败");

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值