Logic for Chapter 3 Coding

Product Class

Create a Product class object with all of the pertinent members for a Product instance.

CartItem Class

Create a CartItem class object with all of the pertinent members for an instance of a CartItem.

Order Page

Module-level

Declare SelectedProduct as an instance of the Product class

Order Page - Load Event

Bind the DropDownList control the first time the page loads.

Store the product to the SelectedProduct object by calling the GetSelectedProduct function.

Store the SelectedProduct member values to the Label controls on the page.

Store the image of the product to the ImageUrl property of the Image control on the page.

Order Page - Add Button Click Event

If the page is valid then

Create an instance of the CartItem class.

Store the SelectedProduct (module level object) to the Product member of the CartItem.

Update the Quantity of the CartItem by storing the quantity from the

TextBox to the Quantity property.

Call the AddToCart sub procedure - pass the CartItem to the sub procedure.

Response.redirect to the Cart.aspx page.

End if

Order Page - AddToCart sub procedure

Create an instance of a SortedList named Cart and store the current Cart contents to the sorted list

by calling the GetCart function (GetCart returns a SortedList)

Store the ProductID of the SelectedProduct (module level remember?) to a strProductID string variable.

'Check to see if the ProductID is already in the SortedList - if it is, create a CartItem from the existing

'CartItem in the SortedList and update the quantity to reflect the additional purchase

If Cart.ContainsKey(strProductID) Then 'This checks for the ProductID within the cart listing.

Convert the Cart to a CartItem with the CType function.

CartItem.Quantity += additional quantity in the TextBox

Else

'Add this new product to the SortedList cart with the Add method.

Cart.Add(strProductID, CartItem)

End If

Order Page - GetCart Function

Check for the existence of a Cart object in session state.

If Session("Cart") Is Nothing Then

'There is no cart, create and add a new one.

End If

Return the Cart as a SortedList to the calling procedure.

Cart Page

Module-Level

Declare an object named Cart as a SortedList object.

Cart Page - Continue Button. No code is required. Set PostBackURL for the button = ~/Order.aspx

Cart Page - Load Event

Call GetCart and store the returned value to the Cart SortedList object.

If this is not a PostBack, then call DisplayCart to display the cart contents to the web page.

DisplayCart

End If

Cart Page - GetCart Function. The code here is the same as it is for the Order page.

Check for the existence of a Cart object in session state.

If Session("Cart") Is Nothing Then

'There is no cart, create and add a new one.

End If

Return the Cart as a SortedList to the calling procedure.

Cart Page - DisplayCart sub procedure.

Clear the ListBox to ensure products aren't listed twice.

Declare a CartItem object as an instance of CartItem.

'Since each item in a SortedList is a DictionaryEntry object that includes a key and value, we'll

'use this with a For Each loop to display objects from the SortedList to the ListBox control

Declare a CartEntry object as a DictionaryEntry object

For Each CartEntry in Cart (remember Cart is the module level SortedList)

CartItem = CType(CartEntry.Value, CartItem)

lstCart.Items.Add(CartItem.Display)

Next

Cart Page - Remove Button Click Event

'If you remove a product from the customer cart, you must update both the SortedList

'and also update the ListBox display.

If lstCart.SelectedIndex > -1 And Cart.Count > 0 Then 'ensures an item is selected and can be removed

Cart.RemoveAt(lstCart.SelectedIndex) 'gives the number of the item to remove from the Cart

Me.DisplayCart 'updates ListBox display

End If

Cart Page - Empty Button Click Event

'If you empty the cart, you then simply also empty the ListBox

Cart.Clear() 'Clears the Cart SortedList

lstCart.Items.Clear() 'Clears the ListBox

lblMessage.Text = "" 'Clears feedback label.

Cart Page - CheckOut Button Click Event

This is your first graded lab assignment. Feature is not yet implemented.