ArrayList图书搜索

时间:2015-03-23 11:38:50

标签: java arrays search arraylist bluej

我在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;
}

3 个答案:

答案 0 :(得分:5)

首先,在您的代码中 - 只有最后一本书很重要,如果您找到合适的书籍,则需要中断,因为无需检查提醒(并将值重置为&#34;未找到任何书籍和# 34;)找到一本书后。

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 ");
    }
}

如果您的收藏是空的并且您搜索了一本书(显然不存在),则上述剧照会失败。
您可以通过避免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
    }
}

这样你就开始悲观了 - 这本书不存在,你改变主意#34;如果你以后找到它,并停止这个结果。

上面仍然缺少你实际想要的标题,这可以通过添加它作为参数并寻找它来实现:

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
}

答案 1 :(得分:1)

如果您使用的是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()

答案 2 :(得分:-1)

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

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

代码。我收到的错误是&#34;非静态方法&#39; getTitle()&#39;不能从静态上下文引用。&#34;

这是我的图书代码:

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 ";
}


}

顺便说一句,此刻没有使用布尔值。