Saturday, March 24, 2012

Shopping cart class

Hello,

I'm trying to adapt the sample code below that creates a shopping cart class with cart items.

I would like to be able to allow an administrator to add optional items on the fly in an admin area.

So for instance the administrator has a new product, and he decided he wishes to add a size option with has 3 sizes, small, medium and large. Later on he decides he wishes to add another option that he calls 'direction' with the choices 'left' and 'right'.

I presume the way to do it might be using an array or some kind of collection?

Any advice would be greatly appreciated.

Below is the code:

=========================

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,

Michael,

There's a few ways you could to this. In order to make it as flexible as possible, you could create an attributes property for each product. For example, a shirt might have in its attributeoptions property "Size" and "Color". Each attribute key would have n attribute options. Size:S,M,L Color: Blue, White.


Hi Mike,

So would the class CartItem have an attributes property that might be an array? that holds all the attributes such as size and color etc.

How would the attribute options work? and how would the database fit in?

Thanks again,

Michael.

0 comments:

Post a Comment