这是运行出来是的页面!!
采用了MVC模型,如果有错误,请大家指出来
主页面V层
public class TeamView {
NameListService nameListService = new NameListService();
TeamService teamService = new TeamService();
public void View() throws NumberFormatException, TeamException, ArrayIndexOutOfBoundsException {
try {
label:
for (; ; ) {
System.out.println("********************************开发团队成员调度*********************************");
Employee[] employees = nameListService.getAllEmployees();
for (Employee employee : employees) {
System.out.println(employee);
}
System.out.println("*******************************************************************************");
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出系统 请选择[1-4]:");
int index;
try {
index = teamService.getIndex();
} catch (InputMismatchException e) {
System.out.print("输入的数据不正确,请重新输入:");
index = teamService.getIndex();
} catch (NumberFormatException e) {
System.out.print(e.getMessage());
index = teamService.getIndex();
}
switch (index) {
case 1:
teamService.queryMember();
break;
case 2:
teamService.addMember(teamService.getIndexOfEmployee(nameListService.getAllEmployees()));
break;
case 3:
teamService.removeMember(teamService.getIndexTeam());
break;
case 4:
System.out.println("确定要退出吗(Y/y)");
if (teamService.breakView()) {
break label;
}
break;
}
}
System.out.println("*******************************************************************************");
} catch (TeamException | NumberFormatException e) {
System.out.println(e.getMessage());
View();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("输入错误,查无此人");
View();
}
}
public static void main(String[] args) {
new TeamView().View();
}
}
C层主要代码
public class TeamService {
private static int counter = 1;
final int MAX_MEMBER = 5;
private final Programmer[] team = new Programmer[MAX_MEMBER];
private int total = 0;
public Programmer[] getTeam() throws TeamException {
Programmer[] team = new Programmer[total];
if (total >= 0) System.arraycopy(this.team, 0, team, 0, total);
return team;
}
public void addMember(Employee e) throws TeamException {
isExistEmployee();
if (total >= MAX_MEMBER)
throw new TeamException("成员已满");
else if (!(e instanceof Programmer))
throw new TeamException("此人不是程序员");
else if (isExist(e))
throw new TeamException("该员工已经是团队成员");
else if ("BUSY".equals(((Programmer) e).getStatus().getNAME()))
throw new TeamException("该员工已是其他团队成员");
else if ("VACATION".equals(((Programmer) e).getStatus().getNAME()))
throw new TeamException("该员正在休假,无法添加");
else if (e instanceof Architect && numOfArch >= 1)
throw new TeamException("架构师人数以满");
else if (e instanceof Designer && numOfDes >= 2)
throw new TeamException("设计师人数已满");
else if (numOfPro >= 3) {
throw new TeamException("普通程序员以满");
}
((Programmer) e).setStatus(Status.BUSY);
((Programmer) e).setMemberId(counter++);
team[total++] = (Programmer) e;
System.out.println("添加成功");
}
public void removeMember(int memberId) throws ArrayIndexOutOfBoundsException{
System.out.println("确定要删除该员工吗(Y/y)");
if(breakView()) {
for (int i = memberId - 1; i < total; i++) {
Programmer temp;
temp = team[i];
team[i] = team[i + 1];
team[i + 1] = temp;
}
team[total] = null;
total--;
System.out.println("删除成功");
}else{
System.out.println("操作成功");
}
}
public boolean isExist(Employee e) {
for (int i = 0; i < total; i++) {
if (team[i].getId() == e.getId())
return true;
}
return false;
}
int numOfArch,numOfDes, numOfPro;
public void isExistEmployee() {
numOfPro=numOfDes=numOfArch=0;
for (int i = 0; i < total; i++) {
if (team[i] instanceof Architect) {
numOfArch++;
} else if (team[i] instanceof Designer) {
numOfDes++;
} else {
numOfPro++;
}
}
}
public void queryMember() {
if(total==0)
System.out.println("团队里空无一人");
for(int i=0;i<total;i++){
System.out.println(team[i].getMemberId()+"/"+total+"\t"+team[i].display()+"\t"+isEmployee(i));
}
}
public String isEmployee(int i){
if(team[i] instanceof Architect){
return "架构师";
}else if(team[i] instanceof Designer){
return "设计师";
}else{
return "程序员";
}
}
Scanner scanner = new Scanner(System.in);
public int getIndex(){
int index = 0;
try {
index = scanner.nextInt();
if (index > 4 || index <= 0)
throw new NumberFormatException("请输入正确的数字:");
return index;
} catch (InputMismatchException e) {
System.out.print("输入的数据不正确,请重新输入:");
index=getIndex();
} catch (NumberFormatException e) {
System.out.print(e.getMessage());
index=getIndex();
}
return index;
}
public Employee getIndexOfEmployee(Employee[] e) throws NumberFormatException,TeamException{
int index;
System.out.print("请输入要添加进团队的程序员的号码:");
index=scanner.nextInt();
index--;
if(index<0||index>10)
throw new TeamException("输入工号错误");
return e[index];
}
public int getIndexTeam(){
int index;
System.out.print("请输入您要删除的职工编号");
index=scanner.nextInt();
if(index<=0||index>total)
throw new TeamException("输入工号错误");
return index;
}
public boolean breakView(){
String isBreak;
isBreak=scanner.next();
return isBreak.equalsIgnoreCase("Y");
}
}
C层第二个主要类
public class NameListService {
private Employee[] employees = new Employee[Data.EMPLOYEES.length];
public NameListService() {
for (int i = 0; i < Data.EMPLOYEES.length; i++) {
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]);
Equipment equipment;
switch (type) {
case Data.EMPLOYEE:
employees[i] = new Employee(id, name, age, salary);
break;
case Data.PROGRAMMER:
equipment = createEquipment(i);
employees[i] = new Programmer(id, name, age, salary, equipment);
break;
case Data.DESIGNER:
equipment = createEquipment(i);
employees[i] = new Designer(id, name, age, salary,
Double.parseDouble(Data.EMPLOYEES[i][5]), equipment);
break;
case Data.ARCHITECT:
equipment = createEquipment(i);
employees[i] = new Architect(id, name, age, salary,
Double.parseDouble(Data.EMPLOYEES[i][5]),
Integer.parseInt(Data.EMPLOYEES[i][6]), equipment);
break;
}
}
}
public Equipment createEquipment(int i) {
Equipment equipment = null;
String equipmentName = Data.EQUIPMENTS[i][1];
int equipmentType = Integer.parseInt(Data.EQUIPMENTS[i][0]);
switch (equipmentType) {
case Data.PC:
equipment = new PC(equipmentName, Data.EQUIPMENTS[i][2]);
break;
case Data.NOTEBOOK:
equipment = new NoteBook(equipmentName,
Double.parseDouble(Data.EQUIPMENTS[i][2]));
break;
case Data.PRINTER:
equipment = new Printer(equipmentName, Data.EQUIPMENTS[i][2]);
break;
}
return equipment;
}
public Employee[] getAllEmployees() {
return employees;
}
public Employee getEmployee(int id) throws TeamException {
for (Employee employee : employees) {
if (employee.getId() == id) {//id为integer类型,范围(-128~127)
return employee;
}
}
throw new TeamException("未找到这位员工");
}
}
Data数据类,还没有学到数据库
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;
//employ :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", "1500", "2000"},
{"11", "3", "刘强东", "24", "7000"},
{"11", "4", "李彦荣", "26", "7300"},
{"12", "5", "雷军", "28", "10000", "5200"},
{"13", "6", "柳传志", "29", "15000", "7000", "5000"},
{"11", "7", "任志强", "30", "7900"},
{"11", "8", "丁磊", "30", "7700"},
{"13", "9", "孙悟空", "18", "30000", "2000", "1000"},
{"12", "10", "杨致远", "30", "8600", "9600"},
};
//PC :21,model,display;
//NoteBook:22,model,price;
//Printer:23,name,type;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
public static final String[][] EQUIPMENTS = {
{},
{"22", "联想T4", "6000"},
{"21", "戴尔", "NEC17寸"},
{"21", "戴尔", "三星17寸"},
{"23", "佳能2900", "激光"},
{"21", "华硕", "三星17村"},
{"21", "华硕", "三星17寸"},
{"22", "惠普m6", "5800"},
{"23", "如意金箍棒", "紧箍咒"},
{"21", "戴尔", "NEC 17寸"}
};
}
C层第四个类
public class Status {
private final String NAME;
public static final Status BUSY = new Status("BUSY");
public static final Status FREE = new Status("FREE");
public static final Status VOCATION = new Status("VOCATION");
private Status(String name) {
this.NAME = name;
}
@Override
public String toString() {
return NAME;
}
public String getNAME() {
return NAME;
}
}
C层异常类
public class TeamException extends RuntimeException {
static final long serialVersionUID = -70348971907459L;
public TeamException(String msg){
super(msg);
}
}
提取码9999

该博客展示了使用Java MVC架构开发的团队管理页面,主要包括主页面视图、控制器类和数据类。虽然目前未涉及数据库操作,但提供了源码供读者参考,同时也欢迎大家指出可能存在的错误。
1101

被折叠的 条评论
为什么被折叠?



