java轻松实现购物车(HashMap技术实现购物车)

烟台网站建设中越来越多的朋友开始选择java技术实现自己的网站,oa,商城,做商城必须考虑的一个技术点就是购物车的实现,有人选择字符串截取方式,然后记录入session ,有人选择java的类  用对类的元素的增删读改实现购物车物品的维护,下面为大家带来一种不同的方式:hashmap购物车

购物车代码(Servlet处理段):

public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  request.setCharacterEncoding("UTF-8");
  //声明会话对象
  HttpSession session = request.getSession();
  
  //接收超链接传过来的参数
  String foodId = request.getParameter("foodId");
  //为了避免覆盖,先获得购物车对象
  HashMap cart = (HashMap)session.getAttribute("cart");
  
  //如果购物车不存在,则创建购物车
  if (cart == null) {
   cart = new HashMap();
   //将购物车存入会话中
   session.setAttribute("cart", cart);
  }
  
  //判断商品是否在购物车中
  if (cart.get(foodId) == null) {
   //如果不在,创建一个条目到购物车中
   cart.put(foodId, 1);
  } else {
   //如果在,更新其数量
   int num = Integer.parseInt(cart.get(foodId).toString())+1;
   cart.put(foodId, num);
  } 
  //在此将购物车保存到会话中
  session.setAttribute("cart", cart);
  
  //跳转页面
  response.sendRedirect("/RestaurantSys/shopCart.jsp");

  out.flush();
  out.close();
 }

记住:其中,foodId为超链接传过来的参数。

大家在参看购物车的jsp中,写如下代码:

  <%!double total = 0; %>
  <%
   HashMap cart = (HashMap)session.getAttribute("cart");
   Object[] isbn = cart.keySet().toArray();
   for (int i = 0; i < isbn.length; i++) {
    DBOperate dbo = new DBOperate();
    dbo.getCon();
    FoodBean fBean = (FoodBean)dbo.searchFoodInfo(isbn[i].toString());
    int num = Integer.parseInt(cart.get(isbn[i]).toString());
    double price = fBean.getFoodPrice();
    double sum = price * num;
    total += sum;
   %>
  <tr>
    <td><div align="center" class="STYLE1"><%=isbn[i] %></div></td>
    <td><div align="center" class="STYLE1"><%=fBean.getFoodName() %></div></td>
    <td><div align="center" class="STYLE1">¥<%=new DecimalFormat("0.00").format(price) %></div></td>
    <td><div align="center" class="STYLE1"><%=num %></div></td>
    <td><div align="center" class="STYLE1">¥<%=new DecimalFormat("0.00").format(sum) %></div></td>
  </tr>
  <%} %>

好啦,代码分享完毕,欢迎大家访问我的网站有技术问题  可以相互交流http://www.gongyingwl.com/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的购物车 Java 代码示例,使用 HttpSession 对象实现: ``` import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class ShoppingCartServlet extends HttpServlet { private Map<String, Integer> cart; public void init() { cart = new HashMap<>(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); // 尝试获取购物车内容 Map<String, Integer> cart = (Map<String, Integer>) session.getAttribute("cart"); if (cart == null) { // 如果购物车不存在,则创建一个新的购物车 cart = new HashMap<>(); session.setAttribute("cart", cart); } // 添加商品到购物车 String item = request.getParameter("item"); if (item != null) { Integer quantity = cart.get(item); if (quantity == null) { quantity = 0; } quantity++; cart.put(item, quantity); session.setAttribute("cart", cart); } // 显示购物车内容 response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Shopping Cart</title></head><body>"); out.println("<h1>Shopping Cart</h1>"); if (cart.isEmpty()) { out.println("<p>Your cart is empty.</p>"); } else { out.println("<ul>"); for (Map.Entry<String, Integer> entry : cart.entrySet()) { String name = entry.getKey(); int quantity = entry.getValue(); out.println("<li>" + name + " x " + quantity + "</li>"); } out.println("</ul>"); } out.println("<form method=\"get\">"); out.println("<p><input type=\"submit\" name=\"submit\" value=\"Continue shopping\"></p>"); out.println("</form>"); out.println("</body></html>"); } } ``` 在 doGet 方法中,首先尝试从 HttpSession 对象中获取购物车内容。如果购物车不存在,则创建一个新的 HashMap 对象,并将其添加到 HttpSession 中。然后,通过 request.getParameter 方法获取要添加到购物车中的商品名称,并将其添加到购物车中。最后,显示购物车内容并提供一个“继续购物”按钮。 需要注意的是,此代码示例仅用于演示目的,实际的购物车实现需要更多的细节,例如:处理多个用户的购物车、处理库存、处理价格、处理订单等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值