package library;
import java.util.Scanner;
public class borrowBook {
public String book[][]= {{"001","高数"},{"002","英语"},{"003","语文"}};
public void Solve() throws CommandException,NotFoundException{
Scanner input=new Scanner(System.in);
int a=1;
while(a==1) {
System.out.println("请选择查询方式:1.按照名称查找图书;2.按照序号查找图书");
int choose=input.nextInt();
boolean falg=false;
switch(choose) {
case 1:
System.out.println("输入图书名称:");
String BookName=input.next();
try{
for(int i=0;i<book.length;i++) {
if(book[i][1].equals(BookName)) {
System.out.println("book:"+book[i][1]);
falg=true;
}
}
if(falg==false)
throw new NotFoundException("图书不存在!");
}catch(NotFoundException e) {
System.out.println("图书不存在!");
}
break;
case 2:
System.out.println("输入图书序号:");
String BookNumber=input.next();
try {
for(int i=0;i<book.length;i++) {
if(book[i][0].equals(BookNumber)) {
System.out.println("book:"+book[i][1]);
falg=true;
}
}
if(falg==false)
throw new NotFoundException("图书不存在!");
}catch(NotFoundException e) {
System.out.println("图书不存在!");
}
break;
default:
try {
throw new CommandException("命令输入错误!请根据提示输入数字命令!");
}catch(CommandException e) {
System.out.println("命令输入错误!请根据提示输入数字命令!");
}
}
}
}
public static void main(String[] args) {
borrowBook main=new borrowBook();
try {
main.Solve();
}
catch(CommandException e) {
e.printStackTrace();
}
catch(NotFoundException e) {
e.printStackTrace();
}
catch(Exception e) {
}
}
}
package library;
public class CommandException extends Exception{
public CommandException() {
}
public CommandException(String e) {
super(e);
}
}
package library;
public class NotFoundException extends Exception{
public NotFoundException() {
}
public NotFoundException(String e) {
super(e);
}
}