private decimal CalculateDiscountAmount(SpecialOfferRule specialOfferRule, ShoppingBasket shoppingBasket) { // SpecialOfferRule product not matched basket int specialOfferRuleMatchCount = shoppingBasket.GetItemCountById(specialOfferRule.SpecialOfferMatchRule.Product.Id) / specialOfferRule.SpecialOfferMatchRule.MinimumQuantity; if (specialOfferRuleMatchCount == 0) { return(0m); } // SpecialOfferDiscount product not found in basket int discountedItemCount = shoppingBasket.GetItemCountById(specialOfferRule.SpecialOfferDiscountRule.Product.Id); if (discountedItemCount == 0) { return(0m); } // if there is a maximum limit on discountCount int discountAppliedCount = Math.Min(specialOfferRuleMatchCount, discountedItemCount); if (specialOfferRule.SpecialOfferDiscountRule.MaximumQuantity.HasValue) { discountAppliedCount = Math.Min(discountedItemCount, specialOfferRule.SpecialOfferDiscountRule.MaximumQuantity.Value); } // calculate the discount based on the basket items decimal percentageDiscount = Decimal.Divide(specialOfferRule.SpecialOfferDiscountRule.DiscountPercent, 100); decimal discountAmount = Decimal.Multiply(specialOfferRule.SpecialOfferDiscountRule.Product.Price, percentageDiscount); return(Decimal.Multiply(discountAmount, discountAppliedCount)); }
public void CheckSpecialOfferDiscountRuleNullArgumentException() { try { SpecialOfferRule specialOfferRule = new SpecialOfferRule( new SpecialOfferMatchRule(new Product(1, "dummy", 0.2m), 1), new SpecialOfferDiscountRule(null, 1)); Assert.Fail("This should throw null exception"); } catch (ArgumentNullException ex) { Assert.IsTrue(ex.ParamName == "product"); } }
public static SpecialOffersDL GetAppleBeansBreadSpecialOffers(ProductsDL productsDL) { // 10 % off apples SpecialOfferRule apples10percentOff = new SpecialOfferRule(new SpecialOfferMatchRule(productsDL.GetProductByName("apple"), 1), new SpecialOfferDiscountRule(productsDL.GetProductByName("apple"), 10)); // buy 2 cans of beans, get 1 loaf of bread 50% off SpecialOfferRule twoCans1BreadLoaf = new SpecialOfferRule(new SpecialOfferMatchRule(productsDL.GetProductByName("beans"), 2), new SpecialOfferDiscountRule(productsDL.GetProductByName("bread"), 50, 1)); SpecialOffersDL specialOffers = new SpecialOffersDL(new List <SpecialOfferRule>() { apples10percentOff, twoCans1BreadLoaf }); return(specialOffers); }
public static SpecialOffersDL GetSpecialOffers3oranges1applefree(ProductsDL products) { // buy 90 % off strawberries SpecialOfferRule apples10percentOff = new SpecialOfferRule( new SpecialOfferMatchRule(products.GetProductByName("Strawberries"), 1), new SpecialOfferDiscountRule(products.GetProductByName("Strawberries"), 90)); // buy 3 oranges, get 1 apple for free SpecialOfferRule threeOrangesOneAppleFree = new SpecialOfferRule( new SpecialOfferMatchRule(products.GetProductByName("oranges"), 3), new SpecialOfferDiscountRule(products.GetProductByName("apple"), 100, 1)); SpecialOffersDL specialOffers = new SpecialOffersDL(new List <SpecialOfferRule>() { apples10percentOff, threeOrangesOneAppleFree }); return(specialOffers); }
static void Main(string[] args) { try { // Create Product List for example in paper ProductsDL productsDL = new ProductsDL(new List <Product>() { new Product(1, "Beans", 0.65m), new Product(2, "Bread", 0.80m), new Product(3, "Milk", 1.30m), new Product(4, "Apple", "Apples", 1.00m) }); // create special offers // Offer 1: 10% off apples SpecialOfferRule apples10percentOff = new SpecialOfferRule( new SpecialOfferMatchRule(productsDL.GetProductByName("apple"), 1), new SpecialOfferDiscountRule(productsDL.GetProductByName("apple"), 10)); // Offer 2: Buy 2 cans of beans get 1 loaf of bread 50 percent off SpecialOfferRule twoCans1BreadLoaf = new SpecialOfferRule( new SpecialOfferMatchRule(productsDL.GetProductByName("beans"), 2), new SpecialOfferDiscountRule(productsDL.GetProductByName("bread"), 50, 1)); SpecialOffersDL specialOffersDL = new SpecialOffersDL(new List <SpecialOfferRule>() { apples10percentOff, twoCans1BreadLoaf }); // process user imput with products and special offers IAppMain appMain = new AppMain(productsDL, specialOffersDL); string output = appMain.Process(args); Console.WriteLine(output); } catch (Exception ex) { // log out error not just to console. Console.WriteLine("Fatal Error occured: ", ex); } }
public SpecialOfferApplied(SpecialOfferRule specialOfferRule, decimal discountedAmount) { SpecialOfferRule = specialOfferRule; DiscountedAmount = discountedAmount; }