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
public static readonly ShoppingCart Instance;
static ShoppingCart() {
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"];
}
}
protected ShoppingCart() { }
#endregion
#region Item Modification Methods
public void AddItem(int productId) {
CartItem newItem = new CartItem(productId);
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
public int Quantity { get; set; }
private int _productId;
public int ProductId {
get { return _productId; }
set {
_product = null;
_productId = value;
}
}
private Product _product = null;
public Product Prod {
get {
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
public CartItem(int productId) {
this.ProductId = productId;
}
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) {
ShoppingCart.Instance.AddItem(1);
Response.Redirect("ViewCart.aspx");
Can any one please review the code and advise what is wrong init