Package homework03

Source Code of homework03.Inventory

package homework03;

import java.util.ArrayList;
import java.util.GregorianCalendar;


/**This class is used to keep track of some kind of Inventory.
* The only requirement on the items in the inventory is they must
* make generic use of the Item class.
*
* @author drakebennion
* @date 1/23/2014
* @param <I>
*/

public class Inventory<I extends Item>
{
  private ArrayList<DatedItem<I>> list;
 
  /**Default constructor for Inventory class, sets the list to be empty.
   *
   */
  public Inventory()
  {
    list=new ArrayList<DatedItem<I>>();
  }
 
  /**If the Item and CriticalDate are not null, and quantity is positive,
   * this Item, criticalDate and quantity are stored in a DatedItem<I> object,
   * which is then added to the arraylist. If there are already Items of the
   * same name and criticaldate in the list, then that DatedItem's quantity is increased appropriately.
   * if item=null or date=null, or quantity<=0, nothing is done.
   *
   * @param item--Item object
   * @param criticalDate--GregorianCalendar object
   * @param quantity--how many of the Item to add
   */
  public void addItem(I item, GregorianCalendar criticalDate, int quantity)
  {
    if (!(item==null)&&!(criticalDate==null)&&quantity>0)
    { 
      //creates a DatedItem<I> object from the given parameters
      DatedItem<I> dateditem=new DatedItem<I>(item, criticalDate, quantity);
      //this loops through the list and checks each dated item.
      //if the exact same item/date is found, the quantity is changed as described below
      for(DatedItem<I> i:list)
      {
        if(i.sameDate(criticalDate)&&i.sameItem(item))
          //i.getQuantity is the quantity of the item prior to adding the new one
          //the setQuantity method will update to the new quantity (old+current=new)
          i.setQuantity((i.getQuantity())+quantity);
      }
      list.add(dateditem);
    }
  }
 
  /** Searches through the list for the given item with the given critical date: if it
   * finds a match, the method returns the quantity attached to the dateditem object.
   * Otherwise, it returns 0;
   *
   * @param item
   * @param criticalDate
   * @return int quantity
   */
  public int getQuantity(I item, GregorianCalendar criticalDate)
  {
    //remember, this syntax loops through the array by each element, no index required! yaaaaay Java
    for(DatedItem<I> i:list)
    {
      if(i.sameDate(criticalDate)&&i.sameItem(item))
        return i.getQuantity();
    }
    return 0;
  }
 
  /**As long as item!=null, date!=null, and quantity>0, this method
   * decreases the quantity of a DatedItem, specified by the given item and date, and decreases the
   * quantity by the given parameter. If the new quantity is <=0, the dateditem is removed
   * from the list.
   * if item=null or date=null or quantity<=0, nothing happens.
   *
   * @param item
   * @param criticalDate
   * @param quantity
   */
  public void removeItem(I item, GregorianCalendar criticalDate,int quantity)
  {
    if(!(item==null)&&!(criticalDate==null)&&quantity>0)
    {  //incredibly similar to the above add function
      //this loops through the list and checks each dated item.
      //if the exact same item/date is found, the quantity is changed as described below
      //although I just discovered that the remove function on an ArrayList requires an integer parameter...
      int pos=0;
      int newQuantity=1; //initialized to 1 to not automatically satisfy the if statement at bottom of method
      for(DatedItem<I> i:list)
      {
        if(i.sameDate(criticalDate)&&i.sameItem(item))
          {
            //keep track of position of element to be removed (need integer position for remove function)
            pos=list.indexOf(i);
            newQuantity=i.getQuantity()-quantity;
            if(newQuantity>0)
              i.setQuantity(newQuantity);
          }
      }
      if (newQuantity<=0)
        list.remove(pos);
    }
  }
 
  /**
   * This method returns the critical date for a dateditem in the inventory. 
   * If multiple matching items are in the inventory with different critical dates,
   * the oldest critical date for that kind of item is returned.
   *  If no such item is found in the inventory, null is returned.
   *
   * @param item
   * @return the date
   */
  public GregorianCalendar getDate(I item)
  {
    //if this boolean remains false, function will return null by default.
    //will only change if, while looping through list, a matching Item is found.
    boolean isInList=false;
   
    GregorianCalendar oldest=new GregorianCalendar();
    GregorianCalendar temp=oldest;
    for(DatedItem<I> i:list)
    {
      if(i.sameItem(item))
        {
          temp=i.getDate();
          isInList=true;
        }
      //Gregorian Calendar uses a compareTo function, returns negative number if temp is before oldest
      //compareTo returns 0 if temp equals oldest, returns positive number if temp is after oldest
      if(temp.compareTo(oldest)<0)
        oldest=temp;
    }
   
    if(isInList)
      return oldest;
    else
      return null;
  }
 
  /**This method returns an ArrayList of items whose critical date is before the target date.("expired")
   *
   * ****the @SuppressWarnings should probably not be there--deal with it later.
   * ****Dealt with it.
   *
   * @param targetDate
   * @return an Arraylist<I>
   */
 
  public ArrayList<I> getItemsPastDate(GregorianCalendar targetDate)
  {
    ArrayList<I> temp=new ArrayList<I>();
    for(DatedItem<I> i:list)
    {
      //see notes on compareTo function above
      if((i.getDate().compareTo(targetDate))>0)
        temp.add(i.getItem());
    }
    return temp;
  }
}
TOP

Related Classes of homework03.Inventory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.