要是我要把如下输出:
输入命令:1-按照名称查找图书;2-按照序号查找图书
2
输入图书序号:
0003
图书不存在!
输入命令:1-按照名称查找图书;2-按照序号查找图书
2
输入图书序号:
0002
book:高数
变为:
输入命令:1-按照名称查找图书;2-按照序号查找图书
2
输入图书序号:
0003
图书不存在!
输入图书序号:
0002
book:高数
代码应该怎么改?
public class BookSearch {
BookDAO bDAO[] = {new BookDAO("0001", "语文"), new BookDAO("0002", "高数")};
boolean flagNoFould = true;
public void searchBook() throws TypeException,NoBookException{
while(flagNoFould){
try{
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
searchModel(input);
if(input == 1){
System.out.println("输入图书名称:");
String name = scan.next();
searchByName(name);
}
if(input == 2){
System.out.println("输入图书序号:");
String bookID = scan.next();
searchByID(bookID);
}
}catch(TypeException e){
this.searchBook();
}catch(NoBookException e){
continue;
}
}
}
private void searchModel(int model) throws TypeException {
try{
if(!(model== 1 || model ==2)){
throw new TypeException("命令输入错误!请根据提示输入数字命令!");
}
}catch(TypeException e){
System.out.println(e.getMessage());
}
}
private void searchByName(String name) throws NoBookException {
try{
for(int i = 0; i < bDAO.length; i++){
if(name.equals(bDAO[i].getName())){
System.out.println("book:" + bDAO[i].getName());
flagNoFould = false;
break;
}
}
if(flagNoFould){
throw new NoBookException("图书不存在!");
}
}catch(NoBookException e){
System.out.println(e.getMessage());
}
}
private void searchByID(String bookID) throws NoBookException {
try{
for(int i = 0; i < bDAO.length; i++){
if(bookID.equals(bDAO[i].getBookID())){
System.out.println("book:" + bDAO[i].getName());
flagNoFould = false;
break;
}
}
if(flagNoFould){
throw new NoBookException("图书不存在!");
}
}catch(NoBookException e){
System.out.println(e.getMessage());
}
}
}