package borrow_books;
import java.util.Scanner;
import java.util.InputMismatchException;
public class Test {
int command;
String inName;
int inNum;
Scanner input = new Scanner(System.in);
Book[] books = {new Book("数据结构", 1), new Book("高数", 2)};
public static void main(String[] args) {
while(true) {
Test t = new Test();
t.inputCommand();
if(t.command == 1) {
t.inputName();
}else if(t.command == 2){
t.inputNumber();
}
}
}
public void inputCommand() {
try {
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
command = input.nextInt();
if((command != 1) && (command != 2)) {
throw new ErrorCommandException();
}
}catch(InputMismatchException ie) {
ErrorCommandException ee = new ErrorCommandException();
System.out.println(ee.getMessage());
}catch(ErrorCommandException ee) {
System.out.println(ee.getMessage());
}
}
public void inputName() {
try {
System.out.println("输入图书名称");
inName = input.next();
for(int i = 0; i < books.length; i++) {
if(inName.equals(books[i].name)) {
books[i].showBookName();
return;
}
}
throw new BooknotExistException();
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void inputNumber() {
while(true) {
try {
System.out.println("输入图书序号");
inNum = input.nextInt();
for(int i = 0; i < books.length; i++) {
if(inNum == books[i].number) {
books[i].showBookName();
return;
}
}
throw new BooknotExistException();
}catch(InputMismatchException ie) {
input.nextLine();
ErrorCommandException ee = new ErrorCommandException();
System.out.println(ee.getMessage());
}catch(BooknotExistException be) {
System.out.println(be.getMessage());
return;
}
}
}
}
package borrow_books;
public class ErrorCommandException extends Exception {
public ErrorCommandException() {
super("命令输入错误!请根据提示输入数字命令!");
}
}
package borrow_books;
public class BooknotExistException extends Exception {
public BooknotExistException() {
super("图书不存在!");
}
}
package borrow_books;
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);
}
}