首页  >  文章  >  Java  >  分享一个网上商城实战--购物模块和订单模块

分享一个网上商城实战--购物模块和订单模块

零下一度
零下一度原创
2017-06-25 10:26:203569浏览

今日任务

  • 完成购物模块的功能

  • 完成订单模块的功能

1.1      购物模块:

1.1.1    功能演示:

商品详情:

 

购物车模块:

 

1.1.2    代码实现:

1.在商品详情的页面中点击【加入购物车】链及.

2.提交到Servlet中:

    * 提交购买的商品的数量.

    * 提交购买的商品的ID.

3.将购物的信息存入到session中.

    * 将购物车的信息存入到session中.

    * 购物项对象的封装(购物车中的每个购买商品的信息)

        * 商品的对象:

        * 数量

        * 小计

    * 购物车对象的封装(购买所有商品的信息)

        * 购物项的集合

        * 总计

4.在页面中将购物车的信息获得到.

    * 在页面中显示出来.

 

【购物项的实体的封装:CartItem】

public class CartItem {private Product product;// 购买的商品的信息private int count; // 购买的数量private double subtotal; // 购买商品的小计

   public Product getProduct() {return product;

    }public void setProduct(Product product) {this.product = product;

    }public int getCount() {return count;

    }public void setCount(int count) {this.count = count;

    }public double getSubtotal() {return count * product.getShop_price();

    }/*public void setSubtotal(double subtotal) {

        this.subtotal = subtotal;

    }*/}

 

【购物车的实体:Cart】

public class Cart {// 定义一个购物项的集合的属性:集合采用Map集合,因为移除购物项的时候方便.使用商品的id作为Map的key// 使用购物项作为Map的value.private Map<String,CartItem> map = new LinkedHashMap<String,CartItem>();// 定义购物车中的总计:private double total;   public Map<String, CartItem> getMap() {return map;

    }   public double getTotal() {return total;

    }   // 方法:将购物项添加到购物车public void addCart(CartItem cartItem){// 判断购物车中是否已经存在该购物项.String id = cartItem.getProduct().getPid();if(map.containsKey(id)){// 如果已经存在:在原来的数量的基础上+新买的数量. 总计发生变化.// 获得购物车中的原来购物项的信息CartItem _cartItem = map.get(id);

            _cartItem.setCount(_cartItem.getCount()+cartItem.getCount());

        }else{// 如果不存在:在集合中添加一个新的购物项. 总计发生变化.map.put(id, cartItem);

        }

       

        total += cartItem.getSubtotal();

       

    }   // 方法:从购物车中移除购物项public void removeCart(String id){// 从map中移除选中的元素.// CartItem cartItem = map.get(id);CartItem cartItem = map.remove(id);// 将总计 - 移除购物项的小计total -= cartItem.getSubtotal();

       

    }   // 方法:清空购物车public void clearCart(){ ////  将map集合清空.map.clear();// 将总结设置为0.total = 0;

    }

}

 

 

 

【在购物详情页面点击加入购物车的链接】

   

public String addCart(HttpServletRequest req,HttpServletResponse resp){// 接收参数:String pid = req.getParameter("pid");int count = Integer.parseInt(req.getParameter("count"));       try {// 封装购物项:CartItem cartItem = new CartItem();// 商品对象:通过商品ID查询商品.ProductService productService = (ProductService) BeanFactory.getBean("productService");

            Product product = productService.findById(pid);

            cartItem.setProduct(product);

            cartItem.setCount(count);// 调用购物车中的添加到购物车的方法:// Cart cart = new Cart();Cart cart = getCart(req);

            cart.addCart(cartItem);

           

            resp.sendRedirect(req.getContextPath()+"/jsp/cart.jsp");

        } catch (Exception e) {

            e.printStackTrace();throw new RuntimeException();

        }return null;

    }

 【在购物车页面点击清空购物车】

    public String clearCart(HttpServletRequest req,HttpServletResponse resp){// 获得购物车对象.Cart cart = getCart(req);// 调用购物车中的方法:cart.clearCart();try {

            resp.sendRedirect(req.getContextPath()+"/jsp/cart.jsp");

        } catch (Exception e) {

            e.printStackTrace();throw new RuntimeException();

        }return null;

    }

 

【在购物车页面点击删除链接】

   

public String removeCart(HttpServletRequest req,HttpServletResponse resp){try {// 接收参数:String pid = req.getParameter("pid");// 获得购物车:Cart cart = getCart(req);

            cart.removeCart(pid);// 页面跳转resp.sendRedirect(req.getContextPath()+"/jsp/cart.jsp");

        } catch (Exception e) {

            e.printStackTrace();throw new RuntimeException();

        }return null;

    }

 

1.2      订单模块:

1.2.1    功能演示:

 

 

1.2.2    代码实现:

1.2.2.1  创建表和实体:

CREATE TABLE `orders` (

  `oid` varchar(32) NOT NULL,

  `ordertime` datetime DEFAULT NULL,

  `total` double DEFAULT NULL,

  `state` int(11) DEFAULT NULL,

  `address` varchar(30) DEFAULT NULL,

  `name` varchar(20) DEFAULT NULL,

  `telephone` varchar(20) DEFAULT NULL,

  `uid` varchar(32) DEFAULT NULL,  PRIMARY KEY (`oid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

CREATE TABLE `orderitem` (

  `itemid` varchar(32) NOT NULL,

  `count` int(11) DEFAULT NULL,

  `subtotal` double DEFAULT NULL,

  `pid` varchar(32) DEFAULT NULL,

  `oid` varchar(32) DEFAULT NULL,  PRIMARY KEY (`itemid`),  KEY `fk_0001` (`pid`),  KEY `fk_0002` (`oid`),  CONSTRAINT `fk_0001` FOREIGN KEY (`pid`) REFERENCES `product` (`pid`),  CONSTRAINT `fk_0002` FOREIGN KEY (`oid`) REFERENCES `orders` (`oid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

 

1.2.2.2  生成订单:

【在购物车的页面点击提交订单】

提交到Servlet:

    * 封装订单和订单项.

    * 调用业务层

    * 清空购物车

    * 页面跳转

以上是分享一个网上商城实战--购物模块和订单模块的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn