/// <summary> /// Calculate the basket value from the given input /// </summary> /// <param name="items">The items in string array form</param> /// <returns>The result of the calculation</returns> public CalculationResult Calculate(string[] items) { var basket = new Basket(items); var result = new CalculationResult(); foreach (var kvp in basket) { result.Add(GetTotal(kvp.Key, kvp.Value)); } // copy basket because we're taking stuff out // This is not strictly necessary but still a good idea for future // extensions, i.e. in case we need to list the initial basket var offerBasket = new Basket(basket); foreach (var offer in _config.Offers) { // How many times do the required items fit? int fitsTimes = offerBasket.FitsTimes(offer.Condition); if (fitsTimes > 0) { // take items out and apply the offer offerBasket.SubtractTimes(offer.Condition, fitsTimes); result.AddOffer(offer, offer.PriceDelta * fitsTimes); } } return(result); }
public void AddOffer_WithValidOffer_AddsToTotal() { var cResult = new CalculationResult(); cResult.Add(12.34M); cResult.AddOffer(new Offer { Description = "TestOffer", PriceDelta = -1M }, -3M); Assert.AreEqual(12.34M, cResult.SubTotal); Assert.AreEqual(9.34M, cResult.Total); }
public void ToString_WithOffersInPence2Digits_FormatsOutputCorrectly() { var cResult = new CalculationResult(); cResult.Add(12.34M); cResult.AddOffer(new Offer { Description = "TestOffer", PriceDelta = -0.1M }, -0.3M); Assert.AreEqual( "Subtotal: £12.34" + Environment.NewLine + "TestOffer: -30p" + Environment.NewLine + "Total: £12.04" + Environment.NewLine, cResult.ToString()); }