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" 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> </TD> <TD> </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.
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.
|
I l @ ve RuBoard |