如何在Java库项目中查看书籍和搜索书籍?

时间:2017-04-29 17:59:49

标签: java

我正在为我的Java编程类开发一个Library系统项目。我需要能够按作者或标题搜索Book数组中的书籍列表。另外,我还想看看如何查看系统中的书籍。我将不胜感激任何帮助或见解。 searchBooks和checkoutBook方法位于底部。再次感谢!

此致 月亮

package Project;
import java.util.ArrayList;
import java.util.Scanner;

public class Library {

static Scanner input = new Scanner(System.in);
ArrayList<Book> booklist = new ArrayList<Book>();
ArrayList<Users> user = new ArrayList<Users>();
static int running = 0;
String booksearch;
public static void main(String[] args)  {
    (new Library()).run();
    }
    public void run() {
        while(true){
            System.out.println("********************Welcome to the Public Library!********************");
            System.out.println("              Please Select From The Following Options:               ");
            System.out.println("**********************************************************************");


            System.out.println("1: Create a new account");
            System.out.println("2: Admin login");
            System.out.println("3: User login");
            System.out.println("0: Exit");  
            int option = input.nextInt();
            switch (option) {
            case 1: newAccount();  break;
            case 2: admin(); break;
            case 3: userLogin();   break;
            case 0: System.exit(option); break;
            case 5: return;
            default: System.out.println("Wrong option, try again.");

        }


    }

}
    public void addBook(){
        Book book1 = new Book("Game of Thrones","Fantasy","George RR Martin","a book about dragons",9001,5);
        Book book2 = new Book("Harry Potter","Adventure","JK Rowling","a boy goes to a magical school",23454,3);
        Book book3 = new Book("Carrie","Horror","Stephen King","a girl goes crazy",3332,15);
        Book book4 = new Book("Dragonaball","Action Manga","Akira Toriyama","a boy with power fights evil",27894,2);
        booklist.add(book1);
        booklist.add(book2);
        booklist.add(book3);
        booklist.add(book4);

    }

    private void userLogin() {


                Scanner keyboard = new Scanner (System.in);
                System.out.println("Enter your username ");
                String user = keyboard.nextLine();
                System.out.println("Enter your password ");
                String pass = keyboard.nextLine(); // looks at selected file in scan


                if (user.equals("olumide1") && pass.equals("umbc")) {
                    System.out.println("********************Welcome Home, User!********************");
                    System.out.println("              Please Select From The Following Options:               ");
                    System.out.println("**********************************************************************");
                    System.out.println("1: checkout a book:");
                    System.out.println("2: search a book:");
                    System.out.println("3: exit library:");
                    int option = input.nextInt();
                    switch(option){
                    case 1: checkOutBook(); break;
                    case 2: searchBooks(); break;
                    case 3: System.exit(0);
                    }


                } else {
                    System.out.print("Try again");
                }
            }

    public void searchBooks() {
        System.out.println("Which book are you searching for today?");
        booksearch = input.nextLine();

         }

    private void checkOutBook() {
    }

3 个答案:

答案 0 :(得分:0)

要搜索图书,只需遍历您的<input type='text' placeholder='Otel adını veya Bölgeyi yazmaya başla' class='flexdatalist'> <span></span> 并按标题或作者搜索即可。另外,我建议您的bookList方法应返回searchBooks()个实例:

Book

}

要跟踪已签出/免费的图书,只需创建一个新的public Book searchBooks() { System.out.println("Which book are you searching for today?"); booksearch = input.nextLine(); for (Book book : booklist) { if (book.getTitle().contains(booksearch) || book.getAuthor().contains(booksearch) { return book; } } return null; //no book was found 已签出的图书List。您checkedOutBooks方法应接受checkOutBook参数:

Book

答案 1 :(得分:0)

对于搜索工具方法,您可以使用hashmap,google并花一些时间在其上。请提供结帐手册的详细信息,以及需要在其中完成的工作。

答案 2 :(得分:0)

创建2个Pojo&#39>

debugger;
function greet(who) { 
debugger;
  console.log("Hello " + who);
}
debugger;
greet("Harry");
debugger;
console.log("hi");  
console.log("bye");

使用3个地图初始化课程

     Class Book
    {
    int id;
    int quantity;
     String bookName;
    Author author;
    } 

    Class Author
    {
    int authorId;
    String authorName;
    List<Integer>bookIdsList;
    }

所以2 TreeMap将帮助您进行搜索 和HashMap将帮助您结账。 如果你想要一个前缀搜索,你可以使用TRIE结构,但你必须实现。