javaweb购物车案列学习开发

网友投稿 219 2023-02-01


javaweb购物车案列学习开发

本文实例为大家分享了javaweb购物车案列的具体代码,供大家参考,具体内容如下

一、项目目录结构

二、源代码

dao包——dao层:BookDao.java

package com.dao;

import java.util.Map;

import com.DB.DB;

import com.domain.Book;

public class BookDao {

public Map getAll(){

return DB.getAll();

}

public Book find(String id){

return (Book) DB.getAll().get(id);

}

}

DB包:DB.java——模拟数据库

package com.DB;

import java.util.LinkedHashMap;

import java.util.Map;

import com.domain.Book;

import com.sun.org.apache.bcel.internal.generic.NEW;

//代表数据库

//代表数据库

public class DB {

private static Map map = new LinkedHashMap();

static{

map.put("1", new Book("1","javaweb开发","老张",38,"一本好书"));

map.put("2", new Book("2","jdbc开发","老黎",18,"一本好书"));

map.put("3", new Book("3","ajax开发","老佟",328,"一本好书"));

map.put("4", new Book("4","jbpm开发","老毕",58,"一本好书"));

map.put("5", new Book("5","struts开发","老方",28,"一本好书"));

map.put("6", new Book("6","spring开发","老方",98,"一本好书"));

}

public static Map getAll(){

return map;

}

}

domain包:

Book.java:书的实体类

package com.domain;

//书的实体类

public class Book {

private String id;

private String name;

private String author;

private double price;

private String description;

public Book() {

super();

// 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;

}

}

Cart.java:购物车类

package com.domain;

import java.util.LinkedHashMap;

import java.util.Map;

//代表用户的购物车

//代表用户的购物车

public class Cart {

private Map map = new LinkedHashMap();

private double price; //记住购物车所有商品多少钱

public void add(Book book){

//看购物车中有没有,要添加的书对应的购物项

CartItem item = map.get(book.getId());

if(item==null){

item = new CartItem();

item.setBook(book);

item.setQuantity(1);

map.put(book.getId(), item);

}else{

item.setQuantity(item.getQuantity()+1);

}

}

public Map getMap() {

return map;

}

public void setMap(Map map) {

this.map = map;

}

public double getPrice() {

double totalprice = 0;

for(Map.Entry entry : map.entrySet()){

CartItem item = entry.getValue();

totalprice += item.getPrice();

}

this.price = totalprice;

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

CartItem.java:购物项

package com.domain;

//用于代表某个商品,以及商品出现的次数(购物项)

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;

this.price = this.book.getPrice() * this.quantity;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

service包:service层

BusinessService.java:

package com.service;

import java.util.Map;

import com.dao.BookDao;

import com.domain.Book;

import com.domain.Cart;

import com.domain.CartItem;

//业务类,统一对web层提供所有服务

public class BusinessService {

private BookDao dao = new BookDao();

public Map getAllBook(){

return dao.getAll();

}

public Book findBook(String id){

return dao.find(id);

}

//删除购物车中的购物项

public void deleteCartItem(String id, Cart cart) {

cart.getMap().remove(id);

}

//清空购物车

public void clearCart(Cart cart) {

cart.getMap().clear();

}

//改变购物项的数量

public void changeItemQuantity(String id, String quantity, Cart cart) {

CartItem item = cart.getMap().get(id);

item.setQuantity(Integer.parseInt(quantity));

}

}

web层:

ListBookServlet.java:显示所有书籍

package com.web.controller;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.service.BusinessService;

//获取所有书籍

//获取所有书

public class ListBookServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

BusinessService seTebGZLHrvice = new BusinessService();

Map map = service.getAllBook();

request.setAttribute("map", map);

request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

BuyServlet.java:处理购买请求

package com.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.domain.Book;

import com.domain.Cart;

import com.service.BusinessService;

//完成书籍购买

//完成书籍购买

public class BuyServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String id = request.getParameter("id");

BusinessService service = new BusinessService();

Book book = service.findBook(id);

//得到用户的购物车

Cart cart = (Cart) request.getSession().getAttribute("cart");

if(cart==null){

cart = new Cart();

request.getSession().setAttribute("cart", cart);

}

//把书加到用户购物车中,完成购买

cart.add(book);

//response.sendRedirect("/WEB-INF/jsp/listcart.jsp");

request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

DeleteItemServlet.java:删除某一种商品

package com.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.domain.Cart;

import com.service.BusinessService;

//删除指定的购物项

public class DeleteItemServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String id = request.getParameter("id");

Cart cart = (Cart) request.getSession().getAttribute("cart");

BusinessService service = new BusinessService();

service.deleteCartItem(id,cart);

//删除成功

request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response)http://;

}

}

ClearCartServlet.java:清空购物车

package com.web.controller;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.domain.Cart;

import com.service.BusinessService;

//清空购物车

public class ClearCartServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Cart cart = (Cart) request.getSession().getAttribute("cart");

BusinessService service = new BusinessService();

service.clearCart(cart);

request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

ChangeQuantityServlet.java:修改购物车中指定商品的数量

package com.web.controller;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.domain.Cart;

import com.service.BusinessService;

//把购物车中的书修改为指定数量

public class ChangeQuantityServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String id = request.getParameter("id");

String quantity = request.getParameter("quantity");

Cart cart = (Cart) request.getSession().getAttribute("cart");

BusinessService service = new BusinessService();

service.changeItemQuantity(id,quantity,cart);

request.getRequestDispatcher("/WEB-INF/jsp/listcart.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

jsp页面:

WebRoot/WEB-INF/jsp/listbook.jsp:显示书籍列表

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

购买

WebRoot/WEB-INF/jsp/listcart.jsp:显示购物车列表

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

您没有购买任何商品!!!

删除

清空购物车


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:连接共享文件夹提示限制(共享文件上限)
下一篇:LibrarySystem图书管理系统开发(一)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~