public void DeleteItemFromCart(string itemId) { model = (ShopModel)Session["model"]; // get the model object Item currentItem = null; // For each item in the item catalog, check whether the item's ID matches the // input ID. foreach (var i in model.itemCatalog) { if (i.itemID == Int32.Parse(itemId) /*Parse Item ID, TODO: exception handling... */) { currentItem = i; break; // there is a match - store the matching item and break out of the loop } } if (currentItem != null) // if we have found an item { model.shoppingCart = CheckoutFacilitator.RemoveItemFromCart(currentItem, model); } Session["model"] = model; // reassign model. }
public void RemoveItemFromCartTest() { ShopModel tempModel = new ShopModel(); tempModel.shoppingCart = new Dictionary <Item, int>(); Item item1 = new Item { itemID = 1, name = "Item1", sellingPrice = 50 }; tempModel.shoppingCart.Add(item1, 5); // added 5 of item 1 to cart. Item item2 = new Item { itemID = 2, name = "Item2", sellingPrice = 50 }; tempModel.shoppingCart.Add(item2, 2); // currently cart should have 8 items total, and 2 entries in the cart. // now remove item 2 CheckoutFacilitator.RemoveItemFromCart(item2, tempModel); // now should only have 1 entry, which should be item 1. if (tempModel.shoppingCart.Count != 1) { Assert.Fail(); } if (tempModel.shoppingCart.ContainsKey(item2)) { Assert.Fail(); } if (tempModel.shoppingCart.Sum(x => x.Value) != 5) // should have a qty of 5 { Assert.Fail(); } }