简体   繁体   中英

MOOC JAVA part 1 EXERCISE 101 (search functionality). Not getting the desired output

So the exercise is: "Add to the class Library the methods public ArrayList searchByTitle(String title), public ArrayList searchByPublisher(String publisher) and public ArrayList searchByYear(int year). The methods return the list of books that match the given title, publisher or year."

We have to use contains() method. This is what I have done so far

public class Book {
    private String title;
    private String publisher;
    private int year;
    
    
    
    
    public Book(String title, String publisher, int year){
        this.title=title;
        this.publisher=publisher;
        this.year=year;
    }
    
    public String title(){
        return this.title;
    }
    public String publisher(){
        return this.publisher;
    }
    
    public int year(){
        return this.year;
    }
    
    public String toString(){
        return this.title+", "+ this.publisher+ ", "+this.year;
    }
}

import java.util.ArrayList;

public class Library {

    private ArrayList<Book> books;
    
    
    public Library(){
        this.books=new ArrayList<Book>();
    }
    
    public void addBook(Book newBook){
        this.books.add(newBook);
    }
    
    public void printBooks(){
        for(Book book: books){
            System.out.println(book);
        }
    }
    
    public ArrayList<Book> searchByTitle(String title){
        ArrayList<Book> found=new ArrayList<Book>();
        for(Book book: books){
            if(books.contains(title)){
                found.add(book);
            }
        }
        
        
        return found;
    }
    
    public ArrayList<Book> searchByPublisher (String publisher){
        ArrayList<Book> found=new ArrayList<Book>();
        for(Book book: books){
            if(books.contains(publisher)){
                found.add(book);
            }
        }
        
        
        return found;
    
    }
    
    public ArrayList<Book> searchByYear(int year){
        ArrayList<Book> found=new ArrayList<Book>();
        for(Book book: books){
            if(books.contains(year)){
                found.add(book);
            }
        }
        
        
        return found;
        
    }

}

import java.util.ArrayList;


public class Main {

    public static void main(String[] args) {
        // test your program here

        Library Library = new Library();

        Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
        Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
        Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
        Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));

        ArrayList<Book> result = Library.searchByTitle("Cheese");
        for (Book book : result) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : Library.searchByPublisher("Penguin Group  ")) {
            System.out.println(book);
        }

        System.out.println("---");
        for (Book book : Library.searchByYear(1851)) {
            System.out.println(book);
        }

    }
}

Whats wrong with my search methods? The only thing it prints is "---". Any suggestions how to do this. I am a beginner in programming, and they want me to use ArrayList<Book> found in the search methods.

if(books.contains(year)) , if(books.contains(title)) and if(books.contains(publisher)) will never be true because books is an ArrayList that contains instances of Book class.

You probably meant to do the following:

search by year

if(book.year() == year) {
   found.add(book);
}

search by publisher

if(book.publisher().equals(publisher)) {
   found.add(book);
}

search by title

if(book.title().equals(title)) {
   found.add(book);
}

In search by title functionality, you could also use .contains() method if you not only want matching titles but also those book titles which contain title as a sub-string.

if(book.title().contains(title)) {
   found.add(book);
}

Example:

public static void main(String args[]) {
   String str = "In Search of Lost Tim";
  
   System.out.println(str.contains("In Search of Lost Tim"));   // true
   System.out.println(str.contains("Lost Tim"));                // true
   System.out.println(str.contains("In Search"));               // true
}

In your methods searchByTitle , searchByPublisher and searchByYear , you are searching for the string in the Books list instead of your book object. It should be

public ArrayList<Book> searchByTitle(String title){
    ArrayList<Book> found=new ArrayList<Book>();
    for(Book book: books){
        if(book.title().contains(title)){
            found.add(book);
        }
    }
    
    
    return found;
}

Similarly for the other methods.For searching by year, you can do a straight up ==

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM