【JAVAWEB學習筆記】網上商城實戰3:購物模塊和訂單模塊


今日任務

  • 完成購物模塊的功能
  • 完成訂單模塊的功能

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:

    * 封裝訂單和訂單項.

    * 調用業務層

    * 清空購物車

    * 頁面跳轉


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM