Home  >  Article  >  Java  >  Share a practical online shopping mall-shopping module and order module

Share a practical online shopping mall-shopping module and order module

零下一度
零下一度Original
2017-06-25 10:26:203607browse

Today’s Task

  • Complete the functions of the shopping module

  • Complete the functions of the order module

1.1 Shopping module:

1.1.1 Function demonstration:

Product details:

Shopping cart module :

1.1.2 Code implementation:

1. Click the [Add to Cart] link on the product details page and .

2. Submit to the Servlet:

* Submit the quantity of the purchased product.

* Submit the ID of the purchased product.

3. Store the shopping information into the session.

* Store the shopping cart information into the session.

* Encapsulation of the shopping item object (each purchased item in the shopping cart Information)

* Product object:

* Quantity

* Subtotal

* Encapsulation of the shopping cart object (information about purchasing all products)

* Collection of shopping items

* Total

4. Get the shopping cart information on the page.

* Display it on the page.

【Shopping cart entity encapsulation: 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;

    }*/}

【Shopping cart entity: 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;

    }

}

[Click the link to add to cart on the shopping details page]

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;

    }

【Click to clear the shopping cart on the shopping cart page】

    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;

    }

【In the shopping cart Click the delete link on the page]

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 Order module:

1.2.1 Function demonstration:

1.2.2 Code implementation:

1.2.2.1 Create tables and entities:

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 Generate order:

[Click on the shopping cart page to submit the order]

Submit to Servlet:

* Encapsulate orders and line items.

* Call the business layer

* Clear the shopping cart

* Page jump

The above is the detailed content of Share a practical online shopping mall-shopping module and order module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn