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

Books System design

标签:
Java

//Define a Books Class;

package com.imooc;
public class Books {
public String name;
public int num;

public Books(int num, String name){
    this.num = num;
    this.name = name;
}

}

//Define a LibrarySystem;

package com.imooc;

import java.util.Scanner;

public class LibrarySystem {
//Step1. Declare an array of book (named books);
Books[] books = new Books[10];

public static void main(String[] args) throws Exception {
    LibrarySystem system = new LibrarySystem();
    system.start();
}

public void start() throws Exception {

    *//Step2. Initialise the array of books;*
    fillbooksArray(books);

    *//Step3. Asking user to choose a way to search the book*
    UserInput01();
}

private void fillbooksArray(Books[] books) {
    books[0] = new Books(1,"Glen story");
    books[1] = new Books(2,"Jane");
    books[2] = new Books(3,"Human-being history");
    books[3] = new Books(4,"Sorting");
    books[4] = new Books(5,"Cooking");
    books[5] = new Books(6,"How to write Java");
    books[6] = new Books(7,"Economy");
    books[7] = new Books(8,"Education method");
    books[8] = new Books(9,"Human-being mind");
    books[9] = new Books(10,"History");

}

private void UserInput01() throws Exception {
    try {
        System.out.println("1.Searching according the name;   2.Searching according the index");
        Scanner scan = new Scanner(System.in);
        int Input = scan.nextInt();
        if(Input==1){
            InputName();
        }else if(Input==2){
            InputIndex();
        }else{
            System.out.println("Please choose number1 or number2");
            UserInput01();
        }
    } catch (Exception e) {
        System.out.println("Your input is wrong, please follow the instruction to choose again");
        UserInput01();
    }
}

private void InputName() throws Exception {
    try {
        System.out.println("Please enter the Name of the Book~");
        Scanner scan = new Scanner(System.in);
        String Input01 = scan.next();
        boolean isMatch = false;
        for (int i = 0; i < books.length; i++) {
            if(Input01.equals(books[i].name)){
                System.out.println("the book name is: " + books[i].name + "    the Index of book is: " + books[i].num);
                isMatch = true;
            }
        }
        if(isMatch == false){
            System.out.println("We cannot find this book, please enter the name of the book again~");
            InputName();
        }
    } catch (Exception e) {
        System.out.println("Sorry, the type of name is wrong, please do it again");
        InputName();
    }
}

private void InputIndex() throws Exception {
    try {
        System.out.println("Please enter the Index of the Book~");
        Scanner scan = new Scanner(System.in);
        int Input02 = scan.nextInt();
        boolean isMatch = false;
        for (int i = 0; i < books.length; i++) {
            if(Input02 == books[i].num){
                System.out.println("the book name is: " + books[i].name + "    the Index of book is: " + books[i].num);
                isMatch = true;
            }
        }
        if(isMatch == false){
            System.out.println("We cannot find this book, please enter a interger between 1-10");
            InputIndex();
        }
    } catch (Exception e) {
        System.out.println("sorry, your input is wrong, do it again");
        InputIndex();
    }
}

}

//Run it:

1.Searching according the name; 2.Searching according the index
2
Please enter the Index of the Book~
a
sorry, your input is wrong, do it again
Please enter the Index of the Book~
13
We cannot find this book, please enter a interger between 1-10
Please enter the Index of the Book~
11
We cannot find this book, please enter a interger between 1-10
Please enter the Index of the Book~
8
the book name is: Education method the Index of book is: 8

Process finished with exit code 0

点击查看更多内容
TA 点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消