ホームページ  >  記事  >  Java  >  実用的なオンライン ショッピング モールのショッピング モジュールと注文モジュールを共有する

実用的なオンライン ショッピング モールのショッピング モジュールと注文モジュールを共有する

零下一度
零下一度オリジナル
2017-06-25 10:26:203570ブラウズ

今日のタスク

  • ショッピングモジュールの機能を完了する

  • 注文モジュールの機能を完了する

1.1 ショッピングモジュール:

1.1.1 機能デモンストレーション:

製品詳細:

ショッピング カート モジュール:

1.1.2 コードの実装:

1. 製品詳細ページで [カートに追加] リンクをクリックします:

2.購入を送信します。製品の数量を送信します。

* 購入した製品の ID を送信します。

3. ショッピング情報をセッションに保存します。

* ショッピング カートの情報をセッションに保存します。

*ショッピングアイテムオブジェクト (ショッピングカート内の各購入アイテムの情報)

* 合計

4. ページ上のショッピング カート情報を取得します。エンティティショッピングカートの: カート]

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;

    }*/}

【ショッピング詳細ページでショッピングカートに追加するにはクリックしてください】

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;

    }

1.2 注文モジュール:

1.2.1 機能デモ:

1.2.2 コード実装:

1.2。 2.1 作成テーブルとエンティティ:

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.2.2 注文の生成:

[注文を送信するには、ショッピング カート ページをクリックしてください]

サーブレットに送信:

* パッケージ注文と注文Item.

* ビジネス層に電話する

* ショッピングカートを空にする

* ページジャンプ

以上が実用的なオンライン ショッピング モールのショッピング モジュールと注文モジュールを共有するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。