SOURCECODE |
How to... incorporate a CountingStockFilter on a CountingStock
Description:
A CountingStockFilter is a CountingStock itself, in which a certain number of elements are filtered.
(See also: HowTo..incorporate a CountingStock on a Catalog, HowTo..define a StockItem )
This CountingStockFilter could be used in the same way as a normal CountingStock.
ToDo's:
- Incorporate a subclass of abstract CountingStockFilter.
- Add constructors to set the Filters name and attributes.
- Implement
public int countItems(String sKey, DataBasket db)
.
(This method determines, how many items of the source stock will be in the CountingStockFilter.)
- Implement
public Object clone()
.
- Make an instance of your CountingStockFilter, where it is to be used.
(Ex.: MyCountingStockFilter mcsf = new MyCountingStockFilter(CountingStock csToBeFiltered)
)
Use it instead of the source stock, where you only want the unfiltered items.
Uses:
CountingStockFilter CountingStock StockItem
// mainly imports
import data.filters.CountingStockFilter;
import data.StockItem;
import data.DataBasket;
import data.CountingStock;
1
// Main Class
public class MyCountingStockFilter extends CountingStockFilter
{
2
// Constructor
public MyCountingStockFilter(CountingStock csSource)
{
super(csSource);
}
3
// this is the core method, in which the filter gets its characteristic
public int countItems(String sKey, DataBasket db)
{
// Iterate all MoneyBagItems with given sKey
java.util.Iterator it = m_stSource.get(sKey, db, false);
// Instanciate a number for the Items which should not be filtered
int number = 0;
// Search for Items which should not to be filtered
StockItem si;
while (it.hasNext())
{
si = (StockItem)it.next();
if (si.getName().equals("Item which is unfiltered"))
number ++;
}
return number;
}
4
// clone()-method is abstract in AbstractStockFilter
// and has to be implemented
public Object clone()
{
MyCountingStockFilter mcsf =
new MyCountingStockFilter((CountingStock)m_stSource.clone());
return mcsf;
}
}