I am stuck in this java search instruction. I am trying to search a product array called stocks that is initialized in the Stockmanager class, which contains the id
name field and stock level. these product objects are produced in a separate Product class.
Stockmanager constructor:
private ArrayList<Product> stock;
public StockManager()
{
stock = new ArrayList<>();
}
Product Designer:
private int id;
private String name;
private int quantity;
public Product(int id, String name)
{
this.id = id;
this.name = name;
quantity = 0;
}
There is an access method in Product to get the identifier of the product object:
public int getID()
{
return id;
}
Now I have my own search method in Stockmanager, but it looks like this method will complain about incompatible data types if I don't use my for-each loop or complain about the lack of a return statement if I use for-every loop.
method:
public Product findProduct(int id)
{
int index = 0;
boolean searching = true;
for (Product item : stock)
{
while(searching && index < stock.size())
{
if (item.getID() == id)
{
searching = false;
return item;
} else {
index++;
}
if(searching)
{
return null;
} else {
return item;
}
}
}
}
while
, , , .
, ?