购物车

[color=blue][/color][align=left][/align]一.购物车类
/**
* 购物车类
* @author Administrator
*/
public class ShoppingCart {
private List<Item> itemList = new ArrayList<Item>();
public void addItem(Item order) {
boole :( an has = false;
// 遍历购物车内的商品条目
for (Item item : itemList) {
/*
*当购物车中的商品的编号和用户所选的商品的编号相同的时候,可以认为用户是再次购买该商品则只需要直接修改商品的数量即可
*/
if (item.getShoppingInfo().getShoppingId() == order
.getShoppingInfo().getShoppingId()) {
item.setCount(item.getCount() + 1);
has = true;
return;
}
}
// 当在购物车内不包含该商品的时候,直接加入该商品
if (!has) {
itemList.add(order);
}
}
/**
* 清空购物车
*/
public void clear() {
itemList.clear();
}
/**
* 从购物车中移除指定的商品
*/
public void removeItem(Integer id) {
for(Iterator <Item>iter= itemList.iterator();iter.hasNext();){
Item item=iter.next();
if (item.getShoppingInfo().getShoppingId() == id) {
iter.remove();
}
}
}
/**
* 购物车中所有商品
*/
public List<Item> getList() {
return itemList;
}
/**
* 购物车中所有商品的总数量
* @return
*/
public int getAllCount() {
int count = 0;
for (Item item : itemList) {
count = count + item.getCount();
}
return count;
}
/**
* 购物车中商品的总金额
* @return
*/
public double getTotalValue() {
double value = 0.0;
for (Item item : itemList) {
value = value
+ (item.getCount() * Integer.parseInt(item
.getShoppingInfo().getShoppingPrice()));
}
return value;
}
}
二、购物条目类
/*
* 购物明细条目
* @author Administrator
*/
public class Item {
// 购物车中商品的数目
private int count = 1;
private ShoppingInfo shoppingInfo;
/**
* 购物条目构造方法 不能出现空白条目 必须提供商品的信息
* @param shoppingInfo
*/
public Item(ShoppingInfo shoppingInfo) {
this.shoppingInfo = shoppingInfo;
}
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public ShoppingInfo getShoppingInfo() {
return shoppingInfo;
}
}
购物车页面:
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>购物车示例</title>
<jsp:useBean id="sdao" class="com.fit.shoppingdaoimpl.ShoppingDAOImpl" scope="application"></jsp:useBean>
<jsp:useBean id="cart" class="com.fit.shopping.ShoppingCart" scope="session"></jsp:useBean>
</head>
<body>
<h1>
请选购你所喜欢的商品
</h1>
<table>
<tr>
<td>商品的编号</td>
<td>商品的名称</td>
<td>商品的价格</td>
<td>商品的描述</td>
</tr>
<c:forEach var="shopping" items="${sdao.allShopping}">
<tr>
<td>${shopping.shoppingId}</td>
<td>${shopping.shoppingName}</td>
<td>${shopping.shoppingPrice}</td>
<td>${shopping.shoppingDescribe}</td>
<td>
<a href="cart.jsp?op=buy&sid=${shopping.shoppingId}">放入购物车</a></td>
</tr>
</c:forEach>
</table>
<hr>
您已经选购了${cart.allCount}件商品,价值${cart.totalValue}元。
</body>
</html>
<html>
<head>
<title>购物车示例</title>
<jsp:useBean id="sdao" class="com.fit.shoppingdaoimpl.ShoppingDAOImpl" scope="application"></jsp:useBean>
<jsp:useBean id="cart" class="com.fit.shopping.ShoppingCart"
scope="session"></jsp:useBean>
<%
String op = request.getParameter("op");
String shoppingId = request.getParameter("sid");
if ("cls".equals(op)) {
cart.clear();} else {
if ("del".equals(op)) {
cart.removeItem(Integer.parseInt(shoppingId));
} else if ("buy".equals(op)) {
ShoppingInfo shopping = sdao.getShoppingById(Integer.parseInt(shoppingId);
Item order = new Item(shopping);
cart.addItem(order);
}
}
%>
</head>
<body>
<h1>您所选中的商品</h1>
<table>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
<th>数量</th>
<th>操作</th>
</tr>
<c:forEach var="item" items="${cart.list}">
<tr>
<td>
${item.shoppingInfo.shoppingId}
</td>
<td>
${item.shoppingInfo.shoppingName}
</td>
<td>
${item.shoppingInfo.shoppingPrice }
</td>
<td>
${item.shoppingInfo.shoppingDescribe }
</td>
<td>
${item.count}
</td>
<td>
<ahref="cart.jsp?op=del&sid=${item.shoppingInfo.shoppingId}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<hr>
您已经选购了${cart.allCount}件商品,价值${cart.totalValue}元。
<br>
<a href="cart.jsp?op=cls">清空购物车</a>
<br />
<a href="index.jsp">继续选购</a>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值