在购物车中,我们可以删除购物项,修改产品的购买数量,清空购物车,进入结算中心。
以下是购物车的代码:
/**
* 购物车
*/
public class BuyCart {
/* 购物项 */
private List<BuyItem> items = new ArrayList<BuyItem>();
/* 配送信息 */
private OrderDeliverInfo deliverInfo;
/* 购买者联系信息 */
private OrderContactInfo contactInfo;
/* 支付方式 */
private PaymentWay paymentWay;
/* 购买者与收货人是否相同 */
private Boolean buyerIsrecipients;
/* 配送费 */
private float deliveFee = 10f;
/* 附言 */
private String note;
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public float getDeliveFee() {
return deliveFee;
}
public void setDeliveFee(float deliveFee) {
this.deliveFee = deliveFee;
}
public PaymentWay getPaymentWay() {
return paymentWay;
}
public void setPaymentWay(PaymentWay paymentWay) {
this.paymentWay = paymentWay;
}
public Boolean getBuyerIsrecipients() {
return buyerIsrecipients;
}
public void setBuyerIsrecipients(Boolean buyerIsrecipients) {
this.buyerIsrecipients = buyerIsrecipients;
}
public OrderContactInfo getContactInfo() {
return contactInfo;
}
public void setContactInfo(OrderContactInfo contactInfo) {
this.contactInfo = contactInfo;
}
public OrderDeliverInfo getDeliverInfo() {
return deliverInfo;
}
public void setDeliverInfo(OrderDeliverInfo deliverInfo) {
this.deliverInfo = deliverInfo;
}
public List<BuyItem> getItems() {
return items;
}
/**
* 添加购物项
* @param item 购物项
*/
public void add(BuyItem item){
if(this.items.contains(item)){
for(BuyItem it : this.items){
if(it.equals(item)){
it.setAmount(it.getAmount()+1);
break;
}
}
}else{
this.items.add(item);
}
}
/**
* 删除指定购物项
* @param item 购物项
*/
public void delete(BuyItem item){
if(this.items.contains(item)) this.items.remove(item);
}
/**
* 清空购物项
*/
public void deleteAll(){
this.items.clear();
}
/**
* 计算商品总销售价
* @return
*/
public float getTotalSellPrice(){
float totalprice = 0F;
for(BuyItem item : this.items){
totalprice += item.getProduct().getSellprice() * item.getAmount();
}
return totalprice;
}
/**
* 计算商品总市场价
* @return
*/
public float getTotalMarketPrice(){
float totalprice = 0F;
for(BuyItem item : this.items){
totalprice += item.getProduct().getMarketprice() * item.getAmount();
}
return totalprice;
}
/**
* 计算总节省金额
* @return
*/
public float getTotalSavedPrice(){
return this.getTotalMarketPrice() - this.getTotalSellPrice();
}
/**
* 计算订单的总费用
* @return
*/
public float getOrderTotalPrice(){
return this.getTotalSellPrice()+ this.deliveFee;
}
}
以下是购物项的代码:
/**
* 购物项
*/
public class BuyItem {
private ProductInfo product;
private Integer amount = 1;
public BuyItem(){}
public BuyItem(ProductInfo product) {
this.product = product;
}
public ProductInfo getProduct() {
return product;
}
public void setProduct(ProductInfo product) {
this.product = product;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
@Override
public int hashCode() {
String result = product.getId().toString();
if(!product.getStyles().isEmpty()) result +="-"+ product.getStyles().iterator().next().getId();
return result.hashCode();
}
//购物车里的产品,最多只可能存在一个样式
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final BuyItem other = (BuyItem) obj;
if (product == null) {
if (other.product != null)
return false;
} else if (!product.equals(other.product))
return false;
if(product.getStyles().size()!=other.product.getStyles().size()){
return false;
}
Integer style = product.getStyles().iterator().next().getId();
Integer othersytle = other.product.getStyles().iterator().next().getId();
if(!style.equals(othersytle)) return false;
return true;
}
}
购物车的Controller:
@Controller("/shopping/cart/manage")
public class BuyCartManageAction extends DispatchAction {
/**
* 删除指定购物项
*/
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
BuyCartForm formbean = (BuyCartForm) form;
if(formbean.getProductid()!=null && formbean.getProductid()>0
&& formbean.getStyleid()!=null && formbean.getStyleid()>0){
ProductInfo product = new ProductInfo(formbean.getProductid());
product.addProductStyle(new ProductStyle(formbean.getStyleid()));
BuyItem item = new BuyItem(product);
cart.delete(item);
}
request.setAttribute("directUrl", "/shopping/cart.do");
return mapping.findForward("directUrl");
}
/**
* 清空购物车
*/
public ActionForward deleteAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
cart.deleteAll();
request.setAttribute("directUrl", "/shopping/cart.do");
return mapping.findForward("directUrl");
}
/**
* 修改购买数量
*/
public ActionForward updateAmount(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
modifyBuyAmount(request);
request.setAttribute("directUrl", "/shopping/cart.do");
return mapping.findForward("directUrl");
}
private void modifyBuyAmount(HttpServletRequest request) {
BuyCart cart = (BuyCart)WebUtil.getBuyCart(request);
for(BuyItem item : cart.getItems()){
StringBuilder sb = new StringBuilder("amount_");
sb.append(item.getProduct().getId()).append("_");
sb.append(item.getProduct().getStyles().iterator().next().getId());
String amount = request.getParameter(sb.toString());
if(amount!=null && !"".equals(amount.trim())){
item.setAmount(new Integer(amount.trim()));
}
}
}
/**
* 结算
*/
public ActionForward settleAccounts(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
modifyBuyAmount(request);
BuyCartForm formbean = (BuyCartForm) form;
String url = "/customer/shopping/deliver.do";
if(formbean.getDirectUrl()!=null && !"".equals(formbean.getDirectUrl())) url = formbean.getDirectUrl();
request.setAttribute("directUrl", url);
return mapping.findForward("directUrl");
}
}
清空购物车的功能就是调用了BuyCartManageAction的deleteAll()方法。删除指定购物项只需调用BuyCartManageAction的delete()方法就行了。
分享到:
相关推荐
### 巴巴运动网商品交易系统详细设计说明书关键知识点解析 #### 一、概述 **巴巴运动网商品交易系统**是一款专为大型企业设计的在线商品交易平台,旨在帮助企业高效管理和运营电子商务活动。该系统不仅提供了商品...
巴巴运动网商品交易系统是一套面向大型企业开发的商品交易系统,具备先进的电子商务运营及管理理念。订单流转实现多部门协同处理,满足大型企业多部门协作处理业务的需求。系统包含以下模块: 产品管理:产品具有...
- **业务分析与环境搭建:** 包括用户管理、商品管理、订单管理、购物车管理等核心业务逻辑的分析与实现。 - **多级联查询:** 实现了省市区三级联动的数据查询功能。 - **异步图片上传:** 使用异步技术上传图片...
新巴巴运动网作为一家电子商务网站,主要功能模块包括商品、品牌、订单、购物车和内容管理等。它面向广大消费者,提供专业运动商品的销售。 3. **系统架构**: - **前台系统**:面向消费者的界面,采用的技术包括...
以上是对“巴巴运动网源码”可能涉及的关键知识点的分析,具体的实现细节将根据实际源码结构和使用的开发框架而有所不同。对这些知识的理解和熟练运用,可以帮助开发者快速理解和定制这个在线商城系统。
在本课程"03_传智播客巴巴运动网"中,主要讲解的是用户前台订单管理和付款模块的分析。这个主题对于理解电商网站的核心功能至关重要,因为这些是直接与用户体验和交易流程相关的部分。我们将深入探讨以下几个关键...