I'm trying to learn Java and am greatly struggling with understanding classes. I have a String array in a class that I need to put into an arrayList of objects, and then use getters and setters for the arrayList in methods from another class. Here is some of my code:
public class Store
{
public static void main(String[] args) {
Book book1 = new Book();
Book book2 = new Book();
Book book3 = new Book();
printAll();
}
public void printAll(){
for(String book : booksOnHand){
super.print()
}
}
}
public class Book extends Store
{
private String title;
private String author;
private int year;
int[] stock = new int[4];//how many books are on hand at each of 5 stores
String [] books = {"War and Peace, Leo Tolstoy, 1869, 12, 7, 3, 9",
"Little Women, Louisa May Alcott, 1868, 4, 5, 2, 8",
"To Kill A Mockingbird, Harper Lee, 1960, 21, 18, 13, 6",
};
ArrayList<Book> booksOnHand = new ArrayList<Book>();
public Book(String title, String author, int year, int [] stock)
{
this.title = title;
this.author = author;
this.year = year;
this.stock = stock;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public String getYear()
{
return year;
}
public int[] getStock()
{
return stock;
}
public void setTitle(String title)
{
this.title = title;
}
public void setAuthor(String author)
{
this.author = author;
}
public void setYear(int year)
{
this.year = year;
}
public void setStock(int count1, int count2, int count3, int count4)
{
stock[0] = count1;
stock[1] = count2;
stock[2] = count3;
stock[3] = count4;
}
void print()
{
System.out.println("Title: " + getTitle() + "\tAuthor: " + getAuthor() + "\tYear: " + getYear() + "\tStock: " + Arrays.toString(getStock()));
}
}
I have much more code that I've tried, including Collections.addAll(booksOnHand, books);
but I don't know where to put the arrayList and how to instantiate it so I can use it in my other class. Thanks in advance to everyone who's willing to help!