FileDocCategorySizeDatePackage
CatalogBean.javaAPI DocExample2523Thu Jun 28 16:14:16 BST 2001com.ora.jsp.beans.shopping

CatalogBean

public class CatalogBean extends Object implements Serializable
This class represents a product catalog. It holds a list of products available for sale.

This is just a demo so the product list is hardcoded, created at instantiation. A real version would get the information from an external data source.

author
Hans Bergsten, Gefion software
version
1.0

Fields Summary
private Vector
catalog
Constructors Summary
public CatalogBean()
Constructor. Creates all ProductBean objects and adds them to the catalog.


                    
      
        ProductBean prod = new ProductBean();
        prod.setId("1");
        prod.setName("JavaServer Pages");
        prod.setDescr("Learn how to develop a JSP based web application.");
        prod.setPrice(32.95f);
        catalog.addElement(prod);

        prod = new ProductBean();
        prod.setId("2");
        prod.setName("Java Servlet Programming");
        prod.setDescr("Learn how to develop a servlet based web application.");
        prod.setPrice(32.95f);
        catalog.addElement(prod);

        prod = new ProductBean();
        prod.setId("3");
        prod.setName("Java In a Nutshell");
        prod.setDescr("Learn the Java programming language.");
        prod.setPrice(32.95f);
        catalog.addElement(prod);
    
Methods Summary
public ProductBeangetProduct(java.lang.String id)
Returns one product, or throws an exception if not found

param
id the product id. The String version is to be able to use the useProperty action
return
a ProductBean
exception
Exception if the id doesn't match a product

        boolean isFound = false;
        ProductBean product = null;
        Enumeration prods = getProductList();
        while (prods.hasMoreElements()) {
            product = (ProductBean) prods.nextElement();
            if (product.getId().equals(id)) {
                isFound = true;
                break;
            }
        }
        if (!isFound) {
            throw new Exception("Product id " + id + " doesn't match a product");
        }
        return product;
    
public java.util.EnumerationgetProductList()
Returns a list of all products.

return
a Enumeration with ProductBean elements

        return catalog.elements();