需要帮助购物车中的错误 [英] need help whats wrong in the shopping cart
问题描述
大家好,
我是一名mca学生,我正在尝试准备我的拼贴项目。为了让我的项目更加真实,我想在我的项目中添加一个购物车。我不知道该怎么做。为了得到我的问题,我浏览了一篇文章,根据指导,我准备了一个演示销售车。
购物车功能正常。当我在免费托管网站上传我的演示项目时出现问题。我发现购物车不是空的。当我从计算机A更新购物车中的信息时,也出现在计算机B中。我认为所有用户之间都使用相同的购物车。
以下是我所拥有的创建,我想要一些人来审查这段代码。我知道我要求很多,但是你的帮助肯定有助于获得好成绩。
首先我创建了3个类:cartitem,product,shoppingcart。和代码如下:
1)产品类别:
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)购物车
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
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");
任何人都可以查看代码并告知错误init
Can any one please review the code and advise what is wrong init
推荐答案
我在几年前的一个项目中遇到过类似的问题..这是因为单例构造函数。由于这个原因,用户A将能够看到用户B的更新。打包车很好。
I faced similar issue in one of my project some years ago.. It was because of the singleton constructor. User-A will be able to see User-B''s update because of this. Its fine to have the cart in session.
这篇关于需要帮助购物车中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!