Skip to content

Commit

Permalink
Add 3 for 2 on oranges
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Chester committed Nov 26, 2021
1 parent aba4d24 commit e401f34
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
7 changes: 4 additions & 3 deletions ChestersCheckout/ChestersCheckout.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ private static IHostBuilder CreateHostBuilder(string[] args)
private static void ConfigureServices(HostBuilderContext builder, IServiceCollection services)
{
services.AddScoped<IProductRepositoryService, StaticProductRepositoryService>();
services.AddScoped<IEnumerable<IDiscounterService>>(sp => new IDiscounterService[]
{ new BogofDiscounterService("apple") }
);
services.AddScoped<IEnumerable<IDiscounterService>>(sp => new IDiscounterService[] {
new BogofDiscounterService("apple"),
new ThreeForTwoDiscounterService("orange")
});
services.AddScoped<BasketBuilderService>();
services.AddScoped<BasketCostCalculatorService>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") });
Expand All @@ -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<string, (int Quantity, int UnitPrice)>
{
{ "kiwi", (quantity, 25) }
}));

// Assert
Assert.Equal(expectedCost, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit e401f34

Please sign in to comment.