为了账号安全,请及时绑定邮箱和手机立即绑定

图书查询系统(java异常学习)

标签:
Java

图书类:

public class Book {
    String name = "";
    int id = -1;
    public Book (int id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public int getId() {
        return id;
    }
}

图书异常类:

public class BookException extends Exception {
        public BookException () {}
        public BookException (String message) {
            super (message);
        }
}

函数体:

import java.util.*;

public class Initial {
    public int getCommand () throws BookException {
        Scanner input = new Scanner (System.in);
        int command;
        try {
            command = input.nextInt(); 
        } catch (Exception e) {
            throw new BookException("错误命令异常");
        }
        if (command < 0  command >2) {
            throw new BookException("错误命令异常");
        }
        return command;
    }

    public Book searchFromName  (Book books[]) throws BookException {
        Scanner input = new Scanner (System.in);
        System.out.println ("请输入图书名称:");
        String name = input.next();
        for (int i = 0; i < books.length; i++) {
            if (books[i].getName().equals(name)) {
                return books[i];
            }
        }
        throw new BookException ("图书不存在异常");
    }

    public Book searchFromID  (Book books[]) throws BookException {
        Scanner input = new Scanner (System.in);
        System.out.println ("请输入图书序号:");
        int id;
        try {
            id = input.nextInt();
        } catch (Exception e) {
            throw new BookException("错误命令异常");
        } 
        for (int i = 0; i < books.length; i++) {
            if (books[i].getId() == id) {
                return books[i];
            }
        }
        throw new BookException ("图书不存在异常");
    }

    public void startSearch () {
        Book books [] = {new Book (1, "高数"),
                new Book (2, "线代"),
                new Book (3, "C语言"),
                new Book (4, "Java"),};
        int command;
        Book target;
        while (true) {
            System.out.println ("输入命令:0-退出;1-按照名称查找图书;2-按照序号查找图书");
            try { 
                command = getCommand(); 
            } catch (Exception e) {
                System.out.println (e.getMessage());
                continue;
            }
            switch (command) {
            case 0:
                return;
            case 1:
                try {
                    target = searchFromName (books);
                } catch (Exception e) {
                    System.out.println (e.getMessage());
                    continue;
                }
                System.out.println ("查找成功!图书序号:"+target.getId()+"\t图书名称:" +target.getName());
                break;
            case 2:
                try {
                    target = searchFromID (books);
                }  catch (Exception e) {
                    System.out.println (e.getMessage());
                    continue;
                }
                System.out.println ("查找成功!图书序号:"+target.getId()+"\t图书名称:" +target.getName());
                break;
            }   
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Initial init = new Initial ();
        System.out.println ("******欢迎进入图书查询系统!******");
        init.startSearch();
        System.out.println ("******谢谢使用,再见!******");
    }

}
点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消