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

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

zyj9527
关注TA
已关注
手记 2
粉丝 0
获赞 20

图书类:

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 ("******谢谢使用,再见!******");
    }

}
打开App,阅读手记
9人推荐
发表评论
随时随地看视频慕课网APP