-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddcart.jsp
38 lines (33 loc) · 1.14 KB
/
addcart.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.ArrayList" %>
<%
// Get the current list of products
@SuppressWarnings({"unchecked"})
HashMap<String, ArrayList<Object>> productList = (HashMap<String, ArrayList<Object>>) session.getAttribute("productList");
if (productList == null)
{ // No products currently in list. Create a list.
productList = new HashMap<String, ArrayList<Object>>();
}
// Add new product selected
// Get product information
String id = request.getParameter("id");
String name = request.getParameter("name");
String price = request.getParameter("price");
Integer quantity = new Integer(1);
// Store product information in an ArrayList
ArrayList<Object> product = new ArrayList<Object>();
product.add(id);
product.add(name);
product.add(price);
product.add(quantity);
// Update quantity if add same item to order again
if (productList.containsKey(id))
{ product = (ArrayList<Object>) productList.get(id);
int curAmount = ((Integer) product.get(3)).intValue();
product.set(3, new Integer(curAmount+1));
}
else
productList.put(id,product);
session.setAttribute("productList", productList);
%>
<jsp:forward page="cart.jsp" />