0

im confused with a question which i was given, which is about bookshelf program. I have to create 2 classes called Book and BookShelf

I have more or less completed the Book class which contains these variables (id, author and title) with ( get, set , toString and constructors methods)

However for the BookShelf class, I am kind of clueless whether what i have done is correct

Here is what I am supposed to do at the BookShelf class

  • Create an addBook method, it takes in book object as input and adds the object into the bookshelf

  • returnBooks method, no parameter / arguments needed just return an arraylist of books in order

  • returnAuthorBooks, takes in author as input and returns an arraylist of books by the author

This is the code that i have done

import java.util.ArrayList;  
import java.util.Collections;

public class BookShelf {
    ArrayList<Book> listOfBooks = new ArrayList<Book>();

    public void addBook(Book getTitle){
        listOfBooks.add(getTitle);
    }

    public ArrayList<Book> returnBooks(){

        ArrayList myBook = new ArrayList();
        Collections.sort(myBook);
        return myBook;
    }

    public ArrayList<Book> returnAuthor(Book author){

        for (Book books : listOfBooks){
            if (author.getAuthor() == books.getTitle()){
                return listOfBooks;
            }
        }
        return null;
    }
}

Would like to clarify if there is any mistake here as for some reason i get the feeling that i have done something incorrect

Rind
  • 175
  • 1
  • 3
  • 14
  • This is borderline off-topic as it would be more suitable for http://codereview.stackexchange.com/ However take a close look at `returnAuthor`. For one, why are you passing a `Book` and not just a String with the author name, don't use `==` for string equality check, and you are returning the entire book list, not just the list of books by that author. Returning `null` is a bad habit as well. – Mark Rotteveel Nov 14 '15 at 11:35
  • @MarkRotteveel I'm not sure about that. codereview is for review of code that works. I'm afraid this code doesn't qualify. – Eran Nov 14 '15 at 11:39
  • @Eran You might be right, but in the absence of actual specific questions it sounds more like a request for code review; anyway, it is also off topic here. – Mark Rotteveel Nov 14 '15 at 11:41
  • To make you code easier to read, you could name your method parameters `book`. For example, a variable name of `author` makes someone reading the code think that may be a string or an Author class. Similarly for `getTitle`, that's more a method name than a variable name, but don't name it just `title` because it's a Book object. – OneCricketeer Nov 14 '15 at 12:57

1 Answers1

1

Your returnBooks method returns an empty List. You should change

ArrayList myBook = new ArrayList();

to

ArrayList<Book> myBook = new ArrayList(listOfBooks);

In order to return a full sorted list.

Your returnAuthor method has several problems :

  1. It should take an author as input (I don't know if it should be a String or whether there is an Author class).
  2. You compare the book's authors with == instead of equals (see this question).
  3. You return the entire list of books if you find a book with the author you are looking for. You should only return the books of that author.
Community
  • 1
  • 1
Eran
  • 387,369
  • 54
  • 702
  • 768
  • @Turing85 Thanks for the edit, but I don't think `books'` was a typo (I'm not a native English speaker, and it has been many years since I finished learning English in high school, so I may be wrong). When you want to add `'s` (I forgot the name of that suffix) to a word that already ends with `s` (as books), you just add `'`. Your fix would be correct if I wanted to say the OP was comparing the authors of a single book. I wanted to say OP was comparing the authors of multiple books. – Eran Nov 14 '15 at 11:47
  • actually, I just changed an \` to an ', you ar probably right (my original intend was to add the link to the String comparison question). – Turing85 Nov 14 '15 at 15:38