Search in ArrayList

I need to create a library system in bluej, and it should be able to search for a book. However, I have a problem. When I try to find a book, the result is not always available for books ... How can I sort so that the result shows that the book is available?

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;
}
+4
source share
3 answers

if you use java 8 you can use stream

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

you can also use parallelStream()insteadstream()

+1
source

-, , , , ( reset " " ) , .

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

, - , "", , .

, , , :

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
}

- :

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
}
+5

@amit .

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

. " getTitle()" ".

:

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



}

Here is my code:

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


}

By the way, the logical value is not currently used.

-1
source

All Articles