Files
Your Name ac45255a80 Slice with lambda: add C# exercise projects and export
- Add SliceWithLambda.slnx + csproj scaffolding for ideas/slice-with-lambda/
  exercise/csharp (examples project + xUnit solution/test project)
- Add exercises/slice-with-lambda (C# version of the prefactored-longrange
  exercise with Buyers and SliceWithLambda projects)
- Add export/slice-with-lambda (nested git repo with exercise export)
- Ignore bin/ and obj/ build output at repo root
2026-09-30 00:01:12 +00:00

49 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
namespace IceCreamBuyers
{
/// <summary>
/// Reference end state of exercise 3 (extract lambda, slide, extract method
/// with a function parameter — the pure rep of Slice with Lambda).
/// </summary>
public class Buyers
{
private readonly PriceList priceList = new();
private readonly List<Buyer> buyers = new()
{
new("Anke", new List<IceCream> { IceCream.Strawberry, IceCream.Vanilla }),
new("Bram", new List<IceCream> { IceCream.Chocolate }),
new("Celine", new List<IceCream> { IceCream.Vanilla, IceCream.Chocolate }),
};
/// <summary>
/// No-arg overload: callers unchanged, the "real" pricing function baked in.
/// </summary>
public int ShoppingListTotal(string buyerName)
{
return ShoppingListTotal(ByName(buyerName), priceList.PriceFor);
}
/// <summary>
/// The seam: the pricing source is injected as a function.
/// Testable: pass any pure function instead of calling the pricing service.
/// </summary>
public int ShoppingListTotal(Buyer buyer, Func<IceCream, int> priceFor)
{
var total = 0;
foreach (var flavour in buyer.Favourites)
{
total += buyer.ScoopsOf(flavour) * priceFor(flavour);
}
return total;
}
private Buyer ByName(string name)
{
return buyers.Find(b => b.Name == name)
?? throw new InvalidOperationException($"Unknown buyer {name}.");
}
}
}