一、构建的商品类

//写一个Goods类,并定义商品的各个属性,返回商品属性的方法,以及商品对象进行比较的方法
//Goods.java

复制内容到剪贴板

代码:

package sobook.shopcar;
public class Goods implements Comparable{
//初始化各成员变量
     private String Id = null;
     private String name = null;
     private float price = 0.00F;
     private int number = 0;
     private String percent = null;
    
     public Goods(String Id,String name,float price,int number,String percent){
         this.Id = Id;
         this.name = name;
         this.price = price;
         this.number = number;
         this.percent = percent;
     }
     public String getId()       //返回订购商品的编号Id
     {
         return this.Id;
     }
     public String getName()    //返回订购商品的名称name
     {
         return this.name;
     }
     public float getPrice()       //返回订购商品的价格price
     {
         return this.price;    
     }
     public int getNumber()        //返回订购商品的数量number
     {
         return this.number;    
     }
     public String getPercent()    //返回订购商品的优惠百分比
     {
         return this.percent;
     }
     public int compareTo(Object m) //返回订购商品比较的结果
     {
         Goods n = (Goods)m;
         int comRs = Id.compareTo(n.Id);
         return comRs;
     }    
}

二、购物车

//首先建立Goods(商品)对象goods,并建立建立ArrayList对象ay
//通过ArrayList对象的方法add()将商品对象添加到ArrayList对象ay中
//由于ArrayList对象是具有添加和删除成员的方法,从而实现多个商品存储管理于ArrayList对象
//将ArrayList对象ay存储于session对象当中,实现购物车功能
//shopcar.jsp

复制内容到剪贴板

代码:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,sobook.shopcar.*,java.util.*" %>
//this code by hongseheike
<%
//设置编码格式
request.setCharacterEncoding("gb2313");
//获取参数信息
String id = request.getParameter("id");
String name = request.getParameter("name");
int number = java.lang.Integer.parseInt(request.getParameter("number"));
float price= java.lang.Float.parseFloat(request.getParameter("price"));
String percent = request.getParameter("percent");
//建立商品对象和ArrayList对象
Goods goods = new Goods(id,name,price,number,percent);
ArrayList ay = null;
//如果session中从未写入过,则将建立的商品对象添加到ArrayList对象当中,并写入 session
if((ArrayList)session.getAttribute("car")==null)
{
ay = new ArrayList();
     ay.add(goods);
     session.setAttribute("car",ay);
     response.sendRedirect("order_index.jsp");
}
//如果写如过,则将商品对象添加到ArrayList对象当中,并写入 session
else
{
ay=(ArrayList)session.getAttribute("car");
     //如果ArrayList 对象为空,则直接添加到ArrayList对象当中
if(ay.isEmpty())
{
         ay.add(goods);
         session.setAttribute("car",ay);
         response.sendRedirect("order_index.jsp");
     }
     //如果ArrayList 对象不为空,则判断购入商品是否已经存在于车中
     else
{
         Iterator it = ay.iterator();
         for(int i = 0;i<ay.size();i++) //下面还有另一种遍历方法
         {
             Goods shop = (Goods)it.next();
//如果购入商品已经存在,则打印输入提示信息
             if(shop.compareTo(goods)==0)
{
             out.println("<script>alert('你已经订购了此图书!');window.close();</script>");
             }
//如果购入商品不存在,则直接将商品添加到ArrayList对象当中,并写入 session
             else
             {
             ay.add(goods);
             session.setAttribute("car",ay);
             response.sendRedirect("order_index.jsp");
             }
         }
     }
}                
%>

三、删除商品

//对购物车中的商品进行删除操作
//removeGoods.jsp

复制内容到剪贴板

代码:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,sobook.shopcar.*,java.util.*" %>
<%
//设置编码格式
request.setCharacterEncoding("gb2313");
//获取参数信息
String id = request.getParameter("id");
String name = request.getParameter("name");
float price = java.lang.Float.parseFloat(request.getParameter("price"));
int number = java.lang.Integer.parseInt(request.getParameter("number"));
String percent = request.getParameter("percent");
//创建符合条件参数要删除的商品对象
Goods goods = new Goods(id,name,price,number,percent);
//获取session 中存储的ArrayList对象
ArrayList ay = (ArrayList)session.getAttribute("car");
Iterator it = ay.iterator();
//遍历ArrayList对象,并将ArrayList对象中的元素和创建的符合参数条件要删除的商品进行比较
for(int i = ay.size();it.hasNext();i--)
{
     Goods shop = (Goods)it.next();
    
//查询是否有ArrayList对象中的元素与要删除的商品相同
     if(shop.compareTo(goods)==0)
{
         int index = ay.indexOf(shop);    
        
//如果ArrayList对象已经为空,则跳转
         if(ay.isEmpty())
{
             response.sendRedirect("order_index.jsp");
         }
        
//如果ArrayList对象不为空,则从其中移去要与要删除的商品条件相符的元素,并重新写session
         else
{
             ay.remove(index);
             session.setAttribute("car",ay);
             response.sendRedirect("order_index.jsp");
         }
     }
     else
{
         out.print("程序异常");
     }
}
%>

Session ArrayList 实现购物车程序相关推荐

  1. Servlet之session实践:session实现简易购物车

    文章目录 Servlet之session实践:session实现简易购物车 1 过程分析 1.1 Product类:存储商品信息 1.2 ShoppingItem类:存储购买条目信息 1.3 Shop ...

  2. python购物车程序2019_Python实现购物车程序

    本文实例为大家分享了程序:Python购物车程序,具体内容如下 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就 ...

  3. 基于cookies的小型购物车程序

    基于cookies的小型购物车程序 最近因为公司项目开发,需要一个购物车程序. 虽然网上很多,但个人很久前就想写个.于是马上动手. 代码有点多,感觉方法不怎么好! 随便post上来,高手可略过~! m ...

  4. python 购物车分析_python 简易购物车程序解析

    购物车程序需求:用户信息为: {'wyf':{'password': '12', 'role': '2', 'money': '100000', 'carts': ['iphone6', 'mi5'] ...

  5. python购物车程序2019_Python——购物车程序(列表的应用)

    购物车基本功能: 1.展示购物清单.价格信息. 2.由客户选择商品序号.加入购物车. 3.结束购物后,展示购物车中的商品信息,余额信息. 其他规则:所选商品价格超出当前余额时,提示钱不够: 直到客户输 ...

  6. 运用循环判断语句和列表的购物车程序

    针对循环判读语句和列表的运用练习,对应Day2中的第一个购物车程序训练. 能力有限,可能存在不足. 1 # Author: JC 2 3 while 1: 4 balance = input(&quo ...

  7. (转)Django ==== 实战学习篇九 在session中保存购物车

    在session中保存购物车: 之前实现了产品目录的界面,用户看到合适的产品,就会加入购物车,对于购物车界面,需要考虑一下几点: 购物车应该显示:物品的清单,列出买方选中的产品: 购物车的清单有没有必 ...

  8. Python自学----- while if 列表结合,购物车程序

    # -*- coding: UTF-8 -*- #!/usr/bin/env python#购物车程序salary = int(input('请输入的工资:')) #输入工资! y ='y' #定义退 ...

  9. python 购物车程序_Python 购物车程序(文件版)

    ''' 购物车程序 用户入口: 1.商品信息存在文件里 2.已购商品,余额记录 商家入口: 1.可以添加商品,修改商品价格 ''' filePath = "D:\Python_work\Lp ...

  10. python 购物车程序_购物车程序python

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 第3点要求: 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒在循环外层 ...

最新文章

  1. 综合素质计算机能力,教资统考中学《综合素质》:信息处理能力(一)
  2. CALayer 了解与使用
  3. 实验6Linux系统开发环境,哈工大嵌入式操作系统课件—6 Linux开发环境1-linux基.ppt...
  4. 【内核驱动】 Kconfig简介
  5. 算法系列教程04 - 算法相关的基础概念
  6. 使用MyBatista----上传图像
  7. 解决SerMyAdmin无法登陆的问题
  8. 【spring学习】02
  9. echarts legend属性_Vue 项目如何使用Echarts , 手摸手带你入门
  10. Qt5.12.9 搜狗输入法不能使用,配置解决方案
  11. 【后缀数组】【线段树】codeforces102028H Can You Solve the Harder Problem?
  12. 安徽身份证网上办理最全攻略
  13. linux 中 lrwxrwxrwx是什么意思?
  14. [jzoj 1285] 奶酪厂 {贪心}
  15. 网工知识角|例行维护检查华为设备,这10个常用命令你必须掌握
  16. 任务栏信息栏中图标闪动
  17. php转mp3的工厂,魔影工厂怎么转成mp3 魔影工厂转换mp3教程
  18. java接口可以写方法体吗_JDK1.8接口中可以写方法体
  19. Android 2D游戏引擎1
  20. 微信小程序直播怎么开通

热门文章

  1. java 只保留字母_java编程问题,急急急!输入一个字符串,如果字符串中存在字母a的次数大于1,则只保留第一个a,...
  2. 如何用阿里云服务器建站-全流程
  3. 微信小程序 简单动画入门
  4. 如何快速找回丢失的数据?
  5. 图神经网络-论文精读-“A Gentle Introduction to Graph Neural Networks“
  6. oracle数据库还原指令,oracle中备份和还原数据库的命令汇总
  7. 使用c语言编写词法分析程序,用C语言编写一个简单的词法分析程序
  8. 怎么授权接入芝麻小程序客服系统?
  9. maven插件安装与使用
  10. Multiple Hypothesis Tracking Revisited