コード例 #1
0
ファイル: ControlServlet.java プロジェクト: sphan/COMP9321
  /**
   * Add the selected items into cart.
   *
   * @param request The HttpServletRequest that contains the items to be added.
   * @return The name of the JSP page that is to be directed to.
   */
  public String add(HttpServletRequest request) {
    Cart myCart = (Cart) request.getSession().getAttribute("cart");
    HashMap<String, HashMap<StockType, LinkedList<Stock>>> itemsAlreadyInCart =
        new HashMap<String, HashMap<StockType, LinkedList<Stock>>>();

    String[] itemsToAdd = request.getParameterValues("addToCart");
    if (itemsToAdd == null) {
      request.setAttribute("cartSize", myCart.getCartSize());
      return "cart.jsp";
    }

    for (String item : itemsToAdd) {
      // if item is not in cart.
      LinkedList<Stock> inCart = itemIsInCart(item, myCart.getItems());
      if (inCart == null) {
        System.out.println(item);
        myCart.addToCart(getItem(item));
      } else {
        HashMap<StockType, LinkedList<Stock>> duplicated =
            new HashMap<StockType, LinkedList<Stock>>();
        duplicated.put(getItem(item).getType(), inCart);
        itemsAlreadyInCart.put(item, duplicated);
      }
    }
    request.setAttribute("cartSize", myCart.getCartSize());
    request.setAttribute("totalCost", getTotal(myCart));
    request.setAttribute("alreadyInCartSize", itemsAlreadyInCart.size());
    request.setAttribute("alreadyInCart", itemsAlreadyInCart);
    return "cart.jsp";
  }
コード例 #2
0
ファイル: ControlServlet.java プロジェクト: sphan/COMP9321
 /**
  * Get total cost of purchase in cart.
  *
  * @param myCart The cart.
  * @return The total cost.
  */
 public float getTotal(Cart myCart) {
   float total = 0;
   for (Stock s : myCart.getItems()) {
     total += s.getPrice();
   }
   return total;
 }
コード例 #3
0
ファイル: ControlServlet.java プロジェクト: sphan/COMP9321
  /**
   * Check out the items that the user had added into cart and display the list of items without
   * check boxes. Users cannot remove items on this page.
   *
   * @param request The HttpServletRequest
   * @return The next page to be directed to.
   */
  public String checkout(HttpServletRequest request) {
    Cart myCart = (Cart) request.getSession().getAttribute("cart");
    float totalPrice = 0;

    for (Stock s : myCart.getItems()) {
      totalPrice += s.getPrice();
    }
    request.setAttribute("totalCost", totalPrice);
    return "confirm.jsp";
  }
コード例 #4
0
 @Override
 public void reserveInventory(Cart cart) {
   for (Item item : cart.getItems()) {
     try {
       inventorySystem.reserve(item.sku, item.quantity);
     } catch (InsufficientInventoryException insufficientInventoryException) {
       throw new OrderException(
           "Insufficient inventory for item " + item.sku, insufficientInventoryException);
     }
   }
 }
コード例 #5
0
ファイル: ControlServlet.java プロジェクト: sphan/COMP9321
  /**
   * Remove the selected items in cart.
   *
   * @param request The HttpServletRequest that contains the items to be removed.
   * @return The name of the JSP page to be directed to.
   */
  public String remove(HttpServletRequest request) {
    Cart myCart = (Cart) request.getSession().getAttribute("cart");
    LinkedList<Stock> itemsInCart = myCart.getItems();

    String[] itemsToRemove = request.getParameterValues("removeFromCart");
    if (itemsToRemove == null) return "cart.jsp";

    for (String item : itemsToRemove) {
      itemsInCart.remove(getItem(item));
    }
    request.setAttribute("cartSize", myCart.getCartSize());
    request.setAttribute("totalCost", getTotal(myCart));

    return "cart.jsp";
  }