public class BorrowBooks {
Book[] books = {new Book("红楼梦",1),new Book("高数",2)};
public static void main(String[] args) {
System.out.println("请输入书名及图书序号来查找图书");
boolean flag = true;
while (flag) {
try {
System.out.println("请输入图书序号");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (number != 1 && number != 2) {
throw new ErrorCommandException();
}
System.out.println("请输入图书名字");
String bookName = scanner.next();
BorrowBooks books = new BorrowBooks();
if (!books.books[number-1].name.equals(bookName)) {
throw new BookNotExistException();
} else {
books.books[number-1].showBookName();
flag = false;
}
} catch (ErrorCommandException e) {
e.printStackTrace();
System.out.println("请重新输入序号");
} catch (BookNotExistException e) {
e.printStackTrace();
System.out.println("请重新输入书名");
} catch (InputMismatchException e){
e.printStackTrace();
System.out.println("请输入数字");
}
}
}
}
public class ErrorCommandException extends Exception{
public ErrorCommandException(){
super("命令输入错误,请按照提示输入数字");
}}
public class BookNotExistException extends Exception{
public BookNotExistException(){
super("图书不存在");
}
}
public class Book {
String name;
int number;
public Book(String name,int number){
this.name = name;
this.number = number;
}
public void showBookName(){
System.out.println("book" + name);
}
}