asp.net2.0 电子商城(购物车)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Ahcom.Member
{
/// <summary>
/// 商品信息类
/// </summary>
public class ShoppingItem
{
#region 成员变量、构造方法
/// <summary>
/// 商品的编号
/// </summary>
public readonly int ID;
/// <summary>
/// 商品的名称
/// </summary>
public string Name;
/// <summary>
/// 商品的分类编号
/// </summary>
public int CateID;
/// <summary>
/// 商品的价格
/// </summary>
public double Price;
/// <summary>
/// 商品的图片地址
/// </summary>
public string Photo;
/// <summary>
/// 商品的总计数量
/// </summary>
public int Total;
public ShoppingItem(int id)
{
this.ID = id;
this.Total = 1;
}
#endregion
#region Equals、GetHashCode 方法
/// <summary>
/// 已重载。比较两个对像是否相等(商品是否相同)
/// </summary>
/// <param name="obj">准备比较的 Object</param>
/// <returns>比较两个对像相等返回 true 否则返回 false</returns>
public override bool Equals(object obj)
{
if (obj is ShoppingItem)
return ((ShoppingItem)obj).ID == this.ID;
else
return false;
}
/// <summary>
/// 已重载。获取当前实例的哈希代码
/// </summary>
/// <returns>当前实例的哈希代码</returns>
public override int GetHashCode()
{
return this.ID;
}
#endregion
#region FromDatabase 静态方法。从数据库中加载商品信息
/// <summary>
/// 从数据库中加载商品信息
/// </summary>
/// <param name="id">商品的编号</param>
/// <returns>加载成功返回 Shopping 实例,否则返回 null</returns>
public static ShoppingItem FromDatabase(int id)
{
Ahcom.Data.IDb db = Ahcom.Data.DbInstance.Create();
string selectCommand = "SELECT [Name], [CateID], [Price], [Photo] FROM [GBC_ProductInfo] WHERE [ID] = " + db.Format(id);
IDataReader dr = db.GetDataReader(selectCommand);
ShoppingItem s = new ShoppingItem(id);
try
{
if (dr != null && dr.Read())
{
s.Name = Convert.ToString(dr["Name"]);
s.CateID = Convert.ToInt32(dr["CateID"]);
s.Price = Convert.ToDouble(dr["Price"]);
s.Photo = Convert.ToString(dr["Photo"]);
return s;
}
return null;
}
catch
{
return null;
}
finally
{
if (dr != null) dr.Close();
}
}
#endregion
}
/// <summary>
/// 购物车类(无法实例化此类,请调用 FromSession 方法初始化实例)。注意:线程间没有同步,不能保证线程安全
/// </summary>
public class ShoppingVehicle
{
#region 成员变量、构造方法、访问器
System.Collections.ArrayList al;
/// <summary>
/// 初始化 ShoppingVehicle 实例
/// </summary>
private ShoppingVehicle()
{
al = new System.Collections.ArrayList();
}
private ShoppingVehicle(System.Collections.ArrayList al)
{
this.al = al;
}
/// <summary>
/// 通过索引获取购物车中的商品 ShoppingItem 实例
/// </summary>
/// <param name="index">从零开始的索引</param>
/// <returns>ShoppingItem 实例</returns>
public ShoppingItem this[int index]
{
get { return (ShoppingItem)this.al[index]; }
}
#endregion
#region Add、Remove 方法。向购物车中添加和删除商品
/// <summary>
/// 向购物车里添加指定的商品
/// </summary>
/// <param name="shopping">ShoppingItem 实例</param>
/// <returns></returns>
public int Add(ShoppingItem shopping)
{
if (shopping == null) return -1;
int index = al.IndexOf(shopping);
if (index == -1)
{
int i = this.al.Add(shopping);
return i;
}
else
{
((ShoppingItem)this.al[index]).Total ++;
return index;
}
}
public ShoppingItem Update(int id, int total)
{
if (id < 1) return null;
for (int index = 0; index < this.al.Count; index++)
{
ShoppingItem si = (ShoppingItem)this.al[index];
if (si.ID == id)
{
si.Total = total;
return si;
}
}
ShoppingItem shopitem = ShoppingItem.FromDatabase(id);
if (shopitem != null)
{
shopitem.Total = total;
this.al.Add(shopitem);
}
return shopitem;
}
/// <summary>
/// 向购物车里添加指定编号的商品。如果购物车中没有此编号的记录,则从数据库中加载该商品记录
/// </summary>
/// <param name="id">商品的编号</param>
/// <returns>添加商品的详细信息(ShoppingItem 实例)</returns>
public ShoppingItem Add(int id)
{
if (id < 1) return null;
for (int index = 0; index < this.al.Count; index++)
{
ShoppingItem si = (ShoppingItem)this.al[index];
if (si.ID == id)
{
si.Total++;
return si;
}
}
ShoppingItem shopitem = ShoppingItem.FromDatabase(id);
if(shopitem != null) this.al.Add(shopitem);
return shopitem;
}
/// <summary>
/// 从购物车中删除指定商品
/// </summary>
/// <param name="shopping">ShoppingItem 实例</param>
public bool Remove(ShoppingItem shopping)
{
if (shopping == null) return false;
int index = al.IndexOf(shopping);
if (index != -1)
{
this.al.RemoveAt(index);
return true;
}
return false;
}
/// <summary>
/// 从购物车中删除指定编号商品
/// </summary>
/// <param name="id">商品的编号</param>
/// <returns>ShoppingItem 实例</returns>
public bool Remove(int id)
{
for (int index = 0; index < this.al.Count; index++)
{
ShoppingItem si = (ShoppingItem)this.al[index];
if (si.ID == id)
{
this.al.RemoveAt(index);
return true;
}
}
return false;
}
#endregion
#region Count 访问器。购物车中的商品总数
/// <summary>
/// 购物车中的商品总数
/// </summary>
public int Count
{
get { return this.al.Count; }
}
#endregion
#region ToDataTable 方法。将购物车中的信息转换成 DataTable 实例
/// <summary>
/// 将购物车中的信息转换成 DataTable 实例
/// </summary>
/// <returns>DataTable 实例</returns>
public DataTable ToDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("CateID", typeof(int)));
dt.Columns.Add(new DataColumn("Price", typeof(double)));
dt.Columns.Add(new DataColumn("Photo", typeof(string)));
dt.Columns.Add(new DataColumn("Total1", typeof(int)));
dt.Columns.Add(new DataColumn("Total2", typeof(double)));
foreach (ShoppingItem i in this.al)
{
dt.Rows.Add(i.ID, i.Name, i.CateID, i.Price, i.Photo, i.Total, i.Price * i.Total);
}
return dt;
}
#endregion
#region Balance 方法 +2 次重载。总计购物车中的商品
/// <summary>
/// 总计购物车中的商品
/// </summary>
/// <param name="sum">购物车中商品总数量</param>
/// <param name="totalize">购物车中商品汇总价格</param>
/// <returns>商品分类总数</returns>
public int Balance(out int sum, out double totalize)
{
int count = this.al.Count;
sum = new int();
totalize = new double();
for (int index = 0; index < count; index++)
{
ShoppingItem item = (ShoppingItem)this.al[index];
sum += item.Total;
totalize += item.Total * item.Price;
}
return count;
}
/// <summary>
/// 总计购物车中的商品
/// </summary>
/// <param name="names">所有在购物车中的分类商品名称(输出参数)</param>
/// <param name="amounts">所有在购物车中的分类商品总数</param>
/// <param name="totals">所有在购物车中的分类商品总计价格</param>
/// <returns>购物车中有商品返回 true 否则返回 false</returns>
public bool Balance(out string[] names, out int[] amounts, out double[] totals)
{
int sum;
double totalize;
return Balance(out names, out amounts, out totals, out sum, out totalize);
}
/// <summary>
/// 总计购物车中的商品
/// </summary>
/// <param name="names">所有在购物车中的分类商品名称(输出参数)</param>
/// <param name="amounts">所有在购物车中的分类商品总数</param>
/// <param name="totals">所有在购物车中的分类商品总计价格</param>
/// <param name="sum">购物车中商品总数量</param>
/// <param name="totalize">购物车中商品汇总价格</param>
/// <returns>购物车中有商品返回 true 否则返回 false</returns>
public bool Balance(out string[] names, out int[] amounts, out double[] totals, out int sum, out double totalize)
{
int count = this.al.Count;
names = new string[count];
amounts = new int[count];
totals = new double[count];
sum = new int();
totalize = new double();
for (int index = 0; index < count; index++)
{
ShoppingItem item = (ShoppingItem) this.al[index];
names[index] = item.Name;
amounts[index] = item.Total;
totals[index] = item.Total * item.Price;
sum += amounts[index];
totalize += totals[index];
}
return count > 0;
}
#endregion
#region FromSession 静态方法。从 Session 集合中加载 ShoppingVehicle 实例
/// <summary>
/// 从 Session 集合中加载 ShoppingVehicle 实例
/// </summary>
/// <returns>ShoppingVehicle 实例</returns>
public static ShoppingVehicle FromSession()
{
object obj = System.Web.HttpContext.Current.Session["__ShoppingVehicle"];
if (obj == null || !(obj is ShoppingVehicle))
{
ShoppingVehicle o = new ShoppingVehicle();
System.Web.HttpContext.Current.Session["__ShoppingVehicle"] = o;
return o;
}
else
{
return (ShoppingVehicle)obj;
}
}
#endregion
#region FromCookie 方法。从 Cookies 集合中加载 ShoppingVehicle 实例
/// <summary>
/// 从 Cookies 集合中加载 ShoppingVehicle 实例
/// </summary>
/// <returns>ShoppingVehicle 实例</returns>
public static ShoppingVehicle FromCookie()
{
throw new Exception("此方法尚没有实现");
}
#endregion
}
}