Methods Summary |
---|
protected void | addItemToCart(java.lang.String itemID, ShoppingCart cart)
Item item = findItem(itemID);
cart.addItem(item);
|
protected void | clearCart(ShoppingCart cart)
cart.clear();
|
protected void | doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
HttpSession session = req.getSession(true);
ShoppingCart cart = (ShoppingCart) session.getAttribute(CART);
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute(CART, cart);
}
updateShoppingCart(req, cart);
|
protected Item | findItem(java.lang.String itemID)
// this would really need to talk with an EJB to retrieve
// an Item from a database. But for this recipe we'll keep
// it simple.
return new Item(itemID, "Description " + itemID);
|
protected java.lang.String | getItemID(javax.servlet.http.HttpServletRequest req)
String itemID = req.getParameter("itemID");
if (itemID == null || "".equals(itemID)) {
return INVALID;
} else {
return itemID;
}
|
protected java.lang.String | getOperation(javax.servlet.http.HttpServletRequest req)
String operation = req.getParameter("operation");
if (operation == null || "".equals(operation)) {
return INVALID;
} else {
if (!INSERT_ITEM.equals(operation)
&& !REMOVE_ITEM.equals(operation)
&& !REMOVE_ALL.equals(operation)) {
return INVALID;
}
return operation;
}
|
protected void | removeItemFromCart(java.lang.String itemID, ShoppingCart cart)
cart.removeItem(itemID);
|
protected void | updateShoppingCart(javax.servlet.http.HttpServletRequest req, ShoppingCart cart)
String operation = getOperation(req);
if (INSERT_ITEM.equals(operation)) {
addItemToCart(getItemID(req), cart);
} else if (REMOVE_ITEM.equals(operation)) {
removeItemFromCart(getItemID(req), cart);
} else if (REMOVE_ALL.equals(operation)) {
clearCart(cart);
} else {
throw new ServletException("Invalid Shopping Cart operation: " +
operation);
}
|