一个购物车的demo,数据时死的。用mvc结构写
上面的是购物车中的结构目录
首先建立数据源 用死数据进行模拟
/**
* 模拟数据库的数据
*/
public class MyDb {
private static Map<String, Book> map=new LinkedHashMap();
static{
map.put("1", new Book("1","javaweb开发","老张",20,"一本经典的书"));
map.put("2", new Book("2","jdbc开发","李勇",30,"一本jdbc的书"));
map.put("3", new Book("3","spring开发","老黎",50,"一本相当经典的书"));
map.put("4", new Book("4","hibernate开发","老佟",56,"一本佟佟的书"));
map.put("5", new Book("5","struts开发","老毕",40,"一本经典的书"));
map.put("6", new Book("6","ajax开发","老张",50,"一本老张的书"));
}
/**
* 得到所有的书本的信息
* @return
*/
public static Map<String, Book> getAll() {
return map;
}
}
书本的实体类
/**
* 书的实体类
*/
public class Book {
private String id;
private String name;
private String author;
private double price;
private String description;
public Book() {
// TODO Auto-generated constructor stub
}
public Book(String id, String name, String author, double price, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.description = description;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
接下来是 Dao
/**
* 根据 需求来和 数据库 打交道
*/
public class BookDao {
/**
* 根据book的id来获取书
*/
public Book find(String id){
return MyDb.getAll().get(id);
}
/**
* 获取所有的书本信息 以map的方式给你
*/
public Map getAll(){
return MyDb.getAll();
}
}
业务处理类
/**
* 书本业务的逻辑 用于和 dao层 和 view进行交互数据
*/
public class BusinessService {
BookDao bookDao=new BookDao();
/**
* 得到所有的书本信息
* @return
*/
public Map getAll(){
return bookDao.getAll();
}
/**
* 买书
*/
public void buybook(String bookid, Cart cart) {
Book book = bookDao.find(bookid);
cart.add(book);
}
/**
* 清空购物车
* @param cart
* @throws CartNotFoundException
*/
public void clear(Cart cart) throws CartNotFoundException{
if (cart ==null) {
throw new CartNotFoundException();
}
cart.clear();
}
/**
* 删除购物车中的商品
* @param cart
* @param bookid
* @throws CartNotFoundException
*/
public void delete(Cart cart,String bookid) throws CartNotFoundException{
if(cart==null){
throw new CartNotFoundException();
}
cart.getMap().remove(bookid);
}
/**
* 将 商品的数量进行改变
* @param cart
* @param bookid
* @param quantity
*/
public void updateCart(Cart cart,String bookid,int quantity) throws CartNotFoundException{
if(cart==null){
throw new CartNotFoundException();
}
CartItem cartItem=cart.getMap().get(bookid);
cartItem.setQuantity(quantity);
}
}
展示书本列表的 servlet
/**
* 书本的列表页
*/
public class ListBookServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//将数据中的书本信息取出 给request 域中 方便jsp中调用显示
BusinessService businessService=new BusinessService();
Map<String, Book> map=businessService.getAll();
request.setAttribute("map", map);
request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
展示书本的jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>书本的列表</title>
</head>
<body style="text-align: center;">
<br/>
<h2>书籍列表</h2>
<br/><br/>
<table border="1" width="80%">
<tr>
<td>书籍编号</td>
<td>书名</td>
<td>作者</td>
<td>售价</td>
<td>描述</td>
<td>操作</td>
</tr>
<%-- Set<Map.Entry<String,Book>> 每次循環出来的就是这个--%>
<c:forEach var="me" items="${map}">
<tr>
<td>${me.key}</td>
<td>${me.value.name}</td>
<td>${me.value.author}</td>
<td>${me.value.price}</td>
<td>${me.value.description}</td>
<td>
<a href="${pageContext.request.contextPath}/BuyServlet?bookid=${me.key}">购买</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
购买书本后的servlet
/**
* 点击购买按钮调到此界面进行处理
*/
public class BuyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookid=request.getParameter("bookid");
Cart cart=(Cart) request.getSession().getAttribute("cart");
if (cart==null) {
cart=new Cart();
request.getSession().setAttribute("cart", cart);
}
//将商品加入购物车中
BusinessService businessService=new BusinessService();
businessService.buybook(bookid, cart);
//展示购买过的商品
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
展示购买的商品的 servlet
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>购买过的商品</title>
<script type="text/javascript">
/* 清空购物车 */
function clearcart() {
var isClear=window.confirm("您确定要清空购物车吗??");
if(isClear){
window.location.href="${pageContext.request.contextPath}/ClearCartServlet";
}
}
/*更新 显示的数据*/
function updateCart(input, id){
// input 输入的内容 id 是更新的是哪个 商品
var quantity=input.value;
var b = window.confirm("请确认改为:" + quantity);
if(b) {
/* 将修改 的构成车中商品的id和修改成的数量给servlet */
window.location.href="${pageContext.request.contextPath}/UpdateCartServlet?bookid="+id + "&quantity=" + quantity;
}
}
</script>
</head>
<body style="text-align: center;">
<br/>
<h2>购物车列表</h2>
<br/><br/>
<!-- 当购物车不是空 -->
<c:if test="${!empty(cart.map)}">
<table border="1" width="80%">
<tr>
<td>书名</td>
<td>作者</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
<td>操作</td>
</tr>
<!-- Set<Map.Entry<String,CarItem>> 每次循環出来的就是这个 -->
<c:forEach var="me" items="${cart.map }">
<tr>
<td>${me.value.book.name} </td>
<td>${me.value.book.author} </td>
<td>${me.value.book.price} </td>
<td>
<input type="text" name="quantity" value="${me.value.quantity}" style="width: 60px" onchange="updateCart(this,${me.value.book.id })">
</td>
<td>${me.value.quantity} </td>
<td>${me.value.price }</td>
<td>
<a href="${pageContext.request.contextPath}/DeleteServlet?bookid=${me.value.book.id}">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="2">
<a href="javascript:clearcart()">清空购物车</a>
</td>
<td colspan="2">合计</td>
<td colspan="2">${cart.price}</td>
</tr>
</table>
</c:if>
<c:if test="${empty(cart.map)}">
对不起,您还没有购买任何商品
</c:if>
</body>
</html>
清空购物车的 servlet
/**
* 清空购物车哦
*/
public class ClearCartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cart cart= (Cart) request.getSession().getAttribute("cart");
BusinessService businessService=new BusinessService();
try {
businessService.clear(cart);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
} catch (CartNotFoundException e) {
request.setAttribute("message","对不起,您还没有购买任何商品!!!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
删除 商品的 sevlet
/**
* 删除 购物列表中的数据
*/
public class DeleteServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookid=request.getParameter("bookid");
//将次数的id从购物车进行删除
Cart cart=(Cart) request.getSession().getAttribute("cart");
BusinessService businessService=new BusinessService();
try {
businessService.delete(cart, bookid);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
} catch (CartNotFoundException e) {
request.setAttribute("message","对不起,您还没有购买任何商品!!!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
当在购买过的商品中进行修改商品的数量的时候
/**
* 当修改购买商品的数量 进行操作的 servlet
*/
public class UpdateCartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookid=request.getParameter("bookid");
String quantity=request.getParameter("quantity");
BusinessService businessService=new BusinessService();
Cart cart=(Cart)request.getSession().getAttribute("cart");
int q=Integer.parseInt(quantity);
try {
businessService.updateCart(cart, bookid, q);
request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);
} catch (CartNotFoundException e) {
request.setAttribute("message","对不起,您还没有购买任何商品!!!");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
购物车的实体
/**
* 购物车
*/
public class Cart {
/**
* 用于存储 购车车中的信息 key--- id value -- 书的信息
*/
private Map<String,CartItem> map = new LinkedHashMap();
private double price; //购车中总的价格
/**
* 向购物车中添加 书本
* @param book
*/
public void add(Book book)
{
//根据书本的id 知道 购物车中是否已经存入该商品
CartItem cartItem= map.get(book.getId());
if (cartItem!=null) {
//已经有该商品了
cartItem.setQuantity(cartItem.getQuantity()+1);
}else{
//该商品还没有 需要进行创建
cartItem=new CartItem();
cartItem.setBook(book);
cartItem.setQuantity(1);
map.put(book.getId(), cartItem);
}
}
/**
* 获取购物车
* @return
*/
public Map<String, CartItem> getMap(){
return map;
}
public void setMap(Map<String, CartItem> map) {
this.map = map;
}
/**
* 得到总价格
* @return
*/
public double getPrice(){
double totalprice =0;
for(Map.Entry<String, CartItem> me:map.entrySet()){
CartItem cartItem=me.getValue();
totalprice+=cartItem.getPrice();
}
return totalprice;
}
/**
* 清空数据
*/
public void clear(){
getMap().clear();
}
}
public class CartItem {
private Book book;
private int quantity;//购买的数量
private double price;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getPrice() {
return this.book.getPrice()*this.quantity;
}
public void setPrice(double price) {
this.price = price;
}
}