1

I have a list of stock as a class and then a class of stores with the constructor shown below. The stores has an array list which links to the stock class.

How would I access the array list for a certain store?

E.G. If I pick the store argos I want all the stock that it has in it. Each store has its own stock

    public Store(int storeId, String name, String location){
      this.storeId = storeId;
     this.name = name;
     this.location = location;
      items = new ArrayList<Stock>();
     }
1
  • 1
    A private local variable with a getter? Commented May 1, 2013 at 14:16

5 Answers 5

3

If each Store has it's own list of Stock items, then this will have to be an attribute, or a private instance variable, of the class Stock. The items of the Store can then be accessed using a getter, eg.

public class Store {
    private List<Stock> items;

    public Store(List<Stock> items){
        this.items = items;
    }

    public List<Stock> getStock(){ 
        // get stock for this Store object. 
        return this.items;
    } 
    public void addStock(Stock stock){
        this.getStock().add(stock);
    }
}

Then, you can access the items for an instance of Store using the getter for Stock items.

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

Comments

1

You can provide safe access this way, but it'd be better encapsulation if you didn't give users the keys to the store and return the stock list.

public class Store {
    private List<Stock> stock;

    public Store(List<Stock> stock) {
        this.stock = ((stock == null) ? new ArrayList<Stock>() : new ArrayList<Stock>(stock));
    }

   public List<Stock> getStock() {
       return Collections.unmodifiableList(this.stock);
   }
}

1 Comment

I like blackpanther's answer the best. His "addStock" is the right idea.
1

To be honest, I would advise using a HashMap. Have each Store as the key, or store ID, and then have the list of Stock as the value. This would allow you to simply do:

Map storeMap = new HashMap<String, List<Stock>();
items = storeMap.get(key);

Comments

1
public class Store {
    private List<Stock> items;

    public Store(int storeId, String name, String location){
        this.storeId = storeId;
        this.name = name;
        this.location = location;
        items = new ArrayList<Stock>();
    }

    public List<Stock> getAllStock(){
        return this.items;
    }
}

Comments

0

There are many possibilities to set the list to the Store object and with a getter you can return the list back.

public Store(int storeId, String name, String location,ArrayList<Stock> list){
    this.storeId = storeId;
    this.name = name;
    this.location = location;
    this.items = new ArrayList<Stock>();  //or any possibility to set list
}

public ArrayList<Stock> getListOfStock(){
    return this.items;
}

Comments

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.