Latest Lists

Iterating in a list help JAVA?

Hi all. I'm in the progress of writing a program that has a few different classes and a couple of interfaces. I'm up to a part I don't really know how to do. In an " Item " class, users create Item instances, so, public Item(String name, double cost, int qty, boolean isFoodSource) { this.name = name; this.cost = cost; this.qty = qty this.isFoodSource = isFoodSource; } Item book = new Item("Cookbook", 10.25, 30, false); I have a Specials class which is like the one above. So, Specials mug = new Specials(..... Specials pen = new Specials(...... In another class, I have a list " stock " which implements the Stock interface. But my issue is that I'm having trouble iterating over the list. Users should add one Item thing they make like 'book' to the stock list, and can add several Specials things they make like 'mug' and 'pen' above. I need to iterate through it to do a few things. 1. throw an exception if two of the Item things are in it I know how to throw the exception part, just now the iterate to check for duplicate Item objects 2. I want to be able to get the Item 3. Check if all the things in the list are of non-food things So, In my Stock class I have some methods: public Item getItem() { returns the Item in the list } I've tried: public Item getItem() { Special t; Item b; for (int i = 0; i < stock.size(); i++) { if(ingredients.contains(t)) { if(t instanceof Item){ b = (Item)t; } } } return b; } Something obviously not right there. For the checking... I don't know how to do that, but I have public boolean isNotFoodSource () { returns true iff all the elements of a list are not of a food source } Thanks in advance. Let me know if you need more code to clarify any weird bits. P.S. I know that hashsets are used to remove/avoid duplicates. My main concern is that a duplicate Item element from the stock list is caught and an error is thrown - I don't want to remove it, or not allow it in the first place... I just want to throw the error :) I believe this should be done in the constructor, iterating using a for loop and doing an if ...?? I don't want a try catch block, I will just do a declaration in the constructor Thanks

Public Comments

  1. The description of your project is a little unclear. You say that people can add only one item to Stock (but maybe you mean only one Item of a given type?). If Stock is only allowed one Item, period, it can make do with a single Item instance variable rather than a list. Then your call to get "the" item can just return that one instance variable. If you allow multiple Items and multiple Specials, unless one is a subclass of the other, I'd keep them in separate lists. So you'd have something like: ============ public class Stock() { List<Item> items; List<Special> specials; ... } ============ No need to mix all those things up in one list unless there is good reason to do so. The key to finding duplicate Items is going to be how you compare your items. If someone makes two Cookbook items with different prices, are they considered the same Item? If you have a list of Items, you can iterate over the list in a doubly nested loop to compare any two. But that makes (1/2)n^2 comparisons for n items in your list.
Powered by Yahoo! Answers