package InventorySystem;
import java.util.Vector;

public class InventoryImpl 
  extends InventorySystem._InventoryImplBase {

  // allocate storage for the Catalog Items.
  private Vector catalogItems = new Vector();

  public InventoryImpl(java.lang.String name) {
    super(name);

    CD cdUnderTable = new CD("Under the Table and Dreaming", 
      "Dave Matthews Band");
    addCatalogItem(new CatalogItem(cdUnderTable, 19, 2));

    CD cdCrash = new CD("Crash", "Dave Matthews Band");
    addCatalogItem(new CatalogItem(cdCrash,15, 3));

    CD cdMMBT = new CD("Don't Know How to Party", 
      "The Mighty Mighty Boss Tones");
    addCatalogItem(new CatalogItem(cdMMBT, 14.5, 1));
  }

  public void addCatalogItem(InventorySystem.CatalogItem inItem) {
    // IMPLEMENT: Operation
    if(!catalogItems.contains(inItem)) {
      catalogItems.addElement(inItem);
      System.out.println("Added item: " + inItem);
    } else {
      System.out.println("Store already contains: " + inItem);
    }
  }

  public boolean inInventory(InventorySystem.CatalogItem inItem) {
    // IMPLEMENT: Operation
    return catalogItems.contains(inItem);
  }

  public InventorySystem.CatalogItem[] getCatalogItems() {
    // IMPLEMENT: Operation
    InventorySystem.CatalogItem[] catalogItemsArray = 
      new InventorySystem.CatalogItem[catalogItems.size()];
    catalogItems.copyInto(catalogItemsArray);
    return catalogItemsArray;
  }

  public int getQuantityInInventory(
    InventorySystem.CatalogItem inItem
  ) {
    // IMPLEMENT: Operation
    CatalogItem item = null;
    if(catalogItems.contains(inItem)) {
      int index = catalogItems.indexOf(inItem);
      item = (CatalogItem)catalogItems.elementAt(index);
      System.out.println("quantity="+item.quantity);
    }
    return item.quantity;
  }

  public InventorySystem.CatalogItem[] catalogItemsArray() {
    // IMPLEMENT: Reader for attribute
    return getCatalogItems();
  }

  public static void main(String args[]) {
    // Initialize the ORB.
    org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
    // Initialize the BOA.
    org.omg.CORBA.BOA boa = orb.BOA_init();
    // Create the MyRandom object.
    InventoryImpl inventory = new InventoryImpl("Inventory");

    // Export the newly created object.
    boa.obj_is_ready(inventory);
    System.out.println(inventory + " is ready.");
    // Wait for incoming requests
    boa.impl_is_ready();
    System.out.println("Inventory created.");
  }
}
