1

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!

1
  • 1
    Have you read the error messages from the compiler? They tell you what is wrong and where. You shouldn't ignore them. Also, why in hell does Book extend Store? A Book is not a Store. So Book should not extend Store. Why does a book contain other books? Why does a book have a stock? Try modelling the reality: a Store has books. A book doesn't have books. Commented Nov 15, 2015 at 21:48

2 Answers 2

1

Think of Java classes in terms of the objects they represent in real life. Book should not extend store. When you use extends you are saying "This object is like this one but more specific."

So you might have a class called Vehicle. All vehicles have an engine. But you might have different kinds of vehicles that do different things. So you might have a class Car that extends Vehicle and a class Boat that extends vehicle. They both have engines but they move differently. This concept is called inheritance. In your case there is no need to use it.

There is also a difference in a class and the object. A class is like a blueprint. Class Car knows all the things that a car needs to know. When you create an object from the Car class, you specify properties like color, speed, etc. This is why you do not need an array of titles and such. The blueprint doesn't need the specific values, only to know there will be a String called title that will contain the title when you build the book.

In your case think of how a bookstore really works. Does a bookstore's record of a book know about if other stores have that book. No.

I would also have a different class called main where you have your main method, and then treat Store as a class that represents a store that holds books. Put your ArrayList in this class, and use methods in store to access information like how many books are on hand of each title.

The general set up should be more like:

public class Main {

    public static void main(String[] args) {

        Store barnesAndNoble = new Store();

        Book book1 = new Book("War and Peace", "Leo Tolstoy", 1869, 12, 7, 3, 9);
        Book book2 = new Book("Little Women", "Louisa May Alcott", 1868, 4, 5, 2, 8);
        Book book3 = new Book("To Kill A Mockingbird", "Harper Lee", 1960, 21, 18, 13, 6);

        barnesAndNoble.add(book1);
        barnesAndNoble.add(book2);
        barnesAndNoble.add(book3);

        barnesAndNoble.printAll();

        Store amazon = new Store();
        amazon.add(book1);
        amazon.add(book2);
        amazon.add(book3);

        amazon.printAll();
    }
}

public class Book {
    //code pertaining to books, no stock information
}

public class Store {

    private List<Book> booksOnHand;

    public Store() {
        booksOnHand = new ArrayList<Book>();
    }

    public void add(Book book) {
        booksOnHand.add(book);
    }

    //other methods for accessing the list of books, returning inventory numbers from list, or other code pertaining to listing store information

    public void printAll(){
        //print each books desired information
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate the explanation so much! How does the original array fit into this though? Would I just reference it in the Book book1 = new Book(super.books[0])?
1

I made some corrections, try code below. I added some comments on code.

import java.util.ArrayList;
import java.util.List;

public class Store {
    private static List<Book> booksOnHand = new ArrayList<Book>();
    public static void main(String[] args) {
        // you should create you object with your constructure
        Book book1 = new Book("War and Peace", "Leo Tolstoy", 1869, 12, 7, 3, 9);
        Book book2 = new Book("Little Women", "Louisa May Alcott", 1868, 4, 5, 2, 8);
        Book book3 = new Book("To Kill A Mockingbird", "Harper Lee", 1960, 21, 18, 13, 6);
        // add them into a list
        booksOnHand.add(book1);
        booksOnHand.add(book2);
        booksOnHand.add(book3);
        printAll();
    }

    public static void printAll() {
        // print them with its own object method.
        for (Book book : booksOnHand) {
            book.print();
        }
    }
}


import java.util.Arrays;
// book shouldn't extends store, so I removed that
public class Book {
    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
    
    // with int...(vararg) you can add stocks of stores
    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 int 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;
    }

    // use varargs instead of "int count1, int count2, int count3, int count4"
    public void setStock(int... stock) {
        this.stock = stock;
    }

    void print() {
        System.out.println("Title: " + getTitle() + "\tAuthor: " + getAuthor()
                + "\tYear: " + getYear() + "\tStock: "
                + Arrays.toString(getStock()));
    }
}

Prints:

Title: War and Peace Author: Leo Tolstoy Year: 1869 Stock: [12, 7, 3,9]

Title: Little Women Author: Louisa May Alcott Year: 1868 Stock: [4, 5, 2, 8]

Title: To Kill A Mockingbird Author: Harper Lee Year: 1960 Stock: [21, 18, 13, 6]

1 Comment

Thanks so much for your help! While it certainly does help, I still have to use the original array and find a way to put THAT into an arrayList, so will keep looking. Thankyou!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.