[Bluej-discuss] Ex.4.38

Jens Wösten gatahkt at t-online.de
Fri Nov 10 08:33:51 GMT 2006


Hi Mike,

the new Item from the parameter isn’t needed to be stored in an ArrayList
like you do here:
<<ArrayList <Product>stockString = new ArrayList<Product>();>>

The reason for that is, that for one method-call on the addProduct-method
there will be only one product to add at one time.
Instead of using an ArrayList and adding the product to it, you can use the
Object item right away from the parameter.
The parameter in this case behaves like the local variable that you actually
wanted.

So the if-statement is more like comparing all the "In-Stock-Items" (many
from the ArrayList "inStock") with the one you want to add and that is given
in the parameter.

//compare current Item in the list while you iterate with current item given
with the parameter.
if(inStock.getID() != item.getID())


Hope it helps you out.
J.

________________________________________
Von: bluej-discuss-bounces at bluej.org
[mailto:bluej-discuss-bounces at bluej.org] Im Auftrag von Mike Stevens
Gesendet: Freitag, 10. November 2006 08:36
An: bluej-discuss at bluej.org
Betreff: [Bluej-discuss] Ex.4.38

Hi I was wondering if I’m headed in the right direction with this exercise.

public void addProduct(Product item)
    {
 
        //Using a local var to store new Item
        ArrayList <Product>stockString = new ArrayList<Product>();
        stockString.add(item);
        
        Iterator it = stock.iterator();
            while(it.hasNext()) {
                Product inStock = (Product) it.next();
 
                //This is the problem.
                //The compiler is not letting me use the getID() method - 
                //for the local var stockString
                if(inStock.getID() != stockString.getID())
                {
                    stock.add(item);
                }else{
                    System.out.print("Id used");
                }
            }
    }

If anybody can point me in the right direction for comparing the new Object
id and exciting Object id’s that would be great

Thanks
Mike



More information about the bluej-discuss mailing list