castclass.asp
<%Class ShopBag
'放商品的空间 Private ProSpace
'商品信息个数 Private ItemsCout
'=============================================================' 共有的'=============================================================
'方法名:PutAPro '参数:一个存有商品信息的数组--ProData '作用:将一个商品放入购物车 '返回值: '成功放入商品,返回true '商品存在,返回false Public Function PutAPro(ByRef ProData) 'size:'最后一个 dim size,result,flag ItemsCout = ubound(ProData) '商品信息个数 GetPro
if CreateSpace then '有商品需要检测商品是否存在 MyPrint "有商品需要检测商品是否存在" flag = CheckProExists(ProData(0)) else flag = false '购物车没有商品 MyPrint "购物车没有商品" end if
if not flag then
size = Ubound(ProSpace)
for i = 0 to ItemsCout ProSpace(size,i) = ProData(i) next MyPrint "放入一个商品....<br>" SavePro result = true else result = false end if
PutAPro = result End Function '----------------------------------------------
'得到所有商品 Public Function GetAllPro(ByRef ProList) ProList = Session("ProID") End Function '----------------------------------------------
'更新产品个数 Public Function UpdatePro(ByRef ProNumList)
dim ProNumData GetPro ProNumData = split(ProNumList,",")
for i =0 to Ubound(ProNumData) ProSpace(i,3) = Cint(trim(ProNumData(i))) next SavePro MyPrint "更新了所有商品个数" End Function
'删除一个商品 Public Function DeleteAPro(id) MyPrint "准备删除一个商品" dim count GetPro count = UBound(ProSpace) if count=0 then MyPrint "商品已经是最后一个了, 将session设为null" Session("ProID") = null else redim tempAr(count-1,3) MyPrint "开始查找要删除的商品id..." for i = 0 to count if Cint(ProSpace(i,0)) = Cint(id) then MyPrint "找到ID,删除!" for j=i to count-1 ProSpace(j,0) = ProSpace(j+1,0) ProSpace(j,1) = ProSpace(j+1,1) ProSpace(j,2) = ProSpace(j+1,2) ProSpace(j,3) = ProSpace(j+1,3) next
exit for end if next
for i = 0 to count-1 tempAr(i,0) = ProSpace(i,0) tempAr(i,1) = ProSpace(i,1) tempAr(i,2) = ProSpace(i,2) tempAr(i,3) = ProSpace(i,3) next Session("ProID") = tempAr end if End Function '--------------------------------------------- '得到商品的某项信息列表 , 号隔开 Public Function GetProList(n) dim result GetPro if isnull(ProSpace) then result = null else for i =0 to ubound(ProSpace) if i = 0 then result = ProSpace(i,n) else result =result&","&ProSpace(i,n) end if next end if KillMe GetProList = result End Function
'==================================================================
'=================================================================' 私有的'================================================================= '检测商品是否已经存在 '存 在 返回 true '不存在返回 false Private Function CheckProExists(ProID) MyPrint "检测商品是否存在?<br>" dim result result = false
for i = 0 to Ubound(ProSpace) if Cint(ProSpace(i,0)) = Cint(ProID) then result = true End if next CheckProExists = result End Function '------------------------------------------------
'作用: '开辟存放物品的空间 '返回值: '新开大小返回 false '重构空间大小返回 true Private Function CreateSpace() dim result MyPrint "开始开辟空间...<br>" 'ReSize: 空间大小 dim ReSize
'计算需要空间大小 if isarray(ProSpace) then '已经有商品了 MyPrint "已有空间,需要重构空间大小!<br>" ReSize = UBound(ProSpace)+1 MyRedim ProSpace,ReSize result = true MyPrint "重构了空间大小=+1...<br>" else redim ProSpace(0,ItemsCout) result = false MyPrint "没有空间,开辟!...<br>" End if CreateSpace = result End Function '---------------------------------------------- '重新构造一个数组 private Function MyRedim(byRef aArray,ByVal Size) MyPrint "开始重构空间大小...<br>" redim TmpArray(Size,ItemsCout) '备份信息 for i = 0 to Ubound(aArray) for j =0 to ItemsCout TmpArray(i,j) = aArray(i,j) next next redim aArray(Size,ItemsCout)
'还原信息 for j = 0 to ubound(TmpArray) for k =0 to ItemsCout aArray(j,k) = TmpArray(j,k) next next
End Function '-----------------------------------------------
'保存商品 Private Function SavePro Session("ProID") = ProSpace MyPrint "保存购物车....<br>" KillMe MyPrint "释放了空间..<br>" End Function '---------------------------------------------- '从Session中取出商品 Private Function GetPro ProSpace = Session("ProID") MyPrint "将Session中的商品放入了购物车....<br>" End Function '----------------------------------------------
'释放空间 private Function KillMe ProSpace = null End Function '----------------------------------------------End Class%>
使用:
<!--#include file="CastClass.asp"--><%
'显示操作过程 Function MyPrint(str) Response.write strEnd Function
dim aShowBag,ProData,ProList
ProData = Array(1,"ProName","ProPrice","ProNum")
set aShowBag = new ShopBag
state = aShowBag.PutAPro(ProData) '放入一个商品 ProData= null aShowBag.GetAllPro ProList
if state then Response.Write "放入一个商品<br>" else Response.Write "商品已经存在<br>" end ifSet ShowBag = nothing
if isarray(ProList) then for i = 0 to ubound(ProList) for j =0 to 3 Response.Write ProList(i,j)&"==" next Response.Write "<br>============<br>" nextelseResponse.write "没有商品"End If
%>
