简体   繁体   中英

Add same item to cart by increasing the quantity value instead of adding the item to a new row

I have a problem on how to add the new item to my shopping cart by increasing the quantity value instead of adding the item in a new row if the item already exists in the cart.

below are the codes JSP implementing MVC

Model

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");
        }
    }

View

<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>

The output should increase the quantity of the item added if the item exists and update the total value.

Because you have defined myCart as an ArrayList. You are adding a new object to the array list.

Assuming that the elements of the list are of class Item, if you want to modify the object, then you need to first find the corresponding element in the list, and then increase its quantity.

ArrayList.add() does not modify the object. It adds new objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM