HowTos - Data Management: Catalog

Define a simple CatalogItem

Description:
CatalogItems are Catalogs/SubCatalogs or items itself. They are meant to represent a named category of items and to be stored in a Catalog.
It is often usefull to subclass CatalogItemImpl, but implementing the interface CatalogItem may also be necessary.
This tutorial shows you a basic implementation of CatalogItemImpl for a simple CatalogItem.

Used classes:

Related topics:

ToDo:

  1. Create a subclass of CatalogItemImpl.
  2. Define the constructor. In this case the item will have a name and a value as the standard implementation of CatalogItemImpl offers. You have to invoke the super constructor either with name or name and value. Thus the simplest implementation of CatalogItemImpl would only have a name.
  3. You also have to implement the protected CatalogItemImpl getShallowClone() method that will return a copy of the current CatalogItemImpl. Simply instantiate a new CatalogItemImpl of your class and set the current values to the constructor.
  4. Additionally you can define a method for setting the items value.

Example Source Code:

import data.ooimpl.CatalogItemImpl;
			
1
public class DataBasicCatalogItem extends CatalogItemImpl
{
			
    2
    public DataBasicCatalogItem(String name, Value value)
    {
        super(name, value);
    }
			
    3
    protected CatalogItemImpl getShallowClone()
    {
        return (CatalogItemImpl)
            new DataBasicCatalogItem(this.getName(), this.getValue());
    }
			
    4
    public void setValue(Value value)
    {
        this.setValue(value);
    }
			
}
		

Back to:


Define an advanced CatalogItem

Description:
As shown in "Define a simple CatalogItem" now a CatalogItemImpl will be defined with some more possibilities.
Normally you will need more than just a name and a value for your item. You can define as many values as you want for your item, in this case there are two prices and a Catalog (you can nest as you want) defined in addition to the name. You can as well define values that not directly represent the item and you can add some helper/setter/getter methods as needed.

Used classes:

Related topics:

ToDo:

  1. Create a subclass of CatalogItemImpl.
  2. Define the variables for the additional values. price1, price2 and catalog are the values that directly represent the CatalogItem (and therefore later will be set in the constructor).
    comparePrice is an additional variable used for internal calculations and does not represent the item.
  3. Define the constructor and invoke the super constructor with the name. Assign the CatalogItem values as defined before.
  4. Implement the protected CatalogItemImpl getShallowClone() method and return a copy of your current CatalogItemImpl class.
  5. Sometimes it is needful to define a toString() method that will output the items values as string.
  6. You can calculate with your item variables and so define a get method for returning the sum of the two prices in this case.
  7. As mentioned before, comparePrice is not a representating variable of the item and thus not set by the items constructor. Define set-methods if you need to initialize your other variables.

Example Source Code:


1
public class DataAdvancedCatalogItem extends CatalogItemImpl
{
			
    2
    private int price1;
    private int price2;
    private Catalog catalog;

    private int comparePrice;
			
    3
    public DataAdvancedCatalogItem(String name, int price1, int price2, Catalog catalog)
    {
        super(name);
        this.price1 = price1;
        this.price2 = price2;
        this.catalog = catalog;
    }
			

    4
    protected CatalogItemImpl getShallowClone()
    {
        return (CatalogItemImpl)
            new DataAdvancedCatalogItem(this.getName(), this.price1, this.price2, this.catalog);
    }
			
    5
    public String toString()
    {
        return this.getName() + "-" + this.price1 + "-" + 
            this.price2 + "-" + this.catalog.getName();
    }
			
    6
    public int getSum()
    {
        return (this.price1 + this.price2);
    }
			
    7
    public void setComparePrice(int value)
    {
        this.comparePrice = value;
    }
			
}
		

Back to:


Incorporate a Catalog

Description:
An important data structure is the Catalog. It provides all what is needed to store and administrate the Shop´s goods.
It is also easy to display a Catalog´s contents with already implemented FormSheets, the JCatalogTable and the DefaultCatalogItemTED for example.
The use of CatalogImpl, the implementation of the interface Catalog is recommended.

Used classes:

Related topics:

ToDo:

  1. Create a new instance of CatalogItemImpl.
  2. Define a fitting subclass of CatalogItemImpl and add it to the newly created CatalogImpl. Therefore refer to Define a simple CatalogItem or Define an advanced CatalogItem.
    In this case the simple CatalogItemImpl from above is used.
    Assign an instance of DataBasket if the addition to the catalog is performed within a SaleProcess.
  3. Add the catalog to the shop's global list of catalogs.
  4. If you remove an item do not forget to catch the VetoException that can be possible if another process currently handles the catalog.

Example Source Code:

 
        1
        Catalog<DataBasicCatalogItem> simpleCatalog = new CatalogImpl<DataBasicCatalogItem>(SimpleCatalog);
			
        2
        simpleCatalog.add(
                new DataBasicCatalogItem("item1", new IntegerValue(10)),
                null);
			
        3
        Shop.getTheShop().addCatalog(simpleCatalog);
			
        4
        try
        {
            simpleCatalog.remove("item1", null);
        }
        catch (VetoException e)
        {
            e.printStackTrace();
        }
		

Back to:


Define a new AbstractCurrency

Description:
EUROCurrencyImpl provides the recent german currency EURO. If you want to incorporate a new currency you will have two alternatives. You may implement interface Currency extending CatalogImpl and make your own implementation of CurrencyImpl. But this would force you to define your own MoneyBag extending CountingStockImpl, because MoneyBagImpl needs an instance of CurrencyImpl in it´s constructor.
Second alternative is create a subclass of AbstractCurrency and to overwrite the key methods like done here.

Used classes:

Related topics:

ToDo:

  1. Create a subclass of AbstractCurrency.
  2. Add the currency's constructor and invoke the super constructor with the fitting Locale.
  3. Implement the protected CurrencyItemData[] getCurrencyItemData() method. Therein you define the names and values of your currency as CurrencyItemData.

Example Source Code:

 
1
public class DataAbstractCurrency extends AbstractCurrency
{
			
    2
    public DataAbstractCurrency(String name)
    {
        super(name, Locale.GERMANY);
    }
			
    3
    protected CurrencyItemData[] getCurrencyItemData()
    {
        CurrencyItemData[] currencyArrayReturn =
            {
			
                // add CurrencyItemData with name and value
                new CurrencyItemData("1-Bronze-Taler", 10),
                new CurrencyItemData("1-Silber-Taler", 100),
                new CurrencyItemData("1-Gold-Taler", 1000)
            };
        return currencyArrayReturn;
    }
			
}
		

Back to:


Incorporate a Currency

Description:
A Currency is a special Catalog. Its CatalogItems are CurrencyItems that represent the different denominations of the Currency.


Used classes:

Related topics:

ToDo:

  1. Eventually create a new Currency. Therefore refer to Define a new AbstractCurrency.
  2. Create a new instance of CurrencyImpl. In this case the above defined CurrencyEuro class was used.
  3. Add the currency to the Shop's global list of catalogs.

Example Source Code:

 
        2
        AbstractCurrency abstractCurrency = new DataAbstractCurrency("SimpleCurrency");
			
        3
        Shop.getTheShop().addCatalog(abstractCurrency);
		

Back to:


previous Application Architecture: CommonData Management: DataBasket next



by Thomas Ryssel