文章目录
- 前言
- 一、项目需求
- 二、软件架构设计
- 三、实现过程
- 第一步:创建项目基本组件
- 1、TSUtility工具类
- 2、Equipment接口
- PC类
- NoteBook类
- Printer类
- 3、Employee类及其子类的设计
- status类(service包中)
- Employee类
- Programmer类
- Designer类
- Architect类(我这里用SystemArchitect表示了)
- 第二步:实现service包中的类
- 1、NameListService类的设计
- Data类(用于保存数据)
- NameListService类
- 自定义异常类TeamException
- 2、TeamService类的设计
- TeamService类
- 第三步:实现view包中类
- 1、TeamView类
- 2、测试类
- 四、测试结果
- 添加的员工非程序员
- 添加成功
- 员工已在团队中
- 显示团队成员
- 删除操作
- 五、总结
- 六、最后
前言
这个项目源自尚硅谷宋红康老师的Java基础课程,看上去很简单,但是也困扰了我小半天。
一、项目需求
该项目实现以下功能
二、软件架构设计
MVC三层架构
三、实现过程
第一步:创建项目基本组件
1、TSUtility工具类
package pers.victorgong.开发团队的人员调度软件.view;
import java.util.Scanner;
/**
* @Author: shkstart Email:shkstart@126.com
* @Description: 项目中提供了TSUtility.java类,可用来方便地实现键盘访问。
* @Date: Created in 12:02 2019/02/12
*/
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;
}
}
2、Equipment接口
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 设备类的接口
* @Date: Created in 15:01 2020/12/14
*/
public interface Equipment {
public String getDescription();
}
PC类
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 电脑
* @Date: Created in 15:07 2020/12/14
*/
public class PC implements Equipment{
private final String MODEL; //机器的型号
private final String DISPLAY; //显示器型号
public PC(String MODEL, String DISPLAY) {
this.MODEL = MODEL;
this.DISPLAY = DISPLAY;
}
public String getMODEL() {
return this.MODEL;
}
public String getDISPLAY() {
return this.DISPLAY;
}
@Override
public String getDescription() {
return "PC{" +
"MODEL='" + MODEL + '\'' +
", DISPLAY='" + DISPLAY + '\'' +
'}';
}
}
NoteBook类
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 15:12 2020/12/14
*/
public class NoteBook implements Equipment{
private final String MODEL;
private final int PRICE;
public NoteBook(String MODEL, int PRICE) {
this.MODEL = MODEL;
this.PRICE = PRICE;
}
public String getMODEL() {
return this.MODEL;
}
public int getPRICE() {
return this.PRICE;
}
@Override
public String getDescription() {
return "NoteBook{" +
"MODEL='" + MODEL + '\'' +
", PRICE=" + PRICE +
'}';
}
}
Printer类
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 打印机
* @Date: Created in 15:15 2020/12/14
*/
public class Printer implements Equipment{
private final String NAME;
private final String TYPE;
public Printer(String NAME, String TYPE) {
this.NAME = NAME;
this.TYPE = TYPE;
}
public String getNAME() {
return this.NAME;
}
public String getTYPE() {
return this.TYPE;
}
@Override
public String getDescription() {
return "Printer{" +
"NAME='" + NAME + '\'' +
", TYPE='" + TYPE + '\'' +
'}';
}
}
3、Employee类及其子类的设计
status类(service包中)
package pers.victorgong.开发团队的人员调度软件.service;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 15:28 2020/12/14
*/
public class Status {
private final String NAME;
public 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;
}
}
Employee类
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 员工
* @Date: Created in 15:02 2020/12/14
*/
public class Employee {
private final int ID;
private final String NAME;
private final int AGE;
private double salary;
private int memberId; //记录成员加入开发团队后在团队中的ID
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 String getNAME() {
return NAME;
}
public int getAGE() {
return AGE;
}
public double getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getDetails() {
if (NAME.length() == 2) {
return ID + "\t\t" + NAME + "\t\t" + AGE + "\t\t" + salary;
}
return ID + "\t\t" + NAME + "\t" + AGE + "\t\t" + salary;
}
protected String getMemberDetails() {
return getMemberId() + "/" + getDetails();
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t客服";
}
@Override
public String toString() {
return getDetails();
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
}
Programmer类
package pers.victorgong.开发团队的人员调度软件.domain;
import pers.victorgong.开发团队的人员调度软件.service.Status;
/**
* @Author: Victor Gong
* @Description: 程序员
* @Date: Created in 15:25 2020/12/14
*/
public class Programmer extends Employee{
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 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;
}
protected String getMemberDetails() {
return getMemberId() + "/" + super.getDetails();
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t程序员";
}
@Override
public String toString() {
return getDetails() + "\t程序员\t" + status + "\t\t\t\t\t" + equipment.getDescription() ;
}
}
Designer类
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 设计师
* @Date: Created in 15:38 2020/12/14
*/
public class Designer extends Programmer{
private double bonus; //奖金
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 String getDetailsForTeam() {
return getMemberDetails() + "\t设计师\t" + getBonus();
}
@Override
public String toString() {
return getDetails() + "\t设计师\t" + getStatus() + "\t" +
getBonus() +"\t\t\t" + getEquipment().getDescription();
}
}
Architect类(我这里用SystemArchitect表示了)
package pers.victorgong.开发团队的人员调度软件.domain;
/**
* @Author: Victor Gong
* @Description: 架构师
* @Date: Created in 15:40 2020/12/14
*/
public class SystemArchitect extends Designer{
private int stock; //持有股份
public SystemArchitect(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 getDetailsForTeam() {
return getMemberDetails() + "\t架构师\t" +
getBonus() + "\t" + getStock();
}
@Override
public String toString() {
return getDetails() + "\t架构师\t" + getStatus() + "\t" +
getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
}
}
第二步:实现service包中的类
1、NameListService类的设计
Data类(用于保存数据)
package pers.victorgong.开发团队的人员调度软件.service;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 14:53 2020/12/14
*/
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"}
};
//如下的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类
在此类中设置了员工的status
package pers.victorgong.开发团队的人员调度软件.service;
import pers.victorgong.开发团队的人员调度软件.domain.*;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 16:21 2020/12/14
*/
public class NameListService {
private final Employee[] employees;
public NameListService() {
employees = new Employee[12];
for (int i = 0; i < employees.length; i++) {
int id = Integer.parseInt(Data.EMPLOYEES[i][1]); //id号
String name = Data.EMPLOYEES[i][2]; //姓名
int age = Integer.parseInt(Data.EMPLOYEES[i][3]); //年龄
double salary = Double.parseDouble(Data.EMPLOYEES[i][4]); //工资
Equipment equipment = null;
if (i != 0) {
equipment = switch (Data.EQUIPMENTS[i][0]) { //设备信息
case "21" -> new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
case "22" -> new NoteBook(Data.EQUIPMENTS[i][1], Integer.parseInt(Data.EQUIPMENTS[i][2]));
case "23" -> new Printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
default -> null;
};
}
if (Data.EMPLOYEES[i][0].equals("10")) { //普通员工
employees[i] = new Employee(id, name, age, salary);
}else if (Data.EMPLOYEES[i][0].equals("11")) {//程序员
Programmer p = new Programmer(id, name, age, salary, equipment);
Status status = new Status("FREE");
p.setStatus(status);
employees[i] = p;
}else if (Data.EMPLOYEES[i][0].equals("12")) { //设计师
Designer d = new Designer(id, name, age ,salary, equipment,
Double.parseDouble(Data.EMPLOYEES[i][5]));
Status status = new Status("FREE");
d.setStatus(status);
employees[i] = d;
}else if (Data.EMPLOYEES[i][0].equals("13")) { //架构师
SystemArchitect architect = new SystemArchitect(id, name, age, salary, equipment,
Double.parseDouble(Data.EMPLOYEES[i][5]), Integer.parseInt(Data.EMPLOYEES[i][6]));
Status status = new Status("FREE");
architect.setStatus(status);
employees[i] = architect;
}
}
}
/**
*获取当前所有员工
* @return 包含所有员工对象的数组
*/
public Employee[] getAllEmployees() {
return this.employees;
}
/**
*获取指定ID的员工对象
* @param id 指定员工的ID
* @return 指定员工对象
*/
public Employee getEmployee(int id) throws TeamException{
if (id > 12 || id < 1) {
throw new TeamException("找不到该成员");
}
return employees[id-1];
}
}
自定义异常类TeamException
package pers.victorgong.开发团队的人员调度软件.service;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 17:11 2020/12/14
*/
public class TeamException extends Exception{
static final long serialVersionUID = -33875169124229948L;
public TeamException() {
}
public TeamException(String message) {
super(message);
}
}
2、TeamService类的设计
TeamService类
我在此类中添加了一个输入TID得到员工信息的方法
package pers.victorgong.开发团队的人员调度软件.service;
import pers.victorgong.开发团队的人员调度软件.domain.Designer;
import pers.victorgong.开发团队的人员调度软件.domain.Employee;
import pers.victorgong.开发团队的人员调度软件.domain.Programmer;
import pers.victorgong.开发团队的人员调度软件.domain.SystemArchitect;
/**
* @Author: Victor Gong
* @Description: 关于开发团队成员的管理:添加、删除等
* @Date: Created in 17:38 2020/12/14
*/
public class TeamService {
private static int counter = 1; //团队中员工的id
private final int MAX_MEMBER = 5; //最多成员数量
private Programmer[] members; //团队成员
private int total; //团队成员的实际人数
public TeamService() {
members = new Programmer[MAX_MEMBER];
total = 0;
}
/**
*
* @return 当前团队的所有对象
*/
public Employee[] getTeam() {
Employee[] temp = new Employee[total];
for (int i = 0; i < total; i++) {
temp[i] = members[i];
}
return temp;
}
/**
* 向团队中添加成员
* @param e 员工
* @throws 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;
if (isExist(p)) {
throw new TeamException("该员工已在本团队中");
}
if(p.getStatus().getNAME().equals("BUSY")) {
throw new TeamException("该员工已是某团队成员");
}else if(p.getStatus().getNAME().equals("VOCATION")) {
throw new TeamException("该员正在休假,无法添加");
}
int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0;
for (int i = 0; i < total; i++) {
if (members[i] instanceof SystemArchitect) numOfArch++;
else if (members[i] instanceof Designer) numOfDsgn++;
else if (members[i] instanceof Programmer) numOfPrg++;
}
if (p instanceof SystemArchitect) {
if (numOfArch >= 1) throw new TeamException("团队中至多只能有一名架构师");
} else if (p instanceof Designer) {
if (numOfDsgn >= 2) throw new TeamException("团队中至多只能有两名设计师");
} else if (p instanceof Programmer) {
if (numOfPrg >= 3) throw new TeamException("团队中至多只能有三名程序员");
}
p.setStatus(Status.BUSY); //设置队员状态
p.setMemberId(counter++);
members[total++] = p;
}
/**
*
* @param e 员工
* @return 员工是否已经添加到团队中
*/
private boolean isExist(Programmer p) {
for (int i = 0; i < total; i++) {
if (members[i].getID() == p.getID()) {
return true;
}
}
return false;
}
/**
*从团队中删除成员
* @param memberId 待删除成员的memberId
* @throws TeamException
*/
public void removeMember(int memberId) throws TeamException{
int i;
for (i = 0; i < total; i++) {
if (members[i].getMemberId() == memberId) {
members[i].setStatus(Status.FREE);
break;
}
}
//如果遍历一遍,都找不到,则报异常
if (i == total) {
throw new TeamException("找不到该成员,无法删除");
}
//后面的元素覆盖前面的元素
for (int j = i; j < total - 1; j++) {
members[j] = members[j+1];
}
members[--total] = null;
}
public int getMAX_MEMBER() {
return this.MAX_MEMBER;
}
public int getTotal() {
return this.total;
}
/**
*
* @param tId 团队列表中的编号
* @return 员工信息
*/
public Programmer getEmployee(int tId) throws TeamException{
int i;
for (i = 0; i < total; i++) {
if (members[i].getMemberId() == tId) {
return members[i];
}
}
if (i == total) {
throw new TeamException("找不到该成员,无法删除");
}
return null;
}
}
第三步:实现view包中类
1、TeamView类
我在此类的deleteMember()方法中稍作改进,使得在删除之前还能显示要删除的这名员工的信息
package pers.victorgong.开发团队的人员调度软件.view;
import pers.victorgong.开发团队的人员调度软件.domain.Employee;
import pers.victorgong.开发团队的人员调度软件.domain.Programmer;
import pers.victorgong.开发团队的人员调度软件.service.NameListService;
import pers.victorgong.开发团队的人员调度软件.service.TeamException;
import pers.victorgong.开发团队的人员调度软件.service.TeamService;
/**
* @Author: Victor Gong
* @Description:
* @Date: Created in 18:49 2020/12/14
*/
public class TeamView {
private final NameListService listSvc; //公司成员
private final TeamService teamSvc; //开发团队成员
public TeamView() {
listSvc = new NameListService();
teamSvc = new TeamService();
}
/**
* 主界面显示及控制方法
*/
public void enterMainMenu() {
char key = 0;
while (true) {
if (key != '1') {
listAllEmployees();
}
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
key = TSUtility.readMenuSelection();
System.out.println();
switch (key) {
case '1' -> getTeam();
case '2' -> addMember();
case '3' -> deleteMember();
case '4' -> {
System.out.print("确认是否退出(Y/N):");
char yn = TSUtility.readConfirmSelection();
if (yn == 'Y') {
return;
}
}
}
}
}
/**
* 以表格形式列出公司所有成员
*/
private void listAllEmployees() {
System.out.println("-------------------------------------开发团队调度软件--------------------------------------");
Employee[] employees = listSvc.getAllEmployees();
if (employees.length == 0) {
System.out.println("没有客户记录!");
}else {
System.out.println("ID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备");
}
for (Employee e : employees) {
System.out.println(" " + e);
}
System.out.println("----------------------------------------------------------------------------------------------");
}
/**
* 显示团队成员列表操作
*/
private void getTeam() {
System.out.println("--------------------团队成员列表---------------------");
Employee[] employees = teamSvc.getTeam();
if (teamSvc.getTotal() == 0) {
System.out.println("开发团队目前没有成员!");
}else {
System.out.println("TID/ID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票");
for (Employee e : employees) {
System.out.println(" " + e.getDetailsForTeam());
}
}
System.out.println("-----------------------------------------------------");
}
/**
* 添加成员操作
*/
private void addMember() {
System.out.println("---------------------添加成员---------------------");
System.out.println("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try {
Employee e = listSvc.getEmployee(id);
teamSvc.addMember(e);
System.out.println("添加成功");
} catch (TeamException teamException) {
System.out.println("添加失败,原因:" + teamException.getMessage());
}
// 按回车键继续...
TSUtility.readReturn();
}
/**
* 删除成员操作
*/
private void deleteMember() {
System.out.println("---------------------删除成员---------------------");
System.out.print("请输入要删除员工的TID:");
int id = TSUtility.readInt();
Programmer p;
try {
System.out.println("TID\t\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票");
p = teamSvc.getEmployee(id);
System.out.println(p.getDetailsForTeam());
} catch (TeamException teamException) {
System.out.println("删除失败,原因:" + teamException.getMessage());
}
System.out.print("确认是否删除(Y/N):");
char yn = TSUtility.readConfirmSelection();
if (yn == 'N') {
return;
}
try {
teamSvc.removeMember(id);
System.out.println("删除成功");
} catch (TeamException teamException) {
System.out.println("删除失败,原因:" + teamException.getMessage());
}
// 按回车键继续...
TSUtility.readReturn();
}
}
2、测试类
package pers.victorgong.开发团队的人员调度软件.view;
/**
* @Author: Victor Gong
* @Description: 测试
* @Date: Created in 18:49 2020/12/14
*/
public class ProjectTest {
public static void main(String[] args) {
TeamView teamView = new TeamView();
teamView.enterMainMenu();
}
}
四、测试结果
添加的员工非程序员
添加成功
员工已在团队中
显示团队成员
删除操作
五、总结
1.整个项目是看照着课件思路一个一个做的,中间遇到了很多难题,慢慢解决克服这些困难之后收获非常大。
2.这个项目过后就可以暂时告别JavaSE基础部分的学习,开始后半部分,希望未来的学习之路更加顺利!