HowTo's - Data Management: Stock

Define a StockItem

Description:
StockItems are Stocks (more precisely: SubStocks) or items itself. If they are used in a CountingStock, they'll be the representation of a certain amount of items of a special category. If they are used in a StoringStock, they'll represent an individual item of a special category.
It is often usefull to make a subclass of StockItemImpl, but implementing the interface StockItem may also be necessary.
If you wanted to use a CountingStock, you may make an instance of StockItemImpl itself.

Used classes:

Related topics:

ToDo:

  1. Create a subclass of StockItemImpl.
  2. Add constructors to set the items name and other attributes.Every constructor must invoke super(name), therefore a StockItem has to have a name.
  3. Add other useful methods.

Example Source Code:


1
public class DataStockItem extends StockItemImpl
{
			
    2
    public DataStockItem(String name)
    {
        super(name);
    }
			
    3
    public void setOwner(User user)
    {
        this.setOwner(user);
    }

    public User getOwner()
    {
        return this.getOwner();
    }
			
}
		

Back 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.

Used classes:

Related topics:

ToDo:

  1. Make a new instance of your used implementation of Stock (or select an existing instance of Stock).
  2. Use Stock.fillStockWithValue(DataBasket db, Value vTarget, StockFromValueCreator sfvc) to fill selected Stock with a certain Value.
  3. 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).

Example Source Code:

 
	// first define an AbstractCurrency for the Moneybags
        AbstractCurrency euroCurrency = new EUROCurrencyImpl("Euro-Catalog");
        Shop.getTheShop().addCatalog(euroCurrency);
			
    1
        // create a new MoneyBag and add to Shop
        MoneyBagImpl moneyBag1 = new MoneyBagImpl("MoneyBag1", euroCurrency);
        Shop.getTheShop().addStock(moneyBag1);
			
    2
        // fill the MoneyBag with 357, 12 Euros
        moneyBag1.fillStockWithValue((DataBasket) null,
                new IntegerValue(35712),
                new DefaultCountingStockFromValueCreator(
                        new CatalogItemValue())
                  );
			
    1
        // create a new MoneyBag and add to Shop
        MoneyBagImpl moneyBag2 = new MoneyBagImpl("MoneyBag2", euroCurrency);
        Shop.getTheShop().addStock(moneyBag2);
			
    3
        // fill the second Stock with items of the first
        // using StockFromValueCreator with BackTracking
        moneyBag2.fillStockWithValue((DataBasket) null,
                new IntegerValue(17854),
                new StockFromStockCreatorBT(
                        (Stock) moneyBag1,
                        new CatalogItemValue())
                );
		

Back 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. The use of CountingStockImpl, the implementation of the interface CountingStock, is sufficient in most cases.

Used classes:

Related topics:

ToDo:

  1. Get the instance of Catalog you want to incorporate a CountingStock on.
    (See also: Incorporate a Catalog).
  2. Make a new instance of CountingStock associated with the Catalog.
  3. Define (if necessary) a fitting subclass of StockItemImpl.
    (See also Define a StockItem).
    1. Add and remove StockItems as you wish.
    2. It is not always necessary to add StockItems, but it may also sufficient only to increase the number of counted CatalogItems.
  4. Add the Stock to the Shop's global list of stocks.

Example Source Code:

 
    1
        CatalogImpl catalog = (CatalogImpl) Shop.getTheShop().getCatalog("SimpleCatalog");
			
    2
        CountingStock countingStock = new CountingStockImpl(
                "simpleCatalogCS",
                catalog);
			
    4 a
        for(int i = 0; i < 5; i++)
        {
            countingStock.add(new StockItemImpl("Screw"), (DataBasket) null);
        }
			
    4 b
        countingStock.add("Screw Nut", 5, (DataBasket) null);
			
    5
        Shop.getTheShop().addStock(countingStock);
		

Back to:


Incorporate a StoringStock on a Catalog

Description:
This data structure is meant to store individual representations of entrys in the associated Catalog.
In contrast to the CountingStock it is meant to remember the differences between the StockItems.
The StoringStock could be displayed in the same way as the Catalog itself.
Using StoringStockImpl, the implementation of the interface StoringStock, is sufficient in most cases.

Used classes:

Related topics:

ToDo:

  1. Get the instance of Catalog you want to incorporate a StoringStock on.
    (See also: Incorporate a Catalog).
  2. Make a new instance of StoringStock associated with the Catalog.
  3. Define a fitting subclass of StockItemImpl.
    (See also: Define a StockItem).
  4. Add and remove StockItems as you wish.
  5. Add the Stock to the Shop's global list of stocks.

Example Source Code:

 
    1
        CatalogImpl catalog = (CatalogImpl) Shop.getTheShop().getCatalog("SimpleCatalog");
			
    2
        StoringStock storingStock =
            new StoringStockImpl("simpleCatalogSS", catalog);
			
    4
        for(int i = 0; i < 5; i++)
        {
            storingStock.add(new StockItemImpl("Shoes"), (DataBasket) null);
        }
			
    5
        Shop.getTheShop().addStock(storingStock);
		

Back to:


Incorporate a MoneyBag on a Currency

Description:
This data structure is meant to count the number of coins and notes in the associated Currency. It is a special CountingStock on a special Catalog.
The use of MoneyBagImpl, the implementation of the interface MoneyBag, is sufficient in most cases.

Used classes:

Related topics:

ToDo:

  1. Get the instance of AbstractCurrency you want to incorporate a MoneyBag on.
  2. Make a new instance of MoneyBag associated with the Currency.
  3. Add the MoneyBag to the Shop's global list of stocks.
  4. Add and remove coins and notes as you wish.

Example Source Code:

 
    1
        AbstractCurrency euroCurrency = new EUROCurrencyImpl("Euro_Catalog");
			
    2
        MoneyBagImpl moneyBag = new MoneyBagImpl("MoneyBag", euroCurrency);
			
    3
        Shop.getTheShop().addStock(moneyBag);
			
    4
        for(int i=0; i < euroCurrency.getCurrencyItemData().length; i++)
        {
            moneyBag.add((euroCurrency.getCurrencyItemData())[i].getName(), 5, (DataBasket) null);
            try
            {
                moneyBag.remove(
                        (euroCurrency.getCurrencyItemData())[i].getName(),
                        5,
                        (DataBasket) null);
            }
            catch (VetoException e)
            {
                e.printStackTrace();
            }
        }
		

Back to:


previous Data Management: DataBasketDisplay: FormSheet next


Valid HTML 4.01!