SOURCECODE

How to... incorporate a MoneyBagFilter on a MoneyBag


Description:
A MoneyBagFilter is a MoneyBag itself, in which a certain number of coins and notes are filtered.
(See also: HowTo..incorporate a MoneyBag on a Currency, HowTo..define a new Currency )
This MoneyBagFilter could be used in the same way as a normal MoneyBag.

ToDo's:
  1. Incorporate a subclass of abstract MoneyBagFilter.
  2. Add constructors to set the Filters name and attributes.
  3. Implement public int countItems(String sKey, DataBasket db).
    (This method determines, how many items of the source MoneyBag will be in the MoneyBagFilter.)
  4. Implement public Object clone().
  5. Make an instance of your MoneyBagFilter, where it is to be used.
    (Ex.: MyMoneyBagFilter mcsf = new MyMoneyBagFilter(MoneyBag mbToBeFiltered) )
    Use it instead of the source MoneBag, where you only want the unfiltered items.


Uses:
MoneyBagFilter  MoneyBag  



SourceCode

// mainly imports
   import data.filters.MoneyBagFilter;
   import data.StockItem;
   import data.DataBasket;
   import data.MoneyBag;

 1
// Main Class

   public class MyMoneyBagFilter extends MoneyBagFilter
   {

    2
   // Constructor
      public MyMoneyBagFilter(MoneyBag csSource)
      {
         super(csSource);
      }

    3
   // this is the core method, in which the filter gets its characteristic
      public int countItems(String sKey, DataBasket db)
      {
      // getting currency itemīs code
         int currencyItemCode = -1;
         for (int i = 0; i < CurrencyUS.US$.length; i++)
            if (CurrencyUS.US$[i].equals(sKey)) currencyItemCode = i;

      // filter all Items less worth than $5
         if (currencyItemCode == -1)
            return 0;
         else
         {
            if (CurrencyUS.US$_Value[currencyItemCode] < 500)
               return 0;
            else
               return m_stSource.countItems(sKey, db);
         }
      }

    4
   // clone()-method is abstract in AbstractStockFilter
   // and has to be implemented
      public Object clone()
      {
         MyMoneyBagFilter mmbf =
         new MyMoneyBagFilter((MoneyBag)m_stSource.clone());
         return mmbf;
      }
   }