java 图书管理系统(三)jdbc + servlet + sql server + html

点击查看图书购买管理系统的所有代码

使用 Map 来保存用户对应的购物车信息。

添加购物车功能

Servlet中的关键代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取PrintWriter对象
		PrintWriter writer = response.getWriter();
		// 检测用户是否已经登录
		ServletContext context = getServletContext();
		Object user = context.getAttribute("user");
		String logIn = (String) context.getAttribute("admin");
		if (null == user) {// 还未登录
			writer.write("您还未登录,<a href='login.html'>点此重新登录</a>");
		} else if (logIn == null) {
			writer.write("请选择登录身份!<a href='login.html'>点此重新登录</a>");
		} else {
			String goodName = request.getParameter("bookName");
			String goodPrice = request.getParameter("bookPrice");
			if (goodName == null || goodPrice == null) {
				writer.write("添加购物车失败<a href='book.action'>点击返回图书页面</a>");
			} else {
				// 获取请求参数,构造成一个商品对象
				String name = request.getParameter("bookName");
				String price = request.getParameter("bookPrice");
				Goods good = new Goods();
				good.setBookName(name);
				good.setBookPrice(Float.parseFloat(price));
				// 获取session对象
				HttpSession session = request.getSession();
				// 获取session中的产品
				List<Goods> goods = (List<Goods>) session.getAttribute(user + "");
				Map<Object, List<Goods>> map = (Map<Object, List<Goods>>) session.getAttribute("map");
				// 判断
				if (map == null) {
					map = new HashMap<Object, List<Goods>>();
				}
				if (goods == null) {// 第一次访问
					goods = new ArrayList<Goods>();
					// 第一次访问时,购物车为空
					// 在添加图书的同时,设置图书的数量为1
					goods.add(good);
					good.setBookNum(1);
				} else {
					// 先判断购物车中是否已存在此商品(遍历购物车中的商品)
					// 如果不存在则添加,否则该商品的数量+1
					// 定义一个判断当前添加的商品是否在购物车中存在的标识flag
					int flag = 0;
					for (int i = 0; i < goods.size(); ++i) {
						if (good.getBookName().equals(goods.get(i).getBookName())) {
							goods.get(i).setBookNum(goods.get(i).getBookNum() + 1);
							// 如果购物车中已存在,则改变flag
							flag = 1;
						}
					}
					// 当购物车中不存在时,添加商品并设置商品数量为1
					if (flag == 0) {
						goods.add(good);
						good.setBookNum(1);
					}
				}
				// 将该用户与其购物车信息存入map
				map.put(user, goods);
				// 更新session中的数据
				session.setAttribute(user + "", goods);
				session.setAttribute("map", map);
				// 跳转回产品列表页
				response.sendRedirect("book.action");
			}
		}
	}

查看购物车功能

显示购物车中的商品(图书名称、图书价格、数量、商品总价格) 一个用户名以不同的身份登录认为是同一个人

Servlet中的关键代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取PrintWriter对象
		PrintWriter writer = response.getWriter();
		// 检测用户是否已经登录
		ServletContext context = getServletContext();
		Object user = context.getAttribute("user");
//		System.out.println(user);
		String logIn = (String) context.getAttribute("admin");
		if (null == user) {// 还未登录
			writer.write("您还未登录,<a href='login.html'>点此重新登录</a>");
		} else if (logIn == null) {
			writer.write("请选择登录身份!<a href='login.html'>点此重新登录</a>");
		} else {
			// 获取session对象
			HttpSession session = request.getSession();
			// 获取session中的产品
			List<Goods> goodList = (List<Goods>) session.getAttribute(user + "");
			Map<Object, List<Goods>> map = (Map<Object, List<Goods>>) session.getAttribute("map");
			if (map == null || goodList == null) {
				writer.write("购物车空空如也,返回商品页逛逛吧~");
				writer.write("<a href='book.action'>返回商品页</a>");
			} else {
				// 遍历map寻找该用户的购物车信息
				Iterator iterator = map.entrySet().iterator();
				while (iterator.hasNext()) {
					Map.Entry<Object, List<Goods>> entry = (Entry<Object, List<Goods>>) iterator.next();
					if (entry.getKey().equals(user)) {
						List<Goods> goods = map.get(user);
						// 判断
						if (goods == null || goods.size() == 0) {
							writer.write("购物车空空如也,返回商品页逛逛吧~");
							writer.write("<a href='book.action'>返回商品页</a>");
						} else {
							// 遍历集合,并将集合中的数据显示在浏览器页面上
							writer.write("<table border='1'>");
							writer.write("<tr>");
							writer.write("<th>图书名称</th>");
							writer.write("<th>图书价格</th>");
							writer.write("<th>图书数量</th>");
							writer.write("</tr>");
							float total = 0;
							for (Goods good : goods) {
								String name = good.getBookName();
								float price = good.getBookPrice();
								int num = good.getBookNum();
								total += price * num;
								writer.write("<tr>");
								writer.write("<td>" + name + "</td>");
								writer.write("<td>" + price + "</td>");
								writer.write("<td>" + num + "</td>");
								writer.write("</tr>");
							}
							writer.print("</table>");
							writer.write("<br/>");
							writer.write("<p>商品总价:" + total + "</p>");
							// 打印额外的超链接(返回继续购物、结账)
							writer.write("<br/>");
							writer.write("<a href='book.action'>返回继续购物</a>");
							writer.write("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
							writer.write("<a href='checkout.action'>结账</a>");
						}
						writer.write("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
						writer.write("<a href='createOrder.action'>查看历史订单</a>");
					}
				}
			}
		}
	}

清空购物车功能

Servlet中的关键代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取PrintWriter对象
		PrintWriter writer = response.getWriter();
		// 检测用户是否已经登录
		ServletContext context = getServletContext();
		Object user = context.getAttribute("user");
		String logIn = (String) context.getAttribute("admin");
		if (null == user) {// 还未登录
			writer.write("您还未登录,<a href='login.html'>点此重新登录</a>");
		} else if (logIn == null) {
			writer.write("请选择登录身份!<a href='login.html'>点此重新登录</a>");
		} else {// 已经登录
			// 获取session对象
			HttpSession session = request.getSession();
			// 获取session中的数据
			Map<Object, List<Goods>> map = (Map<Object, List<Goods>>) session.getAttribute("map");
			Map<Object, List<Goods>> history = (Map<Object, List<Goods>>) session.getAttribute("history");
			// 遍历map寻找该用户的购物车信息
			Iterator iterator = map.entrySet().iterator();
			while (iterator.hasNext()) {
				Map.Entry<Object, List<Goods>> entry = (Entry<Object, List<Goods>>) iterator.next();
				if (entry.getKey().equals(user)) {
					List<Goods> good = map.get(user);
					// 判断
					if (history == null) {
						history = new HashMap<Object, List<Goods>>();
					}
					// 保存购物信息
					List<Goods> order = good;
					history.put(user, order);
					session.setAttribute("history", history);
					// 清空购物车
					good = new ArrayList<Goods>();
					map.put(user, good);
					session.setAttribute("submitTime", new Date());
					session.setAttribute(user + "", order);
					session.setAttribute("map", map);
				}
			}
			writer.write("<p>恭喜你,结账成功!</p>");
			writer.write("<br/>");
			writer.write("<a href='ShowShoppingCart.action'>点击返回购物车</a>");
			writer.write("<br/>");
			writer.write("<br/>");
			writer.write("<a href='book.action'>点击返回图书界面</a>");
		}
	}

历史订单功能

1、生成订单
Servlet中的关键代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取PrintWriter对象
		PrintWriter writer = response.getWriter();
		// 检测用户是否已经登录
		ServletContext context = getServletContext();
		Object user = context.getAttribute("user");
		String logIn = (String) context.getAttribute("admin");
		if (null == user) {// 还未登录
			writer.write("您还未登录,<a href='login.html'>点此重新登录</a>");
		} else if (logIn == null) {
			writer.write("请选择登录身份!<a href='login.html'>点此重新登录</a>");
		} else {
			// 获取session对象
			HttpSession session = request.getSession();
			// 获取session中的数据
			Map<Object, List<Goods>> history = (Map<Object, List<Goods>>) session.getAttribute("history");
			// 判断
			if (history == null) {
				writer.write("历史订单为空,<a href='book.action'>点击返回商品页</a>");
				writer.write("<br/><br/>");
				writer.write("<a href='ShowShoppingCart.action'>点击返回购物车</a>");
			} else {
				Iterator iterator = history.entrySet().iterator();
				while (iterator.hasNext()) {
					Map.Entry<Object, List<Goods>> entry = (Entry<Object, List<Goods>>) iterator.next();
					if (entry.getKey().equals(user)) {
						List<Goods> order = history.get(user);
						if (order == null || order.size() == 0) {
							writer.write("历史订单为空,返回商品页逛逛吧~");
							writer.write("<a href='book.action'>返回商品页</a>");
						} else {
							System.out.println("生成订单ing...");
							for (Goods orders : order) {
								Order uOrder = new Order();
								uOrder.setGoodName(orders.getBookName());
								uOrder.setGoodPrice(orders.getBookPrice());
								uOrder.setGoodNum(orders.getBookNum());
								SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								uOrder.setSubmitTime(format.format(session.getAttribute("submitTime")) + "");
								uOrder.setUserName(user + "");
								OrderService orderService = new OrderService();
								orderService.addOrders(uOrder);
							}
							// 添加到数据库之后清空
							while (order.size() > 0) {
								order.remove(0);
							}
							history.put(user, order);
							session.setAttribute("history", history);
						}
					}
				}
			}
		}
		// 跳转到显示历史订单页
		response.sendRedirect("historicalOrder.action");
	}

处理订单的OrderService中的关键代码:

/**
	 * 构造订单对象
	 * 
	 * @param orderId    订单编号
	 * @param goodName   商品名称
	 * @param goodPrice  商品价格
	 * @param goodNum    商品数量
	 * @param submitTime 结账时间
	 * @param userName   用户姓名
	 * @return 构造的订单对象
	 */
	private Order createOrder(int orderId, String goodName, float goodPrice, int goodNum, String submitTime,
			String userName) {
		Order order = new Order();
		order.setOrderId(orderId);
		order.setGoodName(goodName);
		order.setGoodPrice(goodPrice);
		order.setGoodNum(goodNum);
		order.setSubmitTime(submitTime);
		order.setUserName(userName);
		return order;
	}

	/**
	 * 添加历史订单
	 * 
	 * @param Order对象
	 * @return 添加订单是否成功,成功返回true,否则返回false
	 */
	public boolean addOrders(Order order) {
		String goodName = order.getGoodName();
		float goodPrice = order.getGoodPrice();
		int goodNum = order.getGoodNum();
		String submitTime = order.getSubmitTime();
		String userName = order.getUserName();
//		System.out.println(goodName + ":" + goodPrice + ":" + goodNum);
//		System.out.println(userName + "的订单提交时间:" + submitTime);
		// 存储插入的记录数
		int n = -1;
		try {
			// 拼接插入图书的SQL语句
			String sql = "insert into [Order](good_name,good_price,good_num,submit_time,user_name) values('" + goodName
					+ "', " + goodPrice + ", " + goodNum + ", '" + submitTime + "', '" + userName + "')";
			// 将图书信息插入图书表中
			n = dbUtil.addDataToTable(sql);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return n > 0 ? true : false;

	}

2、查看历史订单

Servlet中的关键代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取PrintWriter对象
		PrintWriter writer = response.getWriter();
		// 检测用户是否已经登录
		ServletContext context = getServletContext();
		Object user = context.getAttribute("user");
		String logIn = (String) context.getAttribute("admin");
		if (null == user) {// 还未登录
			writer.write("您还未登录,<a href='login.html'>点此重新登录</a>");
		} else if (logIn == null) {
			writer.write("请选择登录身份!<a href='login.html'>点此重新登录</a>");
		} else {
			// 获取图书信息
			OrderService orderService = new OrderService();
			List<Order> orders = orderService.getOrders("select * from [Order] where user_name='" + user + "'");
			if (orders == null || orders.size() == 0) {
				writer.write("历史订单为空,<a href='book.action'>返回商品页逛逛吧</a>");
			} else {
				// 以表格形式将订单信息返回给客户端浏览器
				writer.write("<table border='1'>");
				writer.write("<tr align='center'>");
				writer.write("<th width='100'>订单编号</th>");
				writer.write("<th width='150'>商品名称</th>");
				writer.write("<th width='100'>商品价格</th>");
				writer.write("<th width='100'>商品数量</th>");
				writer.write("<th width='150'>结账时间</th>");
				writer.write("</tr>");
				for (Order order : orders) {
					writer.write("<tr align='center'>");
					// 打印订单编号
					writer.write("<td>");
					writer.write(new String((order.getOrderId() + "").getBytes(), "utf-8"));
					writer.write("</td>");
					// 打印商品名称
					writer.write("<td>");
					writer.write(order.getGoodName());
					writer.write("</td>");
					// 打印商品价格
					writer.write("<td>");
					writer.write(order.getGoodPrice() + "");
					writer.write("</td>");
					// 打印商品数量
					writer.write("<td>");
					writer.write(new String((order.getGoodNum() + "").getBytes(), "utf-8"));
					writer.write("</td>");
					// 打印结账时间
					writer.write("<td>");
					writer.write(order.getSubmitTime());
					writer.write("</td>");
					writer.write("</tr>");
				}
				writer.write("</table>");
				writer.write("<br/><br/>");
				writer.write("<a href='book.action'>点击返回商品页</a>");
			}
		}
	}

OrderService中相关功能的代码:

/**
	 * 获取某用户的历史订单
	 * 
	 * @param sql语句
	 * @return 订单集合
	 */
	public List<Order> getOrders(String sql) {
		try {
			ResultSet rs = dbUtil.queryDate(sql);
			while (rs.next()) {
				int orderId = rs.getInt("order_id");
				String goodName = rs.getString("good_name");
				float goodPrice = rs.getFloat("good_price");
				int goodNum = rs.getInt("good_num");
				String submitTime = rs.getString("submit_time");
				String userName = rs.getString("user_name");
				Order order = createOrder(orderId, goodName, goodPrice, goodNum, submitTime, userName);
				orders.add(order);
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		return orders;
	}

点击查看图书管理系统需要实现的功能、相关功能的描述以及用户的登录和注册功能

点击查看 图书列表页的显示功能以及管理员对图书的添加、修改、删除功能相关的关键代码

点击查看图书列表界面访问人数统计、字符过滤、数据库单例模式实现的相关代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FF小迷糊吖~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值