尚硅谷项目三整理

在domain包下包含 一套继承,一套接口;

继承父类为Employees,接口为Equipment

 

 Employ代码:

复制代码
package com.atguigu.team.domain;

public class Employee {
    private int id;
    private String name;
    private int age;
    private double salary;

    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;
    }
    //写一个唯一的方法getDetails()来获取toString的信息,可以让子子类调用直接调用超级父类的toString
    public String getDetails(){
        return id + "\t" + name + "\t" + age + "\t" + salary;
    }
    @Override
    /**
     * @author 高槐玉
     * #Description 重写toString方法,显示Data员工信息
     * #Date: 2022/
     * #Shangguigu:
     */
    public String toString() {
        return id + "\t" + name + "\t" + age + "\t" + salary;
    }
}
复制代码

然后后面的三个子类Programmer,Designer,Architecter的toString方法的重写比较重要;而且还要注意一点。对这几个对象的信息展示有两种展示面板,就要写两套toString,代码如下:

复制代码
package com.atguigu.team.domain;

import com.atguigu.team.service.Status;

public class Programmer extends Employee{
    private int memberId;//开发团队中的id
    private Status status = Status.FREE;
    private Equipment equipment;
    public Programmer(){
        super();
    }

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

    public String toString(){
        return super.toString() + "\t程序员\t" + status + "\t\t\t\t" + equipment.getDescription();
    }

    public String DetailsOfTeamView(){
        return memberId + "/" + getId() + "\t" + super.getName() + "\t" + getAge();
    }
    public String DetailsOf(){
        return DetailsOfTeamView() + "\t" + "程序员";
    }
}
复制代码

该包下面还要一个设备方面的代码,有三种设备,PC,Notebook,printer,三个类拥有一个共同的接口Equipment。

这三个类在create Equipment中创建对象,现展示PC端代码:

复制代码
package com.atguigu.team.domain;

public class PC implements Equipment{
    private String model;//机器型号
    private String display;//显示器名称

    public PC(){
        super();
    }

    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 + ")";
    }
}
复制代码

Equipment接口:

package com.atguigu.team.domain;

public interface Equipment {
     String getDescription();
}

 

 

接下来是关于Service包了,该包主要进行数组初始化,返回数组,增删改查的操作,是项目的核心包

 

 

首先是NameListService包,private Empolyee[] employees;定义一个数组

再对数组元素进行初始化,代码相关注意事项在代码注释当中

复制代码
 public NameListService(){
        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]);
       //     double bonus = Double.parseDouble(EMPLOYEES[i][5]);
            //Equipment equipment = createEquipment(i);这样写会有潜在风险,并不是每一个员工都有设备
            //可以只声明,不处理,如下:
            Equipment equipment;
            double bonus;
            int stock;
            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;
                    /**
                     * @author 高槐玉
                     * 因为未加break;报错数组越界(1)
                     */


                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);
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
                  //  employees[i] = new Employee(id,name,age,salary,bonus,stock);
            }
        }
    }
复制代码

再就是获取指定员工设备:

复制代码
/**
 * @author 高槐玉
 * #Description :获取指定员工设备
 * #Date:
 * #Shangguigu:
 */

    private Equipment createEquipment(int i) {
        int EquipmentType = Integer.parseInt(EQUIPMENTS[i][0]);
                String model = EQUIPMENTS[i][1];
        switch(EquipmentType){
            case PC://21,model,display
                String display = EQUIPMENTS[i][2];
                return new PC(model,display);
            case NOTEBOOK://22 model,price
                double price = Double.parseDouble(EQUIPMENTS[i][2]);
                return new NoteBook(model,price);
            case PRINTER://23,name,type
                String type = EQUIPMENTS[i][2];
                return new Printer(model,type);
        }
        return null;//不加会报错,如果什么设备都没有是不是就返回null?
                    //知道了,这个类型为Equipment
    }
复制代码

case PC://这个PC是data中的静态常量,为21

public static final int PC = 21;

复制代码
/**
 * @author 高槐玉
 * #Description 返回所有员工信息
 * #Date:
 * #Shangguigu:
 */

    public Employee[] getAllEmployees(){
        return employees;
    }
    /**
     * @author 高槐玉
     * #Description 返回指定员工信息
     * #Date:
     * #Shangguigu:
     */

    public Employee getEmloyee(int id) throws TeamException{
        for(int i = 0;i < employees.length;i++){
            if(employees[i].getId() == id){
                return employees[i];
            }
        }throw new TeamException("找不到指定员工");
    }
}
复制代码

然后是增删改查TeamService.class

复制代码
package com.atguigu.team.service;

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

public class TeamService {
    private static int counter = 1;//给memberId赋值使用
    private final int MAX_MEMBER = 5;//限制开发团队的人数
    Programmer[] team = new Programmer[MAX_MEMBER];//保存开发团队成员
    private int total = 0;//记录开发团队中的实际人数
    /**
     * @author 高槐玉
     * #Description 获取开发团队中的所有成员
     * #Date: 2022/10/3/10点02分
     * #Shangguigu:394
     */

    public Programmer[] getTeam() {
        Programmer[] team = new Programmer[total];
        for(int i = 0;i < team.length;i++){
            team[i] = this.team[i];
        }
        return team;
    }
    /**
     * @author 高槐玉
     * #Description 将指定员工添加到开发团队中
     * #Date: 2022/
     * #Shangguigu:
     */

    public void addMember(Employee e) throws TeamException {
        //成员已满,无法添加
        if(total >= MAX_MEMBER){
            throw new TeamException("成员已满,无法添加");
        }
//        该成员不是开发人员,无法添加
        if(!(e instanceof Programmer)){
            throw new TeamException("该成员不是开发人员,无法添加");
        }
//        该员工已在本开发团队中
        for(int i = 0;i < total;i++){//这里不能写i < team。length,只能写total,为啥呢?
            if(e.getId() == team[i].getId() ){
                throw new TeamException("该员工已在本开发团队中");
            }
        }
//        该员工已是某团队成员
//        该员正在休假,无法添加
        Programmer p = (Programmer) e;
        if("BUSY".equals(p.getStatus().getNAME())){
            throw new TeamException("该员工已是某团队成员");
        }else if("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())){
            throw new TeamException("该员正在休假,无法添加");
        }

//        团队中至多只能有一名架构师
//        团队中至多只能有两名设计师
//        团队中至多只能有三名程序员
        //Programmer:  11, id, name, age, salary
        //Designer  :  12, id, name, age, salary, bonus
        //Architect :  13, id, name, age, salary, bonus, stock
        int numOfArch = 0,numOfDes = 0,numOfPro = 0;
        for(int i = 0;i < total;i++){
            if(team[i] instanceof Architect){
                numOfArch++;
            }
            if(team[i] instanceof Designer){
                numOfDes++;
            }
            if (team[i] instanceof Programmer) {
                numOfPro++;
            }
        }
        /*
        下面的写法是错误的,逻辑方面,如果第一个if进去了,是一个架构师,但是Arch》1,
        那么还是会跳到第二个if里面去,第二个成立那么架构师就添加进去了
         */
//        if(p instanceof Architect && numOfArch >= 1){
//            throw new TeamException("团队中至多只能有一名架构师");
//        }
//        else if(p instanceof Designer && numOfDes >= 2){
//            throw new TeamException("团队中至多只能有两名设计师");
//        }
//        else if(p instanceof Programmer && numOfPro >= 3){
//            throw new TeamException("团队中至多只能有三名程序员");
//        }
        //正确的写法
        if(p instanceof Architect){
            if(numOfArch >= 1) {
                throw new TeamException("团队中至多只能有一名架构师");
            }
        }
        else if(p instanceof Designer){
            if(numOfDes >= 2) {
                throw new TeamException("团队中至多只能有两名设计师");
            }
        }
        else if(p instanceof Programmer){
            if( numOfPro >= 3) {
                throw new TeamException("团队中至多只能有三名程序员");
            }
        }
        //将p(或e)添加到现有的team中
        team[total] = p;
        total++;
        //p的属性赋值
        p.setStatus(Status.BUSY);
        p.setMemberId(counter++);
    }
    public void removeMember(int memberId) throws TeamException {
        int i;
        for(i = 0;i <= total;i++) {
            if (team[i].getMemberId() == memberId) {
                team[i].setStatus(Status.FREE);
                break;
            }
        }
        //未找到指定memberId情况
        if(i == total){
            throw new TeamException("找不到指定memberId员工");
        }
        for (int j = i;j < total - 1;j++){
        team[j] = team[j+1];
        }
        team[--total] = null;
    }
}
复制代码

view包下TeamView类:

复制代码
package com.atguigu.team.view;

import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Programmer;
import com.atguigu.team.service.Data;
import com.atguigu.team.service.NameListService;
import com.atguigu.team.service.TeamException;
import com.atguigu.team.service.TeamService;

public class TeamView {
    private NameListService listSvs = new NameListService();
    private TeamService teamSvc = new TeamService();
    public void enterMainMenu(){
        boolean loopFlag = true;
        char menu = 0;
        while (loopFlag){
            if(menu != '1'){
                listAllEmployees();
            }
        System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
        menu = TSUtility.readMenuSelection();
        switch (menu){
            case '1':
                getTeam();
                break;
            case '2':
                addMember();
                break;
            case '3':
                deleteMember();
                break;
            case '4':
                System.out.println("确认是否退出(Y/N):");
                char isExit = TSUtility.readConfirmSelection();
                if(isExit == 'Y'){
                    loopFlag = false;
                }
                //看到400集了,打个标签!
                break;

        }
        }
    }/**
     * @author 高槐玉
     * #Description 显示所有员工信息
     * #Date: 2022/10/3 18点16分
     * #Shangguigu:398集
     */

    private void listAllEmployees(){
        System.out.println("-------------------------------------我爱胡婧怡之调度软件-------------------------------------");
        System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t设备");
        Employee[] employees = listSvs.getAllEmployees();//得来一个数组接收所有的成员
        for (int i = 0;i < Data.EMPLOYEES.length;i++){
            System.out.println(employees[i]);
        }
        System.out.println("-------------------------------------我爱胡婧怡之调度软件-------------------------------------");
    }
    /*
            for(条件①;条件②;条件③){
            循环体④
        }
        执行顺序如下:
        ①②④③  ②④③  ②④③... ...直到结束
     */
    private void getTeam(){
        Programmer[] hjy = teamSvc.getTeam();
        int i;
        System.out.println("TID/ID\t姓名\t年龄\t职位\t状态\t奖金\t股票");
        for (i = 0;i < hjy.length;i++){
            System.out.println(hjy[i].DetailsOf());
        }
        if(i == 0){
            System.out.println("暂无开发团队!");
        }
    }
    private void addMember(){
        System.out.println("请输入添加员工id:");
        int id = TSUtility.readInt();
        try {
            Employee p = null;
            p = listSvs.getEmloyee(id);
            teamSvc.addMember(p);
        System.out.println("添加成功!");
        TSUtility.readReturn();
        } catch (TeamException e) {
            System.out.println("添加失败,原因:" + e.getMessage());;
        }
    }
    private void deleteMember(){
        System.out.println("--------------------------------------删除团队成员-----------------------------------");
        System.out.println("删除团队成员的TId:");
        int TID = TSUtility.readInt();
        try {
            teamSvc.removeMember(TID);
        } catch (TeamException e) {
            System.out.println(e.getMessage());;
        }
    }

    public static void main(String[] args) {
        TeamView view = new TeamView();
        view.enterMainMenu();
    }
}
复制代码

 

有时间再写一遍

posted @   高槐玉在隐身  阅读(118)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示