日期:2010-05-26 浏览次数:20481 次
using System;
using System.Web.UI;
using System.Collections; //使用Hashtable类必须引入该命名空间
namespace WendwCart //命名空间名称
{
[Serializable]
public class Stat_Class{ //定义商品类,保存商品的各种属性
String ShangPinID; //商品ID
String Sp_Name; //商品名称
decimal Sp_Price; //商品价格
int Sp_Quan; //商品数量
public String ItemID{
get{return ShangPinID;}
set{ShangPinID=value;}
}
public String ShangpinName{
get{return Sp_Name;}
set{Sp_Name=value;}
}
public decimal Price{
get{return Sp_Price;}
set{Sp_Price=value;}
}
public int Quantity{
get{return Sp_Quan;}
set{Sp_Quan=value;}
}
public Stat_Class(String ItemID,String ShangpinName,decimal Price,int Quantity){ //构造方法,初始化商品的各个属性
ShangPinID=ItemID;
Sp_Name=ShangpinName;
Sp_Price=Price;
Sp_Quan=Quantity;
}
}
[Serializable]
public class ShoppingCart{
Hashtable Cart_Orders=new Hashtable();
public ICollection Orders{
get{return Cart_Orders.Values;}
}
public decimal TotalCost{ //计算总价格
get{
decimal total=0;
foreach(DictionaryEntry entry in Cart_Orders){
Stat_Class order=(Stat_Class)entry.Value;
total+=(order.Price*order.Quantity);
}
return total;
}
}
public void AddItem(Stat_Class Order){ //添加物件方法
Stat_Class order=( Stat_Class)Cart_Orders[Order.ItemID];
if(order!=null)
order.Quantity+=Order.Quantity;
else
Cart_Orders.Add(Order.ItemID,Order);
}
public void DeleteItem (String ItemID){ //删除物件
if(Cart_Orders[ItemID]!=null)
Cart_Orders.Remove(ItemID);
}
}
}
<%@ Import Namespace="WendwCart" %>
<%@ Application Codebehind="Global.asax.cs" Inherits="HDLab.BBS.Global" %>
<script Language="C#" runat="server">
void Session_Start()
{
Session["MyShoppingCart"]=new ShoppingCart();
}
</script>