public IShoppingBasket Process(IShoppingBasket shoppingBasket)
        {
            shoppingBasket.Total =
                shoppingBasket.GetBasketItems().Sum(basketItem => basketItem.Product.Price * basketItem.Quantity);

            return(shoppingBasket);
        }
Esempio n. 2
0
        public IShoppingBasket Process(IShoppingBasket shoppingBasket)
        {
            if (!shoppingBasket.BogofOffers.Any())
            {
                return(shoppingBasket);
            }

            foreach (var basketItem in shoppingBasket.GetBasketItems())
            {
                if (basketItem.Quantity < 2)
                {
                    continue;
                }

                if (shoppingBasket.BogofOffers.All(bogof => bogof.ProductCategory != basketItem.Product.ProductCategory))
                {
                    continue;
                }

                var quantityTodrop     = basketItem.Quantity / 2;
                var bogofOfferDiscount = quantityTodrop * basketItem.Product.Price;
                shoppingBasket.Total -= bogofOfferDiscount;
                shoppingBasket.Messages.Add($"BOGOF discount £{bogofOfferDiscount} applied.");
            }

            return(shoppingBasket);
        }
Esempio n. 3
0
        private static IShoppingBasket ValidateOfferVoucherForThreshold(IShoppingBasket shoppingBasket)
        {
            var basketsTotal = shoppingBasket.GetBasketItems().Where(x => x.Product.Category != Category.GiftVoucher).Sum(basketItem => basketItem.Product.Price * basketItem.Quantity);

            if (basketsTotal < shoppingBasket.OfferVoucher.Threshold)
            {
                var additonalAmountToSpend = shoppingBasket.OfferVoucher.Threshold - basketsTotal + 0.01m;
                shoppingBasket.Messages.Add($"You have not reached the spend threshold for voucher {shoppingBasket.OfferVoucher.Code}. Spend another £{additonalAmountToSpend} to receive £5.00 discount from your basket total.");
                basketsTotal = shoppingBasket.Total;
            }

            shoppingBasket.Total = basketsTotal;

            return(shoppingBasket);
        }
        public BasketServiceResponse GetBasketTotal(IShoppingBasket shoppingBasket)
        {
            try
            {
                if (!shoppingBasket.GetBasketItems().Any())
                {
                    return new BasketServiceResponse()
                           {
                               Notifications = new List <string> {
                                   "Your shopping basket is empty"
                               },
                               Success     = true,
                               BasketTotal = 0.0m
                           }
                }
                ;

                _basketProcessors = CreateBasketProcessors();

                foreach (var processor in _basketProcessors)
                {
                    processor.Process(shoppingBasket);
                }

                // TODO: Use automapper for mapping Basket object to BasketServiceResponse object
                return(new BasketServiceResponse
                {
                    Notifications = shoppingBasket.Messages?.ToList(),
                    BasketTotal = shoppingBasket.Total,
                    Success = true
                });
            }
            catch (Exception exception)
            {
                return(new BasketServiceResponse
                {
                    ErrorMessage = $"Failed to calculate basket total {exception.Message}",
                    Success = false,
                    BasketTotal = 0.0m
                });
            }
        }
Esempio n. 5
0
        private IShoppingBasket ValidateOfferVoucherForProducts(IShoppingBasket shoppingBasket)
        {
            var basketsTotal = 0.00m;

            foreach (var basketItem in shoppingBasket.GetBasketItems())
            {
                if (basketItem.Product.Category == shoppingBasket.OfferVoucher.ProductCategory && !_isOfferApplied)
                {
                    _isOfferApplied = true;
                    var productPrice = basketItem.Product.Price;
                    productPrice -= shoppingBasket.OfferVoucher.Value;
                    productPrice  = productPrice >= 0 ? productPrice : 0m;

                    basketsTotal += productPrice;
                }
                else
                {
                    basketsTotal += basketItem.Product.Price;
                }
            }

            shoppingBasket.Total = basketsTotal;
            return(shoppingBasket);
        }