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

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

标签:
Java

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();
        }
    }
}
点击查看更多内容
5人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消