JavaWeb书城项目(八)——购物车模块

上一部分,我们对用户功能进行了完善,这一部分我们完成购物车模块,主要包括添加商品到购物车,删除商品,清空购物车。

购物车模块分析

购物车模块分析
我们使用Session版本实现购物车,这样就不需要 Dao 层和 Service 层了。


购物车模型编写

  1. 创建CartItem类定义购物车中的商品项,其有以下属性
    private Integer id; //编号
    private String name; //名称
    private Integer count; //数量
    private BigDecimal price; //单价
    private BigDecimal totalprice; //总价
  1. 创建Cart类定义购物车
package com.atguigu.pojo;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 购物车对象
 */
public class Cart {

    /**
     * key 是商品编号
     * value 是商品信息
     */
    private Map<Integer, CartItem> items = new LinkedHashMap<Integer, CartItem>();

    public void addItem(CartItem cartItem) {
        // 先查看购物车中是否已经添加过此商品,如果已添加,则数量累加,总金额更新,如果没有添加过,直接放到集合中即可
        CartItem item = items.get(cartItem.getId());

        if( item == null )
        {
            // 之前没添加过此商品
            items.put(cartItem.getId(), cartItem);
        } else {
            // 已经添加过的情况
            item.setCount( item.getCount() + 1 ); //数量增加
            item.setTotalprice( item.getPrice().multiply(new BigDecimal( item.getCount() ))); // 更新总金额
        }
    }

    /**
     * 删除商品项
     * @param id
     */
    public void deleteItem(Integer id) {
        items.remove(id);
    }

    /**
     * 清空购物车
     */
    public void clear() {
        items.clear();
    }

    /**
     * 修改商品数量
     * @param id
     * @param count
     */
    public void updateCount(Integer id, Integer count) {
        // 先查看购物车是否有此商品。如果有,修改商品数量,更新总金额
        CartItem cartItem = items.get(id);
        if (cartItem != null) {
            cartItem.setCount(count); // 修改商品数量
            cartItem.setTotalprice( cartItem.getPrice().multiply(new BigDecimal( cartItem.getCount() )));
        }
    }

    /**
     * 获得总数量
     * @return
     */
    public Integer getTotalCount() {
        Integer totalCount = 0;

        for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
            totalCount += entry.getValue().getCount();
        }

        return totalCount;
    }

    public BigDecimal getTotalPrice() {
        BigDecimal totalPrice = new BigDecimal(0);

        for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
            totalPrice = totalPrice.add(entry.getValue().getTotalprice());
        }

        return  totalPrice;
    }

    public Map<Integer, CartItem> getItems() {
        return items;
    }

    public void setItems(Map<Integer, CartItem> items) {
        this.items = items;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "totalCount=" + getTotalCount() +
                ", totalPrice=" + getTotalPrice() +
                ", items=" + items +
                '}';
    }
}

  1. 购物车的测试,创建测试类CartTest
package com.atguigu.test;

import com.atguigu.pojo.Cart;
import com.atguigu.pojo.CartItem;
import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.*;

public class CartTest {

    @Test
    public void addItem() {
        Cart cart = new Cart();
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
        System.out.println(cart);
    }

    @Test
    public void deleteItem() {
        Cart cart = new Cart();
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
        cart.deleteItem(1);
        System.out.println(cart);
    }

    @Test
    public void clear() {
        Cart cart = new Cart();
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
        cart.deleteItem(1);
        cart.clear();
        System.out.println(cart);
    }

    @Test
    public void updateCount() {
        Cart cart = new Cart();
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
        cart.deleteItem(1);
        cart.clear();
        cart.addItem(new CartItem(1, "java从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
        cart.updateCount(1, 10);
        System.out.println(cart);
    }
}

加入购物车功能的实现

  1. CartServlet程序中的代码
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求的参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        // 调用 bookService.queryBookById(id):Book 得到图书的信息
        Book book = bookService.queryBookById(id);
        // 把图书信息,转换为CartItem商品项
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        // 调用Cart.addItem(CartItem);添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);

        System.out.println(cart);
        System.out.println("请求头Referer的值:" + req.getHeader("Referer"));

        // 重定向回原来商品所在的地址页面
        resp.sendRedirect(req.getHeader("Referer"));
    }
  1. index.jsp页面js的代码
    修改index.jsp

  2. 图解说明,如何跳回添加商品的页面
    跳回添加商品的页面


购物车的展示

修改 cart.jsp
修改cart,jsp(一)
修改cart.jsp(二)


删除购物车商品项

  1. CartServlet增加deleteItem方法
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            // 删除购物车商品项
            cart.deleteItem(id);
            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
  1. 修改cart.jsp,添加删除的请求地址,并增加确认删除的提示。

修改cart.jsp


清空购物车

CartServlet增加clear方法

    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        // 1 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            // 清空购物车
            cart.clear();
            // 重定向回原来购物车的展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

cart.jsp页面的内容,给购物车添加请求地址,和添加id属性,以及清空确认提示操作
修改cart.jsp


修改购物车商品的数量

  1. CartServlet添加updateCount方法
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        // 获取请求的参数 商品编号,商品数量
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int count = WebUtils.parseInt(req.getParameter("count"), 1);
        // 获取Cart购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            // 修改商品数量
            cart.updateCount(id, count);
            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }
  1. 修改 cart.jsp
    修改cart.jsp(一)
    修改cart.jsp(二)

首页购物车数据回显

  1. 在添加商品到购物车的时候,保存最后一个添加的商品名称。
    修改CartServlet的addItem方法
  2. index.jsp页面中输出购物车信息
    修改index.jsp
  • 4
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaWeb书城项目是一个基于JavaWeb技术开发的在线图书销售平台。该项目旨在为用户提供一个方便快捷的购书平台,并支持在线购买、浏览图书信息等功能。 该项目的主要功能包括用户注册登录、图书分类浏览、商品搜索、购物车管理、订单管理等。 用户可以通过注册登录功能创建自己的账号,并进行个人信息的管理。用户登录后可以浏览不同分类的图书,并支持按关键字搜索特定的图书。同时,用户可以将心仪的图书添加到购物车中,并支持修改购物车数量和删除购物车中的图书。当用户完成商品的选购后,可以提交订单进行结算,实现购物流程的完整。 图书分类浏览的功能可以让用户根据自己的需求,选择不同的图书分类进行浏览和选择。用户可以根据自己的兴趣和需求来选择特定类型的图书。 商品搜索功能可以方便用户根据关键字快速找到所需要的图书。用户只需输入关键字即可快速搜索到与该关键字相关的图书信息。 购物车管理功能可以帮助用户管理已选购的图书。用户可以对购物车中的图书进行数量的增减和删除操作,方便用户根据自己的需求进行调整。 订单管理功能可以帮助用户查看和管理自己的订单。用户可以查看已购买的订单详情,并支持订单的取消和重新购买等操作。 总之,该JavaWeb书城项目通过提供用户注册登录、图书分类浏览、商品搜索、购物车管理、订单管理等功能,为用户打造了一个便利的在线图书销售平台。用户可以通过该平台方便地浏览、购买自己喜欢的图书,提高图书销售的便捷性和用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值