HashTable+Cookie实现购物车
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Web;
5
using System.Runtime.Serialization;
6
using System.Runtime.Serialization.Formatters.Binary;
7
using System.IO;
8
using System.Collections;
9
10
namespace Gtide.ShoppingMall.BLL
11
{
12
public class ShoppingCart
13
{
14
private Hashtable items = new Hashtable();
15
16
private readonly string COOKIE_NAME = "shoppingcart";
17
/// <summary>
18
/// 往购物车加入一个新商品
19
/// </summary>
20
/// <param name="itemID">商品编号</param>
21
public void Add(int itemID)
22
{
23
Load();
24
25
if (items.ContainsKey(itemID))
26
{
27
items[itemID] = int.Parse(items[itemID].ToString()) + 1;
28
}
29
else
30
{
31
items.Add(itemID, 1);
32
}
33
34
Save();
35
}
36
37
/// <summary>
38
/// 从购物车移除一个商品
39
/// </summary>
40
/// <param name="itemID"></param>
41
public void Remove(int itemID)
42
{
43
Load();
44
45
if (items.ContainsKey(itemID))
46
{
47
items.Remove(itemID);
48
}
49
50
Save();
51
}
52
53
/// <summary>
54
/// 更新购物车指定商品数量
55
/// </summary>
56
/// <param name="itemID"></param>
57
/// <param name="itemCount"></param>
58
public void Update(int itemID, int itemCount)
59
{
60
Load();
61
62
if (items.ContainsKey(itemID))
63
{
64
if (itemCount < 1)
65
{
66
itemCount = 1;
67
}
68
69
items[itemID] = itemCount;
70
}
71
72
Save();
73
}
74
75
public void Update(Hashtable hashTable)
76
{
77
Load();
78
79
foreach (DictionaryEntry entry in hashTable)
80
{
81
int value = Convert.ToInt32(entry.Value);
82
if (items.ContainsKey(Convert.ToInt32(entry.Key)))
83
{
84
if (value < 1)
85
{
86
value = 1;
87
}
88
89
items[Convert.ToInt32(entry.Key)] = value;
90
}
91
}
92
93
Save();
94
}
95
96
/// <summary>
97
/// 保存购物车
98
/// </summary>
99
private void Save()
100
{
101
string val = null;
102
103
foreach (DictionaryEntry entry in items)
104
{
105
val += entry.Key.ToString() + "|" + entry.Value.ToString() + "@";
106
}
107
108
HttpCookie myCookie = new HttpCookie(COOKIE_NAME, val);
109
myCookie.Expires = DateTime.Now.AddDays(1);
110
111
HttpContext.Current.Response.Cookies.Add(myCookie);
112
}
113
114
/// <summary>
115
/// 加载购物车
116
/// </summary>
117
private void Load()
118
{
119
HttpCookie myCookie = HttpContext.Current.Request.Cookies[COOKIE_NAME];
120
121
if (myCookie != null)
122
{
123
string val = myCookie.Value;
124
125
string[] temp = val.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);
126
foreach (string s in temp)
127
{
128
string[] tmp = s.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
129
int key = int.Parse(tmp[0]);
130
int value = int.Parse(tmp[1]);
131
items.Add(key, value);
132
}
133
}
134
}
135
136
/// <summary>
137
/// 返回购物车列表
138
/// </summary>
139
/// <returns></returns>
140
public List<Gtide.ShoppingMall.Model.ShoppingCart> GetList()
141
{
142
List<Gtide.ShoppingMall.Model.ShoppingCart> list = new List<Gtide.ShoppingMall.Model.ShoppingCart>();
143
// 加载
144
Load();
145
146
// 商品数据层
147
Gtide.ShoppingMall.DAL.ShoppingCart dal = new Gtide.ShoppingMall.DAL.ShoppingCart();
148
149
foreach (DictionaryEntry entry in items)
150
{
151
Gtide.ShoppingMall.Model.ShoppingCart model = dal.GetList((int)entry.Key);
152
153
model.Quantity = (int)entry.Value;
154
155
list.Add(model);
156
}
157
158
return list;
159
}
160
161
162
清空购物车
172
}
173
}
174
175
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Web;5
using System.Runtime.Serialization;6
using System.Runtime.Serialization.Formatters.Binary;7
using System.IO;8
using System.Collections;9

10
namespace Gtide.ShoppingMall.BLL11
{12
public class ShoppingCart13
{14
private Hashtable items = new Hashtable();15

16
private readonly string COOKIE_NAME = "shoppingcart";17
/// <summary>18
/// 往购物车加入一个新商品19
/// </summary>20
/// <param name="itemID">商品编号</param>21
public void Add(int itemID)22
{23
Load();24

25
if (items.ContainsKey(itemID))26
{27
items[itemID] = int.Parse(items[itemID].ToString()) + 1;28
}29
else30
{31
items.Add(itemID, 1);32
}33

34
Save();35
}36

37
/// <summary>38
/// 从购物车移除一个商品39
/// </summary>40
/// <param name="itemID"></param>41
public void Remove(int itemID)42
{43
Load();44

45
if (items.ContainsKey(itemID))46
{47
items.Remove(itemID);48
}49

50
Save();51
}52

53
/// <summary>54
/// 更新购物车指定商品数量55
/// </summary>56
/// <param name="itemID"></param>57
/// <param name="itemCount"></param>58
public void Update(int itemID, int itemCount)59
{60
Load();61

62
if (items.ContainsKey(itemID))63
{64
if (itemCount < 1)65
{66
itemCount = 1;67
}68

69
items[itemID] = itemCount;70
}71

72
Save();73
}74

75
public void Update(Hashtable hashTable)76
{77
Load();78

79
foreach (DictionaryEntry entry in hashTable)80
{81
int value = Convert.ToInt32(entry.Value);82
if (items.ContainsKey(Convert.ToInt32(entry.Key)))83
{84
if (value < 1)85
{86
value = 1;87
}88

89
items[Convert.ToInt32(entry.Key)] = value;90
}91
}92

93
Save();94
}95

96
/// <summary>97
/// 保存购物车98
/// </summary>99
private void Save()100
{101
string val = null;102

103
foreach (DictionaryEntry entry in items)104
{105
val += entry.Key.ToString() + "|" + entry.Value.ToString() + "@";106
}107

108
HttpCookie myCookie = new HttpCookie(COOKIE_NAME, val);109
myCookie.Expires = DateTime.Now.AddDays(1);110

111
HttpContext.Current.Response.Cookies.Add(myCookie);112
}113

114
/// <summary>115
/// 加载购物车116
/// </summary>117
private void Load()118
{119
HttpCookie myCookie = HttpContext.Current.Request.Cookies[COOKIE_NAME];120

121
if (myCookie != null)122
{123
string val = myCookie.Value;124

125
string[] temp = val.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries);126
foreach (string s in temp)127
{128
string[] tmp = s.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);129
int key = int.Parse(tmp[0]);130
int value = int.Parse(tmp[1]);131
items.Add(key, value);132
}133
}134
}135

136
/// <summary>137
/// 返回购物车列表138
/// </summary>139
/// <returns></returns>140
public List<Gtide.ShoppingMall.Model.ShoppingCart> GetList()141
{142
List<Gtide.ShoppingMall.Model.ShoppingCart> list = new List<Gtide.ShoppingMall.Model.ShoppingCart>();143
// 加载144
Load();145

146
// 商品数据层147
Gtide.ShoppingMall.DAL.ShoppingCart dal = new Gtide.ShoppingMall.DAL.ShoppingCart();148

149
foreach (DictionaryEntry entry in items)150
{ 151
Gtide.ShoppingMall.Model.ShoppingCart model = dal.GetList((int)entry.Key);152

153
model.Quantity = (int)entry.Value;154

155
list.Add(model);156
}157

158
return list;159
}160

161

162
清空购物车172
}173
}174

175




浙公网安备 33010602011771号