views:

51

answers:

2

I am trying to get an author from the BOOK class that extends the Item class.

I have 3 classes as follows

  1. main()
  2. library
  3. Item(CD,DVD,Book) inheritance..

in library class i have a function called

public Collection booksByAuthor(String author)

i also have a hashset in library class that holds information on various books. What i want to do is search for a particular author.

Problem is how can i get the author from the book class that extends the item class?

here is the function in the library class..

public class Library
{
private Set<Item> theCDs = new HashSet<Item>();
private Set<Item> theDVDs = new HashSet<Item>();
private Set<Item> theBooks = new HashSet<Item>();


public Collection<Item> booksByAuthor(String author)
{
    Set<Item> key = new HashSet<Item>();


    //Book b = new Book();


     for(Item s : theBooks)
     {

         if(s.getAuthor().equals(author)) //cannot find getauthor()
           {
           key.add(s);
           break;
        }

    }

    return key;
}

here is the Items class.. i do have a getAuthor() in the book class!!!

 public class Item extends Object
{

private String title;
private String [] keywords;


public String toString()
{

String line1 = "title:    " + title + "\n" + "keywords: " + Arrays.toString(keywords);
return line1;
}


public void print()
{

System.out.println(toString());

}


public Item()
{

}

public Item(String theTitle, String... theKeyword)
{

this.title = theTitle;
this.keywords = theKeyword;

}

public String getTitle()
{
return title;
}

public String [] getKeywords()
{

    return keywords;

}


}

class CD extends Item
{

private String artist;
private String [] members;
private int number;





public CD(String theTitle, String theBand, int Snumber, String... keywords)
{
    super(theTitle, keywords);
    this.artist = theBand;
    this.number = Snumber;

}


  public void addband(String... member)
{
    this.members = member;

}

public String getArtist()
{

    return artist;

}

public String [] getMembers()
{
 return members;   
}

public String toString()
{



   return  "-Music-" + "\n"
     + "band:     " + artist + "\n" 
     + "# songs:  " + number + "\n" 
     + "members:  " + Arrays.toString(members) 
     + "\n" + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;

}


public void print()
{


    System.out.println(toString());

}
}


class DVD extends Item
{

private String director;
private String [] cast;
private int scenes;


public DVD(String theTitle, String theDirector, int nScenes, String... keywords)
{
    super(theTitle, keywords);
    this.director = theDirector;
    this.scenes = nScenes;

}

public void addmoviecast(String... members)
{
    this.cast = members;


}

public String [] getCast()
{
    return cast;

}



public String getDirector()
{
    return director;
}


 public String toString()
 {

    return "-Movie-" + "\n"
     + "director: " + director + "\n"
     + "# scenes: " + scenes + "\n"
     + "cast:     " + Arrays.toString(cast) + "\n"
     + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;

}

public void print()
{

  System.out.println(toString());  

}   
}


class Book extends Item
{

private String author;
private int pages;


public Book()
{

}


public Book(String theTitle, String theAuthor, int  nPages, String... keywords)
{
    super(theTitle, keywords);
    this.author = theAuthor;
    this.pages = nPages;
   // this.keywords = keywords;

}


public String getAuthor()
{

    return author;

}


public void print()
{

    System.out.println(toString());  

}


 public String toString()
{

    return "-Book-" + "\n"
     + "Author:   " + author + "\n" 
     + "# pages   " + pages + "\n"
     + super.toString() 
    // + "keywords: " + Arrays.toString(keywords) 
     + "\n" + "\n" ;


}


}

So, when i try to do this

for(Item s : theBooks)
{
if(s.getAuthor().equals(author))

it cannot find the getAuthor() function in the book class! what can i do to retrieve the authors so i can find a particular match? Thank you..

this is what the info looks like in the hashset

-Book-
author:   Robert A. Heinlein
# pages:  325
title:    Starship Troopers
keywords: science fiction, war, weapons
+1  A: 

If this collection in fact can only contain Books, which seems likely, it should in fact be a collection of Books.

public class Library
{
    private Set<CD> theCDs = new HashSet<CD>();
    private Set<DVD> theDVDs = new HashSet<DVD>();
    private Set<Book> theBooks = new HashSet<Book>();

    public Collection<Book> booksByAuthor(String author)
    {

        for (Book b : theBooks)
        {
            if (b.getAuthor().equals(author)) {
                //
            }
        }
    }
}

The problem with your original code was that you have a collection of Item which can contain Book. So test each item to see if it is a Book, and the cast it.

for(Item s : theBooks)
{
    if (s instanceof Book) 
    {
        Book b = (Book) s;
        if (b.getAuthor().equals(author))
    }
}
Lachlan Roche
I cant change the type in the sets.. than some other functions wont work..
icelated
However, casting will work good.. thank you
icelated
+1  A: 

Rather declare all the Set<Item> sets to represent the actual type. I don't think that you're going to mix the different types CD, DVD and Book with each other in the same Set. Thus, so:

private Set<CD> theCDs = new HashSet<CD>();
private Set<DVD> theDVDs = new HashSet<DVD>();
private Set<Book> theBooks = new HashSet<Book>();

this way you can just iterate over each without the need to cast them back to the actual type:

for (Book book : theBooks) {
    book.getAuthor();
    // ...
}
BalusC
I cant change the sets from Item. It wont let me return the types like: public Collection<Item> books() { return theBooks; }
icelated
@icelated: You can change the type of the sets and then have `public Collection<Book> books()` and when you call this you can get this converted to `Collection<? extends Item>`. So everywhere where you need `Collection<Item>` now it should be `Collection<? extends Item>`.
x4u