[转]用 Jsp 的 Session 机制编写的购物车程序
//写一个Goods类,并定义商品的各个属性,返回商品属性的方法,以及商品对象进行比较的方法
//Goods.java
public class Goods implements Comparable { // 初始化各成员变量 private String Id = null;//商品的编号Id private String name = null;//商品的名称name private float price = 0.00F;//商品的价格price private int number = 0;//商品的数量number public Goods(String Id, String name, float price, int number) { } public String getId() //返回订购商品的编号Id { public String getName() //返回订购商品的名称name { public float getPrice() //返回订购商品的价格price { public int getNumber() //返回订购商品的数量number { public int compareTo(Object m) { Goods n = (Goods) m; } } |
二、购物车
//首先建立Goods(商品)对象goods,并建立建立ArrayList对象ay
//通过ArrayList对象的方法add()将商品对象添加到ArrayList对象ay中
//由于ArrayList对象是具有添加和删除成员的方法,从而实现多个商品存储管理于ArrayList对象
//将ArrayList对象ay存储于session对象当中,实现购物车功能
//shopcar.jsp
request.setCharacterEncoding("GBK"); String id = request.getParameter("id"); //建立商品对象和ArrayList对象 Goods goods = new Goods(id,name,price,number); if((ArrayList)session.getAttribute("car")==null) else if(ay.isEmpty()) else { if(shop.compareTo(goods)==0) else |
三、删除商品
//对购物车中的商品进行删除操作
//removeGoods.jsp
request.setCharacterEncoding("gb2313"); String id = request.getParameter("id"); Goods goods = new Goods(id,name,price,number); ArrayList ay = (ArrayList)session.getAttribute("car"); for(int i = ay.size();it.hasNext();i--) if(shop.compareTo(goods)==0) if(ay.isEmpty()) else |
最新文章
- Ubuntu14.04或16.04下Hadoop及Spark的开发配置
- LINUX中如何查看某个进程打开的网络链接有多少
- C,C++经典笔试题(答案)转自:http://blog.163.com/jianhuali0118@126/blog/static/377499702008230104125229/
- sql基础知识(新手必备)
- LaTeX测试
- SQL获取变量类型以及变量最大长度
- JS的事件监听机制
- Google谷歌推出goo.gl缩短网址服务 - Blog透视镜
- sqlserver2008 中使用MSXML2.ServerXMLHttp拼装soap调用webservice
- MySQL与逻辑模块
- 排序算法总结及Java实现
- java 线程一
- 升级Mac OS X上的git
- python之魔法方法介绍
- php file_get_contents读取大容量文件方法
- Ubuntu下面 PHPSTORM2017.2破解方法
- 【Java面试题】34 List 、Map、Set 区别?
- SVN教程 -- 基于自己学习记录
- Dota2APP--第二天
- (转)Apache和Nginx运行原理解析