首页 > 其他分享 >尚硅谷项目三整理

尚硅谷项目三整理

时间:2022-10-04 10:34:51浏览次数:41  
标签:String 项目 int id team 整理 new 硅谷 public

在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();
    }
}

 

有时间再写一遍

标签:String,项目,int,id,team,整理,new,硅谷,public
From: https://www.cnblogs.com/gaohuaiyu/p/16753326.html

相关文章

  • 我收藏的开源项目
    1)Springmore  ​​https://github.com/tangyanbo/springmore​​项目介绍点评:查看这个项目,可以参考动态数据库适配 2) smart-framework SmartFramework......
  • Unity同项目导出不同的UnityPackage流程
    Unity3D中的root项目导出为一个package;新建项目并将package导入进来;在新建的项目里更改里面的各种信息(脚本、模型、贴图等等);重命名所有的更改了的资源名称;将所有的prefab都......
  • bbs项目前期准备和表设计
    一、前期准备1.新建一个django项目2.连接mysql数据库(注意需要在init文件里面书写importpymysql),告诉django使用pymysql连接数据库3.静态文件路径在settings里配置一下,并......
  • 项目四
    面试官你好,最近做的一个项目是在上一家外包公司做的社区团购系统,主要针对社区使用,通过招募小区业主或者利店店主作为社区合作人(团长),然后团长微信开团、商品推广以......
  • Java项目打成镜像
    前言官方只有java8的docker镜像,如果我们的项目使用java11开发的,那么用java8的镜像打包运行会报错那么我们部署java项目有两种方式如:1.将项目打成war包部署到tomcat容器,2.自......
  • 导入项目后报错问题
    依赖报错idea导入项目后依赖报错,解决方案依赖版本报错:更换其他版本无法下载依赖可参考插件报错pom中plugin报红,添加版本添加其他官方镜像下载到本地后导入其他原因jdk环境配......
  • Spring-Cloud-Alibaba项目构建
    SpringCloudAlibaba项目构建版本说明由于我之前的eblog单机版本的springboot版本为2.1.4,所以之后的所有的项目都以该项目版本为基础。我们打开spring官网里面的spri......
  • codeforces/AtCoder补题整理
    目录cf1738CEvenNumberAddicts(博弈/记忆化搜索)题意题解cf1739EResetKEdges(树,二分+贪心)题意题解cf1730DPrefixesandSuffixes(字符串,思维)题意题解cf1734DS......
  • 盘点一个哔哩哔哩弹幕抓取并词云可视化的项目
    大家好,我是皮皮。一、前言前几天在Python白银交流群【肉丸胡辣汤】问了一个Python网络爬虫和可视化的问题,提问截图如下:代码如下:#导入我们要用到的模块importrequestsimport......
  • 在项目中全局配置 TypeScript 文件类型声明
    全局配置ts文件类型声明-产生的原因有些时候,某几个类型声明在项目中很多次都在使用。我们可以将它抽取成为一个文件。1.可以将这个文件中的类型声明成为全局的。2.在需......