继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

图书馆借书系统-Java异常的学习和处理

慕粉3848213
关注TA
已关注
手记 5
粉丝 5
获赞 110
  • 定义了三个类
  • ErrorCodeException.java

public class ErrorCodeException extends Exception {

    public ErrorCodeException(){
    }

    public ErrorCodeException(String message){
        super(message);
    }
}
  • NoExitBookException.java

public class NoExitBookException extends Exception {

    public NoExitBookException() {
    }

    public NoExitBookException(String message) {
        super(message);
    }
}
  • BorrowBookSystem.java

public class BorrowBookSystem {

    String[] library = {"Java", "Python", "javascript", "Android", "iOS", "Linux", "Unix", "C", "C++", "C#", "Ruby", "Django", "Flask"};

    public static void main(String[] args) {
        BorrowBookSystem system = new BorrowBookSystem();
        system.borrow();
    }

    public void borrow() {
        while (true) {
            int command = 0;
            try {
                command = command();
            } catch (ErrorCodeException e) {
                System.out.println(e.getMessage());
            }
            if (command == 1) {
                try {
                    queryBookName();
                } catch (NoExitBookException e) {
                    System.out.println(e.getMessage());
                }
            } else if (command == 2) {
                try {
                    queryBookCode();
                } catch (ErrorCodeException e) {
                    System.out.println(e.getMessage());
                } catch (NoExitBookException e) {
                    System.out.println(e.getMessage());
                }
            }

        }
    }

    public int command() throws ErrorCodeException {
        int result;
        System.out.println("输入查询命令:1-按照图书名称查询,2-按照图书序列号查询。");
        Scanner scanner = new Scanner(System.in);
        String command = scanner.next().trim();
        if (command.equals("1")) {
            result = 1;
        } else if (command.equals("2")) {
            result = 2;
        } else {
            throw new ErrorCodeException("命令输入错误!请根据提示输入数字命令!");
        }
        return result;
    }

    private void queryBookCode() throws ErrorCodeException, NoExitBookException {
        System.out.println("输入图书序列号:");
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next().trim();
        int number;
        try {
            number = Integer.parseInt(next);
        } catch (Exception e) {
            throw new ErrorCodeException("命令输入错误!请根据提示输入数字命令!");
        }
        try {
            String bookName = library[number];
            System.out.println("book:" + bookName);
        } catch (Exception e) {
            throw new ErrorCodeException("图书不存在!");
        }
    }

    private void queryBookName() throws NoExitBookException {
        System.out.println("输入图书名称:");
        Scanner scanner = new Scanner(System.in);
        String next = scanner.next().trim();
        boolean result = false;
        for (int i = 0; i < library.length; i++) {
            if (library[i].equals(next)) {
                System.out.println("book:" + library[i]);
                result = true;
                break;
            }
        }
        if (!result) {
            throw new NoExitBookException("图书不存在!");
        }
    }
}
打开App,阅读手记
22人推荐
发表评论
随时随地看视频慕课网APP

热门评论

大哥,你的这个手记中分单元怎么实现的?我的程序从eclipse拉出来后直接默认左对齐了。。。。

查看全部评论