Example #1
0
        public void ApplyDiscountWhenCartHasTwoItemsDiscountFalseTest()
        {
            List <Category> categoriesKeyboard = new List <Category>()
            {
                new Category("Keyboards", ""),
            };

            List <Category> categoriesHeadset = new List <Category>()
            {
                new Category("Hardware", ""),
                new Category("Headsets", ""),
            };

            List <ProductItem> products = new List <ProductItem>()
            {
                new ProductItem("Intel's Core i9-9900K", "", 20, (decimal)129.40, categoriesKeyboard),
                new ProductItem("Razer BlackWidow Keyboard", "", 20, (decimal)52.20, categoriesHeadset),
            };

            List <ItemOrder> itemOrders = new List <ItemOrder>()
            {
                new ItemOrder(products[0], 1),
                new ItemOrder(products[1], 1),
            };

            ShoppingCart cart = new ShoppingCart(itemOrders);

            ShoppingCartUtils.SetTotalPriceWithDiscount(cart);

            Assert.Equal((decimal)181.60, cart.TotalPrice);
        }
Example #2
0
        public void InvalidShoppingCartTest()
        {
            List <Category> categoriesKeyboard = new List <Category>()
            {
                new Category("Keyboards", ""),
                new Category("Hardware", ""),
            };

            List <Category> categoriesHeadset = new List <Category>()
            {
                new Category("Hardware", ""),
                new Category("Headsets", ""),
            };

            List <ProductItem> products = new List <ProductItem>()
            {
                new ProductItem("Intel's Core i9-9900K", "", 20, (decimal)129.40, categoriesKeyboard),
                new ProductItem("Razer BlackWidow Keyboard", "", 20, (decimal)52.20, categoriesHeadset),
            };

            List <ItemOrder> itemOrders = new List <ItemOrder>()
            {
                new ItemOrder(products[0], 2),
                new ItemOrder(products[1], 25), // 25 (PurchasedQuantity) > 20 (StockQuantity)
            };

            ShoppingCart cart = new ShoppingCart(itemOrders);

            ShoppingCartUtils.SetTotalPriceWithDiscount(cart);

            Assert.True(ShoppingCartUtils.DebugMessages.Contains("Cart is not valid"));
        }
        public async Task <string> Submit(ShoppingCart cart)
        {
            if (!(AllAsNoTracking().Select(x => x.ID).Contains(cart.ID)))
            {
                return("Cart could not be found in DB");
            }

            await SetShoppingCart(cart);

            if (ShoppingCartUtils.SetTotalPriceWithDiscount(cart))
            {
                cart.IsPaid = true;
                await Update(cart);

                // Updating every productItem's StockQuantity after submitting the cart
                foreach (var itemOrder in cart.ItemOrders)
                {
                    itemOrder.ProductItem.Quantity -= itemOrder.PurchaseQuantity;
                    await productItemService.Update(itemOrder.ProductItem);
                }
            }
            else
            {
                return("Cart is not valid");
            }

            return(ShoppingCartUtils.DebugMessages.Any()
                ? string.Join("\n", ShoppingCartUtils.DebugMessages)
                : "No discounts applied \nCart's TotalValue updated");
        }
Example #4
0
        public void ApplyDiscountWhenCartHasNoItemsTest()
        {
            ShoppingCart cart = new ShoppingCart();

            ShoppingCartUtils.SetTotalPriceWithDiscount(cart);

            Assert.True(ShoppingCartUtils.DebugMessages.Contains("Cart has no orders"));
        }
Example #5
0
        public void ApplyDiscountWhenCartHasOneItemTest()
        {
            List <Category> categories = new List <Category>()
            {
                new Category("Keyboards", ""),
                new Category("Hardware", ""),
            };

            List <ProductItem> products = new List <ProductItem>()
            {
                new ProductItem("Keywire Keyboard", "", 20, (decimal)129.40, categories),
            };

            List <ItemOrder> itemOrders = new List <ItemOrder>()
            {
                new ItemOrder(products[0], 1),
            };

            ShoppingCart cart = new ShoppingCart(itemOrders);

            ShoppingCartUtils.SetTotalPriceWithDiscount(cart);

            Assert.Equal((decimal)129.40, cart.TotalPrice);
        }