I am trying to figure out how I can change up an order by the customer over a web browser. The customer will order an item with product Id (key), the name of product, the price of the product, and the quantity he wants. I would like to know how I can change his order by replacing the OLD item with the old quantity with the same item with a different quanitity SO basically clicking to choose the item and placing 2 different quantities he wishes to purchase. The shopping cart holds the Items that are purchased, so I was wondering how I could destroy an OrderItem from the shopping cart then recreate it.
When my code find the key that is already in the shopping cart, then It needs to be destroyed and recreated with a new Quanitity (a text box from the web app).
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");
}
Here is the OrderItem object
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
}
Here is the Shoppingcart for your viewing:
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
}
ShoppingCart
(whichaddToCart
adds to)? – keyboardP