Monday, March 26, 2012

Shopping basket - Holding multiple values about multiple items

Hi everyone,
I am having trouble getting my head around how to make a shopping cart in ASP.NET / VB.NET. The shopping cart must hold CourseID, VenueID, and Places required ( so i.e. 1, 2, 3) for each item. the cart can obviously hold multiple items.
I have been advised to use a user defined type with three properties, and a hash table or arraylist. Do I then need to use loads of session objects to make the shopping cart accessile across all the pages in the site?
Would someone mind providing me with some sample code to achieve this goal? Or if anyone knows a better way of achieving it?
Thankyou very much! All help appreciated.

Given a user type like this
public class Course
{
private int courseID;
private int venueID;
private int placesRequired;
public int CourseID
{
get { return courseID; }
set { courseID = value; }
}
// and properties for the other two fields
// plus probable a constructor or two might be nice
}
(although you might want to make this a structure...)
Then
public class ShoppingCart
{
private ArrayList items = new ArrayList();
public void AddItem( Course c )
{
items.Add( c );
}
public RemoveItem( int index )
{
items.RemoveAt( index );
}
public Course this[ int index ]
{
get { return (Course) items[index]; }
}
// and anything else your shopping cart needs
}
Now, in your Session object you simply need
Session["Cart"] = new ShoppingCart();
to initialise the one and only shopping cart for that user (my guess is each user only wants one cart).
To access the cart
ShoppingCart sc = (ShoppingCart) Session["Cart"];
if( sc != null )
{
// do something with the cart
}

I recommend using HashTable instead of ArrayList, would give you the chance to get any record by just giving its number in the HashTable, which will be the same in the object inside the hashtable.
public Hashtable _Items=new Hashtable();_Items .Add(ID,new Course(courseID,venueID,placesRequired);

Regards

Thankyou for the reply...
Would anyone be able to provide a VB version of DMW's code?
Thanks again
Check this:
PublicClass CoursePrivate courseIDAsIntegerPrivate venueIDAsIntegerPrivate placesRequiredAsIntegerPublicProperty CourseID()AsIntegerGetReturn courseIDEndGetSet courseID= valueEndSetEndPropertyEndClassPublicClass ShoppingCartPrivate itemsAs ArrayList=New ArrayListPublicSub AddItem(ByVal cAs Course) items.Add(c)EndSubPublicSubNew(ByVal indexAsInteger) items.RemoveAt(index)EndSubDefaultPublicReadOnlyProperty Blubber(ByVal indexAsInteger)As CourseGetReturnCType(items(index), Course)EndGetEndPropertyEnd Class

Regards
Thankyou!
What does this bit do?
DefaultPublicReadOnlyProperty Blubber(ByVal indexAsInteger)As Course
Get
ReturnCType(items(index), Course)
EndGet
EndProperty
End Class
Regards

Well I used the convertor to convert the code. I am a C# programmer.
But maybe as a thought, the convertor did add something to it, it might be something related to getting an item by index property.
Regards


There's not information in the post to recommend using a HashTable.
If retrieval by a known item (i.e. course id) is important and performed frequently, then use a HashTable.
If you won't search by a known item, then use an ArrayList as the insertions and removals are faster.

0 comments:

Post a Comment