/// <summary> /// Methood to apply discount /// </summary> /// <param name="items">Product basket for which discount is applied</param> /// <returns><see cref="DiscountOutput"/></returns> public override IEnumerable <DiscountOutput> ApplyDiscount(IEnumerable <ProductInCart> items) { List <DiscountOutput> discountOutputs = new List <DiscountOutput>(); var discountApplicable = items.FirstOrDefault(x => x.Product.Name == _productQualifiesForDiscount && x.Quantity >= _quantityX); if (discountApplicable != null) { foreach (var item in items) { if (item.Product.Name == ProductName) { var discountFactor = Math.Min((discountApplicable.Quantity / _quantityX), item.Quantity); var amount = Math.Round((item.Product.UnitPrice * discountFactor) * _percentage, 2); var dicountOutput = new DiscountOutput() { DisplayText = $"Buy {_quantityX} {_productQualifiesForDiscount} Get {item.Product.Name} {_percentage:P0} OFF:", DiscountAmount = amount }; discountOutputs.Add(dicountOutput); } } } return(discountOutputs); }
/// <summary> /// Methood to apply discount /// </summary> /// <param name="items">Product basket for which discount is applied</param> /// <returns><see cref="DiscountOutput"/></returns> public override IEnumerable <DiscountOutput> ApplyDiscount(IEnumerable <ProductInCart> items) { List <DiscountOutput> discountOutputs = new List <DiscountOutput>(); foreach (var item in items) { if (item.Product.Name == ProductName) { var amount = Math.Round((item.Product.UnitPrice * item.Quantity) * _percentage, 2); var dicountOutput = new DiscountOutput() { DisplayText = $"{item.Product.Name} {_percentage:P0} OFF:", DiscountAmount = amount }; discountOutputs.Add(dicountOutput); } } return(discountOutputs); }