I'm a fairly novice Java developer. I have a Book class to represent a book, and a Library class to represent a library.
Some code from the Library class:
/**
* method to populate books list
*/
public static void populateBooksList() {
// instantiate books
Book b1 = new Book("Catch 22", "Heller", "Thriller", 5, 1, false);
Book b2 = new Book("The Hobbit", "Tolkien", "Fantasy", 4, 2, false);
Book b3 = new Book("Gullivers Travels", "Swift", "Adventure", 3, 3,
false);
Book b4 = new Book("Moby Dick", "Melville", "Crime", 4, 4, false);
Book b5 = new Book("The Lord of the Rings", "Tolkien", "Fantasy", 4, 5,
false);
// populate the array
books[0] = b1;
books[1] = b2;
books[2] = b3;
books[3] = b4;
books[4] = b5;
}
And some code from the Book class (I didn't paste all the gets / sets in):
public class Book {
/**
* declaring var for genre
*/
private String genre;
/**
* declaring var for rating
*/
private int rating;
/**
* declaring var for name
*/
private String name;
/**
* declaring var for ID
*/
private int iD;
/**
* declaring var for author
*/
private String author;
/**
* declaring var for onLoan
*/
private Boolean onLoan;
/**
* default constructor
*/
public Book() {
}
/**
* constructor with args
*
* @param nameP
* - book name
* @param authorP
* - book author
* @param genreP
* - book genre
* @param ratingP
* - book rating
* @param iDP
* - book ID
* @param onLoanP
* - onLoan
*/
public Book(String nameP, String authorP, String genreP, int ratingP,
int iDP, Boolean onLoanP) {
this.name = nameP;
this.iD = iDP;
this.author = authorP;
this.onLoan = onLoanP;
this.genre = genreP;
this.rating = ratingP;
}
What I want to do is make adding the books to the array more efficient. If I have 50 books, I'll need 50 lines of code just to populate the array. I'd like to use the constructor in the Book class, or the gets / sets in the Book class somehow, or some other way using code contained in the Book class in order to automatically add each object to the array as it is instantiated. Is this possible?
Thanks
S.