Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
hi all,
 
i am a mca student and i was trying to prepare my collage project. to make my project more realistic i want to add a cart to my project. which i am not aware how to do. in order to get the ans to my question i gone through one article and as per the guidance i have a prepared a demo sales cart.
 
cart was properly functional. problem occurred when i uploaded my demo project on free hosting site. i found that cart is not empty. when i updated the information in the cart from computer A was also appearing in the computer B. i think same cart is used between all users.
 
below is what i have created, i want some to review this code. i know i am asking to much, but your help will definitely help in getting good grade.
 
first i created 3 classes: cartitem, product, shoppingcart. and code is as follows:
 
1) product class:
public class Product
{
    public int Id { get; set; }
    public decimal Price { get; set; }
    public string Description { get; set; }
 
    public Product(int id)
    {
        this.Id = id;
        switch (id) {
            case 1:
                this.Price = 19.95m;
                this.Description = "Shoes";
                break;
            case 2:
                this.Price = 9.95m;
                this.Description = "Shirt";
                break;
            case 3:
                this.Price = 14.95m;
                this.Description = "Pants";
                break;
        }
    }
2) Shopping cart
public class ShoppingCart {
	#region Properties
	
	public List<cartitem> Items { get; private set; }
	
	#endregion
 
	#region Singleton Implementation
 
	// Readonly properties can only be set in initialization or in a constructor
	public static readonly ShoppingCart Instance;
	// The static constructor is called as soon as the class is loaded into memory
	static ShoppingCart() {
		// If the cart is not in the session, create one and put it there
		// Otherwise, get it from the session
		if (HttpContext.Current.Session["ASPNETShoppingCart"] == null) {
			Instance = new ShoppingCart();
			Instance.Items = new List<cartitem>();
			HttpContext.Current.Session["ASPNETShoppingCart"] = Instance;
		} else {
			Instance = (ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCart"];
		}
	}
 
	// A protected constructor ensures that an object can't be created from outside
	protected ShoppingCart() { }
 
	#endregion
 
	#region Item Modification Methods
	/**
	 * AddItem() - Adds an item to the shopping 
	 */
	public void AddItem(int productId) {
		// Create a new item to add to the cart
		CartItem newItem = new CartItem(productId);
 
		// If this item already exists in our list of items, increase the quantity
		// Otherwise, add the new item to the list
		if (Items.Contains(newItem)) {
			foreach (CartItem item in Items) {
				if (item.Equals(newItem)) {
					item.Quantity++;
					return;
				}
			}
		} else {
			newItem.Quantity = 1;
			Items.Add(newItem);
		}
	}
3) cartitem class
public class CartItem : IEquatable<cartitem> {
	#region Properties
 
	// A place to store the quantity in the cart
	// This property has an implicit getter and setter.
	public int Quantity { get; set; }
 
	private int _productId;
	public int ProductId {
		get { return _productId; }
		set {
			// To ensure that the Prod object will be re-created
			_product = null;
			_productId = value;
		}
	}
 
	private Product _product = null;
	public Product Prod {
		get {
			// Lazy initialization - the object won't be created until it is needed
			if (_product == null) {
				_product = new Product(ProductId);
			}
			return _product;
		}
	}
 
	public string Description {
		get { return Prod.Description; }
	}
 
	public decimal UnitPrice {
		get { return Prod.Price; }
	}
 
	public decimal TotalPrice {
		get { return UnitPrice * Quantity; }
	}
 
	#endregion
 
	// CartItem constructor just needs a productId
	public CartItem(int productId) {
		this.ProductId = productId;
	}
 
	/**
	 * Equals() - Needed to implement the IEquatable interface
	 *    Tests whether or not this item is equal to the parameter
	 *    This method is called by the Contains() method in the List class
	 *    We used this Contains() method in the ShoppingCart AddItem() method
	 */
	public bool Equals(CartItem item) {
		return item.ProductId == this.ProductId;
	}
Note: I have also created the web page which have link button. code is as follows
protected void btnAddShoes_Click(object sender, EventArgs e) {
        // Add product 1 to the shopping cart
        ShoppingCart.Instance.AddItem(1);
 
        // Redirect the user to view their shopping cart
        Response.Redirect("ViewCart.aspx");
 
Can any one please review the code and advise what is wrong init
Posted 6-Jan-13 2:28am
Edited 6-Jan-13 2:59am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you use a Singleton instance of a shopping cart, then I am not surprised if it is shared between several computers.
I would instead save the Products to the Session (or to cookies) as this is unique to the connection / PC.
 
Cookies are more friendly for coustomers - If I loose the connection half way through I'd be annoyed if my shop-to-date vanished as well.
  Permalink  
Comments
jonlink01 at 6-Jan-13 10:19am
   
Hi Griff,
 
Thank you for coming forward and helping me again:-)
 
i have used the session in creating the cart in above demo project.
 
can you please share some online tutorial for creating a cart with the help of cookies
jonlink01 at 4-Feb-13 16:51pm
   
Hi Griff,
 
could you please help me in completing my project.
OriginalGriff at 5-Feb-13 3:32am
   
Cookies:
http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx
Should tell you what you need to know, and includes examples.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 231
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100