Exemplo n.º 1
0
        public void AnnualPowerUsage_TariffWithNoGas_ReturnsNothing()
        {
            // Arrange
            var tariff = new Tariff("test", null, null, 3m);

            // Act
            var actual = FuelCalculator.AnnualPowerUsage(tariff, TaxedValue.FromPreTaxValue(100, x => x));

            // Assert
            actual.HasValue.Should().BeFalse();
        }
Exemplo n.º 2
0
        public void AnnualPowerUsage_TariffWithGas_ReturnsUsage()
        {
            // Arrange
            var tariff = new Tariff("test", 2m, null, 3m);

            // Act
            var actual = FuelCalculator.AnnualPowerUsage(tariff, TaxedValue.FromPreTaxValue(100, x => x));

            // Assert
            actual.HasValue.Should().BeTrue();
            actual.Value.Value.Should().Be((100 - 3) * 12 * 2);
        }
Exemplo n.º 3
0
        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());
            });
        }