using System;
using System.Collections.Generic;
namespace IceCreamBuyers
{
///
/// Reference end state of exercise 3 (extract lambda, slide, extract method
/// with a function parameter — the pure rep of Slice with Lambda).
///
public class Buyers
{
private readonly PriceList priceList = new();
private readonly List buyers = new()
{
new("Anke", new List { IceCream.Strawberry, IceCream.Vanilla }),
new("Bram", new List { IceCream.Chocolate }),
new("Celine", new List { IceCream.Vanilla, IceCream.Chocolate }),
};
///
/// No-arg overload: callers unchanged, the "real" pricing function baked in.
///
public int ShoppingListTotal(string buyerName)
{
return ShoppingListTotal(ByName(buyerName), priceList.PriceFor);
}
///
/// The seam: the pricing source is injected as a function.
/// Testable: pass any pure function instead of calling the pricing service.
///
public int ShoppingListTotal(Buyer buyer, Func 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}.");
}
}
}