Java
  Home arrow Java arrow Page 6 - Online Store Application
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Online Store Application
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 19
    2005-03-16

    Table of Contents:
  • Online Store Application
  • Understanding the Page Control Flow
  • Structuring the Database Tables
  • The DatabaseUtil Class
  • The MenuBean Class
  • The ShoppingItemBean Class
  • Registering the Beans in the Application Configuration File
  • Adding the ActionListener
  • Creating the JSP Pages
  • The search.jsp Page
  • The shoppingCart.jsp Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By McGraw-Hill/Osborne


       · I go thru the online application, and I'm new to this topic, can anyone recreate...
     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway