Struts 购物车的实现 用Map集合

  1. Struts 购物车的实现 用Map集合
  2. 首先登陆后就给用户一台的购物车
  3. login.action:
  4. String uname=loginForm.getUname();
  5.         String upass=loginForm.getUpassword();
  6.         boolean flag=InvokeUser.getLoginResult(uname, upass);
  7.         if(flag)
  8.         {
  9.             ArrayList list=GoodM.getGoodsList();
  10.             request.getSession().setAttribute("goodslist", list);
  11.             ShoppingCart cart=ShoppingCart.getInstance();
  12.             request.getSession().setAttribute("cart", cart);
  13.             return mapping.findForward("success");
  14.         }
  15.         return mapping.getInputForward();
  16. 登陆成功后跳到产品页面
  17. show.jsp:
  18.  <table border="1">
  19.     
  20.       <logic:present name="goodslist">
  21.       <logic:iterate id="li" name="goodslist" scope="session">
  22.       <logic:present name="li">
  23.         <tr>
  24.           <td><bean:write name="li" property="gid"/></td>
  25.           <td><bean:write name="li" property="gname"/></td>
  26.            <td><bean:write name="li" property="price"/></td>
  27.           <form action="buy.do" method="get" name="form1">
  28.           <td><input type="hidden" name="gid" value="${li.gid}"/></td>
  29.           <td><input type="text" name="number"/></td>
  30.           <td><input type="submit" value="submit"/></td>
  31.            </form>
  32.         </tr>
  33.         </logic:present>
  34.        </logic:iterate>
  35.        </logic:present>
  36.         
  37.       </table>
  38. 选择购买的物品后到BuyAction:
  39. String gid=request.getParameter("gid");
  40.         int count=Integer.parseInt(request.getParameter("number"));
  41.         Goods goods=GoodM.findGoodsById(gid);
  42.         OrderItems order=null;
  43.         float sumMoney=0;
  44.         HttpSession session=request.getSession();
  45.         Map<String,OrderItems> map=null;
  46.         ShoppingCart cart=(ShoppingCart)session.getAttribute("cart");
  47.         order=new OrderItems();
  48.         order.setOrdercount(count);
  49.         order.setGood(goods);
  50.         order.setOrdersum();
  51.         cart.addCart(gid, order,count);
  52.         session.setAttribute("getGoods", cart.getCartAll());
  53.         sumMoney=cart.getSumCart();
  54.         session.setAttribute("sumMoney", sumMoney);
  55.         return mapping.findForward("checkout");
  56. 添加到购物车后到checkout.jsp(察看自己买的产品和总价等信息)
  57.  <table border="1">
  58.     
  59.       <logic:present name="getGoods">
  60.       <logic:iterate id="li" name="getGoods" scope="session">
  61.       <logic:present name="li">
  62.         <tr>
  63.           <td><bean:write name="li" property="key"/></td>
  64.            <td><bean:write name="li" property="value.good.gname"/></td>
  65.            <td><bean:write name="li" property="value.good.price"/></td>
  66.            <td><bean:write name="li" property="value.ordercount"/></td>
  67.           <td><bean:write name="li" property="value.ordersum"/></td>
  68.          </tr>
  69.         </logic:present>
  70.        </logic:iterate>
  71.        </logic:present>
  72.      </table>
  73.     你一共花了:${sumMoney}
  74.     <html:link page="/show.jsp">continue buy</html:link><br><br>
  75.     <html:link page="/go.jsp">checkout</html:link>
  76. 如果还购买那就继续买否则结帐到go.jsp(清理购物车并退出)
  77.  <% 
  78.           ShoppingCart cart=(ShoppingCart)session.getAttribute("cart");
  79.           cart.removeAllCart();
  80.           session.removeAttribute("cart");
  81.           session.removeAttribute("getGoods");
  82.           session.removeAttribute("sumMoney");
  83.     %>
  84.     结帐成功!<br><br>
  85.     
  86.     <a href="login.jsp">login in</a>
  87. 最关键的购物车代码:
  88. public class ShoppingCart {
  89.  private Map<String,OrderItems> map = new HashMap<String,OrderItems>();
  90.  private static ShoppingCart cart = null;
  91.  private ShoppingCart() {
  92.  }
  93. public static ShoppingCart getInstance() {
  94.   if (cart == null) {
  95.    cart = new ShoppingCart();
  96.   }
  97.   return cart;
  98.  }
  99.  public void addCart(String id, OrderItems order,int count) {
  100.   if (map.containsKey(id)) {
  101.       OrderItems or = (OrderItems) map.get(id);
  102.       map.remove(or);   //如果有存在的先移除后在重新添加
  103.       or.setOrdercount(or.getOrdercount() + count);
  104.       or.setOrdersum();
  105.      map.put(id, or);
  106.   } else {
  107.    map.put(id, order);
  108.   }
  109.  }
  110.  public void removeCart(Integer id){
  111.   if(map.containsKey(id)){
  112.    map.remove(id); 
  113.   }
  114.  }
  115.  public void removeAllCart(){
  116.       if(!map.isEmpty()){
  117.        map.clear(); 
  118.       }
  119.      }
  120.  public float getSumCart()
  121.     {
  122.         float sum=0;
  123.         Iterator<OrderItems> iter = this.map.values().iterator();
  124.         while (iter.hasNext()) {
  125.             OrderItems element = (OrderItems) iter.next();
  126.                 sum+=element.getOrdersum();
  127.         }
  128.         return sum;
  129.     }
  130.  public Map getCartAll() {
  131.    return this.map;
  132.  }
  133. }
  134. DAo层:
  135. public class DBManager extends Object{
  136.    private Connection con = null;
  137.     private Statement stmt = null;
  138.    private ResultSet rs = null;
  139.    public void init()
  140.     {
  141.        try
  142.        {
  143.          Class.forName("com.mysql.jdbc.Driver");
  144.          //加载驱动程序
  145.      con = DriverManager.getConnection("jdbc:mysql://192.168.1.11/ttt?
  146. user=root&password=0&useUnicode=true&characterEncoding=gbk");
  147.             //创建连接
  148.         }catch(Exception e){}
  149.     }
  150.   //获取连接
  151.    public Connection getConnection()
  152.    {
  153.         if(con == null)
  154.         init();
  155.       return con;
  156.    }
  157.     //执行有结果集的查询
  158.    public int executeUpdate(String sql) throws SQLException
  159.    {
  160.        stmt = getConnection().createStatement();
  161.         return stmt.executeUpdate(sql);
  162.    }
  163.    public ResultSet executeQuery(String sql) throws SQLException
  164.    {
  165.       stmt = getConnection().createStatement();
  166.        return stmt.executeQuery(sql);
  167.    }
  168.    public void close()
  169.    {
  170.        if(rs!=null)
  171.            try {
  172.               rs.close();
  173.            } catch (SQLException e) { }
  174.         if(stmt!=null)
  175.            try {
  176.                stmt.close();
  177.            } catch (SQLException e) { }
  178.        if(con!=null)
  179.            try {
  180.                con.close();
  181.            } catch (SQLException e) { }
  182.     }
  183. }
  184. 附:
  185. GoodM.java:
  186. public class GoodM {
  187. public static ArrayList getGoodsList()
  188. {
  189.        DBManager dbm = new DBManager();
  190.        ArrayList list = new ArrayList();
  191.        try {
  192.           ResultSet rs = dbm.executeQuery("select * from goods");
  193.           while(rs.next())
  194.           {
  195.               Goods goods = new Goods();
  196.               goods.setGid(rs.getString(1));
  197.               goods.setGname(rs.getString(2));
  198.               goods.setPrice(rs.getFloat(3));
  199.               //goods.setQuantity(rs.getInt(4));
  200.               list.add(goods);
  201.              }
  202.      } catch (SQLException e) {
  203.               // TODO Auto-generated catch block
  204.              e.printStackTrace();
  205.       }
  206.      dbm.close();
  207.      return list;
  208. }
  209. public static Goods findGoodsById(String id)
  210. {
  211.         DBManager dbm = new DBManager();
  212.         Goods goods = new Goods();
  213.         try {
  214.                StringBuffer sql = new StringBuffer();
  215.                sql.append("select * from goods where gid='");
  216.                sql.append(id);
  217.                sql.append("'");
  218.                ResultSet rs = dbm.executeQuery(sql.toString());
  219.                while(rs.next())
  220.                {
  221.                       goods.setGid(rs.getString(1));
  222.                       goods.setGname(rs.getString(2));
  223.                       goods.setPrice(rs.getFloat(3));
  224.                }
  225.         } catch (SQLException e) {
  226.                // TODO Auto-generated catch block
  227.                e.printStackTrace();
  228.         }
  229.         dbm.close();
  230.         return goods;         
  231. }
  232. }
  233. 基础类:
  234. Goods:
  235. public class Goods {
  236. private String gid;
  237. private String gname;
  238. private float price;
  239. public String getGid() {
  240.         return gid;
  241. }
  242. public void setGid(String gid) {
  243.         this.gid = gid;
  244. }
  245. public String getGname() {
  246.         return gname;
  247. }
  248. public void setGname(String gname) {
  249.         this.gname = gname;
  250. }
  251. public float getPrice() {
  252.         return price;
  253. }
  254. public void setPrice(float price) {
  255.         this.price = price;
  256. }
  257. }
  258. OrderItems订单类:
  259. public class OrderItems {
  260.      private int oid;
  261.      private float ordersum;
  262.      private int ordercount;
  263.      private Goods good;
  264.     public Goods getGood() {
  265.         return good;
  266.     }
  267.     public void setGood(Goods good) {
  268.         this.good = good;
  269.     }
  270.     public int getOid() {
  271.         return oid;
  272.     }
  273.     public void setOid(int oid) {
  274.         this.oid = oid;
  275.     }
  276.     public int getOrdercount() {
  277.         return ordercount;
  278.     }
  279.     public void setOrdercount(int ordercount) {
  280.         this.ordercount = ordercount;
  281.     }
  282.     public float getOrdersum() {
  283.         return ordersum;
  284.     }
  285.     public void setOrdersum() {
  286.         this.ordersum = ordercount * good.getPrice();
  287.     }
  288.     
  289.     
  290. }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值