//首先创建一个javabean
package com.ht.qfb;
import java.util.HashMap;
public class Cart
{
private String userID;
private HashMap items;
public Cart()
{
items = new HashMap();
}
//添加商品
public void addItem(String itemID)
{
if(items.containsKey(itemID))
{
int quantity = Integer.parseInt(items.get(itemID).toString());
items.remove(itemID);
items.put(itemID,new Integer(1+quantity));
}
else
items.put(itemID,new Integer(1));
}
//删除商品
public void removeItem(String itemID)
{
items.remove(itemID);
}
//修改商品
public void updateItem(String itemID,int quantity)
{
if(items.containsKey(itemID))
items.remove(itemID);
items.put(itemID,new Integer(quantity));
}
//获得所有商品
public HashMap getItems()
{
return this.items;
}
//设置用户ID
public void setUserID(String id)
{
this.userID = id;
}
//清空购物车
public void clear()
{
items.clear();
}
}
//在JSP页面上显示购物车中的商品
//本JSP页面是通过点击购物按钮时跳到的购物车界面
<%@page contentType="text/html; charset=GBK"%>
<jsp:useBean id="cart" class="com.ht.qfb.Cart" scope="session"/>
<table cellspacing=1 cellpadding=0 width="97%" bgcolor=#cccccc border=0>
<tr bgcolor=#f1f1f1>
<td height="20" align="center"> <strong> 商品编号 </strong> </td>
<td align=middle> <strong>数量</strong> </td>
</tr>
<%
String id=request.getParameter("id");//刚才购买的商品的ID
cart.addItem(id);//添加商品到购物车
java.util.HashMap cart_item=cart.getItems();
Iterator it=cart_item.keySet().iterator();
while(it.hasNext())
{
String itemID=(String)it.next();
%>
<tr bgcolor=#ffffff>
<td height="20" align="center"> <%=itemID%></td>
<td align=middle> <%=cart_item.get(itemID)%> </td>
</tr> <%}%>
</table>
分享到:
相关推荐
微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现微信小程序购物车实现...
购物车实现代码购物车实现代码购物车实现代码购物车实现代码
jquery右侧购物车实现,可实现购物车的功能
SSH框架+SQL Server2005制作的简易购物车实现
简单的实现了购物车的效果简单的实现了购物车的效果简单的实现了购物车的效果
购物车实现完整案例购物车实现完整案例购物车实现完整案例购物车实现完整案例购物车实现完整案例
JAVA 购物车实现.pdf
讲解在android上实现购物车的原理,需要源代码的,单独联系我索取。
asp.net购物车实现的源码,实现了在电子商务中购物车的实现
京东购物车实现.html
jquery购物车 实现原理分析
android购物车实现数量加减 listVIew列表中实现数量的加减
J2EE,model2 购物车实现,采用jsp+servelet+model实现,比较适合于学习,包含了session操作
新手购物车实现方法与代码,很精简的代码,对新手很有帮助
一个包含数据绑定,存储过程分页及购物车实现的例子,对初学者很有帮助。内含DataGrid控件的使用。具备增删查改的功能。采用三层架构实现。下载完后修改连接字符串即可使用。
购物车实现视频代码(第一段),,,,,,,,
微信小程序购物车实现,可供学习使用,学习小程序的朋友可以下载哈。
仿饿了么添加购物车实现动画+购物车数据交互+二维码UI。
简单的购物车实现,主要实现了商品的展示,将商品放入购物车,并对购物车的商品的选择进行多项操作,实现商品的单品数量、实际总价格、购买商品总数