ITPub博客

首页 > IT职业 > IT生活 > Books System design

Books System design

IT生活 作者:qingcai 时间:2021-09-09 09:10:27 0 删除 编辑

//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 

}

//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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2899/viewspace-2799859/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-19

  • 博文量
    71
  • 访问量
    681