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

來源:互聯網
上載者:User

【JAVAWEB學習筆記】網上商城實戰3:購物模組和訂單模組,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:

    * 封裝訂單和訂單項.

    * 調用業務層

    * 清空購物車

    * 頁面跳轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.