【Java】—— 项目三:开发团队人员调度系统

目录

目标

需求说明

软件设计结构

第一步、创建项目基本组件

键盘访问的实现

Equipment接口及其各实现子类的设计

Employee类及其子类的设计

第二步、实现service包中的类

NameListService类的设计

TeamService类的设计

第三步、实现view包中的类

TeamView类的设计

运行示例


目标

模拟实现一个基于文本界面的 开发团队调度系统
熟悉 Java 面向对象的高级特性,进一步掌握编程技巧和调试技巧
主要涉及以下知识点:
Ø 类的继承性和多态性
Ø 对象的值传递、接口
Ø static final 修饰符
Ø 特殊类的使用:包装类、抽象类、枚举类
Ø 异常处理

需求说明

该软件实现以下功能:
  1. 软件启动时,根据给定的数据创建公司部分成员列表(数组)
  2. 根据菜单提示,基于现有的公司成员,组建一个开发团队以开发一个新的项目
  3. 组建过程包括将成员插入到团队中,或从团队中删除某成员,还可以列出团队中现有成员的列表
  4. 开发团队成员包括架构师、设计师和程序员

本软件采用单级菜单方式工作。当软件运行时,主界面显示公司成员的列表,如下:

当选择“添加队成员”菜单时,将执行从列表中添加指定(通过ID)成员到开发团队的功能:

如果添加操作因某种原因失败,将显示类似以下信息(失败原因视具体原因而不同):

失败信息包含以下几种:

成员已满,无法添加
该成员不是开发人员,无法添加
该员工已是某团队成员
该员工正在休假,无法添加
该员工已在本开发团队中
团队中至多只能有一名架构师
团队中至多只能有两名设计师
团队中至多只能有三名程序员

当选择“删除队成员”菜单时,将执行从开发团队中删除指定(通过TeamID)成员的功能:

        删除成功后,按回车键将重新显示主界面。

当选择“团队列表”菜单时,将列出开发团队中的现有成员,例如:

软件设计结构

项目的总体结构:

该软件由以下三个模块组成:

  • team.view 模块为主控模块,负责菜单的显示和处理用户操作
  • team.service 模块为实体对象( Employee 及其子类如程序员等)的管理模块, NameListService TeamService 类分别用各自的数组来管理公司员工和开发团队成员对象
  • domain 模块为 Employee 及其子类等 JavaBean 类所在的包
team.domain 模块中包含了所有实体类:

其中程序员 (Programmer) 及其子类,均会领用某种电子设备 (Equipment)

第一步、创建项目基本组件

  1. 完成以下工作:
    1. 创建TeamSchedule项目
    2. 按照设计要求,创建所有包
  2. 按照设计要求,在team.domain包中,创建Equipment接口及其各实现子类代码
  3. 按照设计要求,在team.domain包中,创建Employee类及其各子类代码
  4. 检验代码的正确性

键盘访问的实现

  • 项目view包中提供了TSUtility.java类,可用来方便地实现键盘访问。
  • 该类提供了以下静态方法:
    1. public static char readMenuSelection()
      用途:
      该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
    2. public static void readReturn()
      用途:
      该方法提示并等待,直到用户按回车键后返回。
    3. public static int readInt()
      用途:
      该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
    4. public static char readConfirmSelection() 
      用途:
      从键盘读取‘Y’’N’,并将其作为方法的返回值。

TSUtility.java

package team.view;

import java.util.*;
/**
 * 
 * @Description 项目中提供了TSUtility.java类,可用来方便地实现键盘访问。

 *
 */
public class TSUtility {
    private static Scanner scanner = new Scanner(System.in);
    /**
     * 
     * @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
     * @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 该方法提示并等待,直到用户按回车键后返回。
	 */
    public static void readReturn() {
        System.out.print("按回车键继续...");
        readKeyBoard(100, true);
    }
    /**
     * 
     * @Description 该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
     * @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’,并将其作为方法的返回值。
     * @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;
    }
}

我们的人员信息存储在service包中:Data.java

package team.service;


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"}
    };
    
    //如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
    //PC      :21, model, display
    //NoteBook:22, model, price
    //Printer :23, name, type 
    public static final String[][] EQUIPMENTS = {
        {},
        {"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"}
    };
}

Equipment接口及其各实现子类的设计

  • 说明:
    • model表示机器的型号 
    • display 表示显示器名称
    • type 表示机器的类型
  • 根据需要提供各属性的get/set方法以及重载构造器
  • 实现类实现接口的方法,返回各自属性的信息
Equipment.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *
 * @Author zyjstart
 * @Create:2024/9/18 16:27
 */
public interface Equipment {
    String getDescription();
}
PC.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *      
 * @Author zyjstart
 * @Create:2024/9/18 16:29
 */
public class PC implements Equipment{
    private String model;   // 机器的型号
    private String display; // 显示器的名称

    // 提供get、set方法与构造器

    public PC() {
    }

    public PC(String model, String display) {
        this.model = model;
        this.display = 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;
    }

    @Override
    public String getDescription() {
        return model + "(" + display + ")";
    }
}
NoteBook.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *
 * @Author zyjstart
 * @Create:2024/9/18 16:33
 */
public class NoteBook implements Equipment{
    private String model;   // 机器的型号
    private double price;   // 价格

    // 构造器与get、set方法
    public NoteBook() {
    }

    public NoteBook(String model, double price) {
        this.model = model;
        this.price = 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;
    }

    @Override
    public String getDescription() {
        return model + "(" + price + ")";
    }
}
Printer.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *      打印机类
 * @Author zyjstart
 * @Create:2024/9/18 16:36
 */
public class Printer implements Equipment{
    private String name;    // 打印机名称
    private String type;    // 机器的类型

    // 构造器与get、set方法
    public Printer() {
    }

    public Printer(String name, String type) {
        this.name = name;
        this.type = 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 + ")";
    }
}

Employee类及其子类的设计

1、员工类         Employee
2、程序员类     Programmer

3、设计师类     Designer
4、架构师类     Aechitect

  • 说明:
    • memberId 用来记录成员加入开发团队后在团队中的ID
    • Status是项目service包下自定义的枚举类,表示成员的状态
      • FREE--空闲
      • BUSY--已加入开发团队
      • VOCATION--正在休假
    • equipment 表示该成员领用的设备
  • 可根据需要为类提供各属性的get/set方法以及重载构造器
Employee.java        员工类
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 * 员工类
 *
 * @Author zyjstart
 * @Create:2024/9/18 16:42
 */
public class Employee {
    private int id;
    private String name;
    private int age;
    private double salary;

    //构造器与get、set方法
    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 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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    // 由于此类的toString()方法需要一直被调用,在子类的子类中toString会被覆盖,因此把它单独拿出来
    protected String getDetails(){
        return id + "\t" + name + "\t" + age + "\t" + salary;
    }

    // 重写tostring方法
    @Override
    public String toString() {
        return getDetails();
    }
}
Programmer.java     程序员类
package team.domain;

import team.service.Status;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 * 程序员类
 *
 * @Author zyjstart
 * @Create:2024/9/18 16:48
 */
public class Programmer extends Employee {
    private int memberId;   // 开发团队中的tId
    private Status status = Status.FREE;
    private Equipment equipment;    // 该成员领用的设备

    // 构造器与get、set方法
    public Programmer() {
    }

    public Programmer(int id, String name, int age, double salary, Equipment equipment) {
        super(id, name, age, salary);
        this.equipment = equipment;
    }

    public int getMemberId() {
        return memberId;
    }

    public void setMemberId(int memberId) {
        this.memberId = memberId;
    }

    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;
    }

    // 重写tostring方法
    @Override
    public String toString() {
        return getDetails() + "\t程序员\t" + status + "\t\t\t\t\t" + equipment.getDescription();
    }

    public String getDetailsForTeam() {
        return memberId + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary() + "\t程序员";
    }
}

Status.java

Status枚举类位于team.service包中,封装员工的状态。其代码为:

package team.service;

public enum Status {
    FREE,BUSY,VOCATION
//    FREE-空闲
//    BUSY-已加入开发团队
//    VOCATION-正在休假

}

  • 说明:
    • bonus 表示奖金
    • stock 表示公司奖励的股票数量
  • 可根据需要为类提供各属性的get/set方法以及重载构造器
Designer.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 * 设计师类
 *
 * @Author zyjstart
 * @Create:2024/9/18 16:59
 */
public class Designer extends Programmer {
    private double bonus;   // 奖金

    // 构造器与get、set方法
    public Designer() {
    }

    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;
    }

    //重写toString方法
    @Override
    public String toString() {
        return getDetails() + "\t设计师\t" + getStatus() + "\t" + getBonus() + "\t\t\t" + getEquipment().getDescription();
    }

    public String getDetailsForTeam() {
        return getMemberId() + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary() + "\t设计师\t" + getBonus();
    }
}
Architect.java
package team.domain;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *      架构师类
 * @Author zyjstart
 * @Create:2024/9/18 17:05
 */
public class Architect extends Designer{
    private int stock;  // 公司奖励的股票数量

    // 构造器与get、set方法
    public Architect() {
    }

    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;
    }

    //重写toString方法
    @Override
    public String toString() {
        return getDetails() + "\t架构师\t" + getStatus() + "\t" + getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
    }

    public String getDetailsForTeam() {
        return getMemberId() + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" +
                getSalary() + "\t架构师\t" + getBonus() + "\t" + getStock();
    }
}

第二步、实现service包中的类

  1. 按照设计要求编写NameListService
  2. NameListService类中临时添加一个main方法中,作为单元测试方法。
  3. 在方法中创建NameListService对象,然后分别用模拟数据调用该对象的各个方法,以测试是否正确。
    注:测试应细化到包含了所有非正常的情况,以确保方法完全正确。
  4. 重复1-3步,完成TeamService类的开发

NameListService类的设计

  • 功能:负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
  • 说明:
    • ​​​​​​​employees用来保存公司所有员工对象
    • NameListService()构造器:
      • ​​​​​​​根据项目提供的Data类构建相应大小的employees数组
      • 再根据Data类中的数据构建不同的对象,包括EmployeeProgrammerDesignerArchitect对象,以及相关联的Equipment子类的对象
      • 将对象存于数组中
      • Data类位于team.service包中
    • getAllEmployees ()方法:获取当前所有员工。
      • ​​​​​​​返回:包含所有员工对象的数组
    • getEmployee(id : int)方法:获取指定ID的员工对象。
      • ​​​​​​​参数:指定员工的ID
      • 返回:指定员工对象
      • 异常:找不到指定的员工
  • service子包下提供自定义异常类:TeamException
  • 另外,可根据需要自行添加其他方法或重载构造器

TeamException.java

package team.service;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *          自定义异常类,
 * @Author zyjstart
 * @Create:2024/9/19 19:40
 */
public class TeamException extends Exception{
    static final long serialVersionUID = -338724229948L;
    public TeamException() {
    }

    public TeamException(String message) {
        super(message);
    }
}
NameListService.java
package team.service;

import team.domain.*;

import static team.service.Data.*;
/**
 * ClassName:IntelliJ IDEA
 * Description:
 *      负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
 * @Author zyjstart
 * @Create:2024/9/18 17:10
 */
public class NameListService {
    private Employee[] employees;

    public NameListService() {
//        根据项目提供的Data类构建相应大小的employees数组
        employees = new Employee[EMPLOYEES.length];
        for (int i = 0; i < employees.length; i++) {

            int type = Integer.parseInt(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 equipment;
            Double bonus;
            int stock;

//        再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Equipment子类的对象
//                将对象存于数组中
//        Data类位于team.service包中
            switch (type){
                case EMPLOYEE:
                    employees[i] = new Employee(id,name,age,salary);
                    break;
                case PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id,name,age,salary, equipment);
                    break;
                case DESIGNER:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Designer(id,name,age,salary, equipment,bonus);
                    break;
                case ARCHITECT:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i] = new Architect(id,name,age,salary, equipment,bonus,stock);
                    break;
            }
        }

    }

    private Equipment createEquipment(int index){
        int equipmentType = Integer.parseInt(EQUIPMENTS[index][0]);

        String modelOrName = EQUIPMENTS[index][1];
        String priceOrDisplayOrType = EQUIPMENTS[index][2];

        switch (equipmentType){
            case PC:
                return new PC(modelOrName,priceOrDisplayOrType);
            case NOTEBOOK:
                double price = Double.parseDouble(priceOrDisplayOrType);
                return new NoteBook(modelOrName,price);
            case PRINTER:
                return new Printer(modelOrName,priceOrDisplayOrType);
        }
        return null;
    }

    /**
     * 获取当前所以员工
     * @return  包含所以员工对象的数组
     */
    public Employee[] getAllEmployees(){
        return employees;
    }

    /**
     * 获取指定id对应的员工
     * @param id
     * @return
     */
    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类的设计

  • 功能:关于开发团队成员的管理:添加、删除等。
  • 说明:
    • ​​​​​​​counter静态变量,用来为开发团队新增成员自动生成团队中的唯一ID,即memberId。(提示:应使用增1的方式)
    • MAX_MEMBER:表示开发团队最大成员数
    • team数组:用来保存当前团队中的各成员对象
    • total记录团队成员的实际人数
    • getTeam()方法:返回当前团队的所有对象
      • ​​​​​​​返回:包含所有成员对象的数组数组大小与成员人数一致
    • addMember(e: Employee)方法:向团队中添加成员
      • ​​​​​​​参数:待添加成员的对象
      • 异常:添加失败, TeamException中包含了失败原因
    • removeMember(memberId: int)方法:从团队中删除成员
      • ​​​​​​​参数:待删除成员的memberId
      • 异常:找不到指定memberId的员工,删除失败
    • 另外,可根据需要自行添加其他方法或重载构造器

TeamService.java

package team.service;

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

/**
 * ClassName:IntelliJ IDEA
 * Description:
 * 功能:关于开发团队成员的管理:添加、删除等
 *
 * @Author zyjstart
 * @Create:2024/9/19 20:44
 */
public class TeamService {
    private static int counter = 1; // 给menmberID进行自动赋值的基数
    private final int MAX_MEMBER = 5;   // 表示开发团队最大成员数
    private Programmer[] team = new Programmer[MAX_MEMBER]; // 表示团队各成员对象的数组
    private int total = 0;  //  记录团队成员的实际人数

    /**
     * 返回当前团队的所有对象
     *
     * @return 包含所有成员对象的数组,数组大小与成员人数一致
     */
    public Programmer[] getTeam() {
        // 由于返回的数组大小需要与成员人数一致,需重新造一个数组进行返回
        Programmer[] team = new Programmer[total];
        for (int i = 0; i < total; i++) {
            team[i] = this.team[i];
        }
        return team;
    }

    /**
     * 向团队中添加成员
     *
     * @param e 待添加的成员
     * @throws TeamException 添加失败, TeamException中包含了失败原因
     */
    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;
        Status status = p.getStatus();
        switch (status) {
            case BUSY:
                throw new TeamException("该员工已是某团队的成员");
            case VOCATION:
                throw new TeamException("该员工正在休假,无法添加");
        }

        //该员工已在本开发团队中
        boolean isExist = isExist(p);
        if (isExist) {
            throw new TeamException("该员工已在本开发团队中");
        }

        // 团队中至多只能有一名架构师
        // 团队中至多只能有两名设计师
        // 团队中至多只能有三名程序员
        // 记录程序员、设计师、架构师的个数
        int progNum = 0, desNum = 0, arcNum = 0;
        for (int i = 0; i < total; i++) {
            if (team[i] instanceof Architect){
                arcNum++;
            }else if (team[i] instanceof Designer){
                desNum++;
            }else {
                progNum++;
            }
        }

        if (p instanceof Architect){
            if (arcNum >= 1){
                throw new TeamException("团队中至多只能有一名架构师");
            }
        }else if (p instanceof Designer){
            if (desNum >= 2){
                throw new TeamException("团队中至多只能有两名设计师");
            }
        }else {
            if (progNum >= 3){
                throw new TeamException("团队中至多只能有三名程序员");
            }
        }


        // 代码如果执行到此位置,意味着p是可以添加到team数组中的。
        team[total++] = p;
        p.setMemberId(counter++);
        p.setStatus(Status.BUSY);   // 将状态改为已加入开发团队

    }

    /**
     * 判断添加的员工p是否在本开发团队中
     *
     * @param p 添加的员工
     * @return true表示在本团队中,false表示不在团队中
     */
    private boolean isExist(Programmer p) {
        for (int i = 0; i < total; i++) {
            if (team[i].getId() == p.getId()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 从团队中删除成员
     *
     * @param memberId 待删除成员的memberId
     * @throws TeamException 找不到指定memberId的员工,删除失败
     */
    public void removeMember(int memberId) throws TeamException {
        int i = 0;
        for (; i < total; i++) {
            if (team[i].getMemberId() == memberId){
                // 找到了这个员工,需要调整其相关属性
                team[i].setStatus(Status.FREE); // 将属性改为空闲
                // 员工的memberId可以不修改
                break;
            }

        }
        // 如果i等于团队人数,说明没有找到
        if (i == total){
            throw new TeamException("找不到指定memberId得员工,删除失败");
        }

        // 调整数组
        for (int j = i; j < total - 1; j++) {
            team[j] = team[j + 1];
        }
        // 最后一个删除
        team[--total] = null;
    }
}

第三步、实现view包中的类

1. 按照设计要求编写 TeamView 类,逐一实现各个方法,并编译
2. 执行 main 方法中,测试软件全部功能

TeamView类的设计

  • 说明:
    • ​​​​​​​listSvcteamSvc属性:供类中的方法使用
    • enterMainMenu ()方法:主界面显示及控制方法。
    • 以下方法仅供enterMainMenu()方法调用:
      • ​​​​​​​listAllEmployees ()方法:以表格形式列出公司所有成员
      • getTeam()方法:显示团队成员列表操作
      • addMember ()方法:实现添加成员操作
      • deleteMember ()方法:实现删除成员操作
TeamView.java
package team.view;

import team.domain.Employee;
import team.domain.Programmer;
import team.service.NameListService;
import team.service.TeamException;
import team.service.TeamService;

/**
 * ClassName:IntelliJ IDEA
 * Description:
 *
 * @Author zyjstart
 * @Create:2024/9/19 22:27
 */
public class TeamView {
    private NameListService listSvc = new NameListService();
    private TeamService teamSvc = new TeamService();

    public void enterMainMenu() {
        boolean loopFlag = true;
        char key = 0;

        do {
            if (key != '1') {
                listAllEmployees();
            }
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4):");
            key = TSUtility.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    listTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N):");
                    char yn = TSUtility.readConfirmSelection();
                    if (yn == 'Y')
                        loopFlag = false;
                    break;
            }
        } while (loopFlag);
    }

    // 显示所有的员工成员
    private void listAllEmployees() {
        System.out
                .println("\n-------------------------------开发团队调度系统--------------------------------\n");
        Employee[] emps = listSvc.getAllEmployees();

        if (emps.length == 0) {
            System.out.println("没有客户记录!");
        } else {
            System.out.println("ID\t姓名\t\t年龄\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
        }
        for(int i = 0;i < emps.length;i++){
            System.out.println(" " + emps[i]);
        }
        System.out
                .println("-------------------------------------------------------------------------------");
    }

    // 显示开发团队成员列表
    private void listTeam() {
        System.out
                .println("\n--------------------团队成员列表---------------------\n");
        Programmer[] team = teamSvc.getTeam();
        if (team.length == 0) {
            System.out.println("开发团队目前没有成员!");
        } else {
            System.out.println("TID/ID\t姓名\t\t年龄\t工资\t\t职位\t\t奖金\t\t股票");
        }
        for (int i = 0; i < team.length; i++) {
            System.out.println(" " + team[i].getDetailsForTeam());
        }
        System.out
                .println("-----------------------------------------------------");
    }

    // 添加成员到团队
    private void addMember() {
        System.out.println("---------------------添加成员---------------------");
        System.out.print("请输入要添加的员工ID:");
        int id = TSUtility.readInt();

        try {
            Employee e = listSvc.getEmployee(id);
            teamSvc.addMember(e);
            System.out.println("添加成功");
        } catch (TeamException e) {
            System.out.println("添加失败,原因:" + e.getMessage());
        }
        // 按回车键继续...
        TSUtility.readReturn();
    }

    // 从团队中删除指定id的成员
    private void deleteMember() {
        System.out.println("---------------------删除成员---------------------");
        System.out.print("请输入要删除员工的TID:");
        int id = TSUtility.readInt();
        System.out.print("确认是否删除(Y/N):");
        char yn = TSUtility.readConfirmSelection();
        if (yn == '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();
    }
}

运行示例

添加功能

团队列表

删除功能

退出功能

至此我们整个项目就完成了,快去动手试一下吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值