find a keyword in a collection and return it
<p>I have a collection i added CD, DVD, book information into hashsets.
Each one has a keyword and i would like to do a search for a specific keyword and return the specific book,CD,dvd... heres the output to give you an idea..</p>

<pre><code>-Book-
Author:   Robert A. Heinlein
# pages   325
title: Starship Troopers
keywords: [science fiction, war, weapons]

-Music-
band:     Grateful Dead
# songs:  12
members:  [Jerry Garcia, Bill Kreutzman, Keith Godcheaux]
title: Europe In '72
keywords: [acid rock, sixties, jam bands]


-Movie-
director: Sofia Coppola
# scenes: 14
cast:     [Bill Murray, Scarlett Johansson]
title: Lost In Translation
keywords: [Japan, loneliness]


&gt;&gt;&gt; items for keyword: science fiction

none

&gt;&gt;&gt; items for keyword: jam bands

none


C:\Java\a03&gt;
</code></pre>

<p>I have 3 classes.</p>

<ol>
<li>Main()</li>
<li>Library - this is where i do all the adding of cd, DVD, books. lookups, etc</li>
<li>Items class(CD class, DVd class, book class) using inheritance..</li>
</ol>

<p>in main() i am sending in information to the library class to add to the sets.
then i print out all the books, cd, movies just added.</p>

<p>then i do a lookup for a specific keyword.
And this is where i am having problems. i wrote a getkeyword function in CD, DVD, book class.</p>

<p>What i want to do is get the keyword and then see if they match and then return it as a collection.</p>

<p>here is main() i will only show some of it to keep this short - i will not show you how i am adding since it works good..</p>

<pre><code>printItemsForKeyword(out, "science fiction");
printItemsForKeyword(out, "jam bands");
printItemsForKeyword(out, "xxx");


private static void printItemsForKeyword (PrintStream out, String keyword)
{
    Collection&lt;Item&gt;    items;

    out.printf("&gt;&gt;&gt; items for keyword: %s\n\n", keyword);
    items = library.itemsForKeyword(keyword);
    printItems(out, items);
}
</code></pre>

<p>now here in the library class is where i need help
in the itemsForKeyword(String keyword) function...</p>

<p>so, the first thing i am trying to find is "Science Fiction"</p>

<p>I think i need to cast item since item has the CD, DVD, book classes and i need to return a colection???</p>

<p>right now i am trying to return key and it wont since its incompatable with the return.</p>

<pre><code>public class Library
{
private Set&lt;Item&gt; theCDs = new HashSet&lt;Item&gt;();
private Set&lt;Item&gt; theDVDs = new HashSet&lt;Item&gt;();
private Set&lt;Item&gt; theBooks = new HashSet&lt;Item&gt;();


public Collection&lt;Item&gt; itemsForKeyword(String keyword)
{

    Item key = new Item();            
    ((CD)key).getKeyword();    // i dont think i am even doing this right

    if(key.equals(keyword))
    {
        return key;             // cant return key
       }



    return null;
}
</code></pre>

<p>I did define a getKeywords() function in each of the classes below.</p>

<p>Here is the Items class since you will need to look it over..</p>

<pre><code>import java.io.PrintStream;
import java.util.Collection;
import java.util.*;


class Item
{

private String title;



public String toString()
{

String line1 = "title:    " + title + "\n";
return line1;
}


public void print()
{

System.out.println(toString());


}


public Item()
{

}

public Item(String theTitle)
{

title = theTitle;

}

public String getTitle()
{
return title;
}



}

class CD extends Item
{

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





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

}


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

}

public String getArtist()
{

    return artist;

}

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


public String [] getKeyword()
{

    return keywords;

}


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;
private String [] keywords;


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

}

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


}

public String [] getCast()
{
    return cast;

}



public String getDirector()
{
    return director;
}

public String [] getKeyword()
{

    return keywords;

}




 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;
private String [] keywords;


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

}


public String getAuthor()
{

    return author;

}

public String [] getKeyword()
{

    return keywords;

}




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


}    
}
</code></pre>

<p>the reason why i think i need to do sometype of cast is because i had to when i added
bandmembers..</p>

<pre><code>public void addBandMembers(Item musicCD, String... members)
{


    ((CD)musicCD).addband(members);


}
</code></pre>

<p>So, what can i do to find a keyword in the items and return those?
would it be better to look directly at the sets and do some type of cast to get it to return? im just stumped..</p>

<p>Thank you..</p>
