Displaying and Editing the Shopping Cart

I l @ ve RuBoard

The mini-cart is nice for a quick heads-up on what you've bought, but the customer will also want to be able to view and edit a more detailed version of the cart. So, it's time to implement the shopping cart page, shown in Listing 10.9.

Listing 10.9 shoppingcart.jsp
 <%@ page import="com.bfg.product.Product" %> <%@ page import="java.util.Iterator" %> <%@ page import="com.bfg.cart.Cart" %> <%@ page import="com.bfg.cart.CartItem" %> <jsp:useBean id="cart" class="com.bfg.cart.Cart" scope="session"/> <head> <title>Your Shopping Cart</title> </head> <%@ include file="/jsp/includes/bfgheader.jsp" %>     <h2 align="center">Your Shopping Cart</h2> <FORM METHOD="POST" ACTION="changeit.jsp" TARGET="tempwindow"> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" graphics/ccc.gif bordercolor="#111111" width="100%">   <tr>     <th width="38%" height="23" align="left">Title</th>     <th width="11%" height="23" align="left">Price</th>     <th width="28%" height="23" align="center">Quantity</td>     <th width="23%" height="23" align="left">Total</th>   </tr>     <%     Iterator iter = cart.getItems();     while (iter.hasNext()) {      CartItem item = (CartItem) iter.next();      Product prod = item.getProduct(); %>   <tr>     <td width="38%" bgcolor="#00FFFF">     <font style="font-size: 10.0pt; font-family: Times New Roman">         <A HREF="Product.jsp?ISBN=<%= prod.getISBN() %>">         <%= prod.getTitle() %></A>     </span><BR><BR></td>     <td width="11%" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">     <%= prod.getPriceString() %>     </span></td>     <td width="28%" bgcolor="#00FFFF">     <p align="center"><font size="2">     <INPUT NAME="ISBN<%= prod.getISBN() %>" TYPE="TEXT" SIZE=2     value=<%= item.getQuantity() %>>     </font></td>     <td width="23%" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">     <%= item.getLineItemPriceString() %>     </span></td>   </tr>        <% }  %>   <TR><TD COLSPAN=4><HR></TD></TR>   <TR><TD>&nbsp;</TD>   <TD>&nbsp</TD>   <TD ALIGN="center">   <span style="font-size: 10.0pt; font-family: Times New Roman">       Total*   </span></TD>     <TD>   <span style="font-size: 10.0pt; font-family: Times New Roman">    <%= cart.getTotalString()%>   </span></TD></TR> </TABLE> <P> <INPUT TYPE=SUBMIT NAME="update" VALUE="Update Order"> <INPUT TYPE=SUBMIT NAME="checkout" VALUE="Check Out"><P> * - Total does not include shipping and/or taxes. </FORM> <%@ include file="/jsp/includes/bfgfooter.jsp" %> 

Again, you are liberally using stolen code from another page: This page works a lot like the Catalog page, except that it iterates over the cart instead of over the catalog.

The Author column has been taken out and replaced at the end with a line-item total. A total at the bottom has also been added for the entire order, with a comment that it doesn't include taxes and the like. The results are shown in Figure 10.3.

Figure 10.3. The shopping cart.

graphics/10fig03.jpg

The shopping cart sends its form data to another child window for processing, just as the catalog does. This child modifies and removes items instead of adding them, as you can see in Listing 10.10.

Listing 10.10 changeit.jsp
 <%@ page import="com.bfg.product.Product" %> <%@ page import="com.bfg.cart.Cart" %> <%@ page import="com.bfg.cart.CartItem" %> <%@ page import="java.util.Enumeration" %> <%@ page import="java.text.NumberFormat" %> <jsp:useBean id="cart" class="com.bfg.cart.Cart" scope="session"/> <% Enumeration names = request.getParameterNames(); NumberFormat nf = NumberFormat.getInstance(); while (names.hasMoreElements()) {     String name = (String) names.nextElement();     if (name.startsWith("ISBN")) {      String ISBN = name.substring(4);      Product prod = Product.findProduct(ISBN);      if (prod != null) {             CartItem item = cart.getItem(prod);         if (item != null) {             if ((request.getParameter(name) != null) &&                 (request.getParameter(name).length() > 0)) {             try {                 int quantity = nf.parse(request.getParameter(name)). intValue();                 out.print(quantity);                 if (quantity > 0) {                     item.setQuantity(quantity);                 }                 if (quantity == 0) {                     cart.removeItem(prod);                 }             }  catch (NumberFormatException e) {             }          }        }      }     } } %> <SCRIPT> if (window.opener && !window.opener.closed) <%     if (request.getParameter("checkout") != null) { %>    window.opener.location = "/bfg/jsp/checkout.jsp"; <% }  else { %>    window.opener.location.reload(); <% }  %> window.close(); </SCRIPT> 

You have the page run through the parameters just as in buyit , but this time you have it look for the matching item in the cart. If it's still there, you want it to check to see if the quantity is greater than 0. If it is, the page should modify the quantity of the cart item. If the quantity is 0, the page should remove the item from the cart altogether.

When the page is finished modifying the cart, it gets down to where it updates the parent window. You gave the Submit buttons names so that you can tell which one was pressed based on its presence in the parameter list. If the Checkout button was pressed, the code redirects the parent window to the as-yet-unwritten checkout page. Otherwise, you have it update the parent window to get the new contents.

CHECK, CHECK, AND CHECK AGAIN

It's just plain old good programming techniques. Never, ever, ever trust that you're going to get the value that you expect back from a method.

For example, in the changeit code, you have the code get the ISBN number from the parameter and then get the item with the corresponding code from the cart. But before the code does anything with the value, it checks to make sure that the value is not null.

How could it be null? After all, this page is called directly from the shopping cart page, and the only things that get passed as parameters are items known to be in the cart.

Well, suppose that someone zeroes out a quantity, removing the item from the cart. Then that customer hits the Back button, getting the previous version of the form with the item still in the cart ("Oops, I didn't want to remove that item."). Then the customer hits the Submit button again. Suddenly you've got the code looking for a match that isn't there. If you don't have the code check for null, it'll be throwing an exception and giving the customer an error page that will make them think much less of you as a developer.

Of course, if you really wanted to do the right thing, you should modify changeit to put new items in the cart if they aren't there.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net