Java基础阶段项目练习总结(持续更新中)

2022.01.19项目三:开发人员调度软件

通过项目是复习知识的最好方式,写项目的过程中多思考多总结,多看优秀代码,多练习,编程能力才能真正提高。

设计概览

Java基础阶段项目练习总结(持续更新中)

 

 


 

问题①:在写NameListService的构造器时,给employees数组添加元素时直接使用了属性.set方法的方式,应当使用构造器。

问题②:在写NameListService的构造器中的逻辑时,使用了if-else结构导致代码比较复杂和混乱,这里最好使用switch-case的方式

以下是正确代码:

package www.fancy.team.service;

import www.fancy.team.domain.*;

import static www.fancy.team.service.Data.*;

public class NameListService {
    private Employee[] employees;

    public NameListService() {
        employees = new Employee[Data.EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {
            int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
            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 Programmer(id, name, age, salary, eq);
                case DESIGNER:
                    eq = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Designer(id, name, age, salary, eq, bonus);
                case ARCHITECT:
                    eq = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i] = new Architect(id, name, age, salary, eq, bonus, stock);
            }
        }
    }

    private Equipment createEquipment(int j) {
        int type = Integer.parseInt(EQIPMENTS[j][0]);

        switch (type) {
            case PC:
                return new PC(EQIPMENTS[j][1], EQIPMENTS[j][2]);

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

            case PRINTER:
                return new Printer(EQIPMENTS[j][1], EQIPMENTS[j][2]);

        }
        return null;
    }

    /**
     * @return 2022/1/19
     * @Name getAllEmployee
     * @Description 获取当前所有员工
     * @Author Fancy
     * @Version 1.0
     */
    public Employee[] getAllEmployee() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException {

        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id) {
                return employees[i];
            }
        }

        throw new TeamException("找不到指定的员工");
    }
}

问题③:在写TeamService的getTeam方法来获取当前团队的所有对象时,疑惑为什么不直接return team;

原因是固定长度为5的team不一定被填满,后续对数组进行操作时有可能造成空指针异常。

问题④:在写addMember()方法(向团队中添加成员)时,对多条添加失败的原因进行代码实现时只使用if_else if的结构,导致逻辑理不清,应采用if和if_else if结合来写

问题⑤:在使用equals方法时,写法需要优化一下

问题⑥:没有想到用throw来抛出异常

package www.fancy.team.service;

import www.fancy.team.domain.Employee;
import www.fancy.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;

    public Programmer[] getTeam(){
        Programmer[] team = new Programmer[total];

        for (int i = 0; i < total; i++) {
            team[i] = this.team[i];
        }
        return team;
    }

    public void addMember(Employee e){
        /*
    成员已满,无法添加
    该成员不是开发人员,无法添加

    该员工已在本开发团队中
    该员工已是某团队成员

    该员正在休假,无法添加

    团队中至多只能有一名架构师
    团队中至多只能有两名设计师
    团队中至多只能有三名程序员

         */
        //成员已满,无法添加
        boolean isFlag1 = false;

        for(int i=0;i<total;i++){
            if(team[i].getId() == e.getId()){
                isFlag1 = true;
                return;
            }
        }


        if(total == 5){
            System.out.println("成员已满,无法添加");
        }else if(e.getClass().getName() == "Employee"){
            System.out.println("该成员不是开发人员,无法添加");
        }else if( isFlag1 == true){
            System.out.println("该员工已在本开发团队中");//问题④、⑥
        }
        Programmer p = (Programmer) e;
        if(p.getStatus().getNAME().equals("BUSY")){ //最好写为"BUSY".equals(p.getStatus().getNAME())//问题⑤

        }
    }

}

正确代码:

package com.atguigu.team.service;

import com.atguigu.team.domain.*;


public class TeamService {
    private static int counter = 1;//用于自动生成团队成员的memberId
    private final int MAX_MEMBER = 5;//团队人数上限
    private Programmer[] team = new Programmer[MAX_MEMBER];//保存当前团队成员
    private int total = 0;//团队实际人数

    public TeamService() {
    }
    //返回team中所有程序员构成的数组
    public Programmer[] getTeam() {
        Programmer[] team = new Programmer[total];

        for (int i = 0; i < total; 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("该成员不是开发人员,无法添加");

        Programmer p = (Programmer)e;
        
        if (isExist(p))
            throw new TeamException("该员工已在本团队中");
        
        if(p.getStatus().getNAME().equals("BUSY")) {
            throw new TeamException("该员工已是某团队成员");
        }else if(p.getStatus().getNAME().equals("VOCATION")) {
            throw new TeamException("该员正在休假,无法添加");
        }

//        switch (p.getStatus()) {
//            case BUSY    :throw new TeamException("该员工已是某团队成员");
//            case VOCATION:throw new TeamException("该员正在休假,无法添加");
//        }
        
        int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0;
        for (int i = 0; i < total; i++) {
            if (team[i] instanceof Architect) numOfArch++;
            else if (team[i] instanceof Designer) numOfDsgn++;
            else if (team[i] instanceof Programmer) numOfPrg++;
        }

        if (p instanceof Architect) {
            if (numOfArch >= 1) throw new TeamException("团队中至多只能有一名架构师");
        } else if (p instanceof Designer) {
            if (numOfDsgn >= 2) throw new TeamException("团队中至多只能有两名设计师");
        } else if (p instanceof Programmer) {
            if (numOfPrg >= 3) throw new TeamException("团队中至多只能有三名程序员");
        }
        //添加到数组
        p.setStatus(Status.BUSY);
        p.setMemberId(counter++);
        team[total++] = p;
    }

    private boolean isExist(Programmer p) {
        for (int i = 0; i < total; i++) {
            if (team[i].getId() == p.getId()) return true;
        }

        return false;
    }
    //删除指定memberId的程序员
    public void removeMember(int memberId) throws TeamException {
        int n = 0;
        //找到指定memberId的员工,并删除
        for (; 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] = null;
    }
}

 

 

 

Java基础阶段项目练习总结(持续更新中)

 

上一篇:2022 Winter Team Contest - 1-D(二分图最大匹配)


下一篇:SSM架构下的增删改操作