/// <summary> /// /// </summary> /// <param name="products"></param> /// <returns></returns> public AfterPromotionResult ApplyPromotion(char[] products) { var response = new AfterPromotionResult(); double price = 0.0; if (!_rules.Any()) { throw new PromotionRulesNotFound(); } foreach (var rule in _rules) { response = rule.Invoke(products); products = response.RemainingProducts.ToArray(); price += response.PromotedProductsPrice; } response.RemainingProducts = products; response.PromotedProductsPrice = price; return(response); }
static public AfterPromotionResult ApplyCDPromotion(IList <char> products) { var promotedProducts = new AfterPromotionResult(); products = products.ToList(); while (true) { if (products.Any(t => t == 'C') && products.Any(t => t == 'D')) { promotedProducts.PromotedProducts.Add('C'); promotedProducts.PromotedProducts.Add('D'); promotedProducts.PromotedProductsPrice = 30; products.Remove('C'); products.Remove('D'); } else { break; } } promotedProducts.RemainingProducts = products; return(promotedProducts); }
static public AfterPromotionResult ApplyBCPromotion(IList <char> products) { products = products.ToList(); var promotedProducts = new AfterPromotionResult(); while (true) { if (products.Count(t => t == 'B') >= 2) { promotedProducts.PromotedProducts.Add('B'); promotedProducts.PromotedProductsPrice += 45; for (int i = 0; i < 2; i++) { products.Remove('B'); } } else { break; } } promotedProducts.RemainingProducts = products; return(promotedProducts); }