예제 #1
0
        public void ApplyDiscount_Throws_NullReferenceException_Ok()
        {
            //Arrange
            List <BasketProductModel> expectedProducts = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName  = "Milk",
                    ProductPrice = 1.15,
                    Quantity     = 2,
                    Total        = 2.30
                }
            };

            BasketModel basketModel = new BasketModel {
                Products = expectedProducts
            };
            var milk = new BasketProductModel()
            {
                ProductName  = "Milk",
                ProductPrice = 1.15,
                Quantity     = 1
            };

            //Assert
            Assert.Throws <NullReferenceException>(() => subject.ApplyDiscount(basketModel, milk));
        }
예제 #2
0
        public Task <BasketModel> ApplyDiscount(BasketModel basket, BasketProductModel product)
        {
            if (discountedProducts.Contains(product.ProductName) && basket.Products.Count > 0)
            {
                product.CalculateDiscount(ref basket, product);
            }

            return(Task.FromResult(basket));
        }
예제 #3
0
        public async Task BasketHasTwoBreadAndTwoButter_ReturnOk()
        {
            //Arrange
            List <BasketProductModel> expectedProducts = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName  = "Butter",
                    ProductPrice = 0.80,
                    Quantity     = 2,
                    Total        = 1.60
                },
                new BasketProductModel()
                {
                    ProductName  = "Bread",
                    ProductPrice = 1.00,
                    Quantity     = 2,
                    Total        = 1.5
                }
            };
            var expectedBasket = new BasketModel {
                Products = expectedProducts
            };

            var butter = new BasketProductModel()
            {
                ProductName  = "Butter",
                ProductPrice = 0.80,
                Quantity     = 2
            };
            var bread = new BasketProductModel()
            {
                ProductName         = "Bread",
                ProductPrice        = 1.00,
                Quantity            = 2,
                OfferForThisProduct = new OfferModel
                {
                    DiscountAmount   = 50,
                    IsFree           = false,
                    OfferName        = "Bread",
                    OfferTriggerFrom = "Butter"
                }
            };

            //Act
            await basketService.AddProductToBasket(butter);

            await basketService.AddProductToBasket(bread);

            basketService.CalculateBasketTotal();
            //Assert
            Assert.IsNotNull(basketService.Baskett);
            Assert.AreEqual(expectedBasket.Products, basketService.Baskett.Products);
            Assert.That(basketService.TotalCost, Is.EqualTo(3.10));
        }
예제 #4
0
        private void updatePlusBasket(ProductModel model)
        {
            basket.orderTotal += model.Price;
            basket.pieces++;
            BasketProductModel basketItem = new BasketProductModel();

            basketItem.Product = model;
            basketItem.Price   = model.Price;
            basketItem.Pieces += 1;
            basket.products.Add(basketItem);
        }
예제 #5
0
 /// <summary>
 /// Calculate the totals if freebies available or not (we calculate the total of each product)
 /// </summary>
 /// <param name="bModel"></param>
 /// <param name="calculateFreebes"></param>
 private void CalculateTotal(ref BasketProductModel bModel, bool calculateFreebes = true)
 {
     if (bModel.Freebies != 0 && calculateFreebes)
     {
         var totalQuantity = bModel.Quantity - bModel.Freebies;
         bModel.Total = totalQuantity * bModel.ProductPrice;
     }
     else
     {
         bModel.Total = bModel.Quantity * bModel.ProductPrice;
     }
 }
예제 #6
0
        public async Task BasketHasTwoButterOneBreadEightMilk_ReturnOk()
        {
            //Arrange
            var butter = new BasketProductModel()
            {
                ProductName  = "Butter",
                ProductPrice = 0.80,
                Quantity     = 2
            };
            var bread = new BasketProductModel()
            {
                ProductName         = "Bread",
                ProductPrice        = 1.00,
                Quantity            = 1,
                OfferForThisProduct = new OfferModel
                {
                    DiscountAmount   = 50,
                    IsFree           = false,
                    OfferName        = "Bread",
                    OfferTriggerFrom = "Butter"
                }
            };

            var milk = new BasketProductModel()
            {
                ProductName         = "Milk",
                ProductPrice        = 1.15,
                Quantity            = 8,
                OfferForThisProduct = new OfferModel
                {
                    IsFree           = true,
                    OfferName        = "Milk",
                    OfferTriggerFrom = "Milk"
                }
            };

            //Act
            await basketService.AddProductToBasket(butter);

            await basketService.AddProductToBasket(bread);

            await basketService.AddProductToBasket(milk);

            basketService.CalculateBasketTotal();

            //Assert
            var totalBasket = Math.Round(basketService.TotalCost, 2);

            Assert.IsNotNull(basketService.Baskett);
            Assert.That(totalBasket, Is.EqualTo(9.00));
        }
예제 #7
0
        public async Task AddToBasket_ReturnOk()
        {
            //Arrange
            List <BasketProductModel> expectedProducts = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName  = "Milk",
                    ProductPrice = 1.15,
                    Quantity     = 2,
                    Total        = 2.30
                }
            };

            BasketModel returnedFromApplyDiscount = new BasketModel {
                Products = expectedProducts
            };
            List <BasketProductModel> productsModel = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName  = "Milk",
                    ProductPrice = 1.15,
                    Quantity     = 1
                }
            };

            subject.Baskett.Products = productsModel;

            discountServiceMock
            .Setup(x => x.ApplyDiscount(subject.Baskett, It.IsAny <BasketProductModel>())).Returns(Task.FromResult(returnedFromApplyDiscount));

            var milk = new BasketProductModel()
            {
                ProductName  = "Milk",
                ProductPrice = 1.15,
                Quantity     = 1
            };

            //Act
            await subject.AddProductToBasket(milk);

            subject.CalculateBasketTotal();

            //Assert
            Assert.AreEqual(2.30, subject.TotalCost);
            Assert.AreEqual(expectedProducts[0].Quantity, subject.Baskett.Products[0].Quantity);
        }
예제 #8
0
        /// <summary>
        /// We calculate quantity of products
        /// </summary>
        /// <param name="basket"></param>
        /// <param name="product"></param>
        /// <param name="productName"></param>
        /// <returns></returns>
        public int NumberOfProductsInBasket(ref BasketModel basket, BasketProductModel product = null, string productName = null)
        {
            var productContained = 0;

            if (product != null)
            {
                productContained = basket.Products.FirstOrDefault(x => x.ProductName == product.ProductName).Quantity;
            }
            else
            {
                if (basket.Products.FirstOrDefault(x => x.ProductName == productName) != null)
                {
                    productContained = basket.Products.FirstOrDefault(x => x.ProductName == productName).Quantity;
                }
            }

            return(productContained);
        }
예제 #9
0
        public async Task BasketHasOneFreeMilk_ReturnOk()
        {
            //Arrange
            List <BasketProductModel> productsModel = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName         = "Milk",
                    ProductPrice        = 1.15,
                    Quantity            = 3,
                    OfferForThisProduct = new OfferModel
                    {
                        IsFree           = true,
                        OfferName        = "Milk",
                        OfferTriggerFrom = "Milk"
                    }
                }
            };

            basketService.Baskett.Products = productsModel;

            var milk = new BasketProductModel()
            {
                ProductName         = "Milk",
                ProductPrice        = 1.00,
                Quantity            = 1,
                OfferForThisProduct = new OfferModel
                {
                    IsFree           = true,
                    OfferName        = "Milk",
                    OfferTriggerFrom = "Milk"
                }
            };

            //Act
            await basketService.AddProductToBasket(milk);

            basketService.CalculateBasketTotal();
            //Assert
            var totalBasket = Math.Round(basketService.Baskett.Products[0].Total, 2);

            Assert.IsNotNull(basketService.Baskett);
            Assert.That(totalBasket, Is.EqualTo(3.45));
        }
예제 #10
0
        public async Task ApplyDiscount_ReturnOk()
        {
            //Arrange
            List <BasketProductModel> expectedProducts = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName         = "Milk",
                    ProductPrice        = 1.15,
                    Quantity            = 2,
                    Total               = 2.30,
                    OfferForThisProduct = new OfferModel
                    {
                        IsFree           = true,
                        OfferName        = "Milk",
                        OfferTriggerFrom = "Milk"
                    }
                }
            };

            BasketModel basketModel = new BasketModel {
                Products = expectedProducts
            };
            var milk = new BasketProductModel()
            {
                ProductName         = "Milk",
                ProductPrice        = 1.15,
                Quantity            = 1,
                OfferForThisProduct = new OfferModel
                {
                    IsFree           = true,
                    OfferName        = "Milk",
                    OfferTriggerFrom = "Milk"
                }
            };

            //Act
            var resultBasket = await subject.ApplyDiscount(basketModel, milk);

            //Assert
            Assert.AreEqual(expectedProducts[0].Total, resultBasket.Products[0].Total);
            Assert.AreEqual(expectedProducts[0].Quantity, resultBasket.Products[0].Quantity);
        }
예제 #11
0
        public void addOrUpdateBasket(ProductModel product)
        {
            Boolean succes = false;

            foreach (BasketProductModel bp in basket.products)
            {
                if (bp.Product == product)
                {
                    bp.Pieces++;
                    succes = true;
                }
            }
            if (!succes)
            {
                BasketProductModel bP = new BasketProductModel();
                bP.Product = product;
                basket.products.Add(bP);
            }
            basket.orderTotal += product.Price;
        }
예제 #12
0
        /// <summary>
        /// Calculating discount
        /// </summary>
        /// <param name="basket"></param>
        /// <param name="product"></param>
        public void CalculateDiscount(ref BasketModel basket, BasketProductModel product)
        {
            var productContained = NumberOfProductsInBasket(ref basket, _obj);
            var obj = basket.Products.FirstOrDefault(x => x.ProductName == _obj.ProductName);

            CalculateTotal(ref obj, false);

            var exists = basket.Products.Select(x => x.ProductName == obj?.OfferForThisProduct?.OfferTriggerFrom).FirstOrDefault();
            var productWithOfferQuanity = basket.Products.FirstOrDefault(prod => prod.ProductName == obj?.OfferForThisProduct?.OfferTriggerFrom);

            switch (_obj.ProductName)
            {
            case "Milk":
                obj.Freebies = numberOfFreeItems(productWithOfferQuanity.Quantity);
                CalculateTotal(ref obj, true);
                break;

            case "Butter":

                break;

            case "Bread":
                if (exists && productWithOfferQuanity != null)
                {
                    for (int i = 0; i < productWithOfferQuanity.Quantity; i++)
                    {
                        if (i % 2 == 0)
                        {
                            CalculateDiscountedPrice(obj.OfferForThisProduct.DiscountAmount, ref obj);
                        }
                    }
                }
                break;

            default:
                break;
            }
        }
예제 #13
0
        public ActionResult GetBasket()
        {
            var cookieBasket = Request.Cookies["basket"]?.Value;
            var basket       = new Basket();

            if (!string.IsNullOrEmpty(cookieBasket))
            {
                basket = JsonConvert.DeserializeObject <Basket>(WebUtility.UrlDecode(cookieBasket));
            }

            var model = new List <BasketProductModel>();

            foreach (var product in basket.Products)
            {
                var productEntity = _producyRepository.GetProductCard(product.Id);
                var inputProduct  = basket.Products.First(x => x.Id == productEntity.Id);

                var productModel = new BasketProductModel
                {
                    Id          = productEntity.Id,
                    Name        = productEntity.Name,
                    Articul     = productEntity.Articul,
                    Count       = inputProduct.Count,
                    Image       = productEntity.ImageNames.First(),
                    Description = productEntity.Description
                };

                var size = productEntity.Prices.First(x => x.SizeId == inputProduct.SizeId);
                productModel.Price = new SizePriceModel {
                    Price = size.Price, SizeId = size.SizeId, SizeName = size.SizeName
                };
                model.Add(productModel);
            }

            return(View("OrderBasketView", new BasketModel {
                Products = model
            }));
        }
예제 #14
0
        public async Task BasketHasOneOfEachProduct_ReturnOk()
        {
            //Arrange
            List <BasketProductModel> expectedProducts = new List <BasketProductModel> {
                new BasketProductModel()
                {
                    ProductName  = "Milk",
                    ProductPrice = 1.15,
                    Quantity     = 1,
                    Total        = 1.15
                },
                new BasketProductModel()
                {
                    ProductName  = "Bread",
                    ProductPrice = 1.00,
                    Quantity     = 1,
                    Total        = 1.00
                },
                new BasketProductModel()
                {
                    ProductName  = "Butter",
                    ProductPrice = 0.80,
                    Quantity     = 1,
                    Total        = 0.80
                }
            };
            var expectedBasket = new BasketModel {
                Products = expectedProducts
            };

            var milk = new BasketProductModel()
            {
                ProductName         = "Milk",
                ProductPrice        = 1.15,
                Quantity            = 1,
                OfferForThisProduct = new OfferModel
                {
                    IsFree           = true,
                    OfferName        = "Milk",
                    OfferTriggerFrom = "Milk"
                }
            };
            var butter = new BasketProductModel()
            {
                ProductName  = "Butter",
                ProductPrice = 0.80,
                Quantity     = 1
            };
            var bread = new BasketProductModel()
            {
                ProductName         = "Bread",
                ProductPrice        = 1.00,
                Quantity            = 1,
                OfferForThisProduct = new OfferModel
                {
                    DiscountAmount   = 50,
                    IsFree           = false,
                    OfferName        = "Bread",
                    OfferTriggerFrom = "Butter"
                }
            };

            //Act
            await basketService.AddProductToBasket(milk);

            await basketService.AddProductToBasket(bread);

            await basketService.AddProductToBasket(butter);

            basketService.CalculateBasketTotal();
            //Assert
            Assert.IsNotNull(basketService.Baskett);
            Assert.AreEqual(expectedBasket.Products, basketService.Baskett.Products);
            Assert.That(basketService.TotalCost, Is.EqualTo(2.95));
        }
예제 #15
0
        /// <summary>
        /// Calculate the discounted price
        /// </summary>
        /// <param name="percentageToSubtract"></param>
        /// <param name="bModel"></param>
        private void CalculateDiscountedPrice(int percentageToSubtract, ref BasketProductModel bModel)
        {
            var discountedPrice = bModel.ProductPrice - (bModel.ProductPrice * percentageToSubtract / 100);

            bModel.Total -= discountedPrice;
        }
예제 #16
0
 protected void Init(BasketProductModel bPModel)
 {
     _obj = bPModel;
 }