I'm trying to figure out the best way to implement the code I'm working on.
I have two options of how to pass the data to my constructor.
First way
private String ISBN;
private String title;
private ArrayList <Person>authors = new ArrayList<>();
private ArrayList <BookCategory>subjectCategories = new ArrayList<>();
/**
* First constructor
* @param isbn String
* @param title String
* @param authors ArrayList <Person>
* @param categories ArrayList <BookCategory>
* Calls the checkISBN method
*/
public Book (String isbn, String title,
ArrayList <Person>authors, ArrayList <BookCategory>categories) {
//call the checkISBN method
boolean check = checkISBN(isbn);
if (check ==true) {
this.ISBN= isbn;
}
else {
throw new IllegalArgumentException("Invalid ISBN");
}
this.title = title;
for(int index =0; index<authors.size(); index++) {
this.authors.add(authors.get(index));
}
}
Second way
private String ISBN;
private String title;
private ArrayList <Person>authors = new ArrayList<>();
private ArrayList <BookCategory>subjectCategories = new ArrayList<>();
/**
* First constructor
* @param isbn String
* @param title String
* @param authors ArrayList <Person>
* @param categories ArrayList <BookCategory>
* Calls the checkISBN method
*/
public Book (String isbn, String title,
ArrayList <Person>authors, ArrayList <BookCategory>categories) {
//call the checkISBN method
boolean check = checkISBN(isbn);
if (check ==true) {
this.ISBN= isbn;
}
else {
throw new IllegalArgumentException("Invalid ISBN");
}
this.title = title;
this.authors = authors;
Does it make a difference?
I'm not sure because it seems like it would be unnecessary to declare the Book with a copy of the Authors arraylist
instead of the original arraylist
.
What is the correct way to do this, and why?
Aucun commentaire:
Enregistrer un commentaire