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

Java入门第三季 第一章练习 模拟借书系统

BePureBeTrue
关注TA
已关注
手记 1
粉丝 1
获赞 11

1.Book类

class Book {
    final int id;
    final String name;
    public Book(int id,String name){
        this.id = id;
        this.name = name;
    }
}

2.bookUnfoundException类

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

3.commandNotFoundException类

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

4.Main类

import java.util.*;
public class Main {
    private final Book[] library = {new Book(1, "高等数学"), new Book(2, "线性代数"), new Book(3,"大学英语"), new Book(4, "C程序设计"), new Book(5, "Java程序设计"), new Book(6, "市场营销学")};

    public static void main(String[] args) throws commandNotFoundException, bookUnfoundException {
        Main main = new Main();
        main.searchEngine();

    }
    private void searchEngine() throws commandNotFoundException, bookUnfoundException {
        Main main = new Main();
        System.out.println("*********欢迎您使用借书系统*********");
        System.out.println("本系统可实现以下功能");
        System.out.println("1.按照名称查询图书");
        System.out.println("2.按照序号查找图书");
        System.out.print("请输入您要使用的功能:");
        int choose = main.getChoose();
        switch (choose) {
            case 1: {
                main.sortByName();
                break;
            }
            case 2: {
                main.sortByID();
                break;
            }
        }
    }

    private int getChoose() throws commandNotFoundException, bookUnfoundException {
        Main main = new Main();
        Scanner input = new Scanner(System.in);
        try {
            int choose = input.nextInt();
            if(choose != 1 && choose != 2) {
                throw new commandNotFoundException();
            }
            return choose;
        }catch(InputMismatchException e){
            System.out.println("命令输入错误(您需要输入数字),请重新输入");
            main.searchEngine();

        }catch(commandNotFoundException e){
            System.out.println("命令输入错误(命令不存在),请重新输入");
            main.searchEngine();
        }
        return 0;
    }

    private void sortByName() throws bookUnfoundException, commandNotFoundException {
        Main main = new Main();
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您要查询的书籍名称:");
        try {
            String search = input.next();
            int i;
            for(i = 0; i < library.length;i++) {
                if (search.equals(library[i].name)) {//比较两个字符串不能使用==
                    break;
                }
            }
            if(i == library.length){
                throw new bookUnfoundException();
            }
            System.out.println("已找到您需要的图书");
            System.out.println("序号:"+library[i].id);
            System.out.println("名称:"+library[i].name);
            System.out.println("谢谢您的使用!");
            main.searchEngine();
        } catch (bookUnfoundException e) {
            System.out.println("很抱歉,没有找到您需要的书籍。");
            main.searchEngine();
        }
    }

    private void sortByID() throws bookUnfoundException,commandNotFoundException{
        Main main = new Main();
        Scanner input = new Scanner(System.in);
        System.out.print("请输入您要查询的书籍的序号:");
        try {
            int choose = input.nextInt();
            if(choose > library.length){
                throw new bookUnfoundException();
            }
            System.out.println("已找到您需要的图书");
            System.out.println("序号:"+library[choose-1].id);
            System.out.println("名称:"+library[choose-1].name);
            System.out.println("谢谢您的使用!");
            main.searchEngine();

        }catch(InputMismatchException e){
            System.out.println("您的输入有误(您需要输入数字),请重新输入");
            main.searchEngine();

        }catch(bookUnfoundException e){
            System.out.println("很抱歉,没有找到您需要的书籍。");
            main.searchEngine();
        }
    }
}
打开App,阅读手记
5人推荐
发表评论
随时随地看视频慕课网APP

热门评论

我有点愚见。。就是我个人觉得在定义Book的时候加上get和set和方法后面取值会方便很多

查看全部评论