Modifying Shopping Cart Class
Below is the current database structure.
The OptionGroup holds the product attribute, e.g. Colour related to the product
Every product has it's own OptionGroup, as every product is unique.
The GroupOptions holds the attributes, e.g. Red, Blue, Green
Products
--------
ProductID (PK)
ProductCode
Name
Description
Price
PictureURL
ThumbURL
CatID
Featured
Active
OptionGroup
-----------------
OptionGroupID (PK)
OptionGroupName
ProductID (FK)
GroupOptions
------------
GroupOptionID (PK)
OptionGroupValues
OptionGroupID (FK)
How can I edit the sample class code below to reflect my database structure above?
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Namespace Wrox.Commerce
<Serializable()> _
Public Class CartItem
Private _productID As Integer
Private _productName As String
Private _productImageUrl As String
Private _quantity As Integer
Private _price As Double
Private _lineTotal As Double
Public Sub New()
End Sub
Public Sub New(ByVal ProductID As Integer, ByVal ProductName As String, ByVal ProductImageUrl As String, ByVal Quantity As Integer, ByVal Price As Double)
_productID = ProductID
_productName = ProductName
_productImageUrl = ProductImageUrl
_quantity = Quantity
_price = Price
_lineTotal = Quantity * Price
End Sub
Public Property ProductID() As Integer
Get
Return _productID
End Get
Set(ByVal value As Integer)
_productID = value
End Set
End Property
Public Property ProductName() As String
Get
Return _productName
End Get
Set(ByVal value As String)
_productName = value
End Set
End Property
Public Property ProductImageUrl() As String
Get
Return _productImageUrl
End Get
Set(ByVal value As String)
_productImageUrl = value
End Set
End Property
Public Property Quantity() As Integer
Get
Return _quantity
End Get
Set(ByVal value As Integer)
_quantity = value
End Set
End Property
Public Property Price() As Double
Get
Return _price
End Get
Set(ByVal value As Double)
_price = value
End Set
End Property
Public ReadOnly Property LineTotal() As Double
Get
Return _quantity * _price
End Get
End Property
End Class
<Serializable()> _
Public Class WroxShoppingCart
Private _dateCreated As DateTime
Private _lastUpdate As DateTime
Private _items As List(Of CartItem)
Public Sub New()
_items = New List(Of CartItem)
_dateCreated = DateTime.Now
End Sub
Public Property Items() As List(Of CartItem)
Get
Return _items
End Get
Set(ByVal value As List(Of CartItem))
_items = value
End Set
End Property
Public Sub Insert(ByVal ProductID As Integer, ByVal Price As Double, ByVal Quantity As Integer, ByVal ProductName As String, ByVal ProductImageUrl As String)
Dim ItemIndex As Integer = ItemIndexOfID(ProductID)
If ItemIndex = -1 Then
Dim NewItem As New CartItem()
NewItem.ProductID = ProductID
NewItem.Quantity = Quantity
NewItem.Price = Price
NewItem.ProductName = ProductName
NewItem.ProductImageUrl = ProductImageUrl
_items.Add(NewItem)
Else
_items(ItemIndex).Quantity += 1
End If
_lastUpdate = DateTime.Now()
End Sub
Public Sub Update(ByVal RowID As Integer, ByVal ProductID As Integer, ByVal Quantity As Integer, ByVal Price As Double)
Dim Item As CartItem = _items(RowID)
Item.ProductID = ProductID
Item.Quantity = Quantity
Item.Price = Price
_lastUpdate = DateTime.Now()
End Sub
Private Function ItemIndexOfID(ByVal ProductID As Integer) As Integer
Dim index As Integer
For Each item As CartItem In _items
If item.ProductID = ProductID Then
Return index
End If
index += 1
Next
Return -1
End Function
Public Sub DeleteItem(ByVal rowID As Integer)
_items.RemoveAt(rowID)
_lastUpdate = DateTime.Now()
End Sub
Public ReadOnly Property Total() As Double
Get
Dim t As Double
If _items Is Nothing Then
Return 0
End If
For Each Item As CartItem In _items
t += Item.LineTotal
Next
Return t
End Get
End Property
End Class
End Namespace
Thanks.
|