Home arrow Java arrow Page 6 - Online Store Application
JAVA

Online Store Application


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

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 54
March 16, 2005
TABLE OF CONTENTS:
  1. · Online Store Application
  2. · Understanding the Page Control Flow
  3. · Structuring the Database Tables
  4. · The DatabaseUtil Class
  5. · The MenuBean Class
  6. · The ShoppingItemBean Class
  7. · Registering the Beans in the Application Configuration File
  8. · Adding the ActionListener
  9. · Creating the JSP Pages
  10. · The search.jsp Page
  11. · The shoppingCart.jsp Page

print this article
SEARCH DEVARTICLES

Online Store Application - The ShoppingItemBean Class
(Page 6 of 11 )

The ShoppingItemBean class, shown in Listing 14-8, represents a shopping item. It has four properties: productId, productName, price, and quantity.

Listing 14-8  The ShoppingItemBean Class

  package buydirect;

  public class ShoppingItemBean {

   private String productId;
   private String productName;
   private float price;
   private int quantity;

   public ShoppingItemBean(
     String productId, String productName,
     float price, int quantity) {
     this.productId = productId;
     this.productName = productName;
     this.price = price;
     this.quantity = quantity;
   }

   public String getProductId() {
    
return productId;
   }

   public void setProductId(String productId) {
     this.productId = productId;
   }

   public String getProductName() {
     return productName;
   }

   public void setProductName(String productName) {
     this.productName = productName;
   }

   public int getQuantity() {
     return quantity;
   }

   public void setQuantity(int quantity) {
     this.quantity = quantity;
   }

   public float getPrice() {
     return price;
   }

   public void setPrice(float price) {
     this.price = price;
   }
}


The ShoppingCartBean Class

The ShoppingCartBean class represents a shopping cart. This class is shown in Listing 14-9.

Listing 14-9  The ShoppingCartBean Class

  package buydirect;

 
import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;

  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-10  The 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.

Listing 14-11  The SearchBean Class

  package buydirect;

  import java.util.ArrayList;
  import java.util.Collection;
  import javax.faces.context.FacesContext;
  import javax.servlet.ServletContext;

 
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.

Listing 14-12  The BrowseBean Class

  package buydirect;

  import java.util.ArrayList;
  import java.util.Collection;
  import javax.faces.context.FacesContext;
  import javax.servlet.ServletContext;

  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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials