需求说明
需求说明2
需求说明3
需求说明4
需求说明6
第一步 - 创建项目基本组件
1
键盘访问的实现
TSUtility.java
TSUtility.java
package com.atguigu.team.domain
import java.util.*;
/**
*
* @Description 项目中提供了TSUtility.java类,可用来方便地实现键盘访问。
* @author shkstart Email:shkstart@126.com
* @version
* @date 2019年2月12日上午12:02:58
*
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
*
* @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
* @author shkstart
* @date 2019年2月12日上午12:03:30
* @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;
}
/**
*
* @Description 该方法提示并等待,直到用户按回车键后返回。
* @author shkstart
* @date 2019年2月12日上午12:03:50
*/
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
/**
*
* @Description 该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:04
* @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;
}
/**
*
* @Description 从键盘读取‘Y’或’N’,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:45
* @return
*/
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;
}
}
Equipment接口及其实现子类的
package com.atguigu.team.domain;
public interface Equipment {
String getDescription();
}
Notebook
package com.project03.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;
}
public String getDescription(){
return model + "(" + price + ")";
}
}
PC
package com.project03.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 + ")";
}
}
Printer
package com.project03.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 +")";
}
}
Employee
Employee
Employee
package com.project03.domain;
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(){
}
public Employee(int id, String name, int age, double salary) {
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 getInfo(){
return id + "\t" + name + "\t"+ age + "\t"+ salary ;
}
@Override
public String toString(){
return getInfo();
}
}
Programmer
package com.project03.domain;
import com.project03.service.Status;
public class Programmer extends Employee {
private int memberId;
private Status status = Status.FREE;
private Equipment equipment = null;
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 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 getInfo() + "\t\t" + "Programmer" + "\t" + getStatus().getName() + "\t\t" + "\t\t\t\t\t\t\t" + getEquipment().getDescription();
}
public String getTeamDetails(){
return memberId + "/" + getId() + "\t\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t程序员";
}
}
Designer
package com.project03.domain;
import com.project03.service.Status;
public class Designer extends Programmer {
private double 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 getInfo() + "\t\t" + "Designer" + "\t" + getStatus().getName() + "\t\t" + bonus + "\t\t\t\t\t\t" + getEquipment().getDescription();
}
public String getTeamDetails(){
return getMemberId() + "/" + getId() + "\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t设计师\t" +bonus;
}
}
Architect
package com.project03.domain;
import com.project03.service.Status;
public class Architect extends Designer{
private int 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 getInfo() + "\t\t" + "架构师" + "\t" + getStatus().getName()+ "\t\t" + getBonus() + "\t\t\t" + stock + "\t\t" +getEquipment().getDescription();
}
public String getTeamDetails(){
return getMemberId() + "/" + getId() + "\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t架构师\t" +getBonus() + "\t\t" + getStock();
}
}
Status类
Status
package com.atguigu.team.service;
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 VOCATION = new Status("VOCATION");
public static final Status BUSY = new Status("BUSY");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
Data类
储存team中的人员包括设备的信息
package com.atguigu.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
/**
* {"10", "1", "马云", "22", "3000"},
* {"13", "2", "马化腾", "32", "18000", "15000", "2000"},
*/
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"}
};
}
NameListService
package com.project03.service;
import com.project03.domain.Architect;
import com.project03.domain.Designer;
import com.project03.domain.Employee;
import com.project03.domain.Equipment;
import com.project03.domain.Notebook;
import com.project03.domain.Programmer;
import com.project03.domain.PC;
import com.project03.domain.Printer;
/*
* @Description 负责将Data中的数据封装到employee[]数组中,同时提供操作employee[]数组的相关方法,比如返回所有员工列表,返回单个员工
* @author zc
* @version
* @date 创建时间:2020年10月27日 下午2:36:41
*/
public class NameListService {
private Employee[] employees;
public NameListService(){
/*
* @Description :根据data的数据将数据填入到employees的数组中
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
employees = new Employee[Data.EMPLOYEES.length];
for(int i = 0; i < employees.length;i++){
//将Data中的信息解析成相对应的格式
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
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]);
int memberId = -1; //表示暂不在团队中
double bonus; //动态绑定,先声明不调用,等调用的时候会判定调用哪个方法
int stock;
Equipment equipment = null; //暂无设备
Status status = Status.FREE;
switch(type){
case Data.EMPLOYEE:
employees[i] = new Employee(id, name, age, salary); //employee不需要设备
break;
case Data.PROGRAMMER:
equipment = grabEquipment(i);
employees[i] = new Programmer(id, name, age, salary, memberId, status, equipment);
break;
case Data.DESIGNER:
equipment = grabEquipment(i);
bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
employees[i] = new Designer(id, name, age, salary, memberId, status, equipment, bonus);
break;
case Data.ARCHITECT:
equipment = grabEquipment(i);
bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
employees[i] = new Architect(id, name, age, salary, memberId, status, equipment, bonus, stock);
break;
}
}
}
/*
* @Description :获取员工的设备
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
public Equipment grabEquipment(int index){
int modelType = Integer.parseInt(Data.EQUIPMENTS[index][0]);
String model = null, display = null,type = null,name = null;
double price = 0;
Equipment equipment = null;
switch(modelType){
case Data.PC:
model = Data.EQUIPMENTS[index][1];
display = Data.EQUIPMENTS[index][2];
equipment = new PC(model,display);
break;
case Data.NOTEBOOK:
model = Data.EQUIPMENTS[index][1];
price = Double.parseDouble(Data.EQUIPMENTS[index][2]);
equipment = new Notebook(model,price);
break;
case Data.PRINTER:
name = Data.EQUIPMENTS[index][1];
type = Data.EQUIPMENTS[index][2];
equipment = new Printer(name,type);
break;
}
return equipment;
}
/*
* @Description :新建一个数组返回所有的员工
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
public Employee[] getAllEmployees(){
Employee[] employeeList = new Employee[employees.length];
for(int i = 0; i< employees.length;i++){
employeeList[i] = this.employees[i];
}
return employeeList;
}
/*
* @Description :根据员工id返回员工信息
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
public Employee getEmployee(int id) throws TeamException{
for(int i = 0; i< employees.length; i++){
if(id == employees[i].getId()){
return employees[i];
}
}throw new TeamException("Cant find the employee!");
}
}
TeamService
package com.project03.service;
import com.project03.domain.Architect;
import com.project03.domain.Designer;
import com.project03.domain.Employee;
import com.project03.domain.Programmer;
/*
* @Description :对新创建的开发团队进行一系列的操作,如增删
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
public class TeamService{
private static int counter = 1; //代表保存在team中的人的序号
private final int max_member = 5;
private Programmer[] team = new Programmer[max_member];
private int total = 0;
//获取team中架构师、设计师、程序员的人数
private int numArc = 0;
private int numDes = 0;
private int numPro = 0;
//返回team
public Programmer[] getTeam(){
Programmer[] team = new Programmer[total];
for(int i = 0; i<team.length;i++){
team[i] = this.team[i];
}
return team;
}
/*
*
* @description 将员工添加到team中
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 18:24
*/
public void addMember(Employee employee) throws TeamException{
//请考虑以下几种失败情况
if(total > max_member){
throw new TeamException("No available headcount");
}
if(!(employee instanceof Programmer)){
throw new TeamException("Not a Programmer"); //过滤掉了非programmer的employee,比如马云
}
//此处对programmer的子类进行向上转型,目的是为了使用programmer特有的setStatus
Programmer programmer = (Programmer)employee;
if(programmer.getStatus() == Status.BUSY){
throw new TeamException("The employee is being busy with something");
}
if(programmer.getStatus() == Status.VACATION){
throw new TeamException("The employee is being in a vocation");
}
if(isExist(employee)){
throw new TeamException("Already in the team");
}
if(employee instanceof Architect){
if(numArc >=1 ){
throw new TeamException("Only 1 Architecture is allowed");
}
numArc++;
}else if(employee instanceof Designer){
if(numDes >= 2){
throw new TeamException("Only 2 Designers are needed");
}
numDes++;
}else{
if(numPro>=3){
throw new TeamException("Only 3 programmers are needed");
}
numPro++;
}
team[total] = programmer;
total++;
programmer.setStatus(Status.BUSY);
programmer.setMemberId(counter++);
}
/*
*
* @description 将员工从team中删除
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 18:24
*/
public void removeMember(int memberId) throws TeamException{
for(int i = 0; i < total; i++){
if(team[i].getMemberId() == memberId){
//修改状态
team[i].setStatus(Status.FREE);
Programmer programmer = team[i];
if(programmer instanceof Architect){
numArc--;
}else if(programmer instanceof Designer){
numDes--;
}else{
numPro--;
}
for(int j = i; j < total-1; j++){
team[j] = team[j+1];
}
team[--total] = null;
return;
}
}
//没有找到memberId的时候
throw new TeamException("Cant find the team member");
}
public boolean isExist(Employee employee){
for(int i = 0; i < total; i ++){
if(team[i].getId() == employee.getId()){
return true;
}
}
return false;
}
}
TeamException
package com.project03.service;
public class TeamException extends Exception{
public final long serialVersionUID = -3387516229948L;
public TeamException(){
super();
}
public TeamException(String msg){
super(msg);
}
}
TeamView
package com.project03.view;
import com.project03.domain.Employee;
import com.project03.domain.Programmer;
import com.project03.service.NameListService;
import com.project03.service.TeamException;
import com.project03.service.TeamService;
public class TeamView {
//实例化类,方便调用其中的方法
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
//创建一个激活主界面的方法
public void enterMainMenu(){
boolean isFlag = true;
TeamView teamview = new TeamView(); //内部实例化一个构造器
char menu = 0;
do{
if(menu != '1'){
teamview.listAllEmployees(); //当输入不是1的数字,一直返回完整的员工列表
}
menu = TSUtility.readMenuSelection();
switch(menu){
case '1':
teamview.getTeam();
break;
case '2':
teamview.addMember();
break;
case '3':
teamview.deleteMember();
break;
case '4':
System.out.println("是否确定退出(Y/N)");
char key = TSUtility.readConfirmSelection();
if(key == 'Y'){
isFlag = false;
System.out.println("退出成功");
}
break;
default:
}
}while(isFlag);
}
//设计列出员工列表的形式
private void listAllEmployees(){
System.out.println("------------------------------------开发团队调度软件---------------------------------------");
System.out.println("ID\t姓名\t\t年龄\t工资\t\t\t职位\t\t状态\t\t\t奖金\t\t\t\t股票\t\t\t领用设备");
//列出所有员工的信息
for(int i = 0; i < listSvc.getAllEmployees().length;i++){
System.out.println(listSvc.getAllEmployees()[i]);
}
System.out.println("-----------------------------------------------------------------------------------------");
System.out.println("1- 团队成员 2- 添加团队成员 3-删除团队成员 4-退出 请选择( 1 - 4 ) ");
}
//显示新的开发团队
private void getTeam(){
System.out.println("------------------------------------团队列表----------------------------------------------");
Programmer[] team = teamSvc.getTeam();
if(team == null || team.length == 0){
System.out.println("None in new Programming team");
}else{
System.out.println("TID/ID\t\t\t姓名\t\t年龄\t\t工资\t\t\t职位\t\t奖金\t\t\t股票");
for(int i = 0; i<team.length;i++){
System.out.println(team[i].getTeamDetails());
}
System.out.println("-----------------------------------------------------------------------------------------");
}
}
//实现添加成员的操作
private void addMember(){
System.out.println("------------------------------------添加员工----------------------------------------------");
System.out.println("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try{
Employee employee = listSvc.getEmployee(id);
teamSvc.addMember(employee);
System.out.println("Successfully added");
}catch(TeamException e){
System.out.println("Failed to add a new member" + e.getMessage() + "\n");
}
System.out.println("-----------------------------------------------------------------------------------------");
//读取回车,按回车键继续
TSUtility.readReturn();
}
private void deleteMember(){
System.out.println("------------------------------------删除成员----------------------------------------------");
System.out.println("请输入要删除员工的TID:");
int memberId = TSUtility.readInt();
System.out.println("请确认是否要删除(Y/N)");
char key = TSUtility.readConfirmSelection();
if(key == 'N'){
return;
}
try{
teamSvc.removeMember(memberId);
System.out.println("Successfully deleted");
}catch(TeamException e){
System.out.println("Failed to delete " + e.getMessage() + "\n");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Failed to delete "+ e.getMessage() + "\n" );
}
TSUtility.readReturn();
System.out.println("-----------------------------------------------------------------------------------------");
}
public static void main(String[] args) {
TeamView teamview = new TeamView();
teamview.enterMainMenu();
}
}
整个项目完整的敲下来还是要费不少力气的,尤其是debug阶段,因为强迫症要求对每个细节、格式进行一次次的调试校正,还是重新学到了不少东西。 然后最后阶段在deleteMember这个方法的调试上几乎花了3h的时间,问题出在无法删除列表的第一个成员,最后在teamService里面找了这个bug。 有时间还是要再敲一遍。如果有人发现新的bug欢迎指出,上面的版本不是最终版,因为挨个粘贴实在是太麻烦了,有时间再上传到github上面吧。