今日案例 :模擬借書系統(tǒng) (異常處理練習(xí))
代碼?:
package?borrowBook; ?import?java.util.Scanner; ? ?public?class?BorrowBook??{ ?????private?Book?[]books?=?{new?Book("數(shù)據(jù)結(jié)構(gòu)",1), ?????????????????????????????new?Book("高數(shù)",2)}; ?????private?static?Boolean?=?false; ?????private?static?boolean?=?false; ?????public?void?cam_1()?throws?InputException{ ?????????Scanner?scanner?=?new?Scanner(System.); ?????????System..println("輸入圖書名稱:"); ?????????String?bookname?=?scanner.next(); ?????????for(Book?i:books){ ?????????????if(bookname.equals(i.getName())){ ?????????????????i.getBook(); ?????????????????=?true; ?????????????????break; ?????????????} ?????????} ?????????if(!){ ?????????????throw?new?InputException("圖書不存在!"); ?????????} ?????} ? ?????public?void?cam_2()?throws?InputException{ ?????????Scanner?scanner?=?new?Scanner(System.); ?????????try{ ?????????????System..println("輸入圖書序號:"); ?????????????int?bookNumber; ?????????????if(scanner.hasNextInt()){ ?????????????????bookNumber=scanner.nextInt(); ?????????????}else{ ?????????????????scanner.next(); ?????????????????=?true; ?????????????????throw?new?InputException("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!"); ?????????????} ?????????????for(Book?i:books){ ?????????????????if(bookNumber==i.getBookNumber()){ ?????????????????????i.getBook(); ?????????????????????=?true; ?????????????????????break; ?????????????????} ?????????????} ?????????????if(!)?{ ?????????????????throw?new?InputException("圖書不存在!"); ?????????????} ?????????}catch(InputException?e){ ?????????????e.printMessage(); ?????????????if(){ ?????????????????=?false; ?????????????????new?BorrowBook().cam_2(); ?????????????}else{ ?????????????????new?BorrowBook().(new?String[1]); ?????????????} ?????????} ?????} ? ?public?static?void?main(String[]?args)?throws?InputException?{ ? ?????????Scanner?scanner?=?new?Scanner(System.); ?????????while(true){ ?????????????try{ ?????????????????System..println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書"); ?????????????????String?cammand?=?scanner.next(); ?????????????????if(cammand.equals("1")){new?BorrowBook().cam_1(); ?????????????????}else?if(cammand.equals("2")){new?BorrowBook().cam_2(); ?????????????????}else{ ?????????????????????throw?new?InputException("命令輸入錯誤!請根據(jù)提示輸入數(shù)字命令!"); ?????????????????} ?????????????????break; ?????????????}catch(InputException?e){ ?????????????????e.printMessage(); ?????????????} ?????????????} ?????????} ?????} ?class?Book{ ?????private?String?bookName;private?int?bookNumber;public?Book?(String?name,int?number){ ?????????bookName?=?name; ?????????bookNumber?=?number; ?????}; ?????public?String?getName(){return?bookName; ?????} ?????public?int?getBookNumber(){return?bookNumber; ?????} ?????public?void?getBook(){System..println("book:"+bookName); ?????} ?} ? ? ?class?InputException?extends?Exception?{ ?????private?String?message; ?????public?InputException(){} ?????public?InputException(String?message){ ?????????super(message); ?????????this.message?=?message; ?????} ?????public?void?printMessage(){ ?????????System..println(message); ?????} ?}
截圖 :
2022-06-30
牛啊,膜拜