package borrow_books;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Test {
int command;
Scanner input = new Scanner(System.in);
String book[] = {"高数", "数据结构"};
public static void main(String[] args) {
while (true){Test test=new Test();
test.inputCommend();
if(test.command==1){
test.inputName();
}else if(test.command==2){
test.inputNumber();
}else if (test.command==3){
System.out.println("退出程序");
break;
}
}}
public class ErrorFanhuiException extends Exception{
public ErrorFanhuiException(){
super("输入错误,请输入数字命令");
}
}
public class BooknotExistException extends Exception{
public BooknotExistException(){
super("图书不存在");
}
}
public void inputCommend(){
try {
System.out.println("输入命令:1-按名称查找图书 2-按数字查找图书 3-退出");
command=input.nextInt();
if((command!=1)&&(command!=2)&&(command!=3)){
throw new ErrorFanhuiException();
}
}catch (InputMismatchException ie){
ErrorFanhuiException ee=new ErrorFanhuiException();
System.out.println(ee.getMessage());
}catch (ErrorFanhuiException ee){
System.out.println(ee.getMessage());
}
}
public void inputName(){
try {
System.out.print("输入图书名称:");
String a2=input.next();
for(int i=0;i< book.length;i++){
if(book[i].equals(a2)){
System.out.println("book:"+book[i]);
return;
}
}
throw new BooknotExistException();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
public void inputNumber(){
try {
System.out.print("输入图书序号:");
int a3=input.nextInt();
for (int i=0;i< book.length;i++){
if (a3==i){
System.out.println("book:"+book[i]);
return;
}
}
throw new BooknotExistException();
}catch (InputMismatchException ie){
input.nextLine();
ErrorFanhuiException ee=new ErrorFanhuiException();
System.out.println(ee.getMessage());
}catch (BooknotExistException be){
System.out.println(be.getMessage());
return;
}
}
}