您的位置:首页 > 编程语言 > Java开发

【JAVAWEB学习笔记】网上商城实战3:购物模块和订单模块

2017-06-05 09:35 447 查看

今日任务

完成购物模块的功能

完成订单模块的功能

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:

* 封装订单和订单项.

* 调用业务层

* 清空购物车

* 页面跳转
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: