问答详情
源自:1-9 经验总结

今日案例 :模拟借书系统 (异常处理练习)

代码 :
package borrowBook;
 import java.util.Scanner;
 
 public class BorrowBook  {
     private Book []books = {new Book("数据结构",1),
                             new Book("高数",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("命令输入错误!请根据提示输入数字命令!");
             }
             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("命令输入错误!请根据提示输入数字命令!");
                 }
                 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);
     }
 }

截图 :

http://img2.mukewang.com/61ebbb780001766908150510.jpg

http://img4.mukewang.com/61ebbb780001194e08150508.jpg




提问者:心形罐装病毒 2022-01-22 16:08

个回答

  • EddieAQ
    2022-06-30 19:35:41

    牛啊,膜拜