public static void Main()
decimal basePrice = 5.00m;
var californiaBottle = new Bottle(new CaliforniaTaxStrategy());
var priceCA = californiaBottle.GetFinalPrice(basePrice, volume, sugar);
Console.WriteLine($"California: {priceCA:F2} USD");
var oregonBottle = new Bottle(new OregonTaxStrategy());
var priceOR = oregonBottle.GetFinalPrice(basePrice, volume, sugar);
Console.WriteLine($"Oregon: {priceOR:F2} USD");
var newYorkBottle = new Bottle(new NewYorkTaxStrategy());
var priceNY = newYorkBottle.GetFinalPrice(basePrice, volume, sugar);
Console.WriteLine($"New York: {priceNY:F2} USD");
var polandBottle = new Bottle(new PolandTaxStrategy());
var pricePL = polandBottle.GetFinalPrice(basePrice, volume, sugar);
Console.WriteLine($"Poland: {pricePL:F2} PLN");
var germanyBottle = new Bottle(new GermanyTaxStrategy());
var priceDE = germanyBottle.GetFinalPrice(basePrice, volume, sugar);
Console.WriteLine($"Germany: {priceDE:F2} €");
public interface ITaxStrategy
decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml);
public class CaliforniaTaxStrategy : ITaxStrategy
private const decimal SodaTaxPerLiter = 0.34m;
private const decimal SalesTaxRate = 0.075m;
public decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
decimal price = basePricePerLiter * volumeLiters;
if (sugarGramsPer100ml > 0)
decimal sodaTax = SodaTaxPerLiter * volumeLiters;
price *= 1 + SalesTaxRate;
public class OregonTaxStrategy : ITaxStrategy
public decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
decimal price = basePricePerLiter * volumeLiters;
public class NewYorkTaxStrategy : ITaxStrategy
private const decimal SalesTaxRate = 0.08875m;
public decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
decimal price = basePricePerLiter * volumeLiters;
price *= 1 + SalesTaxRate;
public class PolandTaxStrategy : ITaxStrategy
public decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
decimal price = basePricePerLiter * volumeLiters;
decimal sugarTaxPerLiter = 0.50m;
if (sugarGramsPer100ml > 5)
var extraSugar = sugarGramsPer100ml - 5;
sugarTaxPerLiter += extraSugar * 0.05m;
decimal sugarTax = sugarTaxPerLiter * volumeLiters;
public class GermanyTaxStrategy : ITaxStrategy
public decimal CalculateFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
decimal price = basePricePerLiter * volumeLiters;
private readonly ITaxStrategy _taxStrategy;
public Bottle(ITaxStrategy taxStrategy)
_taxStrategy = taxStrategy;
public decimal GetFinalPrice(decimal basePricePerLiter, decimal volumeLiters, decimal sugarGramsPer100ml)
return _taxStrategy.CalculateFinalPrice(basePricePerLiter, volumeLiters, sugarGramsPer100ml);