package com.imooc;
public class Book {
String Name;
int Number;
public void setName(String Name){
this.Name=Name;
}
public void setNumber(int Number){
this.Number=Number;
}
public String getName(){
return Name;
}
public int getNumber(){
return Number;
}
public Book(int Number,String Name){
this.setNumber(Number);
this.setName(Name);
}
}
package com.imooc;
import java.util.*;
public class Test {
private static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
while(true){
System.out.println("输入命令:1-按照名称查找图书 2-按照序号查找图书");
Scanner input=new Scanner(System.in);
int key=input.nextInt(); //选择查询方式
Book[] lab={new Book(1,"高数" ),new Book(2,"数据结构"),new Book(3,"资源革命")};
try{
if(key==1){
for(Book Book:lab){
System.out.println("输入图书名称:");
String nam = console.next();
if(nam.equals(Book.getName())){
System.out.println("书名:"+Book.getName());
}
else{
throw new Exception("图书不存在!");
}
}
}
else if(key==2){
for(Book Book:lab){
System.out.println("输入图书序号:");
int num=input.nextInt();
if(num==(Book.getNumber())){
System.out.println("书名:"+Book.getName());
}
else{
throw new Exception("图书不存在!");
}
}
}
else{
throw new Exception("命令输入错误!请根据提示输入数字命令!");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}