[JAVAWEB learning notes] online mall Practice 3: shopping module and order module, javaweb learning notes
Online mall Practice 3
Today's task
- Complete the functions of the shopping module
- Complete the order module function
1.1 shopping module: 1.1.1 function Demonstration:
Product details:
Shopping Cart module:
1.1.2 code implementation:
1. Click [add to shopping cart] link on the product details page and.
2. Submit to Servlet:
* Quantity of purchased items.
* ID of the purchased item.
3. Store the shopping information to the session.
* Store the information of the shopping cart to the session.
* Encapsulation of shopping item objects (information of each purchased item in the shopping cart)
* Product objects:
* Quantity
* Subtotal
* Encapsulation of Shopping Cart objects (information about all purchased items)
* Collection of shopping items
* Total
4. obtain the information about the shopping cart on the page.
* Displayed on the page.
[Encapsulation of shopping items: CartItem]
Public class CartItem {private Product product Product; // private int count of the purchased Product; // Number of purchased private double subtotal; // subtotal public product getProduct () of the purchased Product () {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 {// defines the attributes of a collection of shopping items: the set uses Map, because it is convenient to remove shopping items. use the item id as the Map key // use the shopping item as the Map value. private Map <String, CartItem> map = new LinkedHashMap <String, CartItem> (); // defines the total in the cart: private double total; public Map <String, CartItem> getMap () {return map;} public double getTotal () {return total;} // method: add the shopping item to the shopping cart public void addCart (CartItem cartItem) {// determine whether the shopping item already exists in the shopping cart. string id = cartItem. getProduct (). getPid (); if (map. containsKey (id) {// if it already exists: + the new Quantity Based on the original quantity. total changed. // obtain CartItem _ cartItem = map for the original shopping item in the shopping cart. get (id); _ cartItem. setCount (_ cartItem. getCount () + cartItem. getCount ();} else {// if not, add a new shopping item to the collection. total changed. map. put (id, cartItem);} total + = cartItem. getSubtotal () ;}// method: remove the shopping item public void removeCart (String id) from the shopping cart {// remove the selected element from the map. // CartItem cartItem = map. get (id); CartItem cartItem = map. remove (id); // total-remove the subtotal of the shopping item total-= cartItem. getSubtotal () ;}// method: Clear the public void clearCart () {// clear the map set. map. clear (); // set the summary to 0. total = 0 ;}}
[Click the link to add to the shopping cart on the shopping details page]
Public String addCart (HttpServletRequest req, HttpServletResponse resp) {// receiving parameter: String pid = req. getParameter ("pid"); int count = Integer. parseInt (req. getParameter ("count"); try {// encapsulate shopping items: CartItem cartItem = new CartItem (); // item object: Query items by item ID. productService productService = (ProductService) BeanFactory. getBean ("productService"); Product product = productService. findById (pid); cartItem. setProduct (product); cartItem. setCount (count); // call the method to add to the shopping Cart in the shopping 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 "clear shopping cart" on the shopping cart page]
Public String clearCart (HttpServletRequest req, HttpServletResponse resp) {// get the cart object. cart cart = getCart (req); // call the method in the shopping cart: Cart. clearCart (); try {resp. sendRedirect (req. getContextPath () + "/jsp/cart. jsp ");} catch (Exception e) {e. printStackTrace (); throw new RuntimeException ();} return null ;}
Click Delete link on the shopping cart page]
Public String removeCart (HttpServletRequest req, HttpServletResponse resp) {try {// receiving parameter: String pid = req. getParameter ("pid"); // get the Cart: cart = getCart (req); Cart. removeCart (pid); // page Jump 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 an order:
Click Submit order on the shopping cart page]
Submit to Servlet:
* Encapsulate order and order items.
* Call the Business Layer
* Clear the shopping cart
* Page Jump