• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing objects from Collection

 
Marc Kryzhan
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Store implements hw9.interfaces.StoreInterface {
private Collection inventory = new ArrayList();
public hw9.interfaces.InventoryItemInterface getInventoryItem(String itemName) {
// inventory.indexOf(itemName);
InventoryItem name = new InventoryItem();
name.setName(itemName);
String gotName = name.getName();
inventory.toArray();
inventory.
}
I have a Collection that is created called inventory. I believe I've created an okay method to add to the collection. To test I need to fix my above method. Problem is Collection methods do not have a get function. I'd like to access the methods in ArrayList but don't know how. Please suggest how?
p.s. Here's my method to add input into a collection as an object;
public void addInventory(String name, String type, double cost, double
price) {
System.out.println("Store::addInventory()");
InventoryItem i = new InventoryItem(name, type, cost, price);
inventory.add(i);

}
InventoryItem () is a constructor in InventoryItem class which has the setName() and getName() methods. This is just there to teach me how to think in terms of protecting code I think. To really put into and get out of the collection all I would need I believe is the Store class.
Marc
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33723
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,
You would use an Iterator to loop through the Collection.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic