I am trying to get an author from the BOOK class that extends the Item class.
I have 3 classes as follows
- main()
- library
- 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