巴巴运动商品交易系统对购物车的实现

在购物车中,我们可以删除购物项,修改产品的购买数量,清空购物车,进入结算中心。

以下是购物车的代码:

/**
 * 购物车
 */
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()方法就行了。

利用 TensorFlow 训练自己的目标识别器。本文内容来自于我的毕业设计,基于 TensorFlow 1.15.0,其他 TensorFlow 版本运行可能存在问题。.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值