模拟购物车

一个购物车的demo,数据时死的。用mvc结构写
模拟购物车_第1张图片
QQ截图20170627220828.png

上面的是购物车中的结构目录
首先建立数据源 用死数据进行模拟

/**
 * 模拟数据库的数据
 */
public class MyDb {
    private static Map map=new LinkedHashMap();
    static{
        map.put("1", new Book("1","javaweb开发","老张",20,"一本经典的书"));
        map.put("2", new Book("2","jdbc开发","李勇",30,"一本jdbc的书"));
        map.put("3", new Book("3","spring开发","老黎",50,"一本相当经典的书"));
        map.put("4", new Book("4","hibernate开发","老佟",56,"一本佟佟的书"));
        map.put("5", new Book("5","struts开发","老毕",40,"一本经典的书"));
        map.put("6", new Book("6","ajax开发","老张",50,"一本老张的书"));
    }
    
    /**
     * 得到所有的书本的信息
     * @return
     */
    public static Map getAll() {
        return map;
    }
}

书本的实体类

/**
 * 书的实体类
 */
public class Book {
    private String id;
    private String name;
    private String author;
    private double price;
    private String description;
    
    public Book() {
        // TODO Auto-generated constructor stub
    }
    
    public Book(String id, String name, String author, double price, String description) {
        super();
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
        this.description = description;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

接下来是 Dao

/**
 * 根据 需求来和 数据库 打交道
 */
public class BookDao {
    
    /**
     * 根据book的id来获取书
     */
    public Book find(String id){
        return  MyDb.getAll().get(id);
    }
    
    /**
     * 获取所有的书本信息 以map的方式给你
     */
    public Map getAll(){
        return MyDb.getAll();
    }
}

业务处理类

/**
 * 书本业务的逻辑   用于和 dao层 和 view进行交互数据
 */
public class BusinessService {

    BookDao bookDao=new BookDao();
    
    /**
     * 得到所有的书本信息
     * @return
     */
    public Map getAll(){
        return bookDao.getAll();
    }
    
    /**
     * 买书
     */
    public void buybook(String bookid, Cart cart) {
        Book book = bookDao.find(bookid);
        cart.add(book);
    }
    
    /**
     * 清空购物车
     * @param cart
     * @throws CartNotFoundException 
     */
    public void clear(Cart cart) throws CartNotFoundException{
        if (cart ==null) {
            throw new CartNotFoundException();
        }
        cart.clear();
    }
    
    /**
     * 删除购物车中的商品
     * @param cart
     * @param bookid
     * @throws CartNotFoundException
     */
    public void delete(Cart cart,String bookid) throws CartNotFoundException{
        if(cart==null){
            throw new CartNotFoundException();
        }
        cart.getMap().remove(bookid);
    }
    
    /**
     * 将 商品的数量进行改变
     * @param cart
     * @param bookid
     * @param quantity
     */
    public void updateCart(Cart cart,String bookid,int quantity) throws CartNotFoundException{
        if(cart==null){
            throw new CartNotFoundException();
        }
        CartItem cartItem=cart.getMap().get(bookid);
        cartItem.setQuantity(quantity);
    }
}

展示书本列表的 servlet

 /**
  * 书本的列表页
  */
public class ListBookServlet extends HttpServlet {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据中的书本信息取出  给request 域中 方便jsp中调用显示
        BusinessService businessService=new BusinessService();
        Map map=businessService.getAll();
        request.setAttribute("map", map);
        request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp").forward(request, response);
    }

 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

展示书本的jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




书本的列表


         

书籍列表



<%-- Set> 每次循環出来的就是这个--%>
书籍编号 书名 作者 售价 描述 操作
${me.key} ${me.value.name} ${me.value.author} ${me.value.price} ${me.value.description} 购买

购买书本后的servlet

/**
 *  点击购买按钮调到此界面进行处理
 */
public class BuyServlet extends HttpServlet {
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String bookid=request.getParameter("bookid");
         
         Cart cart=(Cart) request.getSession().getAttribute("cart");
         if (cart==null) {
            cart=new Cart();
            request.getSession().setAttribute("cart", cart);
        }
         //将商品加入购物车中 
         BusinessService businessService=new BusinessService();
         businessService.buybook(bookid, cart);
         //展示购买过的商品
         request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
    }

     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

展示购买的商品的 servlet

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    



购买过的商品


 
    

购物车列表



书名 作者 单价 数量 小计 操作
${me.value.book.name} ${me.value.book.author} ${me.value.book.price} ${me.value.quantity} ${me.value.price } 删除
清空购物车 合计 ${cart.price}
对不起,您还没有购买任何商品

清空购物车的 servlet

/**
 * 清空购物车哦
 */
public class ClearCartServlet extends HttpServlet {
     
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart= (Cart) request.getSession().getAttribute("cart");
        BusinessService businessService=new BusinessService();
        try {
            businessService.clear(cart);
            request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
        } catch (CartNotFoundException e) {
             request.setAttribute("message","对不起,您还没有购买任何商品!!!");
             request.getRequestDispatcher("/message.jsp").forward(request, response);
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

删除 商品的 sevlet

/**
 *  删除 购物列表中的数据
 */
public class DeleteServlet extends HttpServlet {
 
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookid=request.getParameter("bookid");
        //将次数的id从购物车进行删除
        Cart cart=(Cart) request.getSession().getAttribute("cart");
        BusinessService businessService=new BusinessService();
        try {
            businessService.delete(cart, bookid);
            request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); 
        } catch (CartNotFoundException e) {
             request.setAttribute("message","对不起,您还没有购买任何商品!!!");
             request.getRequestDispatcher("/message.jsp").forward(request, response); 
        }
    }

     
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

当在购买过的商品中进行修改商品的数量的时候

/**
 * 当修改购买商品的数量 进行操作的 servlet
 */
public class UpdateCartServlet extends HttpServlet {
     
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
         String bookid=request.getParameter("bookid");
         String quantity=request.getParameter("quantity");
         BusinessService businessService=new BusinessService();
         Cart cart=(Cart)request.getSession().getAttribute("cart");
         int q=Integer.parseInt(quantity);
         try {
            businessService.updateCart(cart, bookid, q);
            request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response); 
        } catch (CartNotFoundException e) {
             request.setAttribute("message","对不起,您还没有购买任何商品!!!");
             request.getRequestDispatcher("/message.jsp").forward(request, response); 
        }
         
         
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

购物车的实体

/**
 * 购物车
 */
public class Cart {
    
    /**
     * 用于存储 购车车中的信息   key--- id   value -- 书的信息
     */
    
    private Map map = new LinkedHashMap();
    private double price;  //购车中总的价格
    
    /**
     * 向购物车中添加 书本
     * @param book
     */
    public void add(Book book)
    {
        //根据书本的id   知道 购物车中是否已经存入该商品
        CartItem cartItem= map.get(book.getId());
        if (cartItem!=null) {
            //已经有该商品了 
            cartItem.setQuantity(cartItem.getQuantity()+1);
        }else{
            //该商品还没有 需要进行创建
            cartItem=new CartItem();
            cartItem.setBook(book);
            cartItem.setQuantity(1);
            map.put(book.getId(), cartItem);
        }
    }
    
    /**
     * 获取购物车
     * @return
     */
    public   Map getMap(){
        return map;
    }
    public void setMap(Map map) {
        this.map = map;
    }
    
    /**
     * 得到总价格
     * @return
     */
    public double getPrice(){
        double totalprice =0;
        for(Map.Entry me:map.entrySet()){
            CartItem cartItem=me.getValue();
            totalprice+=cartItem.getPrice();
        }
        return totalprice;
    }
    
    /**
     * 清空数据
     */
    public void clear(){
        getMap().clear();
    }
}
public class CartItem {
    private Book book;
    private int quantity;//购买的数量
    private double price;
    public Book getBook() {
        return book;
    }
    public void setBook(Book book) {
        this.book = book;
    }
    public int getQuantity() {
        return quantity;
    }
    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }
    public double getPrice() {
        return this.book.getPrice()*this.quantity;
    }
    public void setPrice(double price) {
        this.price = price;
    }
}

你可能感兴趣的:(模拟购物车)