java中购物车的功能怎么实现
3个回答
2013-07-06
展开全部
一般利用session,当货物提交后,让session失效,这样就可以完成简单的购物车。用cookie保存本地也可以。看你的具体需求了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-06
展开全部
package com.wuyadong.toolsbean;/***
*
* **/import java.util.ArrayList;import com.wuyadong.valuebean.GoodsSingle;public class ShopCar {
private ArrayList buylist = new ArrayList();
public void setBuylist(ArrayList buylist)
{
this.buylist = buylist;
}
public void addItem(GoodsSingle single)
{
if(single!=null)
{
if(buylist.size()==0)
{
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setGood_Id(single.getGood_Id());
temp.setSell_id(single.getSell_id());
temp.setPrice(single.getPrice());
temp.setNum(1);
buylist.add(temp);
}
else
{
int i = 0;
for(;i < buylist.size();i++)
{
GoodsSingle temp = (GoodsSingle)buylist.get(i);
if(temp.getGood_Id()==single.getGood_Id())
{
temp.setNum(temp.getNum()+1);
break;
}
}
if(i>=buylist.size())
{
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setGood_Id(single.getGood_Id());
temp.setSell_id(single.getSell_id());
temp.setPrice(single.getPrice());
temp.setNum(1);
buylist.add(temp);
}
}
}
}
public void removeItem(int good_id)
{
for(int i=0;i < buylist.size();i++)
{
GoodsSingle temp = (GoodsSingle)buylist.get(i);
if(temp.getGood_Id() == good_id)
{
if(temp.getNum()>1)
{
temp.setNum(temp.getNum()-1);
break;
}
else
{
buylist.remove(i);
}
}
}
}
}
*
* **/import java.util.ArrayList;import com.wuyadong.valuebean.GoodsSingle;public class ShopCar {
private ArrayList buylist = new ArrayList();
public void setBuylist(ArrayList buylist)
{
this.buylist = buylist;
}
public void addItem(GoodsSingle single)
{
if(single!=null)
{
if(buylist.size()==0)
{
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setGood_Id(single.getGood_Id());
temp.setSell_id(single.getSell_id());
temp.setPrice(single.getPrice());
temp.setNum(1);
buylist.add(temp);
}
else
{
int i = 0;
for(;i < buylist.size();i++)
{
GoodsSingle temp = (GoodsSingle)buylist.get(i);
if(temp.getGood_Id()==single.getGood_Id())
{
temp.setNum(temp.getNum()+1);
break;
}
}
if(i>=buylist.size())
{
GoodsSingle temp = new GoodsSingle();
temp.setName(single.getName());
temp.setGood_Id(single.getGood_Id());
temp.setSell_id(single.getSell_id());
temp.setPrice(single.getPrice());
temp.setNum(1);
buylist.add(temp);
}
}
}
}
public void removeItem(int good_id)
{
for(int i=0;i < buylist.size();i++)
{
GoodsSingle temp = (GoodsSingle)buylist.get(i);
if(temp.getGood_Id() == good_id)
{
if(temp.getNum()>1)
{
temp.setNum(temp.getNum()-1);
break;
}
else
{
buylist.remove(i);
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-06
展开全部
session+cookie。先从cookie里读取上次的购物清单到session,本次添加的购物清单也将加入session。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询