import java.util.*;

public class InventoryImpl extends _InventoryImplBase {

  private Vector catalogItems = new Vector();

  public InventoryImpl(java.lang.String name) {
    super(name);
    addCatalogItem(new CD("Under the Table and Dreaming", 
      "Dave Matthews Band", 19, 2));
    addCatalogItem(new CD("Crash", 
      "Dave Matthews Band", 15, 3));
    addCatalogItem(new CD("Don't Know How to Party", 
      "The Mighty Mighty Boss Tones", 14.5, 1));
  }

  public void addCatalogItem(CatalogItem inCatalogItem) {
    if(!catalogItems.contains(inCatalogItem)) {
      catalogItems.addElement(inCatalogItem);
      System.out.println("Added item: " + inCatalogItem);
    } else {
      System.out.println("Store already contains: " + inCatalogItem);
    }
  }

  public boolean inInventory(CatalogItem inCatalogItem) {
    return catalogItems.contains(inCatalogItem);
  }

  public Vector getCatalogItems() {
    return catalogItems;
  }

  public int getQuantityInInventory(CatalogItem inCatalogItem) {

    CatalogItem item = null;
    int quantity = -1;
    if(catalogItems.contains(inCatalogItem)) {
      int index = catalogItems.indexOf(inCatalogItem);
      item = (CatalogItem)catalogItems.elementAt(index);
      quantity = item.checkQuantity();
      System.out.println("quantity="+quantity);
    }
    return quantity;
  }

  public static void main(String args[]) {
    // Initialize the ORB.
    org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);
    // Initialize the BOA.
    org.omg.CORBA.BOA boa = orb.BOA_init();
    // Create the Inventory 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.");

  }
}
