From: <jm...@us...> - 2005-11-10 03:53:47
|
Update of /cvsroot/struts/dialogs/src/net/jspcontrols/shoppingcart In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18060/src/net/jspcontrols/shoppingcart Added Files: CartAction.java CartItem.java ShoppingCart.java ShoppingItem.java Log Message: --- NEW FILE: CartAction.java --- package net.jspcontrols.shoppingcart; import net.jspcontrols.dialogs.actions.DialogAction; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForm; import org.apache.struts.action.DynaActionForm; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; /** * Created by IntelliJ IDEA. * User: mjouravlev * Date: Nov 9, 2005 * Time: 10:10:29 AM * To change this template use Options | File Templates. */ public class CartAction extends DialogAction { private void returnToSender(HttpServletRequest request, HttpServletResponse response) throws IOException { String originatingURL = request.getRequestURL().toString(); System.out.println("CartAction.onInit, redirecting to: " + originatingURL); response.sendRedirect(originatingURL); } public ActionForward onInit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Redirect to original location; might be the cart itself. returnToSender(request, response); // Report to Struts that request was processed. return null; } public ActionForward onChange(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Redirect to original location; might be the cart itself. returnToSender(request, response); // Report to Struts that request was processed. return null; } public ActionForward onDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm poForm = (DynaActionForm) form; int itemID = ((Integer) poForm.get("itemID")).intValue(); CartItem[] lines = (CartItem[]) poForm.get("lines"); // Shrink array on deletion if (lines != null && itemID < lines.length) { CartItem[] newLines = new CartItem[lines.length-1]; if (newLines.length > 0) { for (int i=0; i<itemID; i++) { newLines[i] = lines[i]; } for (int k=itemID; k<newLines.length; k++) { newLines[k] = lines[k+1]; } } poForm.set("lines", newLines); } // Redirect to original location; might be the cart itself. returnToSender(request, response); // Report to Struts that request was processed. return null; } /** * Returns an <code>ActionForward</code> instance describing the View * for current dialog state, usually a forward to a JSP page. * <p> * If you want to use the default implementation, define the View * under "DIALOG-VIEW" name in <forward> element of your action * mapping. * <p> * To use different mapping name, define the view in <forward> * element of your action mapping, and override this method to return * ActionForward object for your mapping name. * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * * @exception Exception if an exception occurs * @return ActionForward instance describing the View for dialog state */ public ActionForward getDialogView(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { DynaActionForm poForm = (DynaActionForm) form; CartItem[] lines = (CartItem[]) poForm.get("lines"); // Initialize shopping cart with free gifts from our store if (lines == null || lines.length == 0) { lines = new CartItem[] { new CartItem(new ShoppingItem("BFG-666", "Vodoo Doll", 2000), 1), new CartItem(new ShoppingItem("aaa", "some aaa", 100), 2), new CartItem(new ShoppingItem("bbb", "some bbb", 350), 3) }; poForm.set("lines", lines); } return super.getDialogView(mapping, form, request, response); } } --- NEW FILE: CartItem.java --- package net.jspcontrols.shoppingcart; /** * Created by IntelliJ IDEA. * User: mjouravlev * Date: Nov 9, 2005 * Time: 10:47:58 AM * To change this template use Options | File Templates. */ public class CartItem extends ShoppingItem { int quantity; public int getQuantity() {return quantity;} public void setQuantity(int quantity) {this.quantity = quantity;} public CartItem(ShoppingItem item, int quantity) { super(item.getArticleNo(), item.getName(), item.getPrice()); this.quantity = quantity; } } --- NEW FILE: ShoppingCart.java --- package net.jspcontrols.shoppingcart; import java.util.Map; import java.util.HashMap; /** * */ public class ShoppingCart { Map items = new HashMap(); public ShoppingCart() { CartItem cartItem; cartItem = new CartItem(new ShoppingItem("aaa", "some aaa", 100), 1); items.put(cartItem.getArticleNo(), cartItem); cartItem = new CartItem(new ShoppingItem("bbb", "some bbb", 3500), 2); items.put(cartItem.getArticleNo(), cartItem); } /** * Adds item to the basket * @param shoppingItem shopping item */ public void addItem(ShoppingItem shoppingItem) { CartItem cartItem = new CartItem(shoppingItem, 1); items.put(cartItem.getArticleNo(), cartItem); } /** * Adds item to the basket * @param shoppingItem shopping item */ public void addItems(ShoppingItem shoppingItem, int quantity) { CartItem item = (CartItem) items.get(shoppingItem.getName()); if (item != null) { item.setQuantity(item.getQuantity()+quantity); } else { CartItem cartItem = new CartItem(shoppingItem, quantity); items.put(cartItem.getArticleNo(), cartItem); } } /** * Removes item from the basket * @param item shopping item * @return true if item was found and removed */ public boolean removeItem(ShoppingItem item) { return items.remove(item.getName()) != null; } /** * Cleans basket. Does not recreates basket because instance might be used * for other purposes like synchronization. */ public void clear() { items.clear(); } public Map getItems() {return items;} public void setItems(Map items) {this.items = items;} String cartVersion = "ver. 1.0"; public String getCartVersion() {return cartVersion;} public void setCartVersion(String cartVersion) {this.cartVersion = cartVersion;} } --- NEW FILE: ShoppingItem.java --- package net.jspcontrols.shoppingcart; /** * Created by IntelliJ IDEA. * User: mjouravlev * Date: Nov 9, 2005 * Time: 9:05:50 AM * To change this template use Options | File Templates. */ public class ShoppingItem { String articleNo; public String getArticleNo() {return articleNo;} public void setArticleNo(String articleNo) {this.articleNo = articleNo;} String name; public String getName() {return name;} public void setName(String name) { System.out.println("ShoppingItem: setting name to: " + name); this.name = name; } long price; public long getPrice() {return price;} public ShoppingItem(String articleNo, String name, long price) { this.articleNo = articleNo; this.name = name; this.price = price; } } |