I know this is a very simple question about method call.
I have a method that should count elements of the same item in an arraylist.
This method below work, it count all items with the same id, but this is not the correct way to do it after the requierments, see below.
"numberInStock can call the
findProduct method to do the searching, and then call the
getQuantity method on the result."
Yes, I know this is basic
Code:
public int numberInStock(int id)
{
if(id < 0)
{
System.out.println("This is not a valid id " + id);
}
else{
int count = 0;
Iterator it = stock.iterator();
while(it.hasNext())
{
Product product = (Product) it.next();
if(product.getID() == id)
{
count++;
}
}
return count;
}
return 0;
