1.系统功能结构:
系统流程
2.具体实现代码
工具类
package level12.view;
import java.util.Random;
import java.util.Scanner;
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 readstock() {
int n;
for (; ; ) {
String str = readKeyBoard(6, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
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 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;
}
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");
}
}
}
Service类
NameListService
package level12.service;
import level12.domain.*;
import level12.view.TSUtility;
import java.util.ArrayList;
public class NameListService {
//创建ArrayList集合存放员工信息
private ArrayList<Employee> employees = new ArrayList<>();
public NameListService() {
}
public NameListService(ArrayList<Employee> employees) {
this.employees = employees;
}
public ArrayList<Employee> getAllEmployees() {
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
for (int i = 0; i < employees.size(); i++) {
System.out.println(employees.get(i));
}
return employees;
}
public void setEmployees(ArrayList<Employee> employees) {
this.employees = employees;
}
//添加员工的id
private int count = 1;
//初始化默认值
{
employees.add(new Employee(count, "马云 ", 22, 3000));
employees.add(new Architect(++count, "马化腾", 32, 18000, new NoteBook("联想T4", 6000.0), 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.0), 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.0), 3000));
}
//得到当前员工
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() {
System.out.println("请输入需要添加的雇员的职位:");
System.out.println("1(无职位)");
System.out.println("2(程序员)");
System.out.println("3(设计师)");
System.out.println("4(架构师)");
String c = String.valueOf(TSUtility.readMenuSelection());
if (c.equals("1")) {
//无职位
System.out.println("`当前雇员职位分配为:无`");
System.out.println("请输入当前雇员的姓名:");
String name = TSUtility.readKeyBoard(4, 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();
} else if (c.equals("2")) {
//程序员
System.out.println("`当前雇员职位分配为:程序员`");
System.out.println("请输入当前雇员的姓名:");
String name = TSUtility.readKeyBoard(4, false);
System.out.println("请输入当前雇员的年龄:");
int age = TSUtility.readInt();
System.out.println("请输入当前雇员的工资:");
Double salary = TSUtility.readDouble();
System.out.println("请为当前程序员配一台好的台式电脑:");
PC pc = new PC().addPC();
Programmer programmer = new Programmer(++count, name, age, salary, pc);
employees.add(programmer);
System.out.println("人员添加成功!");
TSUtility.readReturn();
} else if (c.equals("3")) {
//设计师
System.out.println("`当前雇员职位分配为:设计师`");
System.out.println("请输入当前雇员的姓名:");
String name = TSUtility.readKeyBoard(4, false);
System.out.println("请输入当前雇员的年龄:");
int age = TSUtility.readInt();
System.out.println("请输入当前雇员的工资:");
Double salary = TSUtility.readDouble();
System.out.println("请为当前设计师配一台好的笔记本电脑:");
NoteBook noteBook = new NoteBook().addNoteBook();
System.out.println("请输入当前设计师的奖金:");
Double bonus = TSUtility.readDouble();
Designer designer = new Designer(++count, name, age, salary, noteBook, bonus);
employees.add(designer);
System.out.println("人员添加成功!");
TSUtility.readReturn();
} else {
//架构师
System.out.println("`当前雇员职位分配为:架构师`");
System.out.println("请输入当前雇员的姓名:");
String name = TSUtility.readKeyBoard(4, false);
System.out.println("请输入当前雇员的年龄:");
int age = TSUtility.readInt();
System.out.println("请输入当前雇员的工资:");
Double salary = TSUtility.readDouble();
System.out.println("请为当前架构师配一台好的笔记本电脑:");
NoteBook noteBook = new NoteBook().addNoteBook();
System.out.println("请输入当前架构师的奖金:");
Double bonus = TSUtility.readDouble();
System.out.println("请输入当前架构师的股票:");
int stock = TSUtility.readstock();
Architect architect = new Architect(++count, name, age, salary, noteBook, bonus, stock);
employees.add(architect);
System.out.println("人员添加成功!");
TSUtility.readReturn();
}
}
//删除员工
public void delEmployee(int id) {
try {
boolean isExists = false;
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == id) {
employees.remove(i);
System.out.println("删除成功!");
isExists = true;
}
}
if (!isExists) {
throw new TeamException("该员工不存在");
} else {
//删掉后的员工号向前补位
for (int i = id - 1; i < employees.size(); i++) {
employees.get(i).setId(i + 1);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//员工的修改 (目前只修改姓名,年龄,工资即可)
public void modifyEmployee(int id) {
boolean flag = false;
for (int i = 0; i < employees.size(); i++) {
Employee emp = employees.get(i);
if (employees.get(i).getId() == id) {
System.out.print("姓名(" + emp.getName() + "):");
String name = TSUtility.readString(4, emp.getName());
System.out.print("年龄(" + emp.getAge() + "):");
//Integer.parseInt将字符串转换成一个10进制的整数
int age = Integer.parseInt(TSUtility.readString(2, emp.getAge() + ""));
System.out.print("工资(" + emp.getSalary() + "):");
//Double.parseDouble将字符串值转换为double值
double salary = Double.parseDouble(TSUtility.readString(10, emp.getSalary() + ""));
emp.setName(name);
emp.setAge(age);
emp.setSalary(salary);
employees.set(i, emp);
flag = true;
}
}
if (flag) {
System.out.println("修改成功!");
} else {
try {
throw new TeamException("该员工不存在");
} catch (TeamException e) {
e.printStackTrace();
}
}
}
public ArrayList<Employee> getAllEmployees1() {
for (int i = 0; i < employees.size(); i++) {
}
return employees;
}
}
TeamService
package level12.service;
import level12.domain.Architect;
import level12.domain.Designer;
import level12.domain.Employee;
import level12.domain.Programmer;
public class TeamService {
//用于自动生成团队成员的memberId
private static int counter = 1;
//团队人数上限
private final int MAX_MEMBER = 5;
//保存当前团队成员
private Programmer[] team = new Programmer[MAX_MEMBER];
//团队实际人数
private int total = 0;
public TeamService() {
}
//返回team中所有程序员构成的数组
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for (int i = 0; i < total; i++) {
team[i] = this.team[i];
}
return team;
}
//初始化当前团队成员数组
public void clearTeam() {
team = new Programmer[MAX_MEMBER];
counter = 1;
total = 0;
}
//增加团队成员
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()) {
throw new TeamException("该员工已是某团队成员");
}
int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0;
for (int i = 0; i < total; i++) {
if (team[i] instanceof Architect) {
numOfArch++;
} else if (team[i] instanceof Designer) {
numOfDsgn++;
} else if (team[i] instanceof Programmer) {
numOfPrg++;
}
}
if (p instanceof Architect) {
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(false);
p.setMemberId(counter++);
team[total++] = p;
}
//判断是否存在于团队中
private boolean isExist(Programmer p) {
for (int i = 0; i < total; i++) {
if (team[i].getId() == p.getId()) return true;
}
return false;
}
//删除指定memberId的程序员
public void removeMember(int memberId) throws TeamException {
int n = 0;
//找到指定memberId的员工,并删除
for (; n < total; n++) {
if (team[n].getMemberId() == memberId) {
team[n].setStatus(true);
break;
}
}
//如果遍历一遍,都找不到,则报异常
if (n == total)
throw new TeamException("找不到该成员,无法删除");
//后面的元素覆盖前面的元素
for (int i = n + 1; i < total; i++) {
team[i - 1] = team[i];
}
team[--total] = null;
}
}
ProjectSrevice
package level12.service;
import level12.domain.Programmer;
import level12.domain.Project;
import level12.view.TSUtility;
import java.util.ArrayList;
import java.util.Random;
public class ProjectService {
//用来存项目的集合
private ArrayList<Project> pro = new ArrayList<>();
//添加项目的标号
private int count = 1;
//新项目添加
public void addProject() throws InterruptedException {
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("请输入你想添加的项目名: ");
char c = TSUtility.readMenuSelection();
switch (c) {
case '1':
Project p1 = new Project();
p1.setProId(count++);
p1.setProjectName("小米官网");
p1.setDesName("开发完成类似于小米官网的web项目.");
pro.add(p1);
TSUtility.loadSpecialEffects();
System.out.println("已添加项目:" + p1.getProjectName());
break;
case '2':
Project p2 = new Project();
p2.setProId(count++);
p2.setProjectName("公益在线商城");
p2.setDesName("猫宁Morning公益商城是中国公益性在线电子商城.");
pro.add(p2);
TSUtility.loadSpecialEffects();
System.out.println("已添加项目:" + p2.getProjectName());
break;
case '3':
Project p3 = new Project();
p3.setProId(count++);
p3.setProjectName("博客系统");
p3.setDesName("Java博客系统,让每一个有故事的人更好的表达想法!");
pro.add(p3);
TSUtility.loadSpecialEffects();
System.out.println("已添加项目:" + p3.getProjectName());
break;
case '4':
Project p4 = new Project();
p4.setProId(count++);
p4.setProjectName("在线协作文档编辑系统");
p4.setDesName("一个很常用的功能,适合小组内的文档编辑。");
pro.add(p4);
TSUtility.loadSpecialEffects();
System.out.println("已添加项目:" + p4.getProjectName());
break;
default:
System.out.println("项目不存在");
break;
}
}
//项目分配团队开发
public Programmer[] dealingPro(Programmer[] team) {
System.out.println("当前团队有人员:");
for (int i = 0; i < team.length; i++) {
System.out.println(team[i]);
}
System.out.println("请为当前团队创建一个团队名称:");
String teamName = TSUtility.readKeyBoard(6, false);
//随机分配项目
Random ra = new Random();
int ranNum = ra.nextInt(pro.size());
Project project = this.pro.get(ranNum);
project.setTeamName(teamName);
project.setTeam(team);
project.setStatus(true);
pro.set(ranNum, project);
return team;
}
//查看项目当前状态
public void showPro() throws InterruptedException {
//加载
TSUtility.loadSpecialEffects();
//项目长度为0
if (pro.size() == 0) {
System.out.println("当前系统无项目分配");
}
for (int i = 0; i < pro.size(); i++) {
System.out.println("项目【" + pro.get(i).getProjectName() + "】----->正在被团队【" + pro.get(i).getTeamName() + "】开发中!");
}
}
//删除选择的项目
public void delPro(int id) {
boolean flag;
for (int i = 0; i < pro.size(); i++) {
if (pro.get(i).getProId() == id) {
try {
//查看开发状态
if (pro.get(i).isStatus()) {
throw new TeamException("项目开发中,不可被删除!");
} 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 (TeamException e) {
System.out.println(e.getMessage());
}
}
}
} catch (TeamException e) {
System.out.println(e.getMessage());
}
}
}
}
//得到所有项目数据集合
public ArrayList<Project> getAllPro() {
return pro;
}
}
TeamException
package level12.service;
public class TeamException extends Exception{
public TeamException() {
}
public TeamException(String message) {
super(message);
}
}
domain包下的内容以及全代码
链接:https://pan.baidu.com/s/1u-MpVDAuEC1d-7L0M3wgaQ
提取码:8jsi