public CostCommand(ITariffSource source) { Spec = CommandSpec.Named( "cost", new[] { "POWER_USAGE_KWH", "GAS_USAGE_KWH" }, ctx => { // model 0's explicitly as no fuel supply for that type var p = ctx.GetRequiredDecimal("POWER_USAGE_KWH"); var powerKwh = p == 0 ? new Kwh <Power>?() : new Kwh <Power>(p); var g = ctx.GetRequiredDecimal("GAS_USAGE_KWH"); var gasKwh = g == 0 ? new Kwh <Gas>?() : new Kwh <Gas>(g); // calculate the costs and output foreach (var line in FuelCalculator .CostsPerTariff(source.GetAll(), powerKwh, gasKwh) .Select(cost => $"{cost.Tariff.Name} {cost.Total.PostTax:F2}")) { ctx.Output.WriteLine(line); } return(Commands.Ok()); }); }
public UsageCommand(ITariffSource source) { Spec = CommandSpec.Named( "usage", new[] { "TARIFF_NAME", "FUEL_TYPE", "TARGET_MONTHLY_SPEND" }, ctx => { var tariffName = ctx.GetRequiredString("TARIFF_NAME"); var fuelType = ctx.GetRequiredEnum <FuelType>("FUEL_TYPE"); var monthlyBudget = TaxedValue.FromPostTaxValue( ctx.GetRequiredDecimal("TARGET_MONTHLY_SPEND"), TaxHelper.RemoveTax); if (!source.TryGet(tariffName, out var tariff)) { return(Commands.Error($"The specified tariff doesn't exist '{tariffName}'.")); } // c# has no proper pattern matching, so I've run out of functional luck here, apologies for the switch statement... switch (fuelType) { case FuelType.Gas: if (!tariff.GasRate.HasValue) { return(Commands.Error($"Tariff '{tariff.Name}' does not include {FuelType.Gas}.")); } ctx.Output.WriteLine( FuelCalculator.AnnualGasUsage(tariff, monthlyBudget).GetValueOrDefault().Value); // just output 0 if tariff doesn't include fuel type break; case FuelType.Power: if (!tariff.PowerRate.HasValue) { return(Commands.Error($"Tariff '{tariff.Name}' does not include {FuelType.Power}.")); } ctx.Output.WriteLine( FuelCalculator.AnnualPowerUsage(tariff, monthlyBudget).GetValueOrDefault().Value); break; default: return(Commands.Error($"Unsupported fuel type : {fuelType}")); } return(Commands.Ok()); }); }