If you want to build an online store application using Java, look no further. This article begins with an overview of the application, and then discusses the applicatin development process. It is taken from chapter 14 of JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837).
public class ShoppingCartBean { ArrayList shoppingItems = new ArrayList(); public ShoppingCartBean() { } public Collection getShoppingItems() { return shoppingItems; } public void setShoppingItems(Collection shoppingItems) { this.shoppingItems = new ArrayList(shoppingItems); } public void addShoppingItem(ShoppingItemBean item) { shoppingItems.add(item); } public void removeShoppingItems() { shoppingItems.clear(); } public float getTotal() { float total = 0; Iterator iterator = shoppingItems.iterator(); while (iterator.hasNext()) { ShoppingItemBean item = (ShoppingItemBean) iterator.next(); total += item.getPrice() * item.getQuantity(); } return total; } }
The ShoppingCartBean class provides the shoppingItems property, whose value is a collection of ShoppingItemBean instances added by the user. Also, it provides three other methods:
addShoppingItem adds a ShoppingItemBean to the collection.
removeShoppingItems clears the shoppingItems collection.
getTotal calculates the amount of the purchase.
The getTotal method iterates all ShoppingItemBean instances in the collection and multiplies its price and quantity properties:
float total = 0; Iterator iterator = shoppingItems.iterator(); while (iterator.hasNext()) { ShoppingItemBean item = (ShoppingItemBean) iterator.next(); total += item.getPrice() * item.getQuantity(); } return total;
The OrderBean Class
The OrderBean class, shown in Listing 14-10, encapsulates user information for a purchase order. This class has five properties: contactName, deliveryAddress, creditCardName, creditCardNumber, and creditCardExpiryDate.
Listing 14-10The OrderBean Class
package buydirect;
public class OrderBean { private String contactName; private String deliveryAddress; private String creditCardName; private String creditCardNumber; private String creditCardExpiryDate;
public String getContactName() { return contactName; } public void setContactName(String contactName) { this.contactName = contactName; } public String getDeliveryAddress() { return deliveryAddress; } public void setDeliveryAddress(String deliveryAddress) { this.deliveryAddress = deliveryAddress; } public String getCreditCardName() { return creditCardName; } public void setCreditCardName(String creditCardName) { this.creditCardName = creditCardName; } public String getCreditCardNumber() { return creditCardNumber; } public void setCreditCardNumber(String creditCardNumber) { this.creditCardNumber = creditCardNumber; } public String getCreditCardExpiryDate() { return creditCardExpiryDate; } public void setCreditCardExpiryDate(String creditCardExpiryDate) { this.creditCardExpiryDate = creditCardExpiryDate; } }
The SearchBean Class
The SearchBean class encapsulates the search key and search result for a product search. This class is presented in Listing 14-11.
public class SearchBean { private String searchKey; public String getSearchKey() { return searchKey; } public void setSearchKey(String searchKey) { this.searchKey = searchKey; } public Collection getSearchResult() { // get DatabaseUtil instance FacesContext facesContext = FacesContext.getCurrentInstance(); ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext(); DatabaseUtil dbUtil = (DatabaseUtil) servletContext.getAttribute("DATABASE_UTIL"); return dbUtil.searchProducts(searchKey); } }
The searchKey property contains the search key. The getSearchResult method returns a collection containing all of the products resulting from a search. The getSearch method obtains its value by calling the searchProducts method of the DatabaseUtil class.
The BrowseBean Class
The BrowseBean class, shown in Listing 14-12, encapsulates all of the products in a category. The bean is referenced by the browse page and retrieves all of the products belonging to a category from the Products table. The browse page must be called by passing a parameter called categoryId, so that the BrowseBean class knows which category to use in its constructor.
public class BrowseBean { private String categoryId; public BrowseBean() { FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext!=null) categoryId = (String) facesContext.getExternalContext(). getRequestParameterMap().get("categoryId"); } public String getCategoryId() { return categoryId; } public void setCategoryId(String categoryId) { this.categoryId = categoryId; } public Collection getResult() { // get DatabaseUtil instance FacesContext facesContext = FacesContext.getCurrentInstance(); ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext(); DatabaseUtil dbUtil = (DatabaseUtil) servletContext.getAttribute("DATABASE_UTIL"); return dbUtil.getProductsByCategory(categoryId); } }
Notice how the constructor obtains the category identifier from the Request object in the ExternalContext object. It assigns this value to its categoryId property. When the getResult method is called, the categoryId property value is passed to the getProductsByCategory method of the DatabaseUtil instance.
This article is excerpted from JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837). Check it out at your favorite bookstore today. Buy this book now.