Java面向对象项目三:开发团队调度软件

目录

一、工具类及数据类提供

TSUtility工具类

Data数据类

二、Equipment接口及实现子类的设计

 Equipment接口

NoteBook类

PC类

Printer类

三、Employee类及其子类的设计

 Employee类

Programmer类

Designer类

Architect类

四、NameListService属性和构造器的实现

 NameListService类

TeamException类

测试NameListService类的getAllEmployees()

测试NameListService类的getEmployees()

五、TeamService类的设计

六、TeamView类的设计

模拟实现一个基于文本界面的《开发团队调度软件》

该软件主要实现以下功能:

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

一、工具类及数据类提供

TSUtility工具类

package com.light.team.view;
/**
 * @Description 项目提供了Utility.java类,可以用来方便实现键盘访问
 * @author light
 *
 */
import java.util.*;
public class TSUtility {
	private static Scanner scanner=new Scanner(System.in);
	/**
	 * @Description 该方法读取键盘,如果用户输入‘1’--‘4’中的任意字符,则方法返回。
	 * @return 返回值为用户输入的数字
	 */
	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.println("按回车键继续...");
		readKeyBoard(100,true);
	}
	/**
	 * 该方法从键盘中读取一个长度不超过2的整数
	 * @return 返回读取到的整数值
	 */
	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;
	}
	/**
	 * 从键盘中读取用户选择‘y’或‘N',
	 * @return 将结果返回
	 */
	public static char redConfirmSelection() {
		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;
		
	}
}

Data数据类

package com.light.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
	//Architer: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"}
	    };
	    
	    //如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
	    //PC      :21, model, display
	    //NoteBook:22, model, price
	    //Printer :23, name, type 
	    public static final String[][] EQUIPMENTS = {
	        {},
	        {"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"}
	    };
}

二、Equipment接口及实现子类的设计

 Equipment接口

package com.light.team.domain;

public interface Euquipment {
	public String getDescription();
}

NoteBook类

package com.light.team.domain;

public class NoteBook implements Equipment{

	private String model;//显示器型号
	private double price;//显示器价格
	
	/**
	 * 构造器
	 */
	public NoteBook() {
		super();
	}

	/**
	 * @param model 显示器型号
	 * @param price 显示器价格
	 */
	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+")";
	}

}

PC类

package com.light.team.domain;

public class PC implements Equipment{

	private String model;//显示器型号
	private String display;//显示器名称
	
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getDeiplay() {
		return display;
	}
	public void setDeiplay(String display) {
		this.display = display;
	}
	/**
	 * @param model  显示器型号
	 * @param deiplay 显示器名称
	 */
	public PC(String model, String display) {
		super();
		this.model = model;
		this.display = display;
	}
	/**
	 * 无参构造器
	 */
	public PC() {
		super();
	}
	
	@Override
	public String getDescription() {
		
		return model+"("+display+")";
	}
	
}

Printer类

package com.light.team.domain;

public class Printer implements Equipment {

	private String name;//显示器型号
	private String type;//显示器类型
	
	/**
	 * 无参构造器
	 */
	public Printer() {
		super();
		
	}
	/**
	 * @param name 显示器型号
	 * @param type 显示器类型
	 */
	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;
	}
    /**
	 * 实现类实现接口的方法,返回各自属性的信息
	 */
	public String getDescription() {
		
		return name+"("+type+")";
	}
}

三、Employee类及其子类的设计

 Employee类

package com.light.team.domain;

public class Employee {
	private int id;
	private String name;
	private int age;
	private double 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;
	}
	/**
	 * @param id 团队成员id
	 * @param name 团队成员姓名
	 * @param age 团队成员年龄
	 * @param 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 String getDetails() {
		return id+"\t"+name+"\t"+age+"\t"+salary;
	}
	
	@Override
	public String toString() {
		return getDetails();
	}
	
}

Programmer类

package com.light.team.domain;

import com.light.team.service.Status;

public class Programmer extends Employee {
	private int memberId;//用来记录成员加入开发团队后在团队中的ID
	//Status 是service包下自定义的类,
	//声明三个属性,分别表示三种状态:
	//FREE-空闲
	//BUSY-已加入开发团队
	//VOCATION-正在休假
	private Status status;
	
	private 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 Programmer() {
		super();
	}
	/**
	 * @param id
	 * @param name
	 * @param age
	 * @param salary
	 */
	public Programmer(int id, String name, int age, double salary,Equipment equipment)            {
		super(id, name, age, salary);
		this.equipment=equipment;
	}
  @Override
	public String toString() {
		
		return super.getDetails()+"\t程序员\t"+status+"\t\t\t"+equipment.getDescription();
	}
	
	public String getDetail() {
		return memberId+"/"+getId()+"\t"+getName()+"\t"+getAge()+"\t"+getSalary();
	}
	
	public String getDetailsFromTeam() {
		return getDetail()+"\t程序员";
	}
	
	
	
}

Designer类

package com.light.team.domain;

public class Designer extends Programmer{
	private double bonus;//奖金
	

	public double getBonus() {
		return bonus;
	}

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

	public Designer() {
		super();
	}

	/**
	 * @param id
	 * @param name
	 * @param age
	 * @param salary
	 * @param equipment
	 */
	public Designer(int id, String name, int age, double salary,Equipment equipment,double bonus) {
		super(id, name, age, salary,equipment);
		this.bonus=bonus;
	}
	@Override
	public String toString() {
		
		return getDetails()+"\t设计师\t"+getStatus()+"\t"+bonus+"\t\t"+getEquipment().getDescription();
	}
	
	
	public String getDetailsFromTeam() {
		return getDetail()+"\t设计师"+"\t"+getBonus();
	}
	
}

Architect类

package com.light.team.domain;

public class Architect extends Designer {
	private int stock;//股票
	
	public int getStock() {
		return stock;
	}

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

	/**
	 * @param id
	 * @param name
	 * @param age
	 * @param salary
	 * @param equipment
	 * @param bonus
	 */
	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;
	}
	@Override
	public String toString() {
		
		return getDetails()+"\t架构师\t"+getStatus()+"\t"+getBonus()+"\t"+stock+"\t"+getEquipment().getDescription();
	}
	
	public String getDetailsFromTeam() {
		return getDetail()+"\t架构师"+"\t"+getBonus()+"\t"+getStock();
	}
}

四、NameListService属性和构造器的实现

 NameListService类

package com.light.team.service;

/**
 * @Description 负责将data中的数据封装到Employee[]数组中,
 * 同时提供相关操作Employee[]的方法
 * @author light
 *
 */
import static com.light.team.service.Data.*;

import com.light.team.domain.*;

public class NameListService {
	private Employee[] employees;

	/**
	 * 对employees数组及数组元素进行初始化
	 */
	public NameListService() {
		// 根据项目提供给的data类构建相应大小的employees数组
		// 再根据data类的数据构造不同的对象,包括Employee、Programmer、
		// Designer和Architect
		// 将对象存进数组中
		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]);
			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;
			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);
				bonus = Double.parseDouble(EMPLOYEES[i][5]);
				stock = Integer.parseInt(EMPLOYEES[i][6]);
				employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
				break;
			}
		}

	}

	/**
	 * 获取指定index位置上的员工的设备
	 * 
	 * @param index
	 * @return
	 */
	private Equipment createEquipment(int index) {
		int e = Integer.parseInt(EQUIPMENTS[index][0]);
		String model = EQUIPMENTS[index][1];
		switch (e) {
		case PC:// 21
			String display = EQUIPMENTS[index][2];
			return new PC(model, display);
		case NOTEBOOK:// 22
			double price = Double.parseDouble(EQUIPMENTS[index][2]);
			return new NoteBook(model, price);
		case PRINTER:// 23
			String type = EQUIPMENTS[index][2];
			return new Printer(model, type);
		}
		return null;

	}
	/**
	 * 获取当前所有员工
	 * @return 
	 */
	public Employee[] getALLEmpolyees() {
		return employees;
	}
	/**
	 * 
	 * @param id
	 * @return 获取指定id的员工 
	 * @throws TeamException 
	 * @throws Exception
	 */
	public Employee getEmpolyees(int id) throws TeamException{
		for(int i=0;i<employees.length;i++) {
			if(employees[i].getId()==id) {
				return employees[i];
			}
		}
		   throw new TeamException("找不到指定员工");
	}

}

TeamException类

package com.light.team.service;
/**
 * 自定义异常类
 * @author light
 *
 */
public class TeamException extends Exception{
	
	static final long serialVersionUID = -3387516993124888948L;

	public TeamException() {
		super();
	}
	/**
	 * @param message
	 */
	public TeamException(String message) {
		super(message);
	}
}

测试NameListService类的getAllEmployees()

package com.light.team.junit;

import org.junit.jupiter.api.Test;

import com.light.team.domain.Employee;
import com.light.team.service.NameListService;
/**
 * 测试NameListService类
 * @author light
 */
public class NameListServiceTest {
	@Test
	public void testGetAllEmployees() {
		NameListService service=new NameListService();
		Employee[] employees=service.getAllEmployees();
		for(int i=0;i<employees.length;i++) {
			System.out.println(employees[i]);
		}
	}
}

测试结果如下:

测试NameListService类的getEmployees()

package com.light.team.junit;
import org.junit.jupiter.api.Test;
import com.light.team.domain.Employee;
import com.light.team.service.NameListService;
/**
 * 测试NameListService类
 * @author light
 */
public class NameListServiceTest {
    @Test
	public void testGetEmployee() throws TeamException {
		NameListService service=new NameListService();
		int i=1;
		System.out.println(service.getEmployee(i));   
	}
}

测试结果如下:

五、TeamService类的设计

package com.light.team.service;

import com.light.team.domain.*;
/**
 * 开发团队成员管理:添加、删除等
 * @author light
 *
 */
public class TeamService {
	private static  int counter=1;//给memberId赋值使用
	private static final int MAX_MEMBER=5;//限制开发团队人数
	private Programmer[] team=new Programmer[MAX_MEMBER];//保存开发团队成员
	private int total=0;//记录开发团队中实际人数
	public TeamService() {
		super();
	}
	/**
	 * 
	 * @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(isExcit(e)) {
			throw new TeamException("该员工已在本开发团队中");
		}
		//该员工已是某团队成员
		Programmer p=(Programmer)e;
		if("BUSY".equals(p.getStatus().getNAME())) {
			throw new TeamException("该员工已是某团队成员");
			
		}
		//该员工正在休假,无法添加
		if("VOCATION".equals(p.getStatus().getNAME())) {
			throw new TeamException("该员工正在休假,无法添加");
		}
		//获取团队架构师、设计师、程序员的个数
		int numOfArc=0,numOfDes=0,numOfPro=0;
		for(int i=0;i<total;i++) {
			if(team[i] instanceof Architect) {
				numOfArc++;
			}else if(team[i] instanceof Designer) {
				numOfDes++;
			}else if(team[i] instanceof Programmer){
				numOfPro++;
			}
		}
		//团队中最多只能有一名架构师
		//团队中最多只能有两名设计师
		//团队中最多只能有三名程序员
		if(p instanceof Architect) {
			if(numOfArc>=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("团队中最多只能有三名程序员");
			}
		}
		//将成员添加到团队中
		team[total++]=p;
		//修改成员状态
		p.setStatus(Status.BUSY);
		p.setMemberId(counter++);
		
	}
	private boolean isExcit(Employee e) {
		for(int i=0;i<total;i++) {
			if(team[i].getId()==e.getId()) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 删除指定memberId的成员
	 * @param memberId
	 * @throws TeamException 
	 */
	public void removeMember(int memberId) throws TeamException {
		int i;
		//寻找指定memberId的成员
		for(i=0;i<total;i++) {
			if(team[i].getMemberId()==memberId) {
				team[i].setStatus(Status.FREE);
				break;
			}
		}
		
		if(i==total) {
			throw new TeamException("未找到指定memberId的成员,删除失败");
		}
		
		//找到指定memberId的成员,进行删除
		for(int j=i+1;j<total;j++) {
			team[j-1]=team[j];
		}
		team[--total]=null;
	}
}

六、TeamView类的设计

package com.light.team.view;
//开发团队调度软件
//从公司中抽调员工进行项目开发

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

public class TeamView {
	private NameListService listSvc=new NameListService();
	private TeamService teamSvc=new TeamService();
	public void enterMain() {
		boolean isFlag=true;
		char menu=0;
		while(isFlag) {
			if(menu!='1') {
				listAllEmplouees();
			}
			System.out.print("1-团队列表 2-添团队成员 3-删除团队成员 4-退出 请选择:");
			 menu = TSUtility.readMenuSelection();
			switch(menu) {
			case '1':
				getTeam();
				break;
			case '2':
				addMember();
				break;
			case '3':
				deleteMember();
				break;
			case '4':
				System.out.print("确认要退出吗(Y/N):");
				char isExit = TSUtility.redConfirmSelection();
				if(isExit=='Y') {
					isFlag=false;
					System.out.println("谢谢使用,再见!");
				}else {
					break;
				}
				
			}
		}
		
	}
	/**
	 * 显示员工主界面
	 */
	private void listAllEmplouees() {
		System.out.println("---------------------------显示公司所有员工信息---------------------\n");
		System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t   领用设备");
		Employee[] employees = listSvc.getALLEmpolyees();
		for(int i=0;i<employees.length;i++) {
			System.out.println(employees[i]);
		}
	}
	private void getTeam() {
		Programmer[] team = teamSvc.getTeam();
		
		System.out.println("--------------------------------团队成员列表--------------------------------");
		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].getDetailsFromTeam());
			}
		}
		System.out.println("-------------------------------------------------------------------");
	}
	
	private void addMember() {
		System.out.println("----------------------------------添加团队成员-------------------------------------");
		System.out.print("请输入要添加员工id:");
		int id = TSUtility.readInt();
		try {
			Employee emp = listSvc.getEmpolyees(id);
			teamSvc.addMember(emp);
			System.out.println("添加成功");
		} catch (TeamException e) {
			System.out.println("添加失败,原因:"+e.getMessage());
		}
		//按回车键继续...
		TSUtility.readReturn();
	}
	private void deleteMember() {
		System.out.println("----------------------------删除团队成员-----------------------------");
		System.out.print("请输入要删除员工的TID:");
		int memberId = TSUtility.readInt();
		System.out.print("是否要删除(Y/N):");
		char isDelete = TSUtility.redConfirmSelection();
		if(isDelete=='N') {
			return;
		}
		
		try {
			teamSvc.removeMember(memberId);
			System.out.println("删除成功");
		} catch (TeamException e) {
			System.out.println("删除失败,原因:"+e.getMessage());
		}
		TSUtility.readReturn();
		
	}
	public static void main(String[] args) {
		TeamView view=new TeamView();
		view.enterMain();
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值