# 简易购物车
就是一个简单的购物车,还可以扩展。
实现原理:
用户在ShowBook页面购书,BuyBookCl进行业务逻辑处理之后,显示在ShowMyCart上。Book是图书信息类,DB是一个内存图书数据库,用来存放初始化图书信息。
使用技术:
1、JAVASE中的ArrayList,HashMap,LinkedHashMap
2、Session技术
3、Servlet技术
4、单例
~~~
package com.pc;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Switch
* 功能:显示图书
*/
public class ShowBook extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>欢迎购买</h1>");
// 取出DB
ArrayList<Book> db = DB.getBooks();
for(Book book : db){
out.println(book.getName() + "<a href='/MyCart/BuyBookCl?id="+ book.getId() + "&name=" + book.getName() + "&price=" + book.getPrice() + "'>点击购买</a><br/>");
}
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
~~~
~~~
package com.pc;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Switch
* 功能:处理购书信息
*/
public class BuyBookCl extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
// 接收用户购书信息
String bookname = request.getParameter("name");
String id = request.getParameter("id");
String price = request.getParameter("price");
// 从会话获取总价
Float totalprice = (Float) request.getSession().getAttribute("totalprice");
// 将总价加入会话中
if(totalprice == null){
request.getSession().setAttribute("totalprice", Float.parseFloat(price));
} else {
request.getSession().setAttribute("totalprice", totalprice + Float.parseFloat(price));
}
// 从session中取出mybooks
HashMap<String, Book> books = (HashMap<String, Book>) request.getSession().getAttribute("mybooks");
// 如果是第一次购物 books == 空
if(books == null){
// 创建集合对象
books = new LinkedHashMap<String, Book>();
// 创建book对象
Book book = new Book();
book.setId(id);
book.setName(bookname);
// 设置图书数量为1
book.setNum(1);
books.put(id, book);
// 将bookd加入session
request.getSession().setAttribute("mybooks", books);
} else {
// 检测集合中是否有该书
if(books.containsKey(id)){
// 表示数已被购买过
// 从集合中取出图书
Book book = books.get(id);
// 数量+1
book.setNum(book.getNum() + 1);
} else {
// 该图书未被购买过
Book book = new Book();
book.setId(id);
book.setName(bookname);
book.setNum(1);
books.put(id, book);
}
}
// 转发到显示购物车界面
request.getRequestDispatcher("/ShowMyCart").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
~~~
~~~
package com.pc;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Switch
* 功能:显示购物车
*/
public class ShowMyCart extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
// 从session中取出购买的书
HashMap<String, Book> books = (HashMap<String, Book>) request.getSession().getAttribute("mybooks");
// 测试
// System.out.println(books);
out.println("您的购物车有以下书籍:<br/>");
Iterator iterator = books.keySet().iterator();
while(iterator.hasNext()){
Book book = books.get(iterator.next());
out.println("书名为:" + book.getName() + " 数量为:" + book.getNum());
out.println("<br/>");
}
out.println("总价为:" + request.getSession().getAttribute("totalprice"));
out.println("<br/>");
out.println("<a href='/MyCart/ShowBook'>返回购物大厅</a>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
~~~
~~~
package com.pc;
/**
* @author Switch
* 功能:存储图书信息
* id------id号
* name----书名
* num-----数量
* price---价格
*/
public class Book {
private String id;
private String name;
private int num;
private float price;
// 设置器与获取器
public String getId() {
return id;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
~~~
~~~
package com.pc;
import java.io.Serializable;
import java.util.ArrayList;
/**
*
* @author Switch
* 功能:模拟内存数据库,单例实现
*
*/
public final class DB implements Serializable{
private static ArrayList<Book> books = null;
private DB(){
}
// 静态代码块初始化图书信息
static{
books = new ArrayList<Book>();
Book book1 = new Book();
book1.setId("1");
book1.setName("Think in Java");
book1.setPrice(99);
books.add(book1);
Book book2 = new Book();
book2.setId("2");
book2.setName("Javascript权威指南");
book2.setPrice(139);
books.add(book2);
Book book3 = new Book();
book3.setId("3");
book3.setName("Head First Servlet & JSP");
book3.setPrice(129);
books.add(book3);
Book book4 = new Book();
book4.setId("4");
book4.setName("设计模式之禅");
book4.setPrice(89);
books.add(book4);
Book book5 = new Book();
book5.setId("5");
book5.setName("Java核心技术");
book5.setPrice(139);
books.add(book5);
}
// 返回图书集合
public static ArrayList<Book> getBooks() {
return books;
}
}
~~~
----------参考《韩顺平.细说Servlet》