SOURCECODE |
How to... fill a Stock with Values
Description:
Every Stock implements the public method FillStockWithValue(DataBasket db, Value vTarget, StockFromValueCreator sfvc)
.
With this method, a CountingStock can be filled with a certain Value by using the DefaultCountingStockFromValueCreator.
An existing Stock can be filled with the contents of another Stock by using a StockFromStockCreator.
The transaction will be canceled, if the StockFromValueCreator doesn´t find enough fitting items to fill the Stock with the certain Value. If backtracking is needed to guarantee that a solution will be found if there is one, use StockFromStockCreatorBT.
These three implementations of the interface StockFromValueCreator are sufficient in most cases.
ToDo's:
- Make a new instance of your used implementation of Stock (or select an existing instance of Stock).
- Use
Stock.fillStockWithValue(DataBasket db, Value vTarget, StockFromValueCreator sfvc)
to fill selected Stock with a certain Value.
- Use the implementation StockFromStockCreator, if you want to fill one Stock with the Values of another Stock (the needed items will be removed from the second Stock).
Uses:
Stock StockFromValueCreator StockFromStockCreator StockFromStockCreatorBT DefaultCountingStockFromValueCreator
// mainly imports
import data.CatalogItemValue;
import data.StockFromValueCreator;
import data.DefaultCountingStockFromValueCreator;
import data.StockFromStockCreator;
import data.StockFromStockCreatorBT;
// 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();
// a new currency
CurrencyImpl dmCatalog = new CurrencyImpl("DM_Catalog");
// adding the currency to the shop
myshop.addCatalog(dmCatalog);
1
// a new moneybag
MoneyBagImpl mybag = new MoneyBagImpl("MoneyBag", dmCatalog);
// adding the bag to the shop
myshop.addStock(mybag);
2
// fill the moneybag with 428,63 DM
mybag.fillStockWithValue((DataBasket) null,
new IntegerValue(42863),
new DefaultCountingStockFromValueCreator(new CatalogItemValue()) );
1
// a new moneybag
MoneyBagImpl mybag2 = new MoneyBagImpl("MoneyBag2", dmCatalog);
// adding the bag to the shop
myshop.addStock(mybag2);
3
// fill a second stock with the items of the first
// (using a StockFromValueCreator with BackTracking)
mybag2.fillStockWithValue((DataBasket) null,
new IntegerValue(22863),
new StockFromStockCreatorBT((Stock) mybag, new CatalogItemValue()) );
}
}