关看标题,可能大家不是很明白说的是什么意思,不如先看看展示图。
这是在上面看到的一个加入购物车功能:
当我们点击加入购物车后:
然后查看购物车信息:
最后的效果就是购物车中的信息展示!!!
现在大家有没有明白我要说的是什么东东?如果还是不太明白,那么我就用最简单的语句描述一下:
就是说我们添加了购物车信息,那么我们要怎样把购物车中的信息向上面一样展示出来。
这里就会涉及到数据的封装了。
多的就不用说了,下面直接来做个项目,大家就知道啦...
=============================================================
项目结构:
运行结果;
看到这里,我想大家一定知道我要干嘛了吧....
/ShoppingCart/src/com/b510/Business.java
1 /** 2 * 3 */ 4 package com.b510; 5 6 import java.util.Date; 7 8 /** 9 * 商家实体类10 * 11 * @date 2012-12-2812 * @author hongten(hongtenzone@foxmail.com)13 * 14 */15 public class Business {16 17 /**18 * 商家编号19 */20 private Integer id;21 /**22 * 商家名称23 */24 private String name;25 /**26 * 创建时间27 */28 private Date createDate;29 30 public Integer getId() {31 return id;32 }33 34 public void setId(Integer id) {35 this.id = id;36 }37 38 public String getName() {39 return name;40 }41 42 public void setName(String name) {43 this.name = name;44 }45 46 public Date getCreateDate() {47 return createDate;48 }49 50 public void setCreateDate(Date createDate) {51 this.createDate = createDate;52 }53 54 }
/ShoppingCart/src/com/b510/CashTicket.java
1 /** 2 * 3 */ 4 package com.b510; 5 6 import java.util.Date; 7 8 /** 9 * 购物券实体类10 * 11 * @date 2012-12-2812 * @author hongten(hongtenzone@foxmail.com)13 * 14 */15 public class CashTicket {16 17 /**18 * 购物券编号19 */20 private Integer id;21 /**22 * 购物券标题23 */24 private String title;25 /**26 * 创建时间27 */28 private Date createDate;29 30 public Integer getId() {31 return id;32 }33 34 public void setId(Integer id) {35 this.id = id;36 }37 38 public String getTitle() {39 return title;40 }41 42 public void setTitle(String title) {43 this.title = title;44 }45 46 public Date getCreateDate() {47 return createDate;48 }49 50 public void setCreateDate(Date createDate) {51 this.createDate = createDate;52 }53 54 }
/ShoppingCart/src/com/b510/MyMap.java
1 /** 2 * 3 */ 4 package com.b510; 5 6 import java.util.ArrayList; 7 import java.util.Date; 8 import java.util.HashMap; 9 import java.util.Iterator; 10 import java.util.List; 11 import java.util.Map; 12 import java.util.Set; 13 14 /** 15 * 测试类 16 * 17 * @date 2012-12-28 18 * @author hongten(hongtenzone@foxmail.com) 19 * 20 */ 21 public class MyMap { 22 23 public static void main(String[] args) { 24 // 封装元数据 25 MaporgData = new HashMap (); 26 // 商家b1 27 Business b1 = new Business(); 28 b1.setId(1); 29 b1.setName("b1"); 30 b1.setCreateDate(new Date()); 31 32 // 商家b1的克隆(和商家b1是一样的,只不过名称不同罢了) 33 Business clone_b1 = new Business(); 34 clone_b1.setId(1); 35 clone_b1.setName("b1"); 36 clone_b1.setCreateDate(new Date()); 37 38 // 商家b2 39 Business b2 = new Business(); 40 b2.setId(2); 41 b2.setName("b2"); 42 b2.setCreateDate(new Date()); 43 44 // 45 // 购物券c1 46 CashTicket c1 = new CashTicket(); 47 c1.setId(1); 48 c1.setTitle("c1"); 49 c1.setCreateDate(new Date()); 50 // 购物券c2 51 CashTicket c2 = new CashTicket(); 52 c2.setId(2); 53 c2.setTitle("c2"); 54 c2.setCreateDate(new Date()); 55 // 购物券c3 56 CashTicket c3 = new CashTicket(); 57 c3.setId(3); 58 c3.setTitle("c3"); 59 c3.setCreateDate(new Date()); 60 61 // 62 // 下面的意思是构造元数据:也即商家b1(clone_b1)拥有购物券c1,c3,商家b2拥有购物券c2 63 // map 64 // map 65 // map 66 // 67 orgData.put(b1, c1); 68 orgData.put(b2, c2); 69 orgData.put(clone_b1, c3); 70 71 getMapInfo(getMap(orgData)); 72 } 73 74 /** 75 * 封装数据 76 * 77 * @param orgData 78 * 元数据 79 * @return 80 */ 81 public static Map > getMap( 82 Map orgData) { 83 Map > resultMap = new HashMap >(); 84 85 Set orgDataKeys = orgData.keySet(); 86 Iterator orgIterator = orgDataKeys.iterator(); 87 Business org_business = null; 88 while (orgIterator.hasNext()) { 89 // 获取元数据中的商家business 90 org_business = orgIterator.next(); 91 // 获取元数据中的商家business对应的购物券 92 CashTicket org_cashTicket = orgData.get(org_business); 93 // 94 95 // 现在加入一个business和一个cashTicket 96 Set keys = resultMap.keySet(); 97 // keySet()获得的是一个集合中所有的键对象 98 Iterator it = keys.iterator(); 99 Business key_business = null;100 boolean flag = false;101 while (it.hasNext()) {102 key_business = it.next();103 if (key_business.getId() == org_business.getId()) {104 flag = true;105 }106 }107 if (flag) {108 resultMap.get(key_business).add(org_cashTicket);109 } else {110 List value_cashTicket = new ArrayList ();111 value_cashTicket.add(org_cashTicket);112 resultMap.put(org_business, value_cashTicket);113 }114 }115 return resultMap;116 }117 118 /**119 * 打印出map中的信息120 * 121 * @param map122 */123 public static void getMapInfo(Map > map) {124 Set end_keys = map.keySet();125 // keySet()获得的是一个集合中所有的键对象126 Iterator it = end_keys.iterator();127 while (it.hasNext()) {128 Business key_business = it.next();129 130 List value_cashTicket = map.get(key_business);131 // 通过键来取得Map中的值132 System.out.println("商家:id : [" + key_business.getId() + "] name : "133 + key_business.getName() + "]");134 for (CashTicket ct : value_cashTicket) {135 System.out.println(" 现金券: id :[" + ct.getId() + "] title : ["136 + ct.getTitle() + "]");137 }138 System.out.println("==================================");139 }140 }141 }
I'm Hongten