Ajax Study Notes-shopping cart

Source: Internet
Author: User

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/4B/F9/wKioL1Q2SYPRBh7ZAACQ5cOHFcE328.jpg "Title =" qq20141009163718.png "alt =" wkiol1q2syprbh7zaacq5cohfce328.jpg "/>

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Shoppingcartitem. Java

public class ShoppingCartItem {private int number;private String bookName;private int price;//...}

Shoppingcart. Java shopping cart

public class ShoppingCart {//key:书名   value:ShoppingCartItem对象private Map<String, ShoppingCartItem> items = new HashMap<String, ShoppingCartItem>();private String bookName;public void addToCart(String bookName, int price) {this.bookName = bookName;if (items.containsKey(bookName)) {ShoppingCartItem shoppingCartItem = items.get(bookName);shoppingCartItem.setNumber(shoppingCartItem.getNumber() + 1);}else{ShoppingCartItem shoppingCartItem = new ShoppingCartItem();shoppingCartItem.setBookName(bookName);shoppingCartItem.setNumber(1);shoppingCartItem.setPrice(price);items.put(bookName, shoppingCartItem);}}public int getTotalBookNum() {int total = 0;for (ShoppingCartItem item:items.values()) {total += item.getNumber();}return total;}public int getTotalMoney() {int money = 0;for (ShoppingCartItem item:items.values()) {money += item.getNumber() * item.getPrice();}return money;}public String getBookName() {return bookName;}}

Addtocart. Java Servlet

String bookName = request.getParameter("id");int price = Integer.parseInt(request.getParameter("price"));ShoppingCart shoppingCart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");if (shoppingCart == null) {shoppingCart = new ShoppingCart();request.getSession().setAttribute("shoppingCart", shoppingCart);}shoppingCart.addToCart(bookName, price);StringBuilder result = new StringBuilder();result.append("{")//必须用双引号.append("\"bookName\":\"" + bookName + "\"").append(",").append("\"totalBookNum\":" + shoppingCart.getTotalBookNum()).append(",").append("\"totalMoney\":" + shoppingCart.getTotalMoney()).append("}");response.setContentType("text/javascript");response.getWriter().print(result.toString());


This article is from the "Avatar" blog, please be sure to keep this source http://shamrock.blog.51cto.com/2079212/1561834

Ajax Study Notes-shopping cart

Related Article

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.