续前文面向对象–员工管理系统基础实现
https://blog.csdn.net/qq_43804919/article/details/120969915
输入操作符,只接受指定类型,如果不是,提醒用户且重新输入直到符合要求!
如果不了解异常,可以看我的博客《java异常处理机制》--https://blog.csdn.net/qq_43804919/article/details/120974801https://blog.csdn.net/qq_43804919/article/details/120974801
利用try{
//可能出错的代码
}catch(Exception e){
//异常提示
}
外围套上while(true){
}
直到满足条件才break;
对前员工管理系统的部分修改
主函数
public static void main(String[] args) {
List employee = new ArrayList();
int choice = 0;
while (true) {
System.out.println("************************************************");
System.out.println("* 1.添加 2.修改 3.查询 4.删除 5.排名 6.部门管理 7.退出 *");
System.out.println("************************************************");
try {
Scanner scanner = new Scanner(System.in);
System.out.println("请选择您要进行的操作:");
choice = scanner.nextInt();
if (choice==1) {
add(scanner, employee);
} else if (choice==2) {
edit(scanner, employee);
} else if (choice==3) {
find(scanner, employee);
} else if (choice==4) {
delete(scanner, employee);
} else if (choice==5) {
sort(employee);
} else if (choice==6) {
departmentmannger(employee);
}
else if (choice == 7) {
System.out.println("感谢使用本系统");
break;
}
}catch(Exception e) {
System.out.println("请输入正确的操作符!");
}
}
}
departmentmannger部分
private static void departmentmannger(List employee) {
while(true) {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("1.部门查询 2.部门修改 3.返回上层");
System.out.println("请输入操作:");
int choice = scanner.nextInt();
if(choice == 1) {
findment(employee);
}else if(choice == 2) {
editment(employee);
}else if(choice == 3) {
break;
}
break;
}catch(Exception e) {
System.out.println("请输入正确的操作符!");
}
}
}
editment部分
private static void editment(List employee) {
while(true) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("1.修改部门号2.修改部门名 3.返回上层");
System.out.println("请输入操作:");
int flag = 0;
int choice = sc.nextInt();
if(choice == 1) {
int count = employee.size();
System.out.println("输入查找部门号:");
int id = sc.nextInt();
for(int i = 0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDid() == id) {
System.out.println("输入部门号:");
int eid = sc.nextInt();
e.setDid(eid);
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有此部门号!");
}else {
System.out.println("修改成功!");
}
break;
}else if(choice == 2) {
int count = employee.size();
System.out.println("输入查找部门名:");
String name= sc.next();
for(int i = 0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDName().equals(name)) {
System.out.println("输入部门名:");
String ename = sc.next();
e.setDName(ename);
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有此部门号!");
}else {
System.out.println("修改成功!");
}
break;
}else if(choice ==3) {
break;
}
break;
}catch(Exception e) {
System.out.println("请输入正确操作符!");
}
}
}
findment部分
private static void findment(List employee) {
while(true) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("1.按部门号查找 2.按部门名查找 3.返回上层");
System.out.println("请输入操作:");
int choice = sc.nextInt();
int count = employee.size();
if(choice == 1) {
int flag = 0;
System.out.println("输入部门号:");
int id =sc.nextInt();
for(int i=0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDid()==id) {
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有找到此部门!");
}else {
System.out.println(id+"部门共有"+flag+"人");
}
break;
}else if(choice == 2) {
int flag = 0;
System.out.println("输入部门名:");
String name =sc.next();
for(int i=0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDName().equals(name)) {
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有找到此部门!");
}else {
System.out.println(name+"部门共有"+flag+"人");
}
break;
}else if(choice == 3) {
break;
}
break;
}catch(Exception e) {
System.out.println("请输入正确操作符!");
}
}
}
其余部分
请参照上一篇本人的博客《面向对象--员工管理系统(基础实现)》,对比上面的修改方式修改即可。
测试截图