castclass.asp
<%
Class ShopBag
'放商品的空間
Private ProSpace
'商品信息個數(shù)
Private ItemsCout
'=============================================================
' 共有的
'=============================================================
'方法名:PutAPro
'參數(shù):一個存有商品信息的數(shù)組--ProData
'作用:將一個商品放入購物車
'返回值:
'成功放入商品,返回true
'商品存在,返回false
Public Function PutAPro(ByRef ProData)
'size:'最后一個
dim size,result,flag
ItemsCout = ubound(ProData) '商品信息個數(shù)
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
'----------------------------------------------
'更新產(chǎn)品個數(shù)
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 "更新了所有商品個數(shù)"
End Function
'刪除一個商品
Public Function DeleteAPro(id)
MyPrint "準備刪除一個商品"
dim count
GetPro
count = UBound(ProSpace)
if count=0 then
MyPrint "商品已經(jīng)是最后一個了, 將session設(shè)為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
'==================================================================
'=================================================================
' 私有的
'=================================================================
'檢測商品是否已經(jīng)存在
'存 在 返回 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
'重構(gòu)空間大小返回 true
Private Function CreateSpace()
dim result
MyPrint "開始開辟空間...<br>"
'ReSize: 空間大小
dim ReSize
'計算需要空間大小
if isarray(ProSpace) then '已經(jīng)有商品了
MyPrint "已有空間,需要重構(gòu)空間大小!<br>"
ReSize = UBound(ProSpace)+1
MyRedim ProSpace,ReSize
result = true
MyPrint "重構(gòu)了空間大小=+1...<br>"
else
redim ProSpace(0,ItemsCout)
result = false
MyPrint "沒有空間,開辟!...<br>"
End if
CreateSpace = result
End Function
'----------------------------------------------
'重新構(gòu)造一個數(shù)組
private Function MyRedim(byRef aArray,ByVal Size)
MyPrint "開始重構(gòu)空間大小...<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 str
End 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 "商品已經(jīng)存在<br>"
end if
Set 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>"
next
else
Response.write "沒有商品"
End If
%>