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

来源:1-9 经验总结

心形罐装病毒

2022-01-22 16:08

代码 :
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




写回答 关注

1回答

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

    牛啊,膜拜

Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409792 学习 · 4340 问题

查看课程

相似问题