Buying More Than One Thing at Once

I l @ ve RuBoard

Buying a single item is relatively straightforward because you can handle it directly from the URL. But the client wants the customer to be able to purchase several items at the same time.

Listing 10.7 shows a section of Category.jsp rewritten to let the customer buy a quantity of multiple books. It replaces the current product display loop.

Listing 10.7 Category.jsp Revisited
 <FORM METHOD="POST" ACTION="buyit.jsp" TARGET="tempwindow"> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse:  collapse" graphics/ccc.gif bordercolor="#111111" width="100%" id="AutoNumber4" height="253">  <tr>     <th width="38%" height="23" align="left">Title</th>     <th width="23%" height="23" align="left">Author</th>     <th width="11%" height="23" align="left">Price</th>     <th width="28%" height="23" align="center">Quantity</td>   </tr>     <%     Iterator iter = cat.getProducts().iterator();     while (iter.hasNext()) {      Product prod = (Product) iter.next(); %>   <tr>     <td width="38%" height="38" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">         <A HREF="Product.jsp?ISBN=<%= prod.getISBN() %>">         <%= prod.getTitle() %></A>     </span></td>     <td width="23%" height="38" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">        <%= prod.getAuthorString() %>     </span></td>     <td width="11%" height="38" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">     <%= prod.getPriceString() %>     </span></td>     <td width="28%" height="38" bgcolor="#00FFFF">     <p align="center"><font size="2">     <INPUT NAME="ISBN<%= prod.getISBN() %>" TYPE="TEXT" SIZE=2>     <INPUT TYPE="SUBMIT" VALUE="Buy">     </font></td>    </tr> </table> </form> 

The entire list of books has been wrapped in a table, a new heading called Quantity has been added to the table, and form fields have been placed beside each item to record the quantity. The Buy link has been changed to form Submit buttons (see Figure 10.2).

Figure 10.2. The multiple-buy category page.

graphics/10fig02.jpg

You might have noticed that the letters ISBN were added to the ISBN code for each book and that it was used as the form field name for each book. When you look at the revised buyit.jsp (see Listing 10.8), it becomes clear why.

Listing 10.8 A Second Go at buyit.jsp
 <%@ page import="com.bfg.product.Product" %> <%@ page import="com.bfg.cart.Cart" %> <%@ 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);      if ((request.getParameter(name) != null) &&          (request.getParameter(name).length() > 0)) {          try {              int quantity = nf.parse(request.getParameter(name)).intValue();              if (quantity > 0) {                  out.print("Looking for " + ISBN + "<BR>");                  Product prod = Product.findProduct(ISBN);                  if (prod != null) {                      cart.addItem(prod, quantity);                  }              }          }  catch (NumberFormatException e) {          }      }     } } %> <SCRIPT> if (window.opener && !window.opener.closed)   window.opener.location.reload(); window.close(); </SCRIPT> 

You need to prepend ISBN to the field name because a number of other parameters might get handed to buyit , including the Submit buttons. To make it easier to pick the actual quantity fields out of the crowd , you put a distinct tag in front to make them stand out.

First, get a list of all the parameters. Iterate over them, looking for ones starting with ISBN. If the value is not blank, try parsing the value (if the customer types "garbage" into a field, it will throw a number format exception and skip over that field). Then, if the quantity is more than 0 (you wouldn't want someone entering " “80" and ending up with a credit of several thousand dollars!), add the quantity to the cart.

WHAT'S THE RIGHT THING TO DO?

Invariably, you'll come across vexing questions of functionality in the middle of the project. For example, the way the display pages have been implemented, 0 is shown for the quantity and whatever the customer types in is added and submitted to the existing order.

You could just as validly display the current quantity ordered in the boxes and alter the quantity when the customer submits it. This would let the customer remove items from an order without having to go to the shopping cart.

Which is the right way to go? Whichever way the client wants it to work! This kind of question underlines the need to stay in close communication with the stakeholder for the project during development.

Nothing strikes more fear in the heart of a developer than a client who says, "Oh, I don't care ”do what you think is right." Nine times out of 10, what you decide to do will turn out to be not what the client wanted.

Extreme programming takes this point to its logical end, requiring the client to work directly with the developer through the entire project. You might not have this luxury in your project, but you should make sure that the guy or gal who has the final say isn't going on a month-long vacation to Tahiti right in the middle of your timeline.

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