借書系統異常處理

  1. import java.util.Scanner;  
  2.   
  3. public class Test {  
  4.     private static Scanner console = new Scanner(System.in);  
  5.   
  6.     public static void main(String[] args) {  
  7.         //定義”圖書“數組  
  8.         String[] books = { "C語言""數據結構""彙編語言""高數""大學語文""毛概" };  
  9.         while (true) {  
  10.             System.out.println("輸入命令:1-按照名稱查找圖書;2-按照序號查找圖書");  
  11.             String book;  
  12.             try {  
  13.                 //取得整型命令  
  14.                 int command = inputCommand();  
  15.                 //根據不同命令值,進行不同操作  
  16.                 switch (command) {  
  17.                 case 1://按照圖書名稱選擇圖書  
  18.                     book = getBookByName(books);  
  19.                     System.out.println("book:" + book);  
  20.                     break;  
  21.                 case 2://按照圖書序號(數組下標)選擇圖書  
  22.                     book = getBookByNumber(books);  
  23.                     System.out.println("book:" + book);  
  24.                     break;  
  25.                 case -1://返回值爲-1,說明輸入有誤  
  26.                     System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");  
  27.                     continue;  
  28.                 default://其他值的命令均認爲是錯誤命令  
  29.                     System.out.println("命令輸入錯誤!");  
  30.                     continue;  
  31.                 }  
  32.                 break;//退出程序  
  33.             } catch (Exception bne) {  
  34.                 //捕獲”圖書不存在異常“時,要求重新輸入命令  
  35.                 System.out.println(bne.getMessage());  
  36.                 continue;  
  37.             }   
  38.         }  
  39.     }  
  40.   
  41.     //按照圖書名稱查詢圖書  
  42.     private static String getBookByName(String[] books)  
  43.             throws Exception {  
  44.         System.out.println("輸入圖書名稱:");  
  45.         //獲取輸入的圖書名稱  
  46.         String name = console.next();  
  47.         for (int i = 0; i < books.length; i++) {  
  48.             if (name.equals(books[i]))  
  49.                 //輸入的名稱與某一圖書名稱匹配,返回該圖書  
  50.                 return books[i];  
  51.         }  
  52.         //若無匹配,拋出”圖書不存在異常“  
  53.         throw new Exception("圖書不存在!");  
  54.     }  
  55.   
  56.     //根據圖書序號(數組下標)查詢圖書  
  57.     private static String getBookByNumber(String[] books)  
  58.             throws Exception {  
  59.         while (true) {  
  60.             System.out.println("輸入圖書序號:");  
  61.             try {  
  62.                 //獲取輸入的圖書序號(數組下標)  
  63.                 int index = inputCommand();  
  64.                 //若返回值爲-1  
  65.                 if(index == -1){  
  66.                     System.out.println("命令輸入錯誤!請根據提示輸入數字命令!");  
  67.                     continue;  
  68.                 }  
  69.                 //若不出現”數組下標越界異常“,則返回相應位置的圖書  
  70.                 String book = books[index];  
  71.                 return book;  
  72.             } catch (ArrayIndexOutOfBoundsException e) {  
  73.                 //輸入的序號不存在(引發”數組下標越界異常“),則拋出”圖書不存在異常“  
  74.                 Exception bookNotExists = new Exception("圖書不存在!");  
  75.                 bookNotExists.initCause(e);  
  76.                 throw bookNotExists;  
  77.             }  
  78.         }  
  79.     }  
  80.   
  81.     //從控制檯輸入命令,用於輸入命令和輸入圖書序號  
  82.     private static int inputCommand(){  
  83.         int command;  
  84.         try {  
  85.             command = console.nextInt();  
  86.             return command;  
  87.         } catch (Exception e) {  
  88.             //若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出”錯誤命令異常“  
  89.             console = new Scanner(System.in);  
  90.             //返回-1  
  91.             return -1;  
  92.         }  
  93.     }  
  94. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章