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:
- Incorporate a subclass of abstract MoneyBagFilter.
- 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 MoneyBag will be in the MoneyBagFilter.)
- Implement
public Object clone()
.
- 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
// 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;
}
}