diff --git a/ChestersCheckout/ChestersCheckout.ConsoleApp/Program.cs b/ChestersCheckout/ChestersCheckout.ConsoleApp/Program.cs index 1bc068d..3f42516 100644 --- a/ChestersCheckout/ChestersCheckout.ConsoleApp/Program.cs +++ b/ChestersCheckout/ChestersCheckout.ConsoleApp/Program.cs @@ -27,9 +27,10 @@ private static IHostBuilder CreateHostBuilder(string[] args) private static void ConfigureServices(HostBuilderContext builder, IServiceCollection services) { services.AddScoped(); - services.AddScoped>(sp => new IDiscounterService[] - { new BogofDiscounterService("apple") } - ); + services.AddScoped>(sp => new IDiscounterService[] { + new BogofDiscounterService("apple"), + new ThreeForTwoDiscounterService("orange") + }); services.AddScoped(); services.AddScoped(); } diff --git a/ChestersCheckout/ChestersCheckout.Core.Tests/Services/BasketCostCalculatorServiceTests.cs b/ChestersCheckout/ChestersCheckout.Core.Tests/Services/BasketCostCalculatorServiceTests.cs index cb6f909..b233614 100644 --- a/ChestersCheckout/ChestersCheckout.Core.Tests/Services/BasketCostCalculatorServiceTests.cs +++ b/ChestersCheckout/ChestersCheckout.Core.Tests/Services/BasketCostCalculatorServiceTests.cs @@ -31,7 +31,7 @@ public void CalculateTotalCost_ReturnsCost_GivenBasketWithValidItems() [InlineData(2, 135)] [InlineData(3, 195)] [InlineData(4, 195)] - public void CalculateTotalCost_ReturnsDiscountedCost_GivenBasketAndBogofDiscounter(int appleQuantity, int expectedCost) + public void CalculateTotalCost_ReturnsDiscountedCost_WhenAppleBogofDiscounter(int appleQuantity, int expectedCost) { // Arrange var service = new BasketCostCalculatorService(new[] { new BogofDiscounterService("apple") }); @@ -46,5 +46,28 @@ public void CalculateTotalCost_ReturnsDiscountedCost_GivenBasketAndBogofDiscount // Assert Assert.Equal(expectedCost, result); } + + [Theory] + [InlineData(0, 0)] + [InlineData(1, 25)] + [InlineData(2, 50)] + [InlineData(3, 50)] + [InlineData(4, 75)] + [InlineData(5, 100)] + [InlineData(6, 100)] + public void CalculateTotalCost_ReturnsDiscountedCost_WhenThreeForTwoDiscounter(int quantity, int expectedCost) + { + // Arrange + var service = new BasketCostCalculatorService(new[] { new ThreeForTwoDiscounterService("kiwi") }); + + // Act + var result = service.CalculateTotalCost(new Models.Basket(new Dictionary + { + { "kiwi", (quantity, 25) } + })); + + // Assert + Assert.Equal(expectedCost, result); + } } } \ No newline at end of file diff --git a/ChestersCheckout/ChestersCheckout.Core/Services/Discounting/ThreeForTwoDiscounterService.cs b/ChestersCheckout/ChestersCheckout.Core/Services/Discounting/ThreeForTwoDiscounterService.cs new file mode 100644 index 0000000..06da0cf --- /dev/null +++ b/ChestersCheckout/ChestersCheckout.Core/Services/Discounting/ThreeForTwoDiscounterService.cs @@ -0,0 +1,28 @@ +using ChestersCheckout.Core.Models; +using ChestersCheckout.Core.Services.Abstractions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ChestersCheckout.Core.Services.Discounting +{ + public class ThreeForTwoDiscounterService : IDiscounterService + { + private readonly string _productName; + + public ThreeForTwoDiscounterService(string productName) + { + _productName = productName; + } + + public int CalculateDiscount(Basket basket) + { + if (!basket.Items.TryGetValue(_productName, out var item)) + return 0; + + return item.UnitPrice * (item.Quantity / 3); + } + } +}