各位大虾们啊!我用的是三层结构,servlet和jsp来编写购物车,当客户购买相同的物品要实现在开始的数量上加1该怎么实现啊?还有如果客户要删除已购买的物品又该怎么写代码啊?写详细一点啊,,,我菜鸟啊,刚学!

解决方案 »

  1.   

    使用Set 定义一个count 物品相同就count++ 删除商品就remove 如果全部删除就clear
      

  2.   

    public class MyCartBo {
    private Connection ct;
    private PreparedStatement ps;
    private ResultSet rs;
    private GoodsBean gb;
    private float totalPrice ;

    //定义一个hashmap集合,用于存放书的ID及数量
    HashMap<String, String > hm = new HashMap<String, String >();

    //得到货物总价
    public float getTotalPrice() {
    return totalPrice;
    }

    //根据货物Id得到货物数量
    public String getGoodsNumByID(String goodsId) {
    return hm.get(goodsId);
    }
    //1添加货物
    public void addGoods(String goodsId,String goodsNum) {
    hm.put(goodsId, goodsNum);
    }
    //2删除货物
    public void delgoods(String goodId) {
    hm.remove(goodId);
    }
    //3清空货物
    public void clearCart() {
    hm.clear();
    }
    //4修改货物数量
    public void updateGoodsNum(String id,String newNum) {
    hm.put(id, newNum);
    }
    //5显示购物车
    public ArrayList<GoodsBean> ShowMyCart() {
    ct = new connDB().getConnection();
    ArrayList<GoodsBean> al = new ArrayList<GoodsBean>();
    String sql = "select * from goods where goodsId in";
    String sub = "(";
    Iterator<String> it = hm.keySet().iterator();
    if(!it.hasNext()) sql= "select * from goods where goodsId in(0)";
    else {
    while(it.hasNext()) {
    String goodId = it.next();
    if(it.hasNext()) {
    sub += goodId+",";
    }else {
    sub += goodId;
    sub+=")";
    }
    }
    sql += sub;
    }
    try {
    ps = ct.prepareStatement(sql);
    //System.out.println(sql);
    rs = ps.executeQuery();
    totalPrice = 0;//把总价清空
    while(rs.next()) {
    gb = new GoodsBean();
    float simplePrice = rs.getFloat(4);
    int goodsId = rs.getInt(1);
    gb.setGoodsId(goodsId);
    gb.setGoodsName(rs.getString(2));
    gb.setGoodsIntro(rs.getString(3));
    gb.setGoodsPrice(simplePrice);
    gb.setGoodsNum(rs.getInt(5));
    gb.setPublisher(rs.getString(6));
    gb.setPhoto(rs.getString(7));
    gb.setType(rs.getString(8));
    al.add(gb);
            totalPrice += simplePrice*Integer.parseInt(this.getGoodsNumByID(goodsId+""));
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }finally {
    this.close();
    }
    return al;
    }
    public void close() {
    try {
    if(rs != null) {
    rs.close();
    rs=null;
    }
    if(ps!=null) {
    ps.close();
    ps=null;
    }if(ct != null) {
    ct.close();
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    java的购物车我不会,我只会用.Net做一个购物车
      

  4.   

    在  APPLICATION里 定义全局变量,用法同SESSION,不同的是 程序结束变量才销毁.函数里调用 ++  --
      

  5.   

    楼上的意思我错了么?  我时间太长没弄JAVA了 错了不要笑话我....
      

  6.   

    package com.fit.shopcar.util;import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;import com.fit.shopcar.dao.DataBase;
    import com.fit.shopcar.dao.impl.GoodsDao;
    import com.fit.shopcar.entity.Goods;
    import com.fit.shopcar.entity.Item;
    import com.fit.shopcar.entity.User;public class Car extends DataBase{
    private List<Item> car = null;

    @SuppressWarnings("unchecked")
    public Car() {
    car = new ArrayList();

    } @SuppressWarnings("unchecked")
    public Car(ArrayList car) {
    super();
    this.car = car;
    } public List<Item> getCar() {
    return car;
    } public void setCar(List<Item> list) {
    this.car = list;
    }


    public List<Item> readData(User u){
    String sql ="select * from item where u_id=?";
    try {
    getConnection();
    ps = con.prepareStatement(sql);
    ps.setInt(1, u.getId());
    rs = ps.executeQuery();
    while(rs.next()){
    Item item = new Item();
    GoodsDao gdao = new GoodsDao();
    item.setU_id(u.getId());
    item.setQuantity(rs.getInt("quantity"));
        item.setGoods(gdao.findGoodsById(rs.getInt("g_id")));
    car.add(item);
    }
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return car;
    }

    public void deleteAll(User u){
    String sql = "delete item where u_id=?";
    try {
    getConnection();
    ps = con.prepareStatement(sql);
    ps.setInt(1, u.getId());
        ps.executeUpdate();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }


    public void writeData(List<Item> car) {
    String sql="insert into item values(?,?,?,?,?)";
    try {
    getConnection();
    for (Item item : car) {
    ps = con.prepareStatement(sql);
    ps.setInt(1, item.getGoods().getId());
    ps.setString(2, item.getGoods().getGoodsName());
    ps.setFloat(3, item.getGoods().getPrice());
    ps.setInt(4, item.getQuantity());
    ps.setInt(5, item.getU_id());
    ps.executeUpdate();


    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    @SuppressWarnings("unchecked")
    public List<Item> addToCar(Goods goods, int quantity,User u) {
    System.out.println(goods.getId());
    if(car == null){
    car = new ArrayList();
    Item item = new Item(goods,quantity);
    item.setU_id(u.getId());
    car.add(item);

    else{

    Object items[] = car.toArray();
    boolean find = false;

    for(int i = 0;i<items.length;i++){
    Item temp = (Item)items[i];
    if(temp.getGoods().getId()==goods.getId()){
    temp.setQuantity(temp.getQuantity()+quantity);
    find=true;
    break;
    }
    temp.setU_id(u.getId());
    }
    if(!find){
    Item item = new Item(goods,quantity);
    car.add(item);
    item.setU_id(u.getId());
    }
    }
    return car;
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) {
    Iterator it =car.iterator();
    while(it.hasNext()){
    Item temp = (Item)it.next();
    if(temp.getGoods().getId() == id){
    car.remove(temp);
    return;
    }
    }
    }
    @SuppressWarnings("unchecked")
    public void update(int id,int quantity){
    if(quantity == 0){
    delete(id);
    } Iterator it = car.iterator();
    while(it.hasNext()){
    Item item = (Item)it.next();
    if(item.getGoods().getId() == id){
    item.setQuantity(quantity);
    }
    }
    }

    @SuppressWarnings("unchecked")
    public float total(ArrayList car){
    float total = 0;
    float AllMoney = 0;

    Object items[] = car.toArray();

    for(int i = 0;i<items.length;i++){
    Item item1 = (Item)items[i];
    total = (item1.getGoods().getPrice())*(item1.getQuantity());
    AllMoney = AllMoney+total;
    }
    return AllMoney;
    }}
      

  7.   

    提示:可以通过session+数据库实现数据记录,亦可以通过cookie+数据库实现。具体实现在这里不便多说,可联系
      

  8.   

    package com.xtgj.biz;import java.util.*;
    import java.util.Map;import com.xtgj.entity.Book;public class ShoppingCat {
    Map<Integer, Book> shopcat = new HashMap<Integer,Book>();
    public Map<Integer, Book> add(int id,Book book) {
    //判断购物车是否存在
    if(shopcat.containsKey(id)) {
    int quantity = shopcat.get(id).getQuantity();
    book.setQuantity(quantity+1);
    }
    shopcat.put(id,book);

    return shopcat;

    }

    //得到所有的书籍
    public Collection<Book> getBooks() {
    return shopcat.values();
    }

    //计算总价格
    public Double money() {
    Double dallor = 0.0;
    Collection<Book> book = getBooks();
    Iterator<Book> it = book.iterator();
    while(it.hasNext()) {
    Book bk = it.next();
    dallor +=bk.mo();
    }
    return dallor;
    }
    }package com.xtgj.servlet;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import com.xtgj.biz.ShoppingCat;
    import com.xtgj.entity.Book;public class AddServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { request.setCharacterEncoding("utf-8"); // 设置字符集
    //得到ID
    int id = Integer.parseInt(request.getParameter("id"));
    String name = request.getParameter("name");
    Double price =Double.parseDouble(request.getParameter("price"));
    HttpSession sc = request.getSession();
    Object obj = sc.getAttribute("shopcat");
    ShoppingCat shopcat = null;
    if(obj == null) {
    shopcat = new ShoppingCat();
    }else{
    shopcat = (ShoppingCat)obj;
    }
    Book book = new Book(id,name,price,1);
    shopcat.add(id, book);
    sc.setAttribute("shopcat",shopcat);
    response.sendRedirect("shopcat.jsp");



    }}
      

  9.   

    可以通过数据库实现。
    也可以通过session实现。 
      将每个商品的主键id存到一个map里面作为健 然后将数量作为值,存入与建对应的map的值。然后将map放入arrayList中, 在将这个arrayList放入session中。 
      如果要对其进行++; 在session中取出arrayList, 然后取出健为你要的商品的主键为建的map的值,进行++再存入map中。 这样就行了。 
      
      

  10.   

    购买的时候把对象放到session中的list中,在删除时到着删除,不然会报错的,因为list移除了之后位置变了,
      

  11.   

    就是这些 你可以把你的商品先保存在map厘米啊,然后你下次添加的时候首先判断是不是已经存在要添加的这件商品,如果存在则在数量上+1,不存在则添加一条新的记录
      

  12.   

    就用SESSION来做,商品数量就是在把客户点击触发的那个方法里面写上该参数++,要减少商品也就参数--,我看了3楼的做法,和我做的一样