コード例 #1
0
        public void AddItemToCartTest()
        {
            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
            };

            CheckoutFacilitator.AddItemToCart(item2, 3, tempModel); // add new item to cart

            // cart should have 8 items total, and 2 entreis in the cart.

            if (tempModel.shoppingCart.Count != 2)
            {
                Assert.Fail();
            }
            if (tempModel.shoppingCart.Sum(x => x.Value) != 8)
            {
                Assert.Fail();
            }
        }
コード例 #2
0
        public void AddItemToCart(string itemId, string quantity)
        {
            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.AddItemToCart(currentItem, Int32.Parse(quantity), model);
            }
            Session["model"] = model; // reassign model.
        }