JAVA笔记11,项目开发团队调度软件,总结之前学习内容

第十一章 项目 开发团队调度软件

一 目标

  • 模拟实现一个基于文本界面的《开发团队调度软件》
  • 复习之前学习的内容

二 需求说明

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

三 运行效果图

  1. 启动
    启动效果图

  2. 显示团队列表

    • 没有团队成员时
      没有团队成员时
    • 有团队成员时
      有团队成员时
  3. 添加团队成员

    • 添加成功
      添加成功

    • 添加失败
      添加失败
      添加失败
      添加失败
      添加失败

  4. 删除成员

    • 删除成功
      删除成功
    • 删除失败
      删除失败

四 代码

启动文件 App.java

import com.la.view.EmployeeView;

public class App {
    public static void main(String[] args){
        EmployeeView.getView();
    }
}

抽象类Employee及其三个设计师、架构师、程序员子类

package com.la.base;

import com.la.service.EmployeeStatus;

public abstract class Employee {
    protected int id;
    protected int age;
    protected String name;
    protected double salary;
    protected EmployeeStatus status;
    protected Device dev;


    public abstract String getJobName();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

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

    public EmployeeStatus getStatus() {
        return status;
    }

    public void setStatus(EmployeeStatus status) {
        this.status = status;
    }

    public Employee(int id, int age, String name, double salary, EmployeeStatus status, Device dev) {
        this.id = id;
        this.age = age;
        this.name = name;
        this.salary = salary;
        this.status = status;
        this.dev = dev;
    }

    @Override
    public String toString() {
        return "Employee [age=" + age + ", dev=" + dev + ", id=" + id + ", name=" + name + ", salary=" + salary
                + ", status=" + status + "]";
    }

    public String toTeamString() {
        return "Employee [age=" + age + ", dev=" + dev  + ", name=" + name + ", salary=" + salary
                + ", status=" + status + "]";
    }

}
package com.la.base;

import com.la.service.EmployeeStatus;

public class Architect extends Employee {
    private double bonus;
    private static final String jobName = "架构师";

    @Override
    public String getJobName() {
        return jobName;
    }

    @Override
    public String toString() {
        return id + "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t" + bonus + "\t\t"
                + dev.getDesc();
    }

    public Architect(int id, int age, String name, double salary, EmployeeStatus status, Device dev, double bonus) {
        super(id, age, name, salary, status, dev);
        this.bonus = bonus;
    }

    @Override
    public String toTeamString() {
        return "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t" + bonus + "\t\t"
                + dev.getDesc();
    }
    


}
package com.la.base;

import com.la.service.EmployeeStatus;

public class Coder extends Employee {
    private static final String jobName = "程序员";

    @Override
    public String getJobName() {
        return jobName;
    }

    @Override
    public String toString() {
        return id + "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t\t\t"
                + dev.getDesc();
    }

    public Coder(int id, int age, String name, double salary, EmployeeStatus status, Device dev) {
        super(id, age, name, salary, status, dev);
    }

    @Override
    public String toTeamString() {
        return "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t\t\t"
                + dev.getDesc();
    }
    

}
package com.la.base;

import com.la.service.EmployeeStatus;

public class Desiger extends Employee {
    private double bonus;
    private int shares;
    private static final String jobName = "设计师";

    @Override
    public String getJobName() {
        return jobName;
    }

    @Override
    public String toString() {
        return id + "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t" + bonus + "\t"
                + shares + "\t" + dev.getDesc();
    }

    public Desiger(int id, int age, String name, double salary, EmployeeStatus status, Device dev, double bonus,
            int shares) {
        super(id, age, name, salary, status, dev);
        this.bonus = bonus;
        this.shares = shares;
    }

    @Override
    public String toTeamString() {
        return "\t" + name + "\t" + age + "\t" + salary + "\t" + jobName + "\t" + status + "\t" + bonus + "\t"
                + shares + "\t" + dev.getDesc();
    }
}

设备接口及其三个实现类

package com.la.base;

public interface Device {
    String getDesc();
}
package com.la.base;

public class NoteBook implements Device{
    private String model;
    private String price;
    
    @Override
    public String getDesc() {
        return model+"("+price+")";
    }

    public NoteBook(String model, String price) {
        this.model = model;
        this.price = price;
    }
    
}
package com.la.base;

public class PC implements Device {
    private String model;
    private String display;

    public PC(String model, String display) {
        this.model = model;
        this.display = display;
    }

    @Override
    public String getDesc() {
        return model+"("+display+")";
    }

    
    
}
package com.la.base;

public class Print implements Device {
    private String model;
    private String type;

    public Print(String model, String type) {
        this.model = model;
        this.type = type;
    }

    @Override
    public String getDesc() {
        return model + "(" + type + ")";
    }

}

员工数据类,文件中存储了所有员工的信息

package com.la.service;

import com.la.base.*;

/**
 * EmployeeDate存储了程序要用的所有数据
 * <p>通过EmployeeDate.getEmployees()可以获取,且EmployeeDate也只提供了这一个方法
 */
public class EmployeeData {
    private static final int DESIGER = 1;// 设计师
    private static final int ARCHITECT = 2;// 架构师
    // private static final int CODER = 3;// 程序员
    private static final int NOTEBOOK = 21;// 笔记本
    private static final int PC = 22;// PC
    // private static final int PRINT = 23;// 打印机

    /**
     * 获取员工的部分数据(所属职位,编号,姓名,年龄,状态,薪水,资金,股票)
     * 
     * @return
     */
    private static String[][] getDatas() {
        return new String[][] { { "1", "1000", "张三", "23", "F", "13000", "1000", "100" },
                { "3", "1001", "李四", "19", "B", "6000" }, { "1", "1002", "王大", "29", "F", "16000", "2000", "400" },
                { "2", "1003", "熊火火", "30", "F", "14000", "1000" },
                { "1", "1004", "李四龙", "27", "F", "12000", "1200", "500" }, { "3", "1005", "黄爱国", "24", "F", "8000" },
                { "2", "1006", "游大城", "33", "B", "12000", "1000" },
                { "1", "1007", "邓小剑", "21", "B", "10000", "4000", "700" },
                { "2", "1008", "TOM", "22", "F", "15000", "3000" }, { "3", "1009", "JOHN", "38", "F", "7000" }, };
    }

    /**
     * 获取对应员工领取的设备
     * 
     * @return
     */
    private static String[][] getDevices() {
        return new String[][] { { "22", "联想", "21寸" }, { "21", "联想", "6600" }, { "23", "兄弟", "br210" },
                { "22", "HP", "17寸" }, { "21", "三星", "5200" }, { "22", "长城", "4100" }, { "23", "三星", "SX9900" },
                { "21", "华为", "hw15" }, { "21", "方正", "fz190" }, { "23", "HP", "s1017" } };
    }

    /**
     * 获取员工的全部数据
     * 
     * @return
     */
    public static Employee[] getEmployees() {
        String[][] data = getDatas();
        String[][] devData = getDevices();
        Employee[] emps = new Employee[data.length];// 员工数组

        for (int i = 0; i < data.length; i++) {
            String[] str = data[i];// 对应的员工部分数据
            String[] devStr = devData[i];// 对应的设备
            int type = Integer.parseInt(str[0]);
            int devType = Integer.parseInt(devStr[0]);
            Device aDevice;
            // 创建对应的设备对象
            switch (devType) {
                case NOTEBOOK:
                    aDevice = new NoteBook(devStr[1], devStr[2]);
                    break;
                case PC:
                    aDevice = new PC(devStr[1], devStr[2]);
                    break;
                default:
                    aDevice = new Print(devStr[1], devStr[2]);
                    break;
            }
            EmployeeStatus status;
            // 创建对应的状态实例
            switch (str[4].charAt(0)) {
                case 'F':
                    status = EmployeeStatus.FREE;
                    break;
                case 'B':
                    status = EmployeeStatus.BUSY;
                    break;
                default:
                    status = EmployeeStatus.INTEAM;
                    break;
            }
            // 创建对应的员工实例,并添加至数组emps
            switch (type) {
                case DESIGER:
                    emps[i] = new Desiger(Integer.parseInt(str[1]), Integer.parseInt(str[3]), str[2],
                            Double.parseDouble(str[5]), status, aDevice, Double.parseDouble(str[6]),
                            Integer.parseInt(str[7]));
                    break;
                case ARCHITECT:
                    emps[i] = new Architect(Integer.parseInt(str[1]), Integer.parseInt(str[3]), str[2],
                            Double.parseDouble(str[5]), status, aDevice, Double.parseDouble(str[6]));
                    break;
                default:
                    emps[i] = new Coder(Integer.parseInt(str[1]), Integer.parseInt(str[3]), str[2],
                            Double.parseDouble(str[5]), status, aDevice);
                    break;
            }
        }
        return emps;
    }
}

员工状态枚举类

package com.la.service;

public class EmployeeStatus {
    private String name;
    public static final EmployeeStatus FREE = new EmployeeStatus("FREE");
    public static final EmployeeStatus BUSY = new EmployeeStatus("BUSY");
    public static final EmployeeStatus INTEAM=new EmployeeStatus("INTEAM");

    private EmployeeStatus() {
    }

    private EmployeeStatus(String name) {
        this.name = name;
    }

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

Team类提供了团队成员增删改查功能

package com.la.service;

import java.util.Arrays;

import com.la.base.Employee;

/**
 * 团队类
 * 
 * <p>提供获取、删除、增加团队成员的功能
 */
public class Team {
    private Employee[] team = new Employee[2];
    //团队成员总数
    private static int total = 0;
    //团队中程序员总数,总数最大为2
    private static int coderTotal=0;
    //团队中设计师总数,总数最大为1
    private static int desigerTotal=0;
    //团队中架构师总数,总数最大为2
    private static int architectTotal=0;
    public static final Team newTeam = new Team();

    private Team() {
    }
    /**
     * 删除指定成员
     * @param index 指定成员的索引
     * @return 删除后相应的描述
     */
    public String delEmployee(int index) {
        if (index>=0 && index<total){
            String jobName = team[index].getJobName();
            //团队中职员职位数量减1
            switch (jobName) {
                case "架构师":
                    architectTotal--;
                    break;
                case "设计师":
                    desigerTotal--;
                    break;
                default:
                    coderTotal--;
                    break;
            }
            //该员工状态设为FREE
            team[index].setStatus(EmployeeStatus.FREE);
            //在团队中删除该员工
            for (int i = index; i < total; i++) {
                if (total==team.length) {
                    if (i==total-1) {
                        team[i]=null;
                    }else{
                        team[i]=team[i+1];
                    }
                }else{
                    team[i]=team[i+1];
                }
            }
            //团队中成员数量减1
            total--;
            return "删除成功";
        }
        return "编号输入错误或者团队中还没有成员,删除失败";
    }
    /**
     * 返回指定index的员工
     * @param index team数组索引
     * @return 团队中的员工实例
     */
    public Employee getEmployee(int index) {
        if (index>=0 && index<total) {
            return team[index];
        }
        return null;
    }

    /**
     * 返回数组team的成员个数
     * 
     * @return
     */
    public int size() {
        return total;
    }

    /**
     * 添加团队成员
     * 
     * @param e 员工实例
     * @return 添加后相应的描述
     */
    public String addEmployee(Employee e) {
        if (e == null || !(e instanceof Employee)) {
            return "添加的数据为空或者数据类型不对";
        }
        //判断员工状态,如果空闲则进入下一步
        if (e.getStatus()==EmployeeStatus.FREE) {
            //给team数组扩容
            if (total == team.length) {
                Employee[] temp = Arrays.copyOf(team, total + 2);
                team = temp;
            }
            
            String name=e.getJobName();
            //判断团队中的相应职位数是否已满
            switch (name) {
                case "程序员":
                    if (coderTotal>=2) {
                        return "团队中最多只能有2个程序员";
                    }
                    coderTotal++;
                    break;
                case "设计师":
                    if (desigerTotal>=1) {
                        return "团队中最多只能有1个设计师";
                    }
                    desigerTotal++;
                    break;
                default:
                    if (architectTotal>=2) {
                        return "团队中最多只能有2个架构师";
                    }
                    architectTotal++;
                    break;
            }
            //把员工添加进团队,并团队成员数+1
            team[total++] = e;
            //设置该员工状态为inteam
            e.setStatus(EmployeeStatus.INTEAM);
            return "添加成功!";
        } else if(e.getStatus()==EmployeeStatus.INTEAM){
            return "对方正在团队中,不需要添加";
        }else{
            return "对方在别的团队,无法添加";
        }
        
    }
}

工具类,提供从控制台获取数据

package com.la.util;
import java.util.Scanner;

public class ScanUtil{
    private static Scanner scan=new Scanner(System.in);
    /**
     * 从控制台获取一个整数,不能为负
     * @param desc 如果用户输入格式不对,则在控制台显示的描述信息
     * @return 用户输入的数字
     */
    public static Integer getNum(String desc) {
        while (scan.hasNextLine()) {
            String str=scan.nextLine();
            try {
                Integer num=Integer.parseInt(str);
                if (num<0) {
                    System.out.print("不能输入负数,请重新输入:");
                    continue;
                }
                return num;
            } catch (Exception e) {
                System.out.print(desc+":");
            }
        }
        return null;
    }
    /**
     * 从控制台获取一个指定范围(start,end)的整数
     * @param start 指定范围的边界,获得的数字不能小于start
     * @param end 指定范围的边界,获得的数字不能大于end
     * @param desc 如果用户输入的整数不在指定范围内,则在控制台显示的描述信息
     * @return 用户输入的数字
     */
    public static Integer getAppointNum(int start,int end,String desc) {
        while (true) {
            int num=getNum(desc);
            if (num>=start && num<= end) {
                return num;
            }
            System.out.print(desc+":");
        }
    }
    /**
     * 按回车键继续
     */
    public static void continueEntry(){
        System.out.print("按回车键继续");
        scan.nextLine();
    }
}

显示类,功能:在控制台显示所有操作

package com.la.view;

import com.la.base.Employee;
import com.la.service.EmployeeData;
import com.la.service.Team;
import com.la.util.ScanUtil;
/**
 * EmployeeView 提供在控制台显示的功能 
 */
public class EmployeeView {
    private Employee[] emps = EmployeeData.getEmployees();
    private Team team = Team.newTeam;
    private static EmployeeView ev;

    private EmployeeView() {
        mainMenu();
    }

    // 单例模式-懒汉式
    public static EmployeeView getView() {
        if (ev == null) {
            ev = new EmployeeView();
        }
        return ev;
    }

    /**
     * 主菜单
     */
    private void mainMenu() {
        while (true) {
            System.out.println("-------------------------------开发团队调度软件-----------------------------");
            System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
            for (Employee employee : emps) {
                System.out.println(employee);
            }
            System.out.println("----------------------------------------------------------------------------");
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出  请选择(1-4):");
            int num = ScanUtil.getAppointNum(1, 4, "只能选择1-4,请重新输入");
            switch (num) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    return;
            }
        }
    }

    /**
     * 添加团队成员
     */
    private void add() {
        System.out.println("---------------------添加成员---------------------");
        System.out.print("请输入要添加的员工ID:");
        int num = ScanUtil.getNum("输入的不是数字,请重新输入");
        num -= 1000;
        if (num<0 || num>=emps.length) {
            System.out.println("输入的编号不正确");
        }else{
            System.out.println(team.addEmployee(emps[num]));
        }
        ScanUtil.continueEntry();
    }

    /**
     * 显示团队成员
     */
    private void list() {
        System.out.println("---------------------团队成员---------------------");
        if (team.size() == 0) {
            System.out.println("团队中还没有成员呢");
            ScanUtil.continueEntry();
            return;
        }
        for (int i = 0; i < team.size(); i++) {
            System.out.println(i + 1 + team.getEmployee(i).toTeamString());
        }
        ScanUtil.continueEntry();
    }

    /**
     * 删除团队成员
     */
    private void del() {
        System.out.println("---------------------删除团队成员---------------------");
        System.out.print("请输入要删除的员工TID:");
        int num = ScanUtil.getNum("输入的不是数字,请重新输入");
        System.out.println(team.delEmployee(num - 1));
        ScanUtil.continueEntry();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值