手记

java 第三季模拟借书系统

package Exception_01;

public class CommandError extends RuntimeException {

    public CommandError() {
        // TODO Auto-generated constructor stub
    }

    public CommandError(String arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }
}
package Exception_01;

public class BookIsNotExist extends RuntimeException {

    public BookIsNotExist() {
        // TODO Auto-generated constructor stub
    }

    public BookIsNotExist(String arg0) {
        super(arg0);
        // TODO Auto-generated constructor stub
    }
}
/*
 * 要求:
 * 1、定义字符串数组保存图书信息
 * 2、提示用户输入,分别按”书名“和”图书序号“查找图书
 * 3、根据输入信息进行适当的异常处理
 *     a、如果输入类型错误,抛出”错误命令异常“,并提示重新输入;
 *     b、如果书名不存在,抛出”图书不存在异常“,并提示重新输入;
 *     c、如果图书序号超过字符串数组范围,抛出”图书不存在异常“,
 *     并提示重新输入
 *
 * */
package Exception_01;

import java.util.Scanner;

public class ExceptionDemo {

    private static Scanner console =new Scanner(System.in);

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String[] books = {"华夏地理","中国地图","麻辣香锅","海底捞","一个演员的自我修养","黑客帝国"};

        while(true){
            System.out.println("请输入:1-按照名称查找,2-按照序号查找");
            String Book;
            try {
                int command = inputCommand();
                switch(command){
                case 1:
                    Book=getBookByName(books);
                    System.out.println("<<"+Book+">>,还没人借!");
                    break;
                case 2:
                    Book=getBookByNumber(books);
                    System.out.println("<<"+Book+">>,还没人借!");
                    break;
                default:
                    System.out.println("命令输入错误!");
                    continue;
                }
                break;
            } catch (CommandError ex) {
                System.out.println(ex.getMessage());
                continue;
            }catch(BookIsNotExist bex){
                System.out.println(bex.getMessage());
                continue;
            }
        }
    }

    private static String getBookByNumber(String[] books) throws BookIsNotExist{

        while(true){
            System.out.println("输入图书序号:");
            try {
                int index = inputCommand();
                if(index>=0&&index<books.length)
                    return books[index];
                else{
                    System.out.println("命令输入错误!请根据提示输入数字命令!");
                    continue;
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                Exception bookNotExists = new Exception("图书不存在!");
                bookNotExists.initCause(e);
            }
        }
    }

    private static String getBookByName(String[] books) throws BookIsNotExist{
        System.out.println("请输入图书名称:");
        String name= console.next();
        for(String b1:books){
            if(name.equals(b1))
                return b1;
        }
        throw new BookIsNotExist("图书不存在!");
    }

    //获取控制台命令
    private static int inputCommand() {

        int command;
        try {
            command = console.nextInt();
            return command;
        } catch (Exception e) {
            console = new Scanner(System.in);
            return -1;
        }
    }

}
0人推荐
随时随地看视频
慕课网APP