package borrowbook;
public class BorrowBook {
public static void main(String[] args) {
Choose cs = new Choose();
cs.runChoose();
}
}
package borrowbook;
import java.util.Scanner;
public class Choose {
private String str;
private int choice;
String[] book;
public Choose() {
this.book = new String[]{"gaoshu", "sjjg", "mg", "English"};
}
public void doChoiceRight()throws Exception{
if(choice > 3 || choice < 1){
throw new Exception("命令输入错误!请根据提示输入数字命令!");
}
}
public void FindBookExist()throws Exception{
for(int i=0 ; i < book.length;i++)
{
if(str.equalsIgnoreCase(book[i])){
System.out.println("book:"+ book[i]);
return;
}
}
throw new Exception("图书不存在!");
}
public void isNumberExist()throws Exception{
if(choice > book.length || choice < 1){
throw new Exception("序号不存在!");
}else{
System.out.println("book:"+ book[choice-1]);
}
}
public void runChoose(){
while(true){
System.out.println("输入命令:1-按照书名查找图书"+
" 2-按照序号查找图书"+"3-退出");
//判断输入的命令是不是数字
try{
Scanner input = new Scanner(System.in);
choice = input.nextInt();
doChoiceRight();
}catch(Exception e){
System.out.println("命令输入错误!请根据提示输入数字命令!");
continue;
}
//判断输入的数字是否有相应的选项
try{
doChoiceRight();
}catch(Exception e){
System.out.println(e.getMessage());
continue;
}
//choice保存正确的输入命令,判断choice
if(choice == 1){
System.out.println("输入图书名:");
Scanner input = new Scanner(System.in);
str = input.next();
try{
FindBookExist();
}catch(Exception e){
System.out.println(e.getMessage());
}
}else if(choice == 2){
System.out.println("输入图书序号:");
Scanner input = new Scanner(System.in);
try{
choice = input.nextInt();
}catch(Exception e){
System.out.println("命令输入错误!");
continue;
}
try{
isNumberExist();
}catch(Exception e){
System.out.println(e.getMessage());
}
}else if(choice ==3 ){
return;
}
}
}
}