【Java20】开发团队调度系统


1.介绍

增删查,没有改。普通员工不要,判断是不是架构师,设计师,程序员用instance of判断对象类型。给定数据就是Data.java需要装成一个个对象为公司员工。
在这里插入图片描述
增:不能添加就抛异常
在这里插入图片描述
在这里插入图片描述
删:删除成功后按回车键重新现实主界面
在这里插入图片描述
查:先择1
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Employee是父类,length是行数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.M

2.1 员工和队员

package com.atguigu.bean;

public class Employee {
    
    
	private int id;
	private String name;
	private int age;
	private double salary;
	public Employee(int id, String name, int age, double salary) {
    
    
		super();
		this.id = id;
		this.name = name;
		this.age = age;
		this.salary = salary;
	}
	public Employee() {
    
    
		super();
	}
	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;
	}
	@Override
	public String toString() {
    
    
		return getBasicInfo();
	}
	protected String getBasicInfo() {
    
    
		return id + "\t" + name + "\t" + age + "\t" + salary;
	}	
}
package com.atguigu.bean;

public class Programmer extends Employee {
    
     //Programmer程序员
	private int memberId;//团队编号     在加入到团队时,才开始分配,不是创建对象时分配
	private Status status = Status.FREE;  //Status.java
	private Equipment equipment;
	public Programmer() {
    
    
		super();
	}
	public Programmer(int id, String name, int age, double salary, int memberId, Status status, Equipment equipment) {
    
     //全的
		super(id, name, age, salary);
		this.memberId = memberId;
		this.status = status;
		this.equipment = equipment;
	}
	public Programmer(int id, String name, int age, double salary, Equipment equipment) {
    
     //Date.java中可看到
		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;
	}
	@Override
	public String toString() {
    
    //没有奖金和股票
		return getBasicInfo()  +"\t程序员\t" + status + "\t\t\t" + equipment;
	}
	public String getMemberInfo(){
    
    
		return memberId + "/" + getBasicInfo() +"\t程序员";
	}
}
package com.atguigu.bean;

public class Designer extends Programmer{
    
    
	private double bonus;
	public Designer() {
    
    
		super();
	}
	public Designer(int id, String name, int age, double salary, Equipment equipment, double bonus) {
    
    
		super(id, name, age, salary, equipment);
		this.bonus = bonus;
	}
	public Designer(int id, String name, int age, double salary, int memberId, Status status, Equipment equipment, double bonus) {
    
    
		super(id, name, age, salary, memberId, status, equipment);
		this.bonus = bonus;
	}
	public double getBonus() {
    
    
		return bonus;
	}
	public void setBonus(double bonus) {
    
    
		this.bonus = bonus;
	}	
	@Override
	public String toString() {
    
     //没有股票
		return getBasicInfo() + "\t设计师\t" + getStatus() + "\t" + bonus +"\t\t" + getEquipment();
	}	
	public String getMemberInfo(){
    
    
		return getMemberId() + "/" + getBasicInfo() +"\t设计师\t" + bonus;
	}
}
package com.atguigu.bean;

public class Architect extends Designer{
    
    
	private int stock;
	public Architect() {
    
    
		super();
	}
	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 Architect(int id, String name, int age, double salary, int memberId, Status status, Equipment equipment, double bonus, int stock) {
    
    
		super(id, name, age, salary, memberId, status, equipment, bonus);
		this.stock = stock;
	}
	public int getStock() {
    
    
		return stock;
	}
	public void setStock(int stock) {
    
    
		this.stock = stock;
	}
	@Override
	public String toString() {
    
    
		return getBasicInfo() + "\t架构师\t" + getStatus() + "\t" + getBonus() +"\t" +stock+ "\t" + getEquipment();
	}
	public String getMemberInfo(){
    
    
		return getMemberId() + "/" + getBasicInfo() +"\t架构师\t" + getBonus() + "\t" + stock;
	}
}
package com.atguigu.bean;

public enum Status {
    
    
	BUSY,FREE,VOCATION
}

2.2 设备

package com.atguigu.bean;

public interface Equipment {
    
    
	String getDescription();
}
package com.atguigu.bean;

public class PC implements Equipment{
    
    
	private String model;
	private String display;
	public PC(String model, String display) {
    
    
		super();
		this.model = model;
		this.display = display;
	}
	public PC() {
    
    
		super();
	}
	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 toString() {
    
    
		return getDescription();
	}
	@Override
	public String getDescription() {
    
    
		return model + "(" + display + ")" ; // 戴尔(NEC17寸)  //model指品牌
	}	
}
package com.atguigu.bean;

public class NoteBook implements Equipment{
    
    
	private String model;
	private double price;
	public NoteBook(String model, double price) {
    
    
		super();
		this.model = model;
		this.price = price;
	}
	public NoteBook() {
    
    
		super();
	}
	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 toString() {
    
    
		return getDescription();
	}
	@Override
	public String getDescription() {
    
    
		return model + "(" + price + ")"; // 联想T4(6000.0)
	}	
}
package com.atguigu.bean;

public class Printer implements Equipment{
    
    
	private String type;
	private String name;
	public Printer(String type, String name) {
    
    
		super();
		this.type = type;
		this.name = name;
	}
	public Printer() {
    
    
		super();
	}
	public String getType() {
    
    
		return type;
	}
	public void setType(String type) {
    
    
		this.type = type;
	}
	public String getName() {
    
    
		return name;
	}
	public void setName(String name) {
    
    
		this.name = name;
	}
	@Override
	public String toString() {
    
    
		return getDescription();
	}
	@Override
	public String getDescription() {
    
    	
		return name + "(" + type + ")"; //佳能 2900(激光)
	}	
}

3.V

package com.atguigu.view;
import com.atguigu.bean.Employee;
import com.atguigu.bean.Programmer;
import com.atguigu.exception.TeamException;
import com.atguigu.service.NameListService;
import com.atguigu.service.TeamService;
import com.atguigu.utils.TSUtility;

public class TeamView {
    
    
	private NameListService ns = new NameListService();
	private TeamService ts = new TeamService();	
	public void menu(){
    
    
		System.out.println("-------------------------------------开发团队调度软件--------------------------------------");
		getAllEmployee();		
		while(true){
    
    
			System.out.println("---------------------------------------------------------------------------------------------------");
			System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4):");
			char select = TSUtility.readMenuSelection();
			switch(select){
    
    
			case '1':
				list();
				break;
			case '2':
				getAllEmployee();
				add();
				break;
			case '3':
				list();
				remove();
				break;
			case '4':
				System.out.print("确认是否退出(Y/N):");
				char confirm = TSUtility.readConfirmSelection();
				if(confirm == 'Y'){
    
    
					return;
				}
			}
		}
	}	
	
//111111111111111111111111111111111111111111111111111111111111111111111111111111111	
	private void remove() {
    
    
		System.out.println("---------------------删除成员---------------------");
		System.out.print("请输入要删除员工的TID:"); //(1)输入要删除的团队成员的团队编号
		int tid = TSUtility.readInt();	
			
		System.out.print("确认是否删除(Y/N):"); //(2)确认是否删除
		char confirm = TSUtility.readConfirmSelection();
		if(confirm == 'N'){
    
    
			System.out.println("删除取消!");
			return;//提前结束删除
		}		
		
		try {
    
    
			ts.removeMemberByTid(tid); //(3)删除,调用TeamService的removeMemberByTid删除
			System.out.println("删除成功!");
		} catch (TeamException e) {
    
    
			System.out.println("删除失败,原因:" + e.getMessage());
		}		
		TSUtility.readReturn();
	}
	
//11111111111111111111111111111111111111111111111111111111111111111111111111111111111	
	private void add() {
    
    
		System.out.println("---------------------添加成员---------------------");
		System.out.print("请输入要添加的员工ID:"); //(1)输入编号
		int id = TSUtility.readInt();		
		
		try {
    
      
			Employee emp = ns.getEmployeeById(id);	//(2)根据编号获取员工对象					
			ts.addMember(emp);	 //(3)添加到团队中		
			System.out.println("添加成功");
		} catch (TeamException e) {
    
    
			System.out.println("添加失败,原因:" + e.getMessage());
		}		
		TSUtility.readReturn();
	}
	
//111111111111111111111111111111111111111111111111111111111111111111111111111	
	private void list() {
    
    
		System.out.println("--------------------团队成员列表---------------------");
		System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
		Programmer[] allMembers = ts.getAllMembers();
		
		for (int i = 0; i < allMembers.length; i++) {
    
    
//			System.out.println(allMembers[i]);//自动调用toString()
			System.out.println(allMembers[i].getMemberInfo());
		}
		System.out.println("-----------------------------------------------------");
	}
	
//111111111111111111111111111111111111111111111111111111111111111111111111111111111
	private void getAllEmployee(){
    
    
		System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
		Employee[] all = ns.getAll(); //(1)获取所有的员工的信息		
		for (int i = 0; i < all.length; i++) {
    
     //(2)遍历
			System.out.println(all[i]);//自动调用toString()
		}
	}
}

4.C

package com.atguigu.service;
import java.util.Arrays;
import com.atguigu.bean.Architect;
import com.atguigu.bean.Designer;
import com.atguigu.bean.Employee;
import com.atguigu.bean.Programmer;
import com.atguigu.bean.Status;
import com.atguigu.exception.TeamException;

public class TeamService {
    
    
	private Programmer[] team;//用来装开发团队的成员//因为开发团队,要求必须是程序员、设计师、架构师
	private int total;//记录实际开发团队的成员的数量
	private static final int MAX_MEMBER = 5;
	private int currentMemberId = 1;//只增不减	
	public TeamService(){
    
    
		team = new Programmer[MAX_MEMBER];
	}	

//1111111111111111111111111111111111111111111111111111111111111111111111111111111	
	public void addMember(Employee emp) throws TeamException{
    
     //添加一个团队成员
		if(total >= MAX_MEMBER){
    
     //(1)判断总人数
			throw new TeamException("成员已满,无法添加");
		}		
		if(!(emp instanceof Programmer)){
    
     //(2)判断是否是程序员或它子类的对象
			throw new TeamException("该成员不是开发人员,无法添加");
		}		
			
		//如果要获取状态信息,需要把emp对象向下转型
		Programmer p = (Programmer) emp; //(3)判断状态
		switch(p.getStatus()){
    
    
		case BUSY:
			throw new TeamException("该员已是团队成员");
		case VOCATION:
			throw new TeamException("该员正在休假,无法添加");
		}		
		
		//现统计当前team中的每一个类型的对象的人数
		int pCount = 0;//程序员人数 //(4)判断每一种人的人数
		int dCount = 0;//设计师人数
		int aCount = 0;//架构师人数		
		//这里用total,有几个人统计几个人
		for (int i = 0; i < total; i++) {
    
    
			//条件判断有顺序要求
			if(team[i] instanceof Architect){
    
    
				aCount++;
			}else if(team[i] instanceof Designer){
    
    
				dCount++;
			}else{
    
    
				pCount++;
			}
		}	
			
		//如果刚才传入的emp-->p,是架构师,我们再看架构师够不够
		//如果刚才传入的emp-->p,是设计师,我们再看设计师够不够
		//如果刚才传入的emp-->p,是程序员,我们再看程序员够不够
		if(emp instanceof Architect ){
    
    
			if(aCount >= 1){
    
    
				throw new TeamException("团队中只能有一名架构师");
			}
		}else if(emp instanceof Designer ){
    
    
			if(dCount >= 2){
    
    
				throw new TeamException("团队中只能有两名设计师");
			}
		}else{
    
    
			if(pCount >= 3){
    
    
				throw new TeamException("团队中只能有三名程序员");
			}
		}		
		
		//添加之前,修改状态,分配团队编号
		p.setStatus(Status.BUSY); //(5)可以正常添加
		p.setMemberId(currentMemberId++);
		team[total++] = p; //添加到team数组
	}	
	
//111111111111111111111111111111111111111111111111111111111111111111111111111	
	public Programmer[] getAllMembers(){
    
     //返回所有团队成员
		return Arrays.copyOf(team, total);
	}

//111111111111111111111111111111111111111111111111111111111111111111111111111	
	//根据团队编号删除团队成员
	public void removeMemberByTid(int tid) throws TeamException{
    
    
		//(1)要查找tid成员对应的下标index
		int index = -1;
		//这里用total,有几个人判断几个人
		for (int i = 0; i < total; i++) {
    
    
			if(team[i].getMemberId() == tid){
    
    
				index = i;
				break;
			}
		}
		if(index == -1){
    
    
			throw new TeamException(tid + "的团队成员不存在!");
		}		
		//(2)先修改要被删除的成员的一些信息
		//team[index]这个成员要被删除
		team[index].setStatus(Status.FREE);
		team[index].setMemberId(0);		
		//(3)把index后面的元素往前移动
		/*
		 * 如果记不住,如何推断
		 * 第一个参数:原数组
		 * 第二个参数:从哪个开始移动
		 * 第三个参数:目标数组
		 * 第四个参数:第二个参数的下标的元素移动到哪个下标,例如:index+1位置的元素移动到index
		 * 第五个参数:一共移动几个
		 * 
		 * 假设total = 5个,删除index= 1位置的元素
		 * 移动[2]->[1],[3]->[2],[4]->[3]  3个  =total - index - 1
		 */
		System.arraycopy(team, index+1, team, index, total-index-1);		
		//(4)把最后一个置为null
		team[total--] = null;//使得这个对象尽快被回收,腾出内存		
	}
}
package com.atguigu.service;
import static com.atguigu.utils.Data.ARCHITECT; //。。。Data.*;
import static com.atguigu.utils.Data.DESIGNER;
import static com.atguigu.utils.Data.EMPLOYEE;
import static com.atguigu.utils.Data.EMPLOYEES;
import static com.atguigu.utils.Data.EQIPMENTS;
import static com.atguigu.utils.Data.NOTEBOOK;
import static com.atguigu.utils.Data.PC;
import static com.atguigu.utils.Data.PRINTER;
import static com.atguigu.utils.Data.PROGRAMMER;
import com.atguigu.bean.Architect;
import com.atguigu.bean.Designer;
import com.atguigu.bean.Employee;
import com.atguigu.bean.Equipment;
import com.atguigu.bean.NoteBook;
import com.atguigu.bean.PC;
import com.atguigu.bean.Printer;
import com.atguigu.bean.Programmer;
import com.atguigu.exception.TeamException;

public class NameListService {
    
    
	private Employee[] all;//用来存储全公司的员工对象	
	public NameListService(){
    
    
		init();
	}
	
//111111111111111111111111111111111111111111111111111111111111111111111111111			
	private void init(){
    
     //初始all数组的方法,数据的来源是Data.java
		//(1)创建all数组,并指定长度,原本Date.EMPLOYEES.length,导包后省略Date.
		all = new Employee[EMPLOYEES.length];	
			
		//(2)遍历Data中EMPLOYEES的二维数组,把一行一行的数据
		//封装为一个一个的Employee,Programmer等的对象,放到all数组中
		for (int i = 0; i < EMPLOYEES.length; i++) {
    
    
			int empType = Integer.parseInt(EMPLOYEES[i][0]); //EMPLOYEES[i][0]是员工类型	
				
			//因为每一种员工,都有id,name,age,salary,所以这些数据的读取转换,放在switch的上面
			int id = Integer.parseInt(EMPLOYEES[i][1]);	//EMPLOYEES[i][1]是员工编号id	
		
			String name = EMPLOYEES[i][2];	//EMPLOYEES[i][2]是员工姓名name
					
			int age = Integer.parseInt(EMPLOYEES[i][3]); //EMPLOYEES[i][3]是员工年龄age
						
			double salary = Double .parseDouble(EMPLOYEES[i][4]); //EMPLOYEES[i][4]是员工薪资salary			
			switch(empType){
    
    
			case EMPLOYEE: //应该是Data.EMPLOYEE,因为有静态导入所以省略Data.	
				all[i] = new Employee(id, name, age, salary); //all[i] = 创建Employee对象;
				break;
			case PROGRAMMER: //Data.java中定义过PROGRAMMER=11
//				all[i] = 创建Programmer对象;getEquipmentByLineNumber(i)得到一个设备对象
				all[i] = new Programmer(id, name, age, salary, getEquipmentByLineNumber(i));
				break;	
			case DESIGNER:
//				all[i] = 创建Designer对象;
				double bonus = Double.parseDouble(EMPLOYEES[i][5]);
				all[i] = new Designer(id, name, age, salary, getEquipmentByLineNumber(i), bonus);
				break;
			case ARCHITECT:
//				all[i] = 创建Architect对象;
				bonus = Double.parseDouble(EMPLOYEES[i][5]);
				int stock = Integer.parseInt(EMPLOYEES[i][6]);
				all[i] = new Architect(id, name, age, salary, getEquipmentByLineNumber(i), bonus, stock);
				break;			
			}
		}
	}

//111111111111111111111111111111111111111111111111111111111111111111111111111		
	private Equipment getEquipmentByLineNumber(int i){
    
     //读取第i行的设备对象
		//(1)读取设备的类型,即EQIPMENTS[i][0]
		int eType = Integer.parseInt(EQIPMENTS[i][0]);
		switch(eType){
    
    
		case PC:
			return new PC(EQIPMENTS[i][1], EQIPMENTS[i][2]);
		case NOTEBOOK:
			return new NoteBook(EQIPMENTS[i][1], Double.parseDouble(EQIPMENTS[i][2]));
		case PRINTER:
			return new Printer(EQIPMENTS[i][1], EQIPMENTS[i][2]);
		}
		return null;
	}

//111111111111111111111111111111111111111111111111111111111111111111111111111
	public Employee[] getAll(){
    
     //返回所有的员工对象
		return all;
	}

//1111111111111111111111111111111111111111111111111111111111111111111111111111
	public Employee getEmployeeById(int id) throws TeamException{
    
     //根据员工编号,获取员工对象
		for (int i = 0; i < all.length; i++) {
    
    
			if(all[i].getId() == id){
    
    
				return all[i];
			}
		}
		throw new TeamException(id+"对应的员工不存在");
	}
}

6.main函数

package com.atguigu.test;
import com.atguigu.view.TeamView;

public class TeamTest {
    
    
	public static void main(String[] args) {
    
    
		TeamView t = new TeamView();
		t.menu();
	}
}

在这里插入图片描述

7.工具

package com.atguigu.exception;

public class TeamException extends Exception{
    
    
	public TeamException() {
    
    
		super();
	}
	public TeamException(String message) {
    
    
		super(message);
	}
}
package com.atguigu.utils;

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"}
    };
    //PC      :21, model, display
    //NoteBook:22, model, price
    //Printer :23, type, name
    public static final String[][] EQIPMENTS = {
    
    
        {
    
    },
        {
    
    "22", "联想Y5", "6000"},
        {
    
    "21", "宏碁 ", "AT7-N52"},
        {
    
    "21", "戴尔", "3800-R33"},
        {
    
    "23", "激光", "佳能 2900"},
        {
    
    "21", "华硕", "K30BD-21寸"},
        {
    
    "21", "海尔", "18-511X 19"},
        {
    
    "23", "针式", "爱普生20K"},
        {
    
    "22", "惠普m6", "5800"},
        {
    
    "21", "联想", "ThinkCentre"},
        {
    
    "21", "华硕","KBD-A54M5 "},
        {
    
    "22", "惠普m6", "5800"}
    };
}
package com.atguigu.utils;
import java.util.*;

public class TSUtility {
    
    
    private static Scanner scanner = new Scanner(System.in);
	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;
    }
    public static void readReturn() {
    
    
        System.out.print("按回车键继续...");
        readKeyBoard(100, true);
    }
    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;
    }
    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;
    }
}

B站/知乎/微信公众号:码农编程录
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43435675/article/details/107545983