package?com.immc.exception;
/**
?*?圖書信息儲存
?*?書名?bookName
?*?圖書序號?bookNumber
?*?@author?Administrator
?*
?*/
public?class?Book?{
private?String?bookName;
private?int?bookNumber;
public?Book()?{
super();
//?TODO?Auto-generated?constructor?stub
}
public?Book(String?bookName,?int?bookNumber)?{
super();
this.bookName?=?bookName;
this.bookNumber?=?bookNumber;
}
public?String?getBookName()?{
return?bookName;
}
public?void?setBookName(String?bookName)?{
this.bookName?=?bookName;
}
public?int?getBookNumber()?{
return?bookNumber;
}
public?void?setBookNumber(int?bookNumber)?{
this.bookNumber?=?bookNumber;
}
}
package?com.immc.exception;
/*
?*?自定義異常:
?*? 圖書不存在異常
?*/
?
public?class?bookNotExistException?extends?Exception{
public?bookNotExistException()?{
super();
System.out.println("圖書不存在!");
//?TODO?Auto-generated?constructor?stub
}
}
package?com.immc.exception;
/*
?*?自定義異常:
?*? 命令序號不在范圍內(nèi)(只能為1或2)
?*/
?
public?class?demandErrorException?extends?Exception{
public?demandErrorException()?{
super();
System.out.println("命令序號不在范圍內(nèi)!");
//?TODO?Auto-generated?constructor?stub
}
}
package?com.immc.exception;
import?java.util.Scanner;
public?class?userInput?{
public?int?inputDemand(){
System.out.println("輸入命令:1-按名稱查找圖書;2-按序號查找圖書");
Scanner?sc?=?new?Scanner(System.in);
return?sc.nextInt(); ?
}
public?String?inputBookName(){
System.out.println("請輸入圖書名稱:");
Scanner?sc?=?new?Scanner(System.in);
return?sc.nextLine(); ?
}
public?int?inputBookNumber(){
System.out.println("請輸入圖書序號:");
Scanner?sc?=?new?Scanner(System.in);
return?sc.nextInt();
}
}
package?com.immc.exception;
import?java.util.InputMismatchException;
public?class?ExceptionTest?{
userInput?us?=?new?userInput();
public?void?searchBook(){
while(true){
int?demand?=?0;
try{
demand?=?us.inputDemand();
demandError(demand);
}catch(InputMismatchException?e){
System.out.println("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!");
continue;
}catch(demandErrorException?e){
continue;
}
switch(demand){
case?1:
String?str?=?us.inputBookName();
try{
showBook(searchByName(str));
break;
}catch(bookNotExistException?e){
continue;
}
case?2:
int?num?=?us.inputBookNumber();
try{
showBook(searchByNumber(num));
break;
}catch(bookNotExistException?e){
continue;
}
}
}
}
private?Book?searchByName(String?name)?throws?bookNotExistException{
//for?(循環(huán)變量類型?循環(huán)變量名稱?:?要被遍歷的對象)?循環(huán)體
for(Book?book?:?BookIinitial.book){
if(book.getBookName().equals(name)){
return?book;
}
}
throw?new?bookNotExistException();
}
private?Book?searchByNumber(int?number)?throws?bookNotExistException{
if(number>BookIinitial.book.length?||?number<1){
throw?new?bookNotExistException();
}else{
for(Book?book?:?BookIinitial.book){
if(book.getBookNumber()?==?number){
return?book;
}
}
return?null;
}
}
private?int?demandError(int?demand)?throws?demandErrorException{
if(demand?==?1||demand?==?2){
return?demand;
}else{
throw?new?demandErrorException();
}
}
private?void?showBook(Book?book){
System.out.println("圖書序號:"+book.getBookNumber()+
"??書名:"+book.getBookName());
}
}
package?com.immc.exception;
public?class?BookIinitial?{
static?Book[]?book?=?{new?Book("高等數(shù)學",1),new?Book("數(shù)據(jù)結(jié)構(gòu)",2),
new?Book("中國語文",3),new?Book("大學英語",4)};
}
package?com.immc.exception;
public?class?Test?{
/**
?*?@param?args
?*/
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
ExceptionTest?ex?=?new?ExceptionTest();
ex.searchBook();
}
}