How to create a search statement in java with a while loop and stop and return an object when it is found

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 idname field and stock level. these product objects are produced in a separate Product class.

Stockmanager constructor:

// A list of the products.
private ArrayList<Product> stock;

/**

 * Initialise the stock manager.

 */
public StockManager()
{
    stock = new ArrayList<>();
}

Product Designer:

// An identifying number for this product.
private int id;
// The name of this product.
private String name;
// The quantity of this product in stock.
private int quantity;

/**
 * Constructor for objects of class Product.
 * The initial stock quantity is zero.
 * @param id The product identifying number.
 * @param name The product name.
 */
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:

/**
 * @return The product id.
 */
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:

/**

 * Try to find a product in the stock with the given id.

 * @return The identified product, or null if there is none

 * with a matching ID.

 */
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, , , . , ?

+4
3

, index searching.

for. Product, . , null, .

public Product findProduct(int id)
{    
    for (Product item : stock) {  
        if (item.getID() == id) {
            return item;
        }
    }
    return null;    
}
+3

Java 8 Stream API

public Optional<Product> findProduct(int id)
{
    return stock.stream().filter(item -> item.getID() == id).findAny();
}

Java 8 for.

public Product findProduct(int id)
{
    for (Product item : stock)
    {  
       if (item.getID() == id) 
       {
                return item;
       }
    }   
    return null;     
}
+2

if(searching),

/**

     * Try to find a product in the stock with the given id.

     * @return The identified product, or null if there is none

     * with a matching ID.

     */
    public Product findProduct(int id){
        for (Product item : stock){  
           if (item.getID() == id){
                return item;
            }
        } 
      return null;
    }

while? while.

+1

Source: https://habr.com/ru/post/1688801/


All Articles