JAVA基础小结(项目三)

模拟实现一个基于文本界面的 开发团队调度软件
熟悉 Java 面向对象的高级特性,进一步掌握编程技巧和调试技巧
主要涉及以下知识点:
Ø 类的继承性和多态性
Ø 对象的值传递、接口
Ø static final 修饰符
Ø 特殊类的使用:包装类 、抽象 类、内部类
Ø 异常处理
该软件实现以下功能:
Ø 软件启动时,根据给定的数据创建公司部分成员列表(数组)
Ø 根据菜单提示,基于现有的 公司成员 ,组建一个 开发团队 以开发一个新的项目
Ø 组建过程包括将成员插入到团队中,或从团队中删除某成员,还可以列出团队中现有成员的列表
Ø 开发团队成员包括架构师、设计师和程序员

 需求说明

  本软件采用单级菜单方式工作。当软件运行时,主界面显示公司成员的列表,如下:

-------------------------------------开发团队调度软件--------------------------------------

ID     姓名      年龄    工资         职位      状态      奖金        股票     领用设备

 1      马 云      22      3000.0

 2      马化腾   32       18000.0  架构师   FREE    15000.0   2000    联想T4(6000.0)

 3      李彦宏   23       7000.0    程序员   FREE                                戴尔(NEC17)

 4      刘强东   24       7300.0    程序员   FREE                                戴尔(三星 17)

 5      雷军      28       10000.0  设计师   FREE    5000.0                 佳能 2900(激光)

 ……

----------------------------------------------------------------------------------------------

1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)_

当选择“添加团队成员”菜单时,将执行从列表中添加指定(通过ID)成员到开发团队的功能:

1-团队列表  2-添加团队成员  3-删除团队成员  4-退出   请选择(1-4)2

---------------------添加成员---------------------

请输入要添加的员工ID2

添加成功

按回车键继续...

添加成功后,按回车键将重新显示主界面。

开发团队人员组成要求:

最多一名架构师
最多两名设计师
最多三名程序员

如果添加操作因某种原因失败,将显示类似以下信息(失败原因视具体原因而不同):

1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)2

---------------------添加成员---------------------

请输入要添加的员工ID2

添加失败,原因:该员工已是某团队成员

按回车键继续...

  失败信息包含以下几种:

成员已满,无法添加
该成员不是开发人员,无法添加
该员工已在本开发团队中
该员工已是某团队成员
该员正在休假,无法添加
团队中至多只能有一名架构师
团队中至多只能有两名设计师
团队中至多只能有三名程序员

当选择“删除团队成员”菜单时,将执行从开发团队中删除指定(通过TeamID)成员的功能:

1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)3

---------------------删除成员---------------------

请输入要删除员工的TID1

确认是否删除(Y/N)y

删除成功

按回车键继续...

  删除成功后,按回车键将重新显示主界面。

提供的文件

数据文件

package com.team.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"}
    };
    
    //如下的EQIPMENTS数组与上面的EMPLOYEES数组元素一一对应
    //PC      :21, model, display
    //NoteBook:22, model, price
    //Printer :23, type, name
    public static final String[][] EQIPMENTS = {
        {},
        {"22", "联想T4", "6000"},
        {"21", "戴尔", "NEC17寸"},
        {"21", "戴尔", "三星 17寸"},
        {"23", "激光", "佳能 2900"},
        {"21", "华硕", "三星 17寸"},
        {"21", "华硕", "三星 17寸"},
        {"23", "针式", "爱普生20K"},
        {"22", "惠普m6", "5800"},
        {"21", "戴尔", "NEC 17寸"},
        {"21", "华硕","三星 17寸"},
        {"22", "惠普m6", "5800"}
    };
}

工具类:

package com.team.view;

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

domain包下的代码:

接口:

package com.team.domain;

public interface Equipment {
	String getDescription();
}

三个实现接口的设备类:

package com.team.domain;

public class PC implements Equipment{

	private String model;	//机器型号
	private String display;	//显示器名称
	
	
	public PC() {
		super();
	}


	public PC(String model, String display) {
		super();
		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 + ")";
	}
	
}
package com.team.domain;

public class NoteBook implements Equipment {
	private String model;
	private double price;

	public NoteBook() {
		super();
	}

	public NoteBook(String model, double price) {
		super();
		this.model = model;
		this.price = 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;
	}

	@Override
	public String getDescription() {
		return model + "(" + price + ")";
	}

}
package com.team.domain;

public class Printer implements Equipment {
	private String name; // 机器型号
	private String type; // 机器类型

	public Printer() {
		super();
	}

	public Printer(String name, String type) {
		super();
		this.name = name;
		this.type = type;
	}

	public String getName() {
		return name;
	}

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

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@Override
	public String getDescription() {
		return name + "(" + type + ")";
	}

}

四个员工类:

package com.team.domain;

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

	public Employee() {
		super();
	}

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

	
}
package com.team.domain;

import com.team.service.Status;

public class Programmer extends Employee {

	private int memberId;
	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;
	}

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

	public String getTeamBaseDetails() {
		return memberId + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary();
	}

	public String getDetailForTeam() {
		return getTeamBaseDetails() + "\t程序员";
	}
}
package com.team.domain;


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 double getBonus() {
		return bonus;
	}

	public void setBonus(double bonus) {
		this.bonus = bonus;
	}

	@Override
	public String toString() {
		return super.getDetail() + "\t设计师\t" + getStatus() + "\t" + bonus + "\t\t" + getEquipment().getDescription();
	}
	@Override
	public String getDetailForTeam() {
		return  getTeamBaseDetails() + "\t设计师" + "\t" + bonus;
	}
}
package com.team.domain;

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 int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}

	@Override
	public String toString() {
		return super.getDetail() + "\t架构师\t" + getStatus() + "\t" + getBonus() + "\t" + stock + "\t"
				+ getEquipment().getDescription();
	}

	@Override
	public String getDetailForTeam() {
		return getTeamBaseDetails() + "\t架构师\t" + getBonus() + "\t" + stock;
	}
}

services包下:

状态类:

package com.team.service;

/**
 * 
 * @Description 表示员工的状态
 * @author Alkaid Email:123456@qq.com
 * @version
 * @date 2022年8月8日下午4:39:35
 *
 */
public class Status {
	private final String NAME;

	private Status(String name) {
		this.NAME = name;
	}

	public static final Status FREE = new Status("FREE");
	public static final Status BUSY = new Status("BUSY");
	public static final Status VOCATION = new Status("VOCATION");

	public String getNAME() {
		return NAME;
	}

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

自定义异常类:

package com.team.service;

public class TeamException extends Exception {
	static final long serialVersionUID = -3387516993124229948L;

	public TeamException() {

	}

	public TeamException(String msg) {
		super(msg);
	}
}

数据封装类:

package com.team.service;

import java.util.Iterator;

import com.team.domain.Architect;
import com.team.domain.Designer;
import com.team.domain.Employee;
import com.team.domain.Equipment;
import com.team.domain.NoteBook;
import com.team.domain.PC;
import com.team.domain.Printer;
import com.team.domain.Programmer;

/**
 * 
 * @Description 负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
 * @author Alkaid Email:123456@qq.com
 * @version v1.0
 * @date 2022年8月8日下午5:16:44
 *
 */
public class NameListService {
	private Employee[] employees;

	public NameListService() {
//		根据项目提供的Data类构建相应大小的employees数组
//		再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Equipment子类的对象
//		将对象存于数组中
		employees = new Employee[Data.EMPLOYEES.length];

		for (int i = 0; i < Data.EMPLOYEES.length; i++) {
			// 获取员工的类型
			int type = Integer.parseInt(Data.EMPLOYEES[i][0]);

			// 获取EMPLOYEE的4个基本信息
			int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
			String name = Data.EMPLOYEES[i][2];
			int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
			double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
			Equipment equipment;
			double bonus;

			switch (type) {
			case Data.EMPLOYEE:
				employees[i] = new Employee(id, name, age, salary);
				break;
			case Data.PROGRAMMER:
				equipment = createEquipment(i);
				employees[i] = new Programmer(id, name, age, salary, equipment);
				break;
			case Data.DESIGNER:
				equipment = createEquipment(i);
				bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
				employees[i] = new Designer(id, name, age, salary, equipment, bonus);
				break;
			case Data.ARCHITECT:
				equipment = createEquipment(i);
				bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
				int stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
				employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
				break;
			}
		}
	}

	/**
	 * 
	 * @Description 指定index上的员工的设备
	 * @author Alkaid
	 * @date 2022年8月8日下午7:25:42
	 * @param i
	 * @return
	 */
	private Equipment createEquipment(int index) {
		int key = Integer.parseInt(Data.EQIPMENTS[index][0]);
		String modelOrname = Data.EQIPMENTS[index][1];

		switch (key) {
		case Data.PC:
			String display = Data.EQIPMENTS[index][2];
			return new PC(modelOrname, display);
		case Data.NOTEBOOK:
			double price = Double.parseDouble(Data.EQIPMENTS[index][2]);
			return new NoteBook(modelOrname, price);
		case Data.PRINTER:
			String type = Data.EQIPMENTS[index][2];
			return new Printer(modelOrname, type);
		}
		return null;
	}
	/**
	 * 
	* @Description	获取当前所有员工
	* @author Alkaid
	* @date 2022年8月8日下午8:39:19
	* @return	包含所有员工对象的数组
	 */
	public Employee[] getAllEmployees() {
		return employees;
	}

	/**
	 * 
	* @Description	获取指定ID的员工对象。
	* @author Alkaid
	* @date 2022年8月8日下午8:40:01
	* @param id	指定员工的ID
	* @return	指定员工对象
	 */
	public Employee getEmployee(int id) throws TeamException{
		for (int i = 0; i < employees.length; i++) {
			if(employees[i].getId() == id) {
				return employees[i];
			}
		}
		throw new TeamException("找不到指定的员工");
	}
}

成员管理:

package com.team.service;

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

/**
 * 
 * @Description 关于开发团队成员的管理:添加、删除等。
 * @author Alkaid Email:123456@qq.com
 * @version
 * @date 2022年8月8日下午9:59:16
 *
 */
public class TeamService {
	private static int counter = 1; // 给memberID赋值
	private int MAX_MEMBER = 5; // 限制开发团队的人数
	private Programmer[] team = new Programmer[MAX_MEMBER]; // 保存开发团队成员
	private int total; // 记录开发团队中现有的人数

	public TeamService() {
		super();
	}

	/**
	 * 
	 * @Description 获取开发团队中的所有成员
	 * @author Alkaid
	 * @date 2022年8月8日下午11:01:31
	 * @return
	 */
	public Programmer[] getTeam() {
		Programmer[] team = new Programmer[total];
		for (int i = 0; i < team.length; i++) {
			team[i] = this.team[i];
		}
		return team;
	}

	public void addMember(Employee e) throws TeamException {
		// 成员已满,无法添加
		if (total >= MAX_MEMBER) {
			throw new TeamException("成员已满,无法添加");
		}
		// 该成员不是开发人员,无法添加
		if (!(e instanceof Programmer)) {
			throw new TeamException("该成员不是开发人员,无法添加");
		}
		// 该员工已在本开发团队中
		if (isExist(e)) {
			throw new TeamException("该员工已在本开发团队中");
		}
		;
		// 该员工已是某团队成员
		// 该员正在休假,无法添加
		Programmer p = (Programmer) e;
		if ("BUSY".equals(p.getStatus().getNAME())) {
			throw new TeamException("该员工已是某团队成员");
		} else if ("VOCATION".equals(p.getStatus().getNAME())) {
			throw new TeamException("该员正在休假,无法添加");
		}
		
		// 团队中至多只能有一名架构师
		// 团队中至多只能有两名设计师
		// 团队中至多只能有三名程序员
		int numOfArch = 0, numOfDes = 0, numOfPro = 0;
		for (int i = 0; i < total; i++) {
			if (team[i] instanceof Architect) {
				numOfArch++;
			} else if (team[i] instanceof Designer) {
				numOfDes++;
			} else {
				numOfPro++;
			}
		}

		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添加到team中
		team[total++] = p;
		// 修改p的状态
		p.setStatus(Status.BUSY);
		p.setMemberId(counter++);
	}

	/**
	 * 
	 * @Description 判断指定的员工是否存在于开发团队中
	 * @author Alkaid
	 * @date 2022年8月8日下午11:17:24
	 * @param e
	 * @return
	 */
	private boolean isExist(Employee e) {
		for (int i = 0; i < total; i++) {
			if (team[i].getId() == e.getId()) {
				return true;
			}
		}
		return false;
	}

	public void removeMember(int memberId) throws TeamException {
		int i = 0;
		for (; i < total; i++) {
			if (team[i].getMemberId() == memberId) {
				team[i].setStatus(Status.FREE);
				break;
			}
		}
		// 未找到指定成员
		if (i == total) {
			throw new TeamException("找不到指定memberId的员工,删除失败");
		}
		// 后一个元素覆盖前一个元素
		for (int j = i + 1; j < total + 1; j++) {
			team[j] = team[j + 1];
		}

		team[--total] = null;

	}
}

view包:

package com.team.view;

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

public class TeamView {
	private NameListService listSvc = new NameListService();
	private TeamService teamSvc = new TeamService();

	public void enterMainMenu() {
		boolean loopFlag = true;
		char menu = 0;
		while (loopFlag) {
			if (menu != '1') {
				listAllEmployees();
			}

			System.out.print("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;
				}
			}
		}
	}

	/**
	 * 
	 * @Description 显示所有的员工的信息
	 * @author Alkaid
	 * @date 2022年8月9日上午12:56:09
	 */
	private void listAllEmployees() {
		System.out.println("-------------------------------开发团队调度软件--------------------------------");
		Employee[] employees = listSvc.getAllEmployees();

		if (employees == null || employees.length == 0) {
			System.out.println("公司没有任何员工信息");
		} else {
			System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
			for (int i = 0; i < employees.length; i++) {
				System.out.println(employees[i]);
			}
			System.out.println("-------------------------------------------------------------------------------");
		}

	}

	private void getTeam() {
		System.out.println("\n--------------------团队成员列表---------------------\n");
		Programmer[] team = teamSvc.getTeam();
		if (team == null || team.length == 0) {
			System.out.println("开发团队目前没有成员!");
		} else {
			System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
			for (int i = 0; i < team.length; i++) {
				System.out.println(team[i].getDetailForTeam());
			}
		}
		System.out.println("-----------------------------------------------------");
	}

	private void addMember() {
		System.out.println("\n---------------------添加成员---------------------");
		System.out.print("请输入要添加的员工ID:");
		int id = TSUtility.readInt();
		try {
			Employee employee = listSvc.getEmployee(id);
			teamSvc.addMember(employee);
			System.out.println("添加成功!");
			TSUtility.readReturn();
		} catch (TeamException e) {
			System.out.println("添加失败,原因:" + e.getMessage());
		}
	}

	private void deleteMember() {
		System.out.println("---------------------删除成员---------------------");
		System.out.print("请输入要删除员工的TID:");
		int memberId = TSUtility.readInt();
		System.out.print("确认是否删除(Y/N):");
		char isDelete = TSUtility.readConfirmSelection();
		if (isDelete == 'N') {
			return;
		} else {
			try {
				teamSvc.removeMember(memberId);
				System.out.println("删除成功");
				TSUtility.readReturn();
			} catch (TeamException e) {
				System.out.println("删除失败,原因:" + e.getMessage());
			}
		}
	}

	public static void main(String[] args) {
		TeamView test = new TeamView();
		test.enterMainMenu();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值