Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
9.4 An On Line Store Using a Shopping Cart and Session Tracking
221
Listing 9.6 ShoppingCart.java
package coreservlets;
import java.util.*;
/** A shopping cart data structure used to track orders.
* The OrderPage servlet associates one of these carts
* with each user session.
*/
public class ShoppingCart {
private Vector itemsOrdered;
/** Builds an empty shopping cart. */
public ShoppingCart() {
itemsOrdered = new Vector();
}
/** Returns Vector of ItemOrder entries giving
* Item and number ordered.
*/
public Vector getItemsOrdered() {
return(itemsOrdered);
}
/** Looks through cart to see if it already contains
* an order entry corresponding to item ID. If it does,
* increments the number ordered. If not, looks up
* Item in catalog and adds an order entry for it.
*/
public synchronized void addItem(String itemID) {
ItemOrder order;
for(int i=0; i
order = (ItemOrder)itemsOrdered.elementAt(i);
if (order.getItemID().equals(itemID)) {
order.incrementNumItems();
return;
}
}
ItemOrder newOrder = new ItemOrder(Catalog.getItem(itemID));
itemsOrdered.addElement(newOrder);
}
/** Looks through cart to find order entry corresponding
* to item ID listed. If the designated number
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved