Reputation: 1249
Since my post yesterday regarding constructing a link list from scratch I have made some progress.
I have encountered a new hurdle: storing an object within an object.
Lets say I have a 'book' class with the following attributes (forgetting all the set & get methods which I am familiar with):
private String title;
private int rating;
How would I then reference another class such as 'author' as obviously a book must have an author and many books may have the same or more then one author.
Here is my 'author' class attributes (again ignoring gets & sets):
String authorName;
String authorEmail;
Am I right in thinking I need to initiate an object of 'author' within the 'book' class as such:
private String title;
private int rating; //mine
private Author author = new Author();
Would I then have to set the attributes authorName and authorEmail everytime I made a new instance of 'book'?
Many thanks in advance for constructive feedback.
Upvotes: 3
Views: 214
Reputation: 365
You don't necessarily need to instantiate the Author object right there where you declare the attribute. I would suggest something like passing in an already-instantiated Author either to the constructor of the Book class or to a setter. Then you could pass the same Author in to each Book you create that should be associated with it.
EDIT: added some code snippets:
For instance, if you made your Book constructor something like this:
public Book(String title, int rating, Author author) {
// set this.title, this.rating, and this.author to the passed-in parameters...
}
Then you would invoke it in code like this:
Author bob = new Author();
// You can set the name and email of the Author here using setters,
// or add them as args in the Author constructor
Book firstBook = new Book("The First Book", 1, bob);
Book secondBook = new Book("The Second Book", 2, bob);
Upvotes: 1
Reputation: 4885
Your on the right track. You can use an ArrayList
to be able to dynamically add new authors without having to resize anything. Let me clarify it for you:
class Book {
private String title;
private int rating;
private List<Author>authors = new ArrayList<Author>();
public Book(String title, int rating, Author author) {
this.title = title;
this.rating = rating;
authors.add(author);
}
public Book(String title, int rating, Author author) {
this.title = title;
this.rating = rating;
this.author = author;
}
public void addAuthor(Author a) {
authors.add(a);
}
public int numberOfAuthors() {return authors.size();}
}
class Author {
private String name;
private String email;
public Author(String name, String email) {
//...Same thing
}
}
class Main {
public static void main(String[] args) {
Book book = new Book("Java Programming", 5, new Author("Me", "[email protected]"));
Author contributingAuthor = new Author("My Friend", "[email protected]");
book.addAuthor(contributingAuthor);
}
}
Upvotes: 0
Reputation: 934
You might want some kind of singleton author list in order to prevent many copies of the same author. Either that, or you will definitely need to override the equals method of author.
If you use a singleton, you could have a getAuthor routine in your AuthorList object that either makes an author if it doesn't exist or fetches the already created author.
Upvotes: 0
Reputation: 15042
This is a many to many relationship. You need your Author class to simply be a link between a person and a book. Then things will work out.
Upvotes: 0