JavaWeb世界(十一):购物车的设计与实现

购物车概述

购物车的设计

  1. 基于 Session 的购物车
    购物车是存储到 Session 作用域的,浏览器关闭购物车就没有了
  2. 基于 Cookie 的购物车
    和基于 Session 一样,内容存在浏览器中,不能在不同电脑上共享信息
  3. 基于 Cookie + 数据库 的购物车
    购买商品时,如果还没有登录,此时就临时存到 Cookie 中
    若已经登录,先读取 Cookie 中的数据,然后保存到数据库中(可以在任何地方查看数据库中信息)

购物车模型和对象的建立

流程大致如下:
购物车流程

CartItem.java:
//购物车中的商品对象
@Data
public class CartItem {
	private String id;
	private String name;//商品名称
	private BigDecimal price;//单价
	private Integer num;//购买数量
}
ShoppingCart.java:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//购物车对象
public class ShoppingCart {
	//购物车中的多个商品对象
	private List<CartItem> items = new ArrayList<>();
	//购物车总价
	private BigDecimal totalPrice;

	//把商品添加进购物车
	public void save(CartItem newItem) {
		for (CartItem item : items) {
			//如果该商品已经在购物车里,则增加数量
			if (item.getId().equals(newItem.getId())) {
				item.setNum(item.getNum() + newItem.getNum());
				return;
			}
		}
		items.add(newItem);
	}

	//从购物车中移除指定id的商品
	public void delete(String id) {
		Iterator<CartItem> it = items.iterator();
		while (it.hasNext()) {
			CartItem item = it.next();
			if (item.getId().equals(id)) {
				//items.remove(item);//错误,防止并发修改
				it.remove();
				break;
			}
		}
	}

	//购物车中所有的商品
	public List<CartItem> getItem() {
		return items;
	}

	//购物车总价
	public BigDecimal getTotalPrice() {
		BigDecimal totalPrice = BigDecimal.ZERO;
		for (CartItem item : items) {
			totalPrice = totalPrice.add(item.getPrice().multiply(new BigDecimal(item.getNum())));
		}
		return totalPrice;
	}
}

购物车的实现

首先写 Servlet 请求:

ShoppingCartServlet.java:
import java.io.IOException;
import java.math.BigDecimal;

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 com.cherry._02_shoppingcart.domain.CartItem;
import com.cherry._02_shoppingcart.domain.ShoppingCart;

//处理购物车的添加删除操作
@WebServlet("/shoppingcart")
public class ShoppingCartServlet extends HttpServlet {

	private static final long serialVersionUID = -5568788051811437779L;

	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String cmd = req.getParameter("cmd");
		if ("save".equals(cmd)) {
			this.save(req, resp);
		} else if ("delete".equals(cmd)) {
			this.delete(req, resp);
		}
		resp.sendRedirect("/shoppingcart/cart_list.jsp");
	}

	protected void save(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1.接收请求参数
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		String id = "";
		BigDecimal price = BigDecimal.ZERO;
		//写死了编号和单价
		if ("iPhone 11".equals(name)) {
			id = "11";
			price = new BigDecimal("5000");
		} else if ("iPhone 11 Pro".equals(name)) {
			id = "22";
			price = new BigDecimal("8000");
		} else if ("iPhone 11 Pro Max".equals(name)) {
			id = "33";
			price = new BigDecimal("11000");
		}
		CartItem item = new CartItem(id, name, price, Integer.valueOf(num));

		//2.调用业务方法
		ShoppingCart cart = (ShoppingCart) req.getSession().getAttribute("SHOPPINGCART_IN_SESSION");
		System.out.println(cart);
		if (cart == null) {
			cart = new ShoppingCart();
			req.getSession().setAttribute("SHOPPINGCART_IN_SESSION", cart);
		}
		cart.save(item);
		System.out.println(cart);

		//3.控制界面跳转
	}

	protected void delete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1.接收请求参数
		String id = req.getParameter("id");
		//2.调用业务方法
		ShoppingCart cart = (ShoppingCart) req.getSession().getAttribute("SHOPPINGCART_IN_SESSION");
		cart.delete(id);
		//3.控制界面跳转
	}
}

这里简单起见就先将商品的单价和编号写死了,实际上应该是通过数据库进行获取。
然后写 JSP 页面:

cart_list.jsp:
package com.cherry._02_shoppingcart.servlet;

import java.io.IOException;
import java.math.BigDecimal;

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 com.cherry._02_shoppingcart.domain.CartItem;
import com.cherry._02_shoppingcart.domain.ShoppingCart;

//处理购物车的添加删除操作
@WebServlet("/shoppingcart")
public class ShoppingCartServlet extends HttpServlet {

	private static final long serialVersionUID = -5568788051811437779L;

	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String cmd = req.getParameter("cmd");
		if ("save".equals(cmd)) {
			this.save(req, resp);
		} else if ("delete".equals(cmd)) {
			this.delete(req, resp);
		}
		resp.sendRedirect("/shoppingcart/cart_list.jsp");
	}

	protected void save(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1.接收请求参数
		String name = req.getParameter("name");
		String num = req.getParameter("num");
		String id = "";
		BigDecimal price = BigDecimal.ZERO;
		if ("iPhone 11".equals(name)) {
			id = "11";
			price = new BigDecimal("5000");
		} else if ("iPhone 11 Pro".equals(name)) {
			id = "22";
			price = new BigDecimal("8000");
		} else if ("iPhone 11 Pro Max".equals(name)) {
			id = "33";
			price = new BigDecimal("11000");
		}
		CartItem item = new CartItem(id, name, price, Integer.valueOf(num));

		//2.调用业务方法
		ShoppingCart cart = (ShoppingCart) req.getSession().getAttribute("SHOPPINGCART_IN_SESSION");
		System.out.println(cart);
		if (cart == null) {
			cart = new ShoppingCart();
			req.getSession().setAttribute("SHOPPINGCART_IN_SESSION", cart);
		}
		cart.save(item);
		System.out.println(cart);

		//3.控制界面跳转
	}

	protected void delete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1.接收请求参数
		String id = req.getParameter("id");
		//2.调用业务方法
		ShoppingCart cart = (ShoppingCart) req.getSession().getAttribute("SHOPPINGCART_IN_SESSION");
		cart.delete(id);
		//3.控制界面跳转
	}
}
product_list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
	<form action="/shoppingcart?cmd=save" method="POST">
		商品名称:
			<select name="name">
				<option>iPhone 11</option>
				<option>iPhone 11 Pro</option>
				<option>iPhone 11 Pro Max</option>			
			</select></br>
		购买数量:<input type="number" name="num" min="1"}/>
		<br/><input type="submit" value="添加进购物车"/>
	</form>
</body>
</html>

其中在 JSP 获取作用域对象的属性时,默认调用 getter 方法,因此要么用 lombok 插件生成 Getter Setter 要么自己写,但是一定是 getXxxx(属性名) 不能写错,否则会报找不到属性的异常。我就是 items 的 get 方法写成了 getItem 找了半天的错误,应该写成 getItems

最终的结果虽然比较丑,但是基本功能都能实现:
1
2

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
javaweb在线选课系统的设计实现需要考虑多方面的因素。首先,系统需要具备用户管理功能,包括学生、教师和管理员的注册和登录功能,以及权限管理和信息修改等功能。其次,系统需要提供课程管理功能,包括课程的发布、修改和删除,以及课程的分类和搜索等功能。此外,系统还需要提供选课功能,包括学生选课和教师管理选课结果等功能。 为了实现这些功能,我们可以采用MVC(Model-View-Controller)架构来设计系统。在模型层,我们可以使用Java设计数据库表结构,并使用JDBC(Java Database Connectivity)技术来实现数据库的连接和操作。在控制层,我们可以使用Servlet来处理用户请求,并调用适当的业务逻辑处理方法。在视图层,我们可以使用JSPJavaServer Pages)来实现页面的呈现和交互。 在系统的安全性方面,我们可以使用HTTPS协议来保证用户信息的安全传输,同时使用加密和解密算法来保护用户数据的存储和传输安全。 在用户体验方面,我们可以采用Ajax技术来实现页面的无刷新加载和交互,提高系统的响应速度和用户体验。 总之,javaweb在线选课系统的设计实现涉及数据库设计、业务逻辑处理、页面呈现和用户交互等多个方面,需要全面考虑用户需求和系统性能,才能设计一个功能完善、安全稳定、用户友好的在线选课系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值