package first;
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
String[] books = new String[5];
books[0] = "高数";
books[1] = "线代";
books[2] = "英语";
books[3] = "化学";
books[4] = "历史";
Hello h = new Hello();
int num = h.welcome();
while(true) {
if (num == 1) {
try {
if (h.byName(books)){
break;
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
System.out.println("图书不存在!");
num = h.welcome();
}
} else if (num == 2) {
try {
if (h.byNum(books)) {
break;
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
System.out.println("图书不存在!");
num = h.welcome();
}
} else {
System.out.println("NUM:"+num);
break;
}
}
}
public int welcome(){
System.out.println("输入命令: 1-按照名称查找图书 2-按照序号查找图书");
Scanner sin = new Scanner(System.in);
int num = 0;
try {
num = sin.nextInt();
} catch (Exception e) {
// TODO: handle exception
System.out.println("命令错误!请根据提示输入数字命令!");
num = welcome();
}
return num;
}
public boolean byName(String[] books) throws NotFoundException{
Scanner sin = new Scanner(System.in);
System.out.println("输入图书名称:");
String name = sin.next();
for (int i = 0; i < books.length;i++) {
if (books[i].equals(name) == true) {
System.out.println("book:"+name);
return true;
}
}
throw new NotFoundException("图书不存在!");
}
public boolean byNum(String[] books) throws NotFoundException{
Scanner sin = new Scanner(System.in);
System.out.println("输入图书序号:");
int num;
try {
num = sin.nextInt();
} catch (Exception e) {
// TODO: handle exception
throw new NotFoundException("图书不存在!");
}
if (num < 0 || num > books.length) {
throw new NotFoundException("图书不存在!");
}
System.out.println("book:"+books[num]);
return true;
}
}