All pastes #922794 Raw Edit

Untitled

public text v1 · immutable
#922794 ·published 2008-02-29 00:55 UTC
rendered paste body
import java.util.ArrayList;

/**
 * Manage the stock in a business.
 * The stock is described by zero or more Products.
 */
public class StockManager
{
  private ArrayList<Product> stock;           // A list of the products.

  /**
   * Initialise the stock manager.
   */
  public StockManager()
  {
    stock = new ArrayList<Product>();
  }

  /**
   * Add a product to the list.
   * @param item The item to be added.
   */
  public void addProduct(Product item)
  {
    stock.add(item);
  }
    
  /**
   * Receive a delivery of a particular product.
   * Increase the quantity of the product by the given amount.
   * @param id The ID of the product.
   * @param amount The amount to increase the quantity by.
   */
  public void delivery(int id, int amount)
  {
    if(amount > 0)
    {
      for(Product product : stock)
      {
        if(id == product.getID()) //if the id matches, increase the 
        {                         //quantity by the amount in the param
          product.increaseQuantity(amount);
        }
      }
    }
    else
    {
      System.out.println("The number entered must be a positive integer");
    }
  }
    
  /**
   * 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 product : stock)
    {
      if(id == product.getID()) //if the id matches, return the product
      {
        return product;
      }      
    }
    return null;
  }
    
  /**
   * Prints all the details of all
   * the products that have a 
   * certain stock level below the given value
   * @param min The minimum value you are searching for
   */
  public void printMinimumStock(int minValue)
  {
    if(minValue > 0) //the minimum value has to be greater than 0
    {
      for(Product product : stock)
      {
        if(minValue > product.getQuantity()) //if the minimum value is greater than the quantity,
        {                                    //prints out all the products that have less than the value
          System.out.println(product.toString());
        }
      }
    }
    else
    {
      System.out.println("The number entered is negative, must enter a positive number");
    }
  }
  
  /**
   * Locate a product with the given ID, and return how
   * many of this item are in stock. If the ID does not
   * match any product, return zero.
   * @param id The ID of the product.
   * @return The quantity of the given product in stock.
   */
  public int numberInStock(int id)
  {
    for(Product product : stock)
    {
      if(id == product.getID()) //if the ID matches the ID in stock,
      {                         //returns the quantity
        return product.getQuantity();
      }      
    }    
    return 0; //if it doesn't match, there are none in stock
  }

  /**
   * Print details of all the products.
   */
  public void printProductDetails()
  {
    for(Product product : stock)
    {
      System.out.println(product.toString());
    }
  }
}