我试图弄清楚如何通过网络浏览器更改客户的订单。客户将订购带有产品 ID(键)、产品名称、产品价格和他想要的数量的项目。我想知道如何更改他的订单,方法是用旧数量的旧物品替换具有不同数量的相同物品,所以基本上点击选择物品并放置他希望购买的 2 个不同数量的物品。购物车包含购买的物品,所以我想知道如何从购物车中销毁 OrderItem 然后重新创建它。
当我的代码找到已经在购物车中的密钥时,它需要被销毁并使用新的 Quantity(来自网络应用程序的文本框)重新创建。
protected void btnOrder_Click(object sender, EventArgs e)
{
//Check for Shoppingcart object
// Create first if not there
if (Session["cart"] == null)
Session["cart"] = new ShoppingCart();
int quantity = 0;
// make sure there is text
if (txtQuantity.Text.Trim().Length != 0)
{
quantity = int.Parse(txtQuantity.Text);
if (((ShoppingCart)Session["cart"]).
keyExists(int.Parse(productID.Text)))
{
//Here I should Destroy the current item that exists and replace with new one
}
else // This is a new item
{
// Make the item
OrderItem item = new OrderItem(
int.Parse(productID.Text), productName.Text,
double.Parse(productPrice.Text),
int.Parse(txtQuantity.Text));
// add to cart
((ShoppingCart)Session["cart"]).addToCart(item);
}
// How does this work? Who is sender?
this.btnReturn_Click(sender, e);
}
else
{
Response.Write("Nothing Ordered<br>You must order some of the product or return to the Catalog");
}
这是 OrderItem 对象
public class OrderItem
{
private int productID;
private string prodName;
private double unitPrice;
private int quantityOrdered;
private string exceptionStr;
public OrderItem(int id, string name, double price, int quantity)
{
prodName = name;
exceptionStr = "Numeric data must not be negative";
if ( id < 0 || price < 0 || quantity < 0)
{
throw new System.ArgumentException(exceptionStr);
}
else
{
productID = id;
unitPrice = price;
quantityOrdered = quantity;
}
}
#region Public Properties
public int ProductID
{
get
{
return productID;
}
}
public string ProductName
{
get
{
return prodName;
}
}
public double UnitPrice
{
get
{
return unitPrice;
}
}
public int QuantityOrdered
{
get
{
return quantityOrdered;
}
set
{
if( value < 0 )
{
throw new ArgumentException(exceptionStr);
}
else
{
quantityOrdered = value;
}
}
}
#endregion
}
这是购物车供您查看:
public class ShoppingCart : IEnumerable
{
private SortedList theCart;
public ShoppingCart() {
theCart = new SortedList();
} // end of Constructor
public bool HasItems {
get{
bool hasItems = false;
if( theCart.Count > 0 )
hasItems = true;
return hasItems;
}
set {
// ignore this is read only
}
} // end of HasItems
public void addToCart(OrderItem item) {
theCart.Add(item.ProductID, item);
}// AddToCaArt
/// <summary>
/// deletes item that is passed
/// </summary>
/// <param name="item"></param>
public void deleteFromCart(OrderItem item)
{
theCart.Remove(item.ProductID);
} // end deleteFromCart
/// <summary>
/// deletes the item with this id key
/// </summary>
/// <param name="id"></param>
public void deleteFromCart(int id)
{
theCart.Remove(id);
} // end deleteFromCart
public OrderItem[] getCartContents()
{
// need to create stuff
OrderItem[] stuff = null;
theCart.Values.CopyTo(stuff, 0);
return (stuff);
} // end getCartContents
public bool keyExists(int ID) {
return theCart.ContainsKey(ID);
}// end keyExists
public ICollection Values
{
get
{
return theCart.Values;
}
}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return theCart.GetEnumerator();
}
#endregion
}