手记

Java入门第三季,模拟借书系统

使用构造方法的重载来判断不同的BookException

package com.book;

public class BookException extends Exception{
    String message;
    public BookException (){
        message="命令输入有错!请根据提示输入数字命令!";
    }
    public BookException (String name){
        message="图书不存在!";
    }
    public BookException (int order){
        message="图书不存在!";
    }

}
package com.book;

public class Book {
    /*指令输入错误,不是1也不是2,抛出输入异常*/
    public int select(int order) throws BookException{
        boolean b1=(order!=1);
        boolean b2=(order!=2);
        if(b1&&b2)
            throw new BookException();
        return order;
    }
    /*书名输入错误,检索不到书名,抛出图书不存在异常*/
    public String jugde(String name,String []books) throws BookException{
        String bookname = null;//方法返回值
        boolean flag=true;
        for(String book:books){//遍历图书
            if(name.equals(book)){     //书名相同,
                flag=false;    
                bookname=book; //返回书名
                break;         //中断循环
            }
        }
        if(flag){              //flag为true,则表明找不到图书
            BookException be=new BookException(name);
            bookname=be.message;//把异常信息返回
            throw be;
            }
        return bookname;
    }
    /*序号输入错误,检索不到对应数组的下标,抛出图书不存在异常*/
    public String jugde(int order,String []books) throws BookException{
        String bookname=null;
        int i=-1;
        boolean b=(order<=books.length)&&(order>=1);//输入的序号满足条件
        //数组下标相对order偏移了-1,即i=order-1;
        if(b){//满足条件,根据序号查书,返回书名
                i=order-1;
                bookname=books[i];              
        }else{
                BookException be=new BookException(order);
                bookname=be.message;//把异常信息返回
                throw be;
                }
        return bookname;
    }
}

Test类,程序的入口

package com.book;

import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String []books={"数据结构","高数","大学英语","离散数学" };
        Book book=new Book();
        Scanner scan=new Scanner(System.in);
        while(true){
        System.out.println("输入命令:1-按照书名查找图书;2-按照序号查找图书;");
        int input=scan.nextInt();
        try {
            int order=book.select(input);//会发生异常,执行到if说明没有报出异常
            if(order==1){
                System.out.println("输入图书名称:");
                String inputBookName=scan.next();
                String name=book.jugde(inputBookName,books);
                System.out.println("book:"+name);
                return;
            }else if(order==2){
                System.out.println("输入图书序号:");
                int number=scan.nextInt();
                String name=book.jugde(number,books);
                System.out.println("book:"+name);
                return;
            }

        } catch (BookException e) {
        //  e.printStackTrace();
            System.out.println(e.message);
        } catch (Exception     e){
        //  e.printStackTrace();
            System.out.println("其他异常");
        }
      }
    }
}
5人推荐
随时随地看视频
慕课网APP