Beispiel #1
0
        public void ProductEligibleForSixForThreeWhenCartCountIsSix()
        {
            //Arrange
            ProductCartItem product = new ProductCartItem()
            {
                Id                   = 2,
                ProductName          = "Banana",
                ProductTitle         = "Best Fruit in the world after big mac.",
                ProductImage         = "banana.jpg",
                ProductPrice         = 2,
                CartCount            = 6,
                ProductDiscountOffer = new DiscountOffer()
                {
                    Id = 2, DiscountType = DiscountType.SixForThree
                }
            };

            var sixForThreeStrategy = new SixForThreeDiscountStrategy();

            //Act
            var calculatedPrice = sixForThreeStrategy.CalculateDiscountPrice(product);

            //Assert
            calculatedPrice.Should().Be(6);
        }
        public void ProductEligibleForThreeForTwoWhenCartCountIsThree()
        {
            //Arrange
            ProductCartItem product = new ProductCartItem()
            {
                Id                   = 1,
                ProductName          = "Apple",
                ProductTitle         = "Troublemaker since Adam & Eve.",
                ProductImage         = "apple.jpg",
                ProductPrice         = 1,
                CartCount            = 3,
                ProductDiscountOffer = new DiscountOffer()
                {
                    Id = 1, DiscountType = DiscountType.ThreeForTwo
                }
            };

            var threeForTwoStrategy = new ThreeForTwoDiscountStrategy();

            //Act
            var calculatedPrice = threeForTwoStrategy.CalculateDiscountPrice(product);

            //Assert
            calculatedPrice.Should().Be(2);
        }
        public List <ProductCartItem> PopulateProductCartItems()
        {
            List <ProductCartItem> productList = new List <ProductCartItem>();

            ProductCartItem product1 = new ProductCartItem()
            {
                Id                   = 1,
                ProductName          = "Apple",
                ProductTitle         = "Troublemaker since Adam & Eve.",
                ProductImage         = "apple.jpg",
                ProductPrice         = 1,
                ProductDiscountOffer = new DiscountOffer()
                {
                    Id = 1, DiscountType = DiscountType.ThreeForTwo
                }
            };

            ProductCartItem product2 = new ProductCartItem()
            {
                Id                   = 2,
                ProductName          = "Banana",
                ProductTitle         = "Best Fruit in the world after big mac.",
                ProductImage         = "banana.jpg",
                ProductPrice         = 2,
                ProductDiscountOffer = new DiscountOffer()
                {
                    Id = 2, DiscountType = DiscountType.SixForThree
                }
            };

            ProductCartItem product3 = new ProductCartItem()
            {
                Id           = 3,
                ProductName  = "Peach",
                ProductTitle = "This is funny.",
                ProductImage = "peach.png",
                ProductPrice = 3
            };

            productList.Add(product1);
            productList.Add(product2);
            productList.Add(product3);

            return(productList);
        }
Beispiel #4
0
        public ActionResult AddProduct(int productId, int qty)
        {
            var cart = _cartContext.Current();

            var checkStockResult = _productService.CheckStock(productId, qty, cart.GetTotalQuantityForProduct(productId, string.Empty));

            if (checkStockResult.StockCheck == CheckStockType.NotAvailable)
                return Json(new { NotAvailable = true });

            if (checkStockResult.StockCheck == CheckStockType.RequestTooLarge)
                return Json(new { QuantityTooLarge = true });

            // All good - proceed to add to the cart
            var productCartItem = new ProductCartItem(checkStockResult.Product, qty);
            cart.AddItem(productCartItem);

            return Json(new { Added = true, Item = productCartItem });
        }
 public decimal CalculateDiscountPrice(ProductCartItem cartItem)
 {
     return(cartItem.CartCount >= 6 ?
            (cartItem.CartCount - 3) * cartItem.ProductPrice :
            (cartItem.CartCount * cartItem.ProductPrice));
 }