![cover](https://img-blog.csdnimg.cn/direct/88142a4879b445c38cca37008b88888c.png)
JavaWeb实现模拟数据库实现购物车功能
Product类 ,是一共父类后续物品都继承它。4.1 增加到购物车的业务逻辑。4.2 删除购物车的业务逻辑。
·
一, 创建JavaWeb项目
1.1在空文件里创建一个文件加例如我创建的是web02文件夹
1.2 idea中创建JavaWeb项目
1.3 选择刚刚创建的文件夹
1.4 创建文件初始化工程文件目录
二,创建POJO类
Product类 ,是一共父类后续物品都继承它
package com.best.pojo;
// 商品类
public class Product {
private int id;
private String name;
private double price;
private Integer quantity;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = 0;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
2.1 Clothing, Flower,HDD,USB 类都继承Product类
2.1.1 Clothing
package com.best.pojo;
// 服装类
public class Clothing extends Product {
public Clothing(int id, String name, double price) {
super(id, name, price);
}
}
2.1.2 HDD
package com.best.pojo;
// 移动硬盘类
public class HDD extends Product {
public HDD(int id, String name, double price) {
super(id, name, price);
}
}
2.1.3 USB
package com.best.pojo;
// U盘类
public class USB extends Product {
public USB(int id, String name, double price) {
super(id, name, price);
}
}
2.1.4 Flower
package com.best.pojo;
// 鲜花类
public class Flower extends Product {
public Flower(int id, String name, double price) {
super(id, name, price);
}
}
三 模拟数据库数据data
Data.java
package com.best.data;
import com.best.pojo.*;
import java.util.HashMap;
import java.util.Map;
public class Database {
private static Map<Integer, Product> products = new HashMap<>();
static {
products.put(1, new USB(1, "USB 闪存盘", 1002));
products.put(2, new HDD(2, "外置硬盘", 3009));
products.put(3, new Flower(3, "玫瑰花束", 75));
products.put(4, new Clothing(4, "T恤衫", 99));
}
public static Product getProductById(int id) {
return products.get(id);
}
public static Map<Integer, Product> getAllProducts() {
return products;
}
}
四, 写servlet核心类
4.1 增加到购物车的业务逻辑
AddToCartServlet.java
package com.best.servlet;
import com.best.data.Database;
import com.best.pojo.Product;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/addToCart")
public class AddToCartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
int productId = Integer.parseInt(request.getParameter("id"));
HttpSession session = request.getSession();
List<Product> cart = (List<Product>) session.getAttribute("cart");
if (cart == null) {
cart = new ArrayList<>();
session.setAttribute("cart", cart);
}
// 检查购物车中是否已存在相同商品,如果存在则增加数量,否则添加新商品到购物车
boolean found = false;
for (Product product : cart) {
if (product.getId() == productId) {
product.setQuantity(product.getQuantity() + 1);
found = true;
break;
}
}
if (!found) {
Product product = Database.getProductById(productId);
product.setQuantity(1);
cart.add(product);
}
response.sendRedirect("displayProducts.jsp");
}
}
4.2 删除购物车的业务逻辑
RemoveFromCartServlet.java
package com.best.servlet;
import com.best.pojo.Product;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
@WebServlet("/removeFromCart")
public class RemoveFromCartServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取要移除的商品 id
int productId = Integer.parseInt(request.getParameter("id"));
// 从 session 中获取购物车列表
List<Product> cart = (List<Product>) request.getSession().getAttribute("cart");
// 在购物车中查找要移除的商品,并移除它
if (cart != null) {
Iterator<Product> iterator = cart.iterator();
while (iterator.hasNext()) {
Product product = iterator.next();
if (product.getId() == productId) {
// 更新商品的数量
int quantity = product.getQuantity();
if (quantity > 1) {
product.setQuantity(quantity - 1);
} else {
iterator.remove(); // 如果数量为1,则直接移除商品
}
break;
}
}
}
// 重定向到查看购物车页面
response.sendRedirect("viewCart.jsp");
}
}
五,渲染数据 到jsp页面
5.1 浏览商品的页面 displayProducts.jsp
<%@ page import="com.best.pojo.Product" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.best.data.Database" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品</title>
</head>
<body>
<h2>商品:</h2>
<ul>
<%
Map<Integer, Product> products = Database.getAllProducts();
for (Product product : products.values()) {
%>
<li><%= product.getName() %> - ¥<%= product.getPrice() %> <a href='addToCart?id=<%= product.getId() %>&viewCart=true'>添加购物车</a></li>
<% } %>
</ul>
<a href="viewCart.jsp">查看购物车</a>
</body>
</html>
5.2 购物车页面 viewCart.jsp
<%@ page import="java.util.List" %>
<%@ page import="com.best.pojo.Product" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查看购物车</title>
</head>
<body>
<h2>购物车内容:</h2>
<ul>
<%
int totalCount = 0; // 总数量
double totalPrice = 0; // 总价钱
List<Product> cart = (List<Product>) session.getAttribute("cart");
if (cart != null && cart.size() > 0) {
for (Product product : cart) {
totalCount += product.getQuantity(); // 累加每个商品的数量
totalPrice += product.getPrice() * product.getQuantity(); // 累加每个商品的总价钱
%>
<li><%= product.getName() %> - ¥<%= product.getPrice()*product.getQuantity() %> - 数量:<%= product.getQuantity() %> <a href='removeFromCart?id=<%= product.getId() %>'>移除</a></li>
<%
}
} else {
%>
<li>您的购物车为空</li>
<%
}
%>
</ul>
<p>购物车中共有 <%= totalCount %> 件商品。</p>
<p>购物车总价钱为 ¥<%= totalPrice %></p>
<a href="displayProducts.jsp">继续购物</a>
</body>
</html>
六 最终实现效果
视频演示
javaweb购物车案例
更多推荐
所有评论(0)