Javaweb Book Mall Design Shopping Cart Module (3) _java

Source: Internet
Author: User

This article continues to share the Javaweb Book Mall shopping Cart module, for your reference, the specific content as follows

Shopping Cart Storage

Save in session
Save in Cookie
Save in database

1. Create related class

The structure of the shopping cart:

cartitem: shopping cart entries, including books and quantities
cart: shopping cart, containing a map

/** * Shopping Cart class * * public class Cart {private map<string,cartitem> Map = new Linkedhashmap<string,cartitem> ()

  ; /** * Calculation Total * @return */public double gettotal () {//total = Subtotal of all entries and BigDecimal sum = new BigDecimal ("0
    ");
      For (Cartitem cartItem:map.values ()) {BigDecimal subtotal = new BigDecimal ("" + cartitem.getsubtotal ());
    Total = Total.add (subtotal);
  return Total.doublevalue (); /** * Add entry to vehicle * @param cartitem/public void Add (Cartitem cartitem) {if Map.containskey (cartitem.ge Tbook (). Getbid ()) {//Determine if the entry exists in the original car cartitem _cartitem = Map.get (Cartitem.getbook (). Getbid ());//return Original entry _cartite M.setcount (_cartitem.getcount () + cartitem.getcount ());//Set the number of old entries for, its own number + the number of new entries Map.put (Cartitem.getbook (). getbid
    (), _cartitem);
    else {map.put (Cartitem.getbook (). Getbid (), cartitem);
  }/** * Empty all entries/public void Clear () {map.clear (); /** * Delete specified entry * @paraM bid */public void Delete (String bid) {map.remove (BID);
  /** * Get ALL Entries * @return * * * public collection<cartitem> Getcartitems () {return map.values ();

 }
}

/**
 * Shopping Cart Entry class
 */Public
class Cartitem {
  private book book;//merchandise
  private int count;//number

  /* *
   * Subtotal Method
   * @return
   * Processing binary error problem/public
  double getsubtotal () {//Subtotal method, but it does not have a corresponding member!
    BigDecimal d1 = new BigDecimal (Book.getprice () + "");
    BigDecimal D2 = new BigDecimal (count + "");
    Return D1.multiply (D2). Doublevalue ();
  }

  Public book GetBook () {return book
    ;
  }

  public void Setbook (book book) {
    this.book = Book;
  }

  public int GetCount () {return
    count;
  }

  public void SetCount (int count) {
    This.count = count;
  }
}

2. Add Shopping Cart Entry

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <%@ taglib prefix= "C" uri= "http://" Java.sun.com/jsp/jstl/core "%> <%@ taglib prefix=" FMT "uri=" http://java.sun.com/jsp/jstl/fmt "%> <%@ Taglib prefix= "FN" uri= "http://java.sun.com/jsp/jstl/functions"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 
public class Cartservlet extends Baseservlet {/** * Add Shopping Item * @param request * @param response * @return * @throws servletexception * @throws ioexception/Public String Add (httpservletrequest request, HTTPSERVLETRESP Onse response) throws Servletexception, IOException {* * * 1. Get the car * 2. Get the entry (get book and quantity) * 3. Add the entry to the car Medium/* * 1.
    Get the car/cart cart = (cart) request.getsession (). getattribute ("cart"); * * * The form is delivered only bid and quantity * 2. Get the entry * > get the Book and quantity * > Get the bid of the book first, then we need to query the database via bid to get the books * > Quantity form has/String bid = Reque
    St.getparameter ("bid");
    Book book = new Bookservice (). Load (BID);
    int count = Integer.parseint (Request.getparameter ("Count"));
    Cartitem Cartitem = new Cartitem ();
    Cartitem.setbook (book);

    Cartitem.setcount (count); * * 3.

    Add the entry to the vehicle * * CART.ADD (Cartitem);
  return "f:/jsps/cart/list.jsp"; /** * Empty Shopping items * @param request
   * @param response * @return * @throws servletexception * @throws ioexception/Public String Clear (Http
     ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {/** * 1. Get the car * 2.
    Set the clear/cart cart = (cart) request.getsession (). getattribute ("cart");
    Cart.clear ();
  return "f:/jsps/cart/list.jsp"; /** * Delete Shopping items * @param request * @param response * @return * @throws servletexception * @throws Ioex Ception */Public String Delete (HttpServletRequest request, httpservletresponse response) throws Servletexcepti On, IOException {/* 1. Get the car * 2. Get the bid/cart cart = (cart) request.getsession () to be deleted. getattribute
    ("cart");
    String bid = Request.getparameter ("bid");
    Cart.delete (BID);
  return "f:/jsps/cart/list.jsp";

 }
}

3, Empty the entry

4. Delete Shopping Cart entry

5. My Shopping Cart

There is a link in the top.jsp: my Shopping cart

My shopping cart accesses/jsps/cart/list.jsp directly, and it displays all the entries in the session in the car.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.