Java基础:Java基于文本界面的实现开发团队成员调度软件(全)

 该软件为基于文本界面的《团队人员调度软件》

–软件启动时,根据列表(数组)给定的数据创建公司部分成员

–根据菜单提示,基于现有的公司成员,组建一个开发团队以开发一个新的项目

–组建过程包括将成员插入到团队中,或从团队中删除某成员,还可以列出团队中现在成员的列表

开发团队成员包括架构师、设计师和程序员

首先,定义好团队人员数据

package com.project.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"}
    };

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

依次实现bean

package com.project.domain;

import com.project.service.Status;

public class Architect extends Designer{
	private int stock;//公司奖励的股票数量
	private Status statue;
	public Status getStatue() {
		return statue;
	}
	public void setStatue(Status statue) {
		this.statue = statue;
	}
	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;
	}
	public String toString() {
        return getDetails() + "\t架构师\t" + getStatus() + "\t" +
               getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
    }
	
}
package com.project.domain;

import com.project.service.Status;

public class Designer extends Programmer{
	private double bonus;
	private Status status;
	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;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public String toString() {
        return getDetails() + "\t设计师\t" + getStatus() + "\t" +
               getBonus() +"\t\t" + getEquipment().getDescription();
    }

}
package com.project.domain;


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 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;
	}
	public String getDetails() {
		return id + "\t" + name + "\t" + age+ "\t" +salary;
	}
    public String toString() {
        return getDetails();
    }

}
package com.project.domain;

import com.project.service.Status;

public class Programmer extends Employee{
	private int memberId;
	private Status status;
	private Equipment equipment;
	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 getDetails() + "\t程序员\t" + status + "\t\t\t" + equipment.getDescription() ;
    }
}

需要领取的设备,设置其为接口,再次实现

package com.project.domain;

public interface Equipment {
	String getDescription();
}
package com.project.domain;

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 String getDescription() {
		return model +"\t" +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;
	}
	
}
package com.project.domain;

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 String getDescription() {
		return model + "\t" + 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;
	}
	
}
package com.project.domain;

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 String getDescription() {
		return type+"\t"+name;
	}
	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;
	}
	
}
package com.project.service;

import com.project.domain.Architect;
import com.project.domain.Designer;
import com.project.domain.Employee;
import com.project.domain.Equipment;
import com.project.domain.NoteBook;
import com.project.domain.PC;
import com.project.domain.Printer;
import com.project.domain.Programmer;
public class NameListService {
	private Employee[] employees;
//	public static void main(String[] args) {
//		NameListService namelist = new NameListService();
//		namelist.getAllEmployees();
//	}
	
	public NameListService() {
		super();
	}
	public Equipment getEquipments(int k) {
			if (Integer.parseInt(Data.EQIPMENTS[k][0]) == 21) {
				Equipment pc =  new PC(Data.EQIPMENTS[k][1], Data.EQIPMENTS[k][2]);
				return pc;
			}
			if (Integer.parseInt(Data.EQIPMENTS[k][0]) == 22) {
				Equipment notebook = new NoteBook(Data.EQIPMENTS[k][1], Double.parseDouble(Data.EQIPMENTS[k][2]));
				return notebook;
			}
			if (Integer.parseInt(Data.EQIPMENTS[k][0]) == 23) {
				Equipment printer = new Printer(Data.EQIPMENTS[k][1], Data.EQIPMENTS[k][2]);
				return printer;
			}
		return null;
	}
	/**
	 * 功能:获取当前所有员工
	 * @return 返回所有包含员工对象的数组	
	 */
	public Employee[] getAllEmployees() {
		employees = new Employee[Data.EMPLOYEES.length];
		
		for (int i = 0; i < employees.length; i++) {
			if (Integer.parseInt(Data.EMPLOYEES[i][0]) == 10) {
				
				employees[i] = new Employee(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], 
						Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]));
				
			}
			if(Integer.parseInt(Data.EMPLOYEES[i][0]) == 11) {
				employees[i] = new Programmer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2],
						Integer.parseInt(Data.EMPLOYEES[i][3]),  Double.parseDouble(Data.EMPLOYEES[i][4]), 
						getEquipments(i));
				Programmer p = (Programmer) employees[i];
				p.setStatus(Status.FREE);
			}
			if(Integer.parseInt(Data.EMPLOYEES[i][0]) == 12) {
				employees[i] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2],
						Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), 
						getEquipments(i), Double.parseDouble(Data.EMPLOYEES[i][5]));
				Designer d = (Designer) employees[i];
				d.setStatus(Status.FREE);
			}
			if(Integer.parseInt(Data.EMPLOYEES[i][0]) == 13) {
				employees[i] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2],
						Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), 
						getEquipments(i), Double.parseDouble(Data.EMPLOYEES[i][5]),
						Integer.parseInt(Data.EMPLOYEES[i][6]));
				Architect d = (Architect) employees[i];
				d.setStatus(Status.FREE);
			}

		}
		
		return employees;
	}
	/**
	 * 功能:获取指定id的员工对象	
	 * @param id  指定员工的id
	 * @return  返回员工对象
	 * @throws TeamException  找不到指定的员工
	 */
	public Employee getEmployee(int id) throws TeamException{
		for (Employee e : employees) {
			if (e.getId() == id)
				return e;
		}
		throw new TeamException("该员工不存在");
	}
}
package com.project.service;

public enum Status {
    FREE, BUSY, VOCATION
}
package com.project.service;

public class TeamException extends RuntimeException{
	public TeamException() {
		super();
	}
	public TeamException(String message) {
		super(message);
	}
	
}
package com.project.service;

import com.project.domain.Architect;
import com.project.domain.Designer;
import com.project.domain.Employee;
import com.project.domain.Programmer;

public class TeamService {
	private static int counter;//为静态变量,用来为开发团队新增成员自动生成团队中的唯一ID,即memberId
	final private int MAX_MEMBER =5;//开发团队最大成员数
	private int total =0;//记录团队成员的实际人数
	private int counterPro = 0,counterAch = 0,counterDes = 0;
	
	
	
	
	Programmer[] team = new Programmer[MAX_MEMBER];
	
	
	public TeamService() {
		super();
	}
	/**
	 * 功能:向团队中添加成员
	 * @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;
		
		switch (p.getStatus()) {
			case BUSY:
				throw new TeamException("该成员正忙!");
			case VOCATION:
				throw new TeamException("该成员正在休假中!");
		}
		if (isTrue(p)) {
			throw new TeamException("该员工已是团队成员!");
		}
		
		if (p instanceof Architect) {
			if (counterAch>=1) {
				throw new TeamException("团队中只能有一名架构师!");
			}
		}
		if (p instanceof Designer) {
			if (counterAch>=2) {
				throw new TeamException("团队中只能有两名设计师!");
			}
		}
		if (p instanceof Programmer) {
			if (counterAch>=3) {
				throw new TeamException("团队中只能有三名程序员!");
			}
		}
		p.setStatus(Status.BUSY);
		p.setMemberId(counter++);
		team[total] = p;
		total++;
		
		if (p instanceof Architect) {
			counterAch++;
		}else if (p instanceof Designer) {
			counterDes++;
		}else if (p instanceof Programmer) {
			counterPro++;
		}
		
	}
	/**
	 * 功能:判断是否为团队成员
	 * @param p 需要判断的成员
	 * @return  true:已是团队员工;false:不是团队员工
	 */
	public boolean isTrue(Programmer p) {
		for (int i = 0; i < total; i++) {
			if (p.getId() == team[i].getId()) 
				return true;
		}
		return false;
	}
	/**
	 * 功能:从团队中删除成员
	 * @param memberld	待删除的成员
	 * @throws TeamException	删除失败,TeamException中包含了失败原因
	 */
	public void removeMember(int memberld) throws TeamException{
		 int n = 0;//记录要删除的员工的下标
	        for (; n < total; n++) {
	            if (team[n].getMemberId() == memberld) {
	                team[n].setStatus(Status.FREE);
	                break;
	            }
	        }
	      if (n==total) 
			throw new TeamException("未找到该成员,无法删除!");
	      for (int i = n + 1; i < total; i++) {
	            team[i - 1] = team[i];//后一个元素复制给前一个元素
	        }
	        team[--total] = null;//总人数减1,最后一个元素因为已经复制到前一个元素中了,因此置为null
	}
	/**
	 * 功能:返回当前团队的所有对象
	 * @return	包含所有成员对象的数组,数组大小与成员人数一致
	 */
	public Programmer[] getTeam() {
		Programmer[] team = new Programmer[total];
		
		for (int i = 0; i <total; i++) {
			team[i] = this.team[i];
		}
		return team;
	}
}
package com.project.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;
    }
}

package com.project.view;

import com.project.domain.Architect;
import com.project.domain.Designer;
import com.project.domain.Employee;
import com.project.domain.Programmer;
import com.project.service.NameListService;
import com.project.service.TeamException;
import com.project.service.TeamService;
import com.project.utils.TSUtility;

public class TeamView {
	NameListService listSvc = new NameListService();
	TeamService teamSvc = new TeamService();
	
	public  void enterMainMenu() {
		
		
		boolean loop = true;
		do {
		System.out.println("------------------------开发团队调度软件----------------------");
		System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
		listAllEmployees();
		System.out.println("----------------------------------------------------------");
		System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出  请选择(1-4):");
		char key = TSUtility.readMenuSelection();
			switch (key) {
			case '1':
				listMember();
				break;
			case '2':
				addMember();
				break;
			case '3':
				deleteMember();
				break;
			case '4':
				loop = exit();
				break;
			
			}
		}while(loop);
		
	}
	
	private void listMember() {
		Programmer[] team = teamSvc.getTeam();
		System.out.println("------------------------团队成员列表----------------------");
		if(team.length ==0) {
			System.out.println("团队目前还没有成员!");
		}else {
			System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
			for (Programmer p : team) {
	        	 if (p instanceof Architect) {
	        		 Architect a = (Architect) p;//向下转型,才能访问架构师中的奖金与股票信息
	        		 System.out.println(" " + a.getMemberId() + "/" + a.getDetails() + "\t架构师\t" +  a.getBonus() + "\t" + a.getStock());
	             } else if (p instanceof Designer) {
	            	 Designer d = (Designer) p;//向下转型,才能访问设计师中的奖金信息
	            	 System.out.println(" " + d.getMemberId() + "/" + d.getDetails() + "\t设计师\t" + d.getBonus());
	             } else if (p instanceof Programmer) {
	            	 System.out.println(" " + p.getMemberId() + "/" + p.getDetails() + "\t程序员");
	             }
	        }
	        System.out.println("-----------------------------------------------------");

		}
		
	}

	/**
	 * 以表格形式列出公司所有成员
	 */
	private   void listAllEmployees() {
		Employee[] allEmployees = listSvc.getAllEmployees();
		
		for (int j = 0; j < allEmployees.length; j++) {
			System.out.println(allEmployees[j]);
		}
	}
	/**
	 * 实现添加成员操作
	 */
	private  void addMember() {
		System.out.println("----------------------------添加成员--------------------------");
		System.out.print("请输入要添加员工的ID:");
		try {
			int id = TSUtility.readInt();
			Employee employee = listSvc.getEmployee(id);
			teamSvc.addMember(employee);
			System.out.println("----------------------------添加成功--------------------------");
		} catch (TeamException e) {
			System.out.println("添加失败,原因:"+e.getMessage());
		}
		TSUtility.readReturn();
	}
	/**
	 * 实现删除成员操作
	 */
	private  void deleteMember() {
		System.out.println("----------------------------删除成员--------------------------");
		System.out.print("请输入要删除员工的TID:");
		int id = TSUtility.readInt();
		System.out.print("确认是否删除(Y/N):");
		char key =TSUtility.readConfirmSelection();
		if (key == 'N') return ;
		try {
			teamSvc.removeMember(id);;
			System.out.println("----------------------------删除成功--------------------------");
		} catch (TeamException e) {
			System.out.println("删除失败,原因:"+e.getMessage());
		}
		TSUtility.readReturn();
	}
	public boolean exit() {
		System.out.print("确认是否要退出?Y/N :");
		char key = TSUtility.readConfirmSelection();
		return key=='N';
		
	}
	public static void main(String[] args) {
		
		TeamView teamV = new TeamView();
		teamV.enterMainMenu();
	}
	
}

完!

猜你喜欢

转载自blog.csdn.net/ma_zhifu/article/details/81428826