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

Books System design

qq_孔洁钰May_03759002
关注TA
已关注
手记 2
粉丝 5
获赞 1

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

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