SOURCECODE

How to... incorporate a CountingStock on a Catalog


Description:
This data structure is meant to count the number of entrys in the associated Catalog.
It should be preferred if it was not important to remember differences between single items of a category.
The CountingStock could be displayed in the same way as the Catalog itself.
(See also: HowTo..create a Table and HowTo..define a new TableLayout)
The use of CountingStockImpl, the implementation of the interface CountingStock, is sufficient in most cases.

ToDo's:
  1. Get the instance of Catalog you want to incorporate a CountingStock on. (See also: HowTo incorporate a Catalog )
    ( ex.: CatalogImpl ciRef = (CatalogImpl) Shop.getTheShop().getCatalog(String sName))
  2. Make a new instance of CountingStock associated with the Catalog.
    ( CountingStock st = new CountingStockImpl(String sName, CatalogImpl ciRef) )
  3. Define (if necessary) a fitting subclass of StockItemImpl (See also HowTo..define a StockItem).
  4. Add the Stock to the Shop´s global list of stocks ( Shop.getTheShop().addStock(Stock st) ).
  5. Add and remove StockItems as you wish.
    ( st.add(StockItem si, DataBasket db); st.remove(StockItem si, Databasket db) )
    It is not always necessary to add StockItems, but it may also sufficient only to increase the number of counted CatalogItems.
    ( st.add(String sKey, int nCount, DataBasket db); st.remove(String sKey, int nCount, DataBasket db) )


Uses:
CountingStock  CountingStockImpl  StockItem  StockItemImpl  Catalog  CatalogImpl  



SourceCode

// mainly imports
   import data.CountingStock;
   import data.ooimpl.CountingStockImpl;
   import data.ooimpl.StockItemImpl;

// Main Class
   public class MyShop extends Shop
   {
   // Constructor
      public MyShop()
      {
         super();
      }

   // Main Method
      public static void main (String[] noArgs)
      {
      // the singleton instance of the shop
         MyShop myshop = new MyShop();

       1
      // getting the catalog
         clothesCatalog = (CatalogImpl) Shop.getTheShop().getCatalog("clothesCatalog");

       2
      // a new instance of CountingStock
         CountingStock clothesCountingStock =
         new CountingStockImpl("clothesCountingStock", (CatalogImpl) clothesCatalog);

       5-1
      // adding five trousers to the stock
         for (int i=0; i<5; i++)
            clothesCountingStock.add(new StockItemImpl("Trousers"), (DataBasket) null);

       5-2
      // another way to add a few hats
         clothesCountingStock.add("Hats", 3, (DataBasket) null);

       4
      // adding the countingstock to the shop
         myshop.addStock(clothesCountingStock);
      }
   }