ArrayList 图书搜索

ArrayList Book Search

我要在 bluej 中创建一个图书馆系统,它必须能够搜索一本书。但是,我有一个问题。当我尝试搜索一本书时,结果总是没有可用的书...我该如何排序以便结果显示该书可用?

private List<Book> collection;

public Library()
{
    collection = new ArrayList<Book>();
}

public void addBook(Book book)
{
    collection.add(book);
}

public String titleSearch()
{
    String titleSearch = "\n ";
    for(int i = 0; i < collection.size(); i++){
        if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){

            titleSearch = ("\n Book Avaliable");

        }else{
            titleSearch = ("\n No Books Avaliable ");
        }
    }
    return titleSearch;
}

首先,在您的代码中 - 只有最后一本书很重要,如果找到合适的书就需要中断,因为不需要检查提醒(并将值重置为 "no book found")找到一本书后。

for(int i = 0; i < collection.size(); i++){
    if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
        titleSearch = ("\n Book Avaliable");
        break; //<- added a break here, no need to go on iterating and reset titleSearch later on

    }else{
        titleSearch = ("\n No Books Avaliable ");
    }
}

如果您的 collection 是空的并且您搜索一本书(显然不存在),则上述剧照将失败。
您可以通过避免 else:

来解决它并改进解决方案
titleSearch = ("\n No Books Avaliable ");
for(int i = 0; i < collection.size(); i++){
    if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
        titleSearch = ("\n Book Avaliable");
        break; //<- added a break here
    }
}

这样你开始悲观 - 这本书不在那里,你 "change your mind" 如果你后来找到它,并以这个结果停止。

上面仍然缺少您实际要查找的标题,可以通过将其添加为参数并查找来实现:

public String searchTitle(String titleSearch) {
    if (titleSearch == null) return "\n No Books Avaliable ";
    for(int i = 0; i < collection.size(); i++){
        if(titleSearch.equalsIgnoreCase(collection.get(i).getTitle())){
            return "\n Book Avaliable";
        }
    }
    return "\n No Books Avaliable "; //reachable only if no book found
}

最后一点,就是使用增强的 for each loop:

public String searchTitle(String titleSearch) {
    if (titleSearch == null) return "\n No Books Avaliable ";        
    for(Book b : collection){
        if(titleSearch.equalsIgnoreCase(book.getTitle())){
            return "\n Book Avaliable";
        }
    }
    return "\n No Books Avaliable "; //reachable only if no book found
}

如果你正在使用 Java 8 你可以使用流

public String searchTitle(String titleSearch) {

    if(collection.stream().anyMatch(book->{return titleSearch.equalsIgnoreCase(book.getTitle());})){
        return "\n Book Avaliable";
    }
    else{
        return "\n No Books Avaliable";
    }
}

您也可以使用 parallelStream() 代替 stream()

@amit 我正在使用你的代码。当我使用

if(titleSearch.equalsIgnoreCase(Book.getTitle()))

代码。我收到一条错误消息 "Non-static method 'getTitle()' cannot be referenced from a static context."

这是我的图书代码:

class Book {
// instance variable
private String title;
private String author;
private String genre;
private String ISBN;
private boolean isCheckOut;


private static int counter;

Book(String _title) {
    this.title = _title;
    counter++;
}

/* Get Methods */

public static int getNumOfInstances() {
    return counter;
}

    String getTitle() {
    return this.title;
}

String getAuthor() {
    return this.author;
}

String getGenre() {
    return this.genre;
}

String getISBN() {
    return this.ISBN;
}

/* All Methods for setting Book */

void setAuthor(String _author) {
    this.author = _author;
}

void setGenre(String _genre) {
    this.genre = _genre;
}

void setISBN(String _isbn) {
    this.ISBN = _isbn;
}



}

这是我的货架代码:

import java.util.ArrayList;
import java.util.List;

public class Shelf {

// instance variable
private String name;
private String genre;

private static int counter;
public ArrayList<Book> books = new ArrayList<Book>();

Shelf(String _name) {
    this.name = _name;
    counter++;
}

static int getNumOfInstances() {
    return counter;
}

String getName() {
    return this.name;
}

String getGenre() {
    return this.genre;
}

Book getBook(int index) {
    return books.get(index);
}

int getBookSize() {
    return books.size();
}

List<Book> getBooks() {
    return this.books;
}

void setName(String _name) {
    this.name = _name;
}

void setGenre(String _genre) {
    this.genre = _genre;
}

void addBook(Book _book) {
    books.add(_book);
}


public String searchTitle(String titleSearch) {
    if (titleSearch == null) return "\n No Books Avaliable ";
    for(Book b : books){
        if(titleSearch.equalsIgnoreCase(Book.getTitle())){
            return "\n Book Avaliable";
        }
    }
    return "\n No Books Avaliable ";
}


}

顺便说一下,目前没有使用布尔值。