java - 通过增加数量值将相同的商品添加到购物车,而不是将商品添加到新行

标签 java jsp

我遇到了如何通过增加数量值将新商品添加到购物车中的问题,而不是在购物车中已存在该商品的情况下将商品添加到新行中。

下面是JSP实现MVC的代码

型号

public class Item {

   public String id;
   public String name;
   public double price;
   public int quantity;

    @Override
    public String toString() {
        return "Item{" + "id=" + id + ", name=" + name + ", price=" + price + ", quantity=" + quantity + '}';
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public Item(String id, String name, double price, int quantity) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

}

Controller

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession mysession = request.getSession();
        ArrayList mycart = (ArrayList) mysession.getAttribute("itemlist");
        double value = (Double) mysession.getAttribute("total");

        String i1 = request.getParameter("item1");
        String i2 = request.getParameter("item2");
        String delete = request.getParameter("del");

        if (i1 != null) {
            Item myitem = new Item("1","Kluang Man",3.99,1);
            value = value + 3.99;
            mycart.add(myitem);
            mysession.setAttribute("itemlist", mycart);
            mysession.setAttribute("total", value);
            response.sendRedirect("cart.jsp");
        } else if (i2 != null) {
           Item myitem = new Item("2","Usop Santorian",5.99,1);
            value = value + 5.99;
            mycart.add(myitem);
            mysession.setAttribute("itemlist", mycart);
            mysession.setAttribute("total", value);
            response.sendRedirect("cart.jsp");

        } else if (delete != null) {
            Item item_to_Delete = (Item) mycart.get(Integer.parseInt(delete));
            value = value - item_to_Delete.price;
            mysession.setAttribute("total", value);

            mycart.remove(Integer.parseInt(delete));

            mysession.setAttribute("tod", delete);

            response.sendRedirect("cart.jsp");
        }
    }

查看

<form method="post" action="catalog">
    <table class="table table-bordered">
      <thead class="thead-light">
         <tr align="center">
            <th scope="col">Product Name</th>
            <th scope="col">Quantity</th>
            <th scope="col">Price</th>
            <th scope="col">Action</th>
         </tr>
      </thead>             
      <tbody> 
        <%  if (session.getAttribute("itemlist") != null) {
          ArrayList mycart = (ArrayList) session.getAttribute("itemlist");
          for (int i = 0; i < mycart.size(); i++) {
            Item it = (Item) mycart.get(i);
        %>  
         <tr>
            <td align="center"><% out.print(it.name);%></td>
            <td align="center"><% out.print(it.quantity);%></td>
            <td align="center"><% out.print(it.price);%></td>
            <td class="text-center align-middle"><input name="del" 
                type="submit" value="Remove from cart" class="btn btn-outline- 
                danger" onclick="this.value=<%out.print(i);%>"></input></td>
         </tr>
       <%
             }
           }
       %>
       <tr>
         <td colspan="3" align="right"><h4>Total (RM)</h4></td>
         <td align="center"><h4><%= session.getAttribute("total") %></h4></td>
      </tr>
    </tbody>
   </table>
 </form>

如果该项目存在,输出应增加添加的项目的数量并更新总值(value)。

最佳答案

因为您已将 myCart 定义为 ArrayList。 您正在向数组列表添加一个新对象。

假设列表的元素是类Item,如果要修改对象,那么需要先在列表中找到对应的元素,然后增加其数量。

ArrayList.add() 不会修改对象。它添加了新对象。

关于java - 通过增加数量值将相同的商品添加到购物车,而不是将商品添加到新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649252/

相关文章:

java - 在 PDFClown 的 StaticNote 中更改字体颜色

java - 如何使用 JavaMail 发送非本地附件

java - 将业务逻辑放在 Java Bean 中?

jquery - 如何对下拉框中选定的名称执行操作

java - <set> 中的属性无效 : "null" in new ArrayList at JSP

css - 当我运行我的 jsp 页面时,只打印了一半的表单

java - 如何访问 GAE 上的拉取队列

java - 开发代码时如何模拟防火墙和代理?

url 连接中的 java.lang.nullpointerException

java - Mockito - 0 个预期匹配器,2 个已记录 (InvalidUseOfMatchersException)