目录
目录
• 模拟实现一个基于文本界面的《项目开发团队分配管理软件》
1 系统结构功能
2 系统流程
3 软件设计
3.0 前提
在之后的代码中,会出现 TSUtility 类,这是一个工具类,用于对输入输出格式和内容的限制
示例代码:
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 char readMenuSelectionPro() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
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 int readInt(int defaultValue) {
int n;
for (; ; ) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static int readstock() {
int n;
for (; ; ) {
String str = readKeyBoard(6, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
//double类型的限制
public static Double readDouble() {
Double n;
for (; ; ) {
String str = readKeyBoard(6, false);
try {
n = Double.parseDouble(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static Double readDouble(double defaultValue) {
double n;
for (; ; ) {
String str = readKeyBoard(2, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Double.parseDouble(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;
}
//String类型的输入限制,并且如果为空,则回车跳过
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("") ? defaultValue : str;
}
//输入限制类
public 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;
}
public static void loadSpecialEffects() throws InterruptedException {
System.out.println("请稍等:");
for (int i1 = 1; i1 <= 100; i1++) {
System.out.print("加载中:" + i1 + "%");
Thread.sleep(new Random().nextInt(25) + 1);
if (i1 == 100) {
Thread.sleep(50);
}
System.out.print("\r");
}
}
}
3.1 用户注册登录模块
LoginView类
public class LoginView {
private String userName;
private String password;
//注册
public void register(){
System.out.println("开始注册");
System.out.println("请输入你的注册账户用户名:");
String userName = TSUtility.readKeyBoard(2,false);
System.out.println("请输入你的注册账户密码:");
this.userName = userName;
String password = TSUtility.readKeyBoard(8,false);
this.password = password;
System.out.println("注册成功!");
}
//注册
public void login(){
int count = 5;
boolean flag = true;
while(flag) {
System.out.println("-------------------登录----------------------");
System.out.println("请输入您的用户名:");
String userName = TSUtility.readKeyBoard(2, false);
System.out.println("请输入您的密码:");
String password = TSUtility.readKeyBoard(8, false);
if (userName.equals(this.userName) && password.equals(this.password)) {
System.out.println("登陆成功!欢迎你:" + userName);
flag = false;
} else if (this.userName.length() == 0 || this.password.length() == 0) {
System.out.println("该账户不存在!请先注册!");
register();
} else {
if (count <= 0) {
System.out.println("登录次数不足,请联系管理!");
return;
} else {
count--;
System.out.println("登录失败,账户和密码不匹配!");
System.out.println("您还有" + count + "次登录机会,请重新输入");
}
}
}
}
//修改
public void update(){
boolean flag = true;
while(flag){
System.out.println("-------------------------用户名和密码修改-------------------------");
System.out.println("1 修改用户名");
System.out.println("2 修改密码");
System.out.println("3 修改用户名和密码");
System.out.println("4 退出");
System.out.println("请输入您需要修改的功能编号:");
char n = TSUtility.readMenuSelection();
switch (n){
case '1':
System.out.println("请输入新用户名:");
String userName = TSUtility.readKeyBoard(4,false);
this.userName = userName;
System.out.println("修改成功!");
break;
case '2':
System.out.println("请输入新密码:");
String password = TSUtility.readKeyBoard(8,false);
this.password = password;
System.out.println("修改成功!");
break;
case '3':
System.out.println("请输入新用户名:");
String userName1 = TSUtility.readKeyBoard(4,false);
this.userName = userName1;
System.out.println("请输入新密码:");
String password1 = TSUtility.readKeyBoard(8,false);
this.password = password1;
System.out.println("修改成功!");
break;
case '4':
System.out.println("退出成功!");
flag = false;
break;
}
}
}
}
运行结果:
3.2 开发人员管理模块
在这个模块中,我们需要创建几个实体类
示例代码如下:
Employee类:
在Employee类中,我们重写了 toString 方法,目的是方便后续查看功能的输出
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\t" + salary + "\t" ;
}
@Override
public String toString() {
return getInfo();
}
}
programmer类
此类继承于Employee类
这里我们再次继承重写 toString 方法,并且添加了两个新方法,getMemberInfo是为了返回团队队员个人信息, 而getInfoForTeam方法则添加了职位
public class Programmer extends Employee{
private int memberId;
private boolean status = true;
private Equipment equipment;
public Programmer(){}
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 boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
public String getMemberInfo(){
return getMemberId() + "/" + getInfo();
}
public String getInfoForTeam(){
return getMemberInfo() + "\t程序员";
}
@Override
public String toString() {
return getInfo() + "\t程序员\t" + status + "\t\t\t\t\t" + equipment.getDescription();
}
}
Designer类:
继承于Programmer类
重写 getInfoForTeam() 和 toString(),两个方法,返回设计师信息
public class Designer extends Programmer{
//奖金
private double bonus;
public Designer(){}
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 getInfoForTeam(){
return getMemberInfo() + "\t设计师\t" + getBonus();
}
@Override
public String toString() {
return getInfo() + "\t设计师\t" + isStatus() + "\t" + getBonus() + "\t\t\t" + getEquipment().getDescription();
}
}
Architect类:
继承于Designer类
重写两个方法,返回架构师信息
public class Architect extends Designer{
//stock 表示公司奖励的股票数量
private int stock;
public Architect() {
}
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 getInfoForTeam() {
return getMemberInfo() + "\t架构师\t" + getBonus() + getStock();
}
@Override
public String toString() {
return getInfo() + "\t架构师\t" + isStatus() + "\t" + getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
}
}
在之前创建的 Programmer 类中我们可以发现里面有个变量的类型是 Equipment ,这里的Equipment 是我们创建的接口
示例代码:
public interface Equipment {
public String getDescription();
}
接口 Equipment 里面就定义了一个抽象方法,这个抽象方法的目的是输出其子类的信息,在 Equipment 接口中,我们需要为其定义三个子类, 分别是NoteBook类,PC类和Printer类,分别对应三种设备,笔记本电脑、台式电脑和打印机
在这三个类中,我们需要对其进行有参和无惨构造,并且重写 getDescription() 方法,还需要写一个添加设备的方法,返回设备信息。
示例代码:
NoteBook类
public class NoteBook implements Equipment{
private String model; //机型
private double price; //价格
public NoteBook(){}
public NoteBook(String model, double price) {
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 NoteBook addNoteBook(){
System.out.println("请输入需要配置的笔记本电脑型号:");
String model = TSUtility.readKeyBoard(10, false);
System.out.println("请输入配置的笔记本电脑的价格:");
Double price = TSUtility.readDouble();
System.out.println("设备添加成功!");
return new NoteBook(model,price);
}
@Override
public String getDescription() {
return model + "(" + price + ")";
}
}
PC类:
public class PC implements Equipment {
private String model;
private String display;
public PC(){}
public PC(String model, String display) {
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;
}
public PC addPC(){
System.out.println("请输入您需要配置的台式电脑的型号:");
String model = TSUtility.readKeyBoard(12, false);
System.out.println("请输入你需要配置的台式电脑的显示器:");
String display = TSUtility.readKeyBoard(10,false);
System.out.println("设备添加成功!");
return new PC(model, display);
}
@Override
public String getDescription() {
return model + "(" + display + ")";
}
}
Printer类:
示例代码:
public class Printer implements Equipment{
private String name;
private String type;
public Printer(){}
public Printer(String name, String type) {
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 Printer addPrinter(){
System.out.println("请输入打印机的名字:");
String name = TSUtility.readKeyBoard(5, false);
System.out.println("请输入打印机的类型:");
String type = TSUtility.readKeyBoard(2, false);
return new Printer(name,type);
}
@Override
public String getDescription() {
return name + "(" + type + ")";
}
}
当这些类创建好以后,我们便正式开始编写开发人员管理模块。根据需求文档要求,我们需要在service 包中创建一个 NameListService 类,并在里面完成功能操作
以及根据需求文档的要求,创建 NameListService 类,要求如下:
此代码块为静态创建
NameListService 类整体示例代码如下:
public class NameListService {
private ArrayList<Employee> employees = new ArrayList<>();
int count = 1;
{
employees.add(new Employee(count, "马云 ", 22, 3000));
employees.add(new Architect(++count, "马化腾", 32, 18000, new NoteBook("联想T4", 6000), 60000, 5000));
employees.add(new Programmer(++count, "李彦宏", 23, 7000, new PC("戴尔", "NEC 17寸")));
employees.add(new Programmer(++count, "刘强东", 24, 7300, new PC("戴尔", "三星 17寸")));
employees.add(new Designer(++count, "雷军 ", 50, 10000, new Printer("激光", "佳能2900"), 5000));
employees.add(new Programmer(++count, "任志强", 30, 16800, new PC("华硕", "三星 17寸")));
employees.add(new Designer(++count, "柳传志", 45, 35500, new PC("华硕", "三星 17寸"), 8000));
employees.add(new Architect(++count, "杨元庆", 35, 6500, new Printer("针式", "爱普生20k"), 15500, 1200));
employees.add(new Designer(++count, "史玉柱", 27, 7800, new NoteBook("惠普m6", 5800), 1500));
employees.add(new Programmer(++count, "丁磊 ", 26, 6600, new PC("戴尔", "NEC17寸")));
employees.add(new Programmer(++count, "张朝阳 ", 35, 7100, new PC("华硕", "三星 17寸")));
employees.add(new Designer(++count, "杨致远", 38, 9600, new NoteBook("惠普m6", 5800), 3000));
}
//获取当前所有员工
public ArrayList<Employee> getAllEmployees(){
return employees;
}
public Employee getEmployee(int id) throws TeamException{
for(int i = 0; i < employees.size(); i++){
if(employees.get(i).getId() == id){
return employees.get(i);
}
}
throw new TeamException("该员工不存在");
}
public void addEmployee() throws InterruptedException{
System.out.println("请输入需要添加的雇员的职位:");
System.out.println("1(无职位)");
System.out.println("2(程序员)");
System.out.println("3(设计师)");
System.out.println("4(架构师)");
char n = TSUtility.readMenuSelection();
switch (n){
case '1':
System.out.println("当前雇员暂无职位分配");
System.out.println("请输入雇员的姓名:");
String name = TSUtility.readKeyBoard(5, false);
System.out.println("请输入雇员的年龄:");
int age = TSUtility.readInt();
System.out.println("请输入雇员的工资:");
Double salary = TSUtility.readDouble();
Employee employee = new Employee(++count,name,age,salary);
employees.add(employee);
System.out.println("添加成功!");
TSUtility.readReturn();
break;
case '2':
System.out.println("当前雇员的职位分配为:程序员");
System.out.println("请输入雇员的姓名:");
String name2 = TSUtility.readKeyBoard(5, false);
System.out.println("请输入雇员的年龄:");
int age2 = TSUtility.readInt();
System.out.println("请输入雇员的工资:");
Double salary2 = TSUtility.readDouble();
System.out.println("请输入为当前雇员配置的台式电脑信息:");
PC pc = new PC().addPC();
Employee employee2 = new Programmer(++count,name2,age2,salary2,pc);
employees.add(employee2);
System.out.println("添加成功!");
TSUtility.readReturn();
break;
case '3':
System.out.println("当前雇员的职位分配为:设计师");
System.out.println("请输入雇员的姓名:");
String name3 = TSUtility.readKeyBoard(5, false);
System.out.println("请输入雇员的年龄:");
int age3 = TSUtility.readInt();
System.out.println("请输入雇员的工资:");
Double salary3 = TSUtility.readDouble();
System.out.println("请输入为当前雇员配置的笔记本电脑信息:");
NoteBook noteBook = new NoteBook().addNoteBook();
System.out.println("请输入雇员的奖金:");
Double bonus = TSUtility.readDouble();
Employee employee3 = new Designer(++count,name3,age3,salary3,noteBook,bonus);
employees.add(employee3);
System.out.println("添加成功!");
TSUtility.readReturn();
break;
case '4':
System.out.println("当前雇员的职位分配为:架构师");
System.out.println("请输入雇员的姓名:");
String name4 = TSUtility.readKeyBoard(5, false);
System.out.println("请输入雇员的年龄:");
int age4 = TSUtility.readInt();
System.out.println("请输入雇员的工资:");
Double salary4 = TSUtility.readDouble();
System.out.println("请输入为当前雇员配置的打印设备信息:");
Printer printer = new Printer().addPrinter();
System.out.println("请输入雇员的奖金:");
Double bonus4 = TSUtility.readDouble();
System.out.println("请输入雇员的股票数量:");
int stock = TSUtility.readstock();
Employee employee4 = new Architect(++count,name4,age4,salary4,printer,bonus4,stock);
employees.add(employee4);
System.out.println("添加成功!");
TSUtility.readReturn();
break;
}
}
public void updateEmployee(int id) throws InterruptedException{
boolean flag = false;
for (int i = 0; i < employees.size(); i++) {
Employee e = employees.get(i);
if(e.getId() == id){
System.out.println("姓名(" + e.getName() + "):");
String name = TSUtility.readString(5,e.getName());
System.out.println("年龄(" + e.getAge() + "):");
int age = TSUtility.readInt(e.getAge());
System.out.println("工资(" + e.getSalary() + ")");
Double salary = TSUtility.readDouble(e.getSalary());
e.setName(name);
e.setAge(age);
e.setSalary(salary);
employees.set(i,e);
flag = true;
}
}
if(flag){
System.out.println("修改成功!");
}else {
try{
throw new TeamException("该员工不存在");
}catch (Exception e){
e.printStackTrace();
}
}
}
public void deleteEmployee(int id){
boolean flag = false;
for (int i = 0; i < employees.size(); i++) {
if(employees.get(i).getId() == id){
employees.remove(i);
//这里使用i = id 是因为集合的索引是从0开始的,所以 i = id 所获取的信息是我们输入的id之后的信息
//这里我们需要把删除信息之后的信息均忘前面移动一位,所以获取删除信息的索引 i-1 并且重新设置其id
for ( i = id; i <= employees.size(); i++) {
employees.get(i - 1).setId(employees.get(i - 1).getId() - 1);
}
flag = true;
}
}
if(flag){
System.out.println("删除成功!");
count--;
}else {
try{
throw new TeamException("该员工不存在");
}catch (Exception e){
e.printStackTrace();
}
}
}
public void showEmployee() throws InterruptedException{
TSUtility.loadSpecialEffects();
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < employees.size(); i++) {
System.out.println(" " + employees.get(i));
}
}
}
3.3 开发团队调度管理模块
3.3.1 需求说明
如果添加操作因某种原因失败,将显示类似以下信息(失败原因视具体原因而不同):
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):2
---------------------添加成员---------------------
请输入要添加的员工ID:2
添加失败,原因:该员工已是某团队成员
按回车键继续...
失败信息包含以下几种: (需要抛出自定义异常)
当选择“添加团队成员”菜单时,将执行从列表中添加指定(通过ID)成员到开发团队的功能:
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):2
---------------------添加成员---------------------
请输入要添加的员工ID:2
添加成功
按回车键继续...
添加成功后,按回车键将重新显示主界面。
开发团队人员组成要求:
当选择“删除团队成员”菜单时,将执行从开发团队中删除指定(通过TeamID)成员的功能:
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):3
---------------------删除成员---------------------
请输入要删除员工的TID:1
确认是否删除(Y/N):y
删除成功
按回车键继续...
删除成功后,按回车键将重新显示主界面。
当选择“团队列表”菜单时,将列出开发团队中的现有成员,例如:
1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):1
--------------------团队成员列表---------------------
TDI/ID 姓名 年龄 工资 职位 奖金 股票
2/4 刘强东 24 7300.0 程序员
3/2 马化腾 32 18000.0 架构师 15000.0 2000
4/6 任志强 22 6800.0 程序员
5/12 杨致远 27 600.0 设计师 4800.0
-----------------------------------------------------------
因为需要抛出自定义异常,所以我们需要创建一个自定义异常
示例代码:
package com.team.service;
public class TeamException extends Exception{
public TeamException(String message){
super(message);
}
}
TeamService类
示例代码:
import com.team.domain.Architect;
import com.team.domain.Designer;
import com.team.domain.Employee;
import com.team.domain.Programmer;
public class TeamView {
NameListService listSvc = new NameListService();
TeamService teamSvc = new TeamService();
ArrayList<Employee> emp = listSvc.getAllEmployees();
ArrayList<Programmer[]> team = new ArrayList<>();
public void listAllEmployee() {
if (emp.size() == 0){
System.out.println("暂无客户记录!");
}else {
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < emp.size(); i++) {
System.out.println(" " + emp.get(i));
}
}
}
public void getTeam() {
teamSvc.showMember();
}
public void getMemberTeam(int num){
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < team.size(); i++) {
if (i == num-1){
System.out.println(" " + Arrays.toString(team.get(num - 1)));
}
}
}
public void addMember() {
System.out.println("请输入你需要添加的团队成员的ID:");
int num = TSUtility.readInt();
for (int i = 0; i < emp.size(); i++) {
if (emp.get(i).getId() == num) {
try {
teamSvc.addMember(emp.get(i));
System.out.println("添加成功!");
} catch (Exception e) {
System.out.println("添加失败!");
e.printStackTrace();
}
}
}
}
public void deleteMember() {
System.out.println("请输入你需要删除的团队成员ID:");
int num = TSUtility.readInt();
System.out.println("是否需要删除?(Y/N):");
char check = TSUtility.readConfirmSelection();
if (check == 'Y') {
try {
teamSvc.deleteMember(num);
System.out.println("删除成功!");
} catch (Exception e) {
e.printStackTrace();
}
} else if (check == 'N') {
TSUtility.readReturn();
}
}
public void enterMainMenu() {
boolean flag = true;
while (flag) {
System.out.println("**********************************开发团队调度软件***************************");
listAllEmployee();
System.out.println("**************************************************************************");
System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1~4):");
char c = TSUtility.readMenuSelection();
switch (c) {
case '1':
System.out.println("-------------团队列表------------");
getTeam();
TSUtility.readReturn();
break;
case '2':
System.out.println("-------------添加成员------------");
addMember();
TSUtility.readReturn();
break;
case '3':
System.out.println("-------------删除成员------------");
deleteMember();
TSUtility.readReturn();
break;
case '4':
System.out.println("是否退出吗?(Y/N):");
char selection = TSUtility.readConfirmSelection();
if (selection == 'Y'){
if (teamSvc.getTeam().length == 0){
flag = false;
}else {
team.add(teamSvc.getTeam());
teamSvc.clearTeam();
flag = false;
}
}
break;
}
}
}
public ArrayList<Programmer[]> getManyTeam(NameListService nameListService){
boolean flag = true;
listSvc = nameListService;
while(flag){
System.out.println("---------------------团队管理------------------");
System.out.println("1-添加团队 2-查看团队 3-删除团队 4-退出 请选择(1~4):");
char num = TSUtility.readMenuSelection();
switch (num){
case '1':
enterMainMenu();
break;
case '2':
System.out.println("------------------------团队列表-------------------------");
if (team.size() == 0){
System.out.println("暂无团队列表,请添加后再查询");
}else{
//for(int i = 0; i < team.size(); i++)
for (Programmer[] team:team) {
for (int i = 0; i < team.length; i++) {
System.out.println(team[i]);
}
}
}
System.out.println("--------------------------------------------------------");
System.out.println("一共有" + team.size() + "个团队");
break;
case '3':
System.out.println("--------------------------团队删除------------------------");
System.out.println("请输入您需要删除的第几个团队:");
int n = TSUtility.readInt();
if (n < team.size()){
System.out.println("是否删除吗?(Y/N):");
char c = TSUtility.readConfirmSelection();
if(c == 'Y'){
team.remove(n-1);
}else {
return getManyTeam(nameListService);
}
}else {
System.out.println("该团队不存在!目前只有" + team.size() + "个团队");
}
break;
case '4':
System.out.println("是否退出吗?(Y/N):");
char c = TSUtility.readConfirmSelection();
if (c == 'Y'){
flag = false;
}else {
return getManyTeam(nameListService);
}
break;
}
}
return team;
}
}
TeamView类:
public class TeamView {
NameListService listSvc = new NameListService();
TeamService teamSvc = new TeamService();
ArrayList<Programmer[]> team = new ArrayList<>();
public void listAllEmployee() {
ArrayList<Employee> emp = listSvc.getAllEmployees();
if (emp.size() == 0) {
System.out.println("暂无客户记录!");
} else {
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < emp.size(); i++) {
System.out.println(" " + emp.get(i));
}
}
/*try {
listSvc.showEmployee();
}catch (Exception e){
System.out.println(e.getMessage());
}*/
}
public void getTeam() {
teamSvc.showMember();
}
public void getMemberTeam(int num) {
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < team.size(); i++) {
if (i == num - 1) {
System.out.println(" " + Arrays.toString(team.get(num - 1)));
}
}
}
public void addMember() {
System.out.println("请输入你需要添加的团队成员的ID:");
int num = TSUtility.readInt();
try {
Employee e = listSvc.getEmployee(num);
teamSvc.addMember(e);
System.out.println("添加成功!");
} catch (Exception e) {
System.out.println("添加失败!");
System.out.println("失败原因:" + e.getMessage());
}
}
public void deleteMember() {
System.out.println("请输入你需要删除的团队成员ID:");
int num = TSUtility.readInt();
System.out.println("是否需要删除?(Y/N):");
char check = TSUtility.readConfirmSelection();
if (check == 'Y') {
try {
teamSvc.deleteMember(num);
System.out.println("删除成功!");
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
} else if (check == 'N') {
TSUtility.readReturn();
}
}
public void enterMainMenu() {
boolean flag = true;
while (flag) {
System.out.println("**********************************开发团队调度软件***************************");
listAllEmployee();
System.out.println("**************************************************************************");
System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1~4):");
char c = TSUtility.readMenuSelection();
switch (c) {
case '1':
System.out.println("-------------团队列表------------");
getTeam();
System.out.println("--------------------------------");
TSUtility.readReturn();
break;
case '2':
System.out.println("-------------添加成员------------");
addMember();
TSUtility.readReturn();
break;
case '3':
System.out.println("-------------删除成员------------");
deleteMember();
TSUtility.readReturn();
break;
case '4':
System.out.println("是否退出吗?(Y/N):");
char selection = TSUtility.readConfirmSelection();
if (selection == 'Y') {
if (teamSvc.getTeam().length == 0) {
flag = false;
} else {
team.add(teamSvc.getTeam());
teamSvc.clearTeam();
flag = false;
}
}
break;
}
}
}
public ArrayList<Programmer[]> getManyTeam(NameListService nameListService) {
boolean flag = true;
listSvc = nameListService;
while (flag) {
System.out.println("---------------------团队管理------------------");
System.out.println("1-添加团队 2-查看团队 3-删除团队 4-退出 请选择(1~4):");
char num = TSUtility.readMenuSelection();
switch (num) {
case '1':
enterMainMenu();
break;
case '2':
System.out.println("------------------------团队列表-------------------------");
if (team.size() == 0) {
System.out.println("暂无团队列表,请添加后再查询");
} else {
//for(int i = 0; i < team.size(); i++)
System.out.println("团队ID/ID\t 姓名\t 年龄\t 工资\t 职位\t 奖金\t 股票");
for (Programmer[] team : team) {
for (int i = 0; i < team.length; i++) {
System.out.println(team[i]);
}
System.out.println("-------------------------------------------------");
}
}
System.out.println("一共有" + team.size() + "个团队");
break;
case '3':
System.out.println("--------------------------团队删除------------------------");
System.out.println("请输入您需要删除的第几个团队:");
int n = TSUtility.readInt();
if (n <= team.size() && n > 0) {
System.out.println("是否删除吗?(Y/N):");
char c = TSUtility.readConfirmSelection();
if (c == 'Y') {
team.remove(n - 1);
} else {
System.out.println("请考虑清楚!");
return getManyTeam(nameListService);
}
} else {
System.out.println("该团队不存在!目前只有" + team.size() + "个团队");
}
break;
case '4':
System.out.println("是否退出吗?(Y/N):");
char c = TSUtility.readConfirmSelection();
if (c == 'Y') {
flag = false;
} else {
return getManyTeam(nameListService);
}
break;
}
}
return team;
}
}
3.4 开发项目管理模块
项目需求
Project 类
这里我们需要重写一下 toString() 方法,以便后面查询功能的输出
public class Project {
private int proId;
private String projectName;
private String desName;
Programmer[] team;
private String teamName;
boolean status = false;
public Project(){}
public Project(int proId, String projectName, String desName, boolean status) {
this.proId = proId;
this.projectName = projectName;
this.desName = desName;
this.status = status;
}
public Project(int proId, String projectName, String desName, Programmer[] team, String teamName, boolean status) {
this.proId = proId;
this.projectName = projectName;
this.desName = desName;
this.team = team;
this.teamName = teamName;
this.status = status;
}
public int getProId() {
return proId;
}
public void setProId(int proId) {
this.proId = proId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getDesName() {
return desName;
}
public void setDesName(String desName) {
this.desName = desName;
}
public Programmer[] getTeam() {
return team;
}
public void setTeam(Programmer[] team) {
this.team = team;
}
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
@Override
public String toString() {
return getProId() + "\t\t" + getProjectName() + "\t" + getDesName() + "\t" + getTeamName() + "\t" + isStatus();
}
ProjectService类
public class ProjectService {
ArrayList<Project> pro = new ArrayList<>();
NameListService nameLi = new NameListService();
TeamService teamSe = new TeamService();
int count = 1;
int key1 = -1;
int key2 = -1;
int key3 = -1;
int key4 = -1;
public void addProject() throws Exception {
System.out.println("项目参考:--------------------------------------------------");
System.out.println("1.小米官网:开发完成类似于小米官网的web项目.");
System.out.println("2.公益在线商城:猫宁Morning公益商城是中国公益性在线电子商城.");
System.out.println("3.博客系统:Java博客系统,让每一个有故事的人更好的表达想法!");
System.out.println("4.在线协作文档编辑系统:一个很常用的功能,适合小组内的文档编辑。");
System.out.println("------------------------------------------------------------");
TSUtility.readReturn();
System.out.println("*************************项目添加页面*************************");
System.out.println("请输入添加的项目名称:");
char proName = TSUtility.readMenuSelection();
switch (proName) {
case '1':
Project p1 = new Project();
p1.setProjectName("小米官网");
p1.setDesName("开发完成类似于小米官网的web项目");
if (key1 == -1) {
p1.setProId(count);
pro.add(p1);
System.out.println("已添加项目:" + p1.getProjectName());
key1++;
} else {
System.out.println("项目" + p1.getProjectName() + "已添加,请勿重复添加");
}
break;
case '2':
Project p2 = new Project();
p2.setProjectName("公益在线商城");
p2.setDesName("猫宁Morning公益商城是中国公益性在线电子商城");
if (key2 == -1) {
p2.setProId(count);
pro.add(p2);
System.out.println("已添加项目:" + p2.getProjectName());
key2++;
} else {
System.out.println("项目" + p2.getProjectName() + "已添加,请勿重复添加");
}
break;
case '3':
Project p3 = new Project();
p3.setProjectName("博客系统");
p3.setDesName("Java博客系统,让每一个有故事的人更好的表达想法!");
if (key3 == -1) {
p3.setProId(count);
pro.add(p3);
System.out.println("已添加项目:" + p3.getProjectName());
key3++;
} else {
System.out.println("项目" + p3.getProjectName() + "已添加,请勿重复添加");
}
break;
case '4':
Project p4 = new Project();
p4.setProjectName("博客系统");
p4.setDesName("Java博客系统,让每一个有故事的人更好的表达想法!");
if (key4 == -1) {
p4.setProId(count);
pro.add(p4);
System.out.println("已添加项目:" + p4.getProjectName());
key4++;
} else {
System.out.println("项目" + p4.getProjectName() + "已添加,请勿重复添加");
}
break;
}
}
public void dealingPro(Programmer[] team) throws Exception {
if (pro.size() == 0) {
System.out.println("暂无项目!请添加项目后再继续操作!");
} else {
System.out.println("请输入团队名称:");
String teamName = TSUtility.readKeyBoard(10, false);
//随机分配项目
Random r = new Random();
int num = r.nextInt(pro.size());
Project p = this.pro.get(num);
if (p.isStatus()){
System.out.println("该项目已经有团队在进行开发!");
}else {
p.setTeamName(teamName);
p.setStatus(true);
p.setTeam(team);
System.out.println("项目【" + p.getProjectName() + "】------------>正在被【" + p.getTeamName() + "】攻坚中");
}
}
}
public void showPro() throws Exception {
if (pro.size() == 0) {
System.out.println("暂无项目!");
} else {
System.out.println("项目号\t项目名称\t项目描述\t\t\t\t\t\t开发团队名称\t开发状态");
for (int i = 0; i < pro.size(); i++) {
System.out.println(" " + pro.get(i));
}
}
}
public void delPro(int id) throws Exception {
boolean flag = false;
if (pro.size() == 0) {
System.out.println("暂无项目!请添加项目后再继续操作!");
} else {
for (int i = 0; i < pro.size(); i++) {
if (pro.get(i).getProId() == id) {
if (pro.get(i).isStatus()) {
System.out.println("删除失败");
} else {
pro.remove(i);
for (i = id; i <= pro.size(); i++) {
pro.get(i - 1).setProId(pro.get(i - 1).getProId() - 1);
}
flag = true;
}
}
}
if (flag) {
System.out.println("删除成功!");
count--;
} else {
try {
throw new TeamException("该项目正在执行或者不存在");
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
}
}
}
}
与此,该项目的功能差不多就此写完,现在我们需要一个主界面,将所有功能进行调用并且提示,与此,我在view包里创建了IndexView类作为主界面
示例代码如下:
indexView:
public class IndexView {
LoginView loginVi = new LoginView();
NameListService nameLi = new NameListService();
TeamService teamSer = new TeamService();
ProjectService proSe = new ProjectService();
TeamView teamVi = new TeamView();
ArrayList<Employee> emp = nameLi.getAllEmployees();
public void menu() {
boolean flag = true;
System.out.println("-------欢迎来到项目开发团队分配软件----------");
System.out.println("********请您先进行登录*********");
TSUtility.readReturn();
try {
loginVi.login();
} catch (InterruptedException e) {
e.printStackTrace();
}
while (flag) {
System.out.println("---------------软件主菜单----------------");
System.out.println("1 用户信息修改");
System.out.println("2 开发人员管理");
System.out.println("3 开发团队调度管理");
System.out.println("4 开发项目管理");
System.out.println("5 退出");
System.out.println("请输入你需要的功能编号:");
char n = TSUtility.readMenuSelectionPro();
switch (n) {
case '1':
try {
loginVi.update();
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
break;
case '2':
boolean flag2 = true;
while (flag2) {
System.out.println("*******************开发人员管理菜单**************");
System.out.println("1 开发人员的添加");
System.out.println("2 开发人员的修改");
System.out.println("3 开发人员的查询");
System.out.println("4 开发人员的删除");
System.out.println("5 退出");
System.out.println("请选择您需要的功能:");
char num = TSUtility.readMenuSelectionPro();
switch (num) {
case '1':
try {
nameLi.addEmployee();
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
break;
case '2':
System.out.println("请输入需要修改信息的员工ID:");
int id = TSUtility.readInt();
try {
nameLi.updateEmployee(id);
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
break;
case '3':
try {
nameLi.showEmployee();
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
TSUtility.readReturn();
break;
case '4':
System.out.println("请输入需要删除的员工信息的ID:");
int id2 = TSUtility.readInt();
try {
nameLi.deleteEmployee(id2);
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
TSUtility.readReturn();
break;
case '5':
System.out.println("退出成功!");
flag2 = false;
}
}
break;
case '3':
teamVi.getManyTeam(nameLi);
break;
case '4':
boolean flag4 = true;
while (flag4) {
System.out.println("*******************开发项目管理菜单**************");
System.out.println("1 添加新项目");
System.out.println("2 开发团队随机分配");
System.out.println("3 查看项目开发情况");
System.out.println("4 删除项目");
System.out.println("5 退出");
System.out.println("请选择(1~5):");
char num4 = TSUtility.readMenuSelectionPro();
switch (num4) {
case '1':
try {
proSe.addProject();
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
TSUtility.readReturn();
break;
case '2':
System.out.println("*************************项目分配团队开发页面*************************");
if (teamVi.team.size() == 0) {
System.out.println("暂无团队,请添加团队后再继续操作");
} else {
Random r = new Random();
int id = r.nextInt(teamVi.team.size()) + 1;
System.out.println("-------------------------------------团队成员名单--------------------------------------------");
try {
System.out.println("ID\t 姓名\t 年龄\t 工资\t\t 职位\t 状态\t 奖金\t 股票\t 领用设备");
for (int i = 0; i < teamVi.team.size(); i++) {
if (i == (id - 1)) {
Programmer[] p = teamVi.team.get(i);
for (int j = 0; j < teamVi.team.get(i).length; j++) {
System.out.println(" " + p[j]);
}
}
}
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
System.out.println("------------------------------------------------------------------------------------------");
try {
proSe.dealingPro(teamSer.getTeam());
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
}
TSUtility.readReturn();
break;
case '3':
System.out.println("*************************项目查看页面*************************");
try {
proSe.showPro();
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
TSUtility.readReturn();
break;
case '4':
System.out.println("*************************项目删除页面*************************");
System.out.println("请输入需要删除的项目号:");
int num = TSUtility.readInt();
try {
proSe.delPro(num);
} catch (Exception e) {
System.out.println("失败原因:" + e.getMessage());
}
TSUtility.readReturn();
break;
case '5':
System.out.println("退出");
flag4 = false;
}
}
break;
case '5':
System.out.println("是否退出?(Y/N):");
char selection = TSUtility.readConfirmSelection();
if (selection == 'Y') {
flag = false;
System.out.println("退出成功!");
} else if (selection == 'N') {
flag = true;
}
}
}
}
public static void main(String[] args) {
IndexView indexVi = new IndexView();
indexVi.menu();
}
}
在原有基础上,为了便利,自行在各个小模块添加了退出功能
遇到的问题:
在此次项目的编写中,我遇到了一一些问题,于此记录
1 StaticOverFlowError
这是在编写 TeamView类时出现的,在多个团队的添加那里,出现的原因是两个代码块之间来回调用,没有停止,导致线程占满,后续程序无法运行
解决办法:停止两代码块的来回调用
2 多个团队的添加
这个模块让我思考了很久。我的思路是:把一个团队放入一个数组里面,然后再把这个数组当成一个整体放入集合,如此就能实现多个团队的添加。但是开始实践的时候发现,添加进去后总是在一个数组里面,无法进入下一个数组
解决办法:在添加团队完成后,在退出的代码块那里,将团队存入集合,并且初始化团队数据
3 返回数据的时候返回的是地址值
原因是没有重写toString方法
解决办法:重写toString
4 添加成员之后,成员状态未改变
原因暂且不明
解决方法:虽然没有找到原因,但是我直接在同一个类中重新写了一个方法进行调用显示成员信息,此时成员状态成功发生了改变
与此,整个项目初步大致完成,需求文档和完整代码放入百度云盘中,需要自取,链接如下:
https://pan.baidu.com/s/1BePad5ycsOAAf3dfAxsYqw?pwd=orfy
提取码:orfy