From bcd34524a4b983072364d42194182d1ec6a49e35 Mon Sep 17 00:00:00 2001 From: DickBaker Date: Sat, 1 Jun 2024 20:34:26 +0100 Subject: [PATCH] reworked Issue #6893 keeping existing subfolders for BillW --- .../toll-calculator/ExternalSystems-ctor.cs | 42 +++++++ .../toll-calculator/ExternalSystems-pctor.cs | 30 +++++ .../toll-calculator/ExternalSystems-record.cs | 17 +++ .../toll-calculator/ExternalSystems.cs | 14 ++- .../finished/toll-calculator/Program.cs | 92 +++++++------- .../toll-calculator/TollCalculator.cs | 119 ++++++++---------- .../toll-calculator/toll-calculator.csproj | 2 +- .../start/toll-calculator/ExternalSystems.cs | 14 ++- .../patterns/start/toll-calculator/Program.cs | 90 +++++++------ .../start/toll-calculator/TollCalculator.cs | 38 ++++++ .../toll-calculator/toll-calculator.csproj | 2 +- 11 files changed, 281 insertions(+), 179 deletions(-) create mode 100644 csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-ctor.cs create mode 100644 csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-pctor.cs create mode 100644 csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-record.cs create mode 100644 csharp/tutorials/patterns/start/toll-calculator/TollCalculator.cs diff --git a/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-ctor.cs b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-ctor.cs new file mode 100644 index 00000000000..20cb17735ac --- /dev/null +++ b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-ctor.cs @@ -0,0 +1,42 @@ +// suggest owner refactor to constructor +namespace ConsumerVehicleRegistration2 +{ + public class Car + { + public Car(int passengers) => Passengers = passengers; + + public required int Passengers { get; set; } + } +} + +namespace CommercialRegistration2 +{ + public class DeliveryTruck + { + public DeliveryTruck(int grossWeight) => GrossWeight = grossWeight; + + public required int GrossWeight { get; set; } + } +} + +namespace LiveryRegistration2 +{ + public class Taxi + { + public Taxi(int fares) => Fares = fares; + + public required int Fares { get; set; } + } + + public class Bus + { + public Bus(int riders, int capacity = 90) + { + Riders = riders; + Capacity = capacity; + } + + public required int Capacity { get; set; } + public required int Riders { get; set; } + } +} diff --git a/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-pctor.cs b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-pctor.cs new file mode 100644 index 00000000000..9986a7d07f4 --- /dev/null +++ b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-pctor.cs @@ -0,0 +1,30 @@ +// suggest owner refactor to primary constructor +namespace ConsumerVehicleRegistration3 +{ + public class Car(int passengers) + { + public required int Passengers { get; set; } = passengers; + } +} + +namespace CommercialRegistration3 +{ + public class DeliveryTruck(int grossWeight) + { + public required int GrossWeight { get; set; } = grossWeight; + } +} + +namespace LiveryRegistration3 +{ + public class Taxi(int fares) + { + public required int Fares { get; set; } = fares; + } + + public class Bus(int riders, int capacity = 90) + { + public required int Capacity { get; set; } = capacity; + public required int Riders { get; set; } = riders; + } +} diff --git a/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-record.cs b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-record.cs new file mode 100644 index 00000000000..5d1ccd34577 --- /dev/null +++ b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems-record.cs @@ -0,0 +1,17 @@ +// suggest owner refactor from class to positional record +namespace ConsumerVehicleRegistration4 +{ + public record Car(int Passengers); +} + +namespace CommercialRegistration4 +{ + public record DeliveryTruck(int GrossWeight); +} + +namespace LiveryRegistration4 +{ + public record Taxi(int Fares); + + public record Bus(int Riders, int Capacity = 90); +} diff --git a/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs index ca71e16dfbd..0016a18173e 100644 --- a/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs +++ b/csharp/tutorials/patterns/finished/toll-calculator/ExternalSystems.cs @@ -1,8 +1,10 @@ -namespace ConsumerVehicleRegistration +// you are not owner, so YOU have no control over these external namespaces or definitions +// but OWNER could refactor with constructor, or from class to positional record [examples below] +namespace ConsumerVehicleRegistration { public class Car { - public int Passengers { get; set; } + public required int Passengers { get; set; } } } @@ -10,7 +12,7 @@ namespace CommercialRegistration { public class DeliveryTruck { - public int GrossWeightClass { get; set; } + public int GrossWeight { get; set; } = 4000; // default weight (hence required omitted) } } @@ -18,12 +20,12 @@ namespace LiveryRegistration { public class Taxi { - public int Fares { get; set; } + public required int Fares { get; set; } } public class Bus { - public int Capacity { get; set; } - public int Riders { get; set; } + public required int Capacity { get; set; } + public required int Riders { get; set; } } } diff --git a/csharp/tutorials/patterns/finished/toll-calculator/Program.cs b/csharp/tutorials/patterns/finished/toll-calculator/Program.cs index cc6ff1a3169..399526b0f57 100644 --- a/csharp/tutorials/patterns/finished/toll-calculator/Program.cs +++ b/csharp/tutorials/patterns/finished/toll-calculator/Program.cs @@ -1,57 +1,53 @@ -using System; +using Calculators; using CommercialRegistration; using ConsumerVehicleRegistration; using LiveryRegistration; -using toll_calculator; - -var tollCalc = new TollCalculator(); - -var soloDriver = new Car(); +var soloDriver = new Car { Passengers = 0 }; var twoRideShare = new Car { Passengers = 1 }; var threeRideShare = new Car { Passengers = 2 }; -var fullVan = new Car { Passengers = 5 }; -var emptyTaxi = new Taxi(); +var fullCar = new Car { Passengers = 5 }; +var emptyTaxi = new Taxi { Fares = 0 }; var singleFare = new Taxi { Fares = 1 }; var doubleFare = new Taxi { Fares = 2 }; -var fullVanPool = new Taxi { Fares = 5 }; +var fullTaxi = new Taxi { Fares = 5 }; var lowOccupantBus = new Bus { Capacity = 90, Riders = 15 }; var normalBus = new Bus { Capacity = 90, Riders = 75 }; var fullBus = new Bus { Capacity = 90, Riders = 85 }; -var heavyTruck = new DeliveryTruck { GrossWeightClass = 7500 }; -var truck = new DeliveryTruck { GrossWeightClass = 4000 }; -var lightTruck = new DeliveryTruck { GrossWeightClass = 2500 }; +var heavyTruck = new DeliveryTruck { GrossWeight = 7500 }; +var truck = new DeliveryTruck(); +var lightTruck = new DeliveryTruck { GrossWeight = 2500 }; -Console.WriteLine($"The toll for a solo driver is {tollCalc.CalculateToll(soloDriver)}"); -Console.WriteLine($"The toll for a two ride share is {tollCalc.CalculateToll(twoRideShare)}"); -Console.WriteLine($"The toll for a three ride share is {tollCalc.CalculateToll(threeRideShare)}"); -Console.WriteLine($"The toll for a fullVan is {tollCalc.CalculateToll(fullVan)}"); +Console.WriteLine($"The toll for a solo driver is {TollCalculator.CalculateToll(soloDriver)}"); +Console.WriteLine($"The toll for a two ride share is {TollCalculator.CalculateToll(twoRideShare)}"); +Console.WriteLine($"The toll for a three ride share is {TollCalculator.CalculateToll(threeRideShare)}"); +Console.WriteLine($"The toll for a full car is {TollCalculator.CalculateToll(fullCar)}"); -Console.WriteLine($"The toll for an empty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); -Console.WriteLine($"The toll for a single fare taxi is {tollCalc.CalculateToll(singleFare)}"); -Console.WriteLine($"The toll for a double fare taxi is {tollCalc.CalculateToll(doubleFare)}"); -Console.WriteLine($"The toll for a full van taxi is {tollCalc.CalculateToll(fullVanPool)}"); +Console.WriteLine($"The toll for an empty taxi is {TollCalculator.CalculateToll(emptyTaxi)}"); +Console.WriteLine($"The toll for a single fare taxi is {TollCalculator.CalculateToll(singleFare)}"); +Console.WriteLine($"The toll for a double fare taxi is {TollCalculator.CalculateToll(doubleFare)}"); +Console.WriteLine($"The toll for a full taxi is {TollCalculator.CalculateToll(fullTaxi)}"); -Console.WriteLine($"The toll for a low-occupant bus is {tollCalc.CalculateToll(lowOccupantBus)}"); -Console.WriteLine($"The toll for a regular bus is {tollCalc.CalculateToll(normalBus)}"); -Console.WriteLine($"The toll for a bus is {tollCalc.CalculateToll(fullBus)}"); +Console.WriteLine($"The toll for a low-occupant bus is {TollCalculator.CalculateToll(lowOccupantBus)}"); +Console.WriteLine($"The toll for a regular bus is {TollCalculator.CalculateToll(normalBus)}"); +Console.WriteLine($"The toll for a full bus is {TollCalculator.CalculateToll(fullBus)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(heavyTruck)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(truck)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(lightTruck)}"); +Console.WriteLine($"The toll for a heavy truck is {TollCalculator.CalculateToll(heavyTruck)}"); +Console.WriteLine($"The toll for a medium truck is {TollCalculator.CalculateToll(truck)}"); +Console.WriteLine($"The toll for a light truck is {TollCalculator.CalculateToll(lightTruck)}"); try { - tollCalc.CalculateToll("this will fail"); + _ = TollCalculator.CalculateToll("this will fail"); } -catch (ArgumentException) +catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type"); + Console.WriteLine($"Caught an argument exception when using the wrong type for {e.ParamName}"); } try { - tollCalc.CalculateToll(null!); + _ = TollCalculator.CalculateToll(null!); } catch (ArgumentNullException) { @@ -62,31 +58,31 @@ var testTimes = new DateTime[] { - new DateTime(2019, 3, 4, 8, 0, 0), // morning rush - new DateTime(2019, 3, 6, 11, 30, 0), // daytime - new DateTime(2019, 3, 7, 17, 15, 0), // evening rush - new DateTime(2019, 3, 14, 03, 30, 0), // overnight + new(2019, 3, 4, 8, 0, 0), // morning rush + new(2019, 3, 6, 11, 30, 0), // daytime + new(2019, 3, 7, 17, 15, 0), // evening rush + new(2019, 3, 14, 03, 30, 0), // overnight - new DateTime(2019, 3, 16, 8, 30, 0), // weekend morning rush - new DateTime(2019, 3, 17, 14, 30, 0), // weekend daytime - new DateTime(2019, 3, 17, 18, 05, 0), // weekend evening rush - new DateTime(2019, 3, 16, 01, 30, 0), // weekend overnight + new(2019, 3, 16, 8, 30, 0), // weekend morning rush + new(2019, 3, 17, 14, 30, 0), // weekend daytime + new(2019, 3, 17, 18, 05, 0), // weekend evening rush + new(2019, 3, 16, 01, 30, 0), // weekend overnight }; -foreach (var time in testTimes) +foreach (DateTime time in testTimes) { - Console.WriteLine($"Inbound premium at {time} is {tollCalc.PeakTimePremiumIfElse(time, true)}"); - Console.WriteLine($"Outbound premium at {time} is {tollCalc.PeakTimePremiumIfElse(time, false)}"); + Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremiumIfElse(time, true)}"); + Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremiumIfElse(time, false)}"); } Console.WriteLine("===================================================="); -foreach (var time in testTimes) +foreach (DateTime time in testTimes) { - Console.WriteLine($"Inbound premium at {time} is {tollCalc.PeakTimePremiumFull(time, true)}"); - Console.WriteLine($"Outbound premium at {time} is {tollCalc.PeakTimePremiumFull(time, false)}"); + Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremiumFull(time, true)}"); + Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremiumFull(time, false)}"); } Console.WriteLine("===================================================="); -foreach (var time in testTimes) +foreach (DateTime time in testTimes) { - Console.WriteLine($"Inbound premium at {time} is {tollCalc.PeakTimePremium(time, true)}"); - Console.WriteLine($"Outbound premium at {time} is {tollCalc.PeakTimePremium(time, false)}"); -} \ No newline at end of file + Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremium(time, true)}"); + Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremium(time, false)}"); +} diff --git a/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs b/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs index 3ed8ea07d88..c247b1514ec 100644 --- a/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs +++ b/csharp/tutorials/patterns/finished/toll-calculator/TollCalculator.cs @@ -1,103 +1,82 @@ -using CommercialRegistration; +using System.Diagnostics.CodeAnalysis; +using CommercialRegistration; using ConsumerVehicleRegistration; using LiveryRegistration; -namespace toll_calculator; -public class TollCalculator +namespace Calculators; +public static class TollCalculator { - public decimal CalculateToll(object vehicle) => + const decimal CarFare = 2.00m, TaxiFare = 3.50m, BusFare = 5.00m, DeliveryTruckFare = 10.00m; + + public static decimal CalculateToll([AllowNull] object vehicle) => vehicle switch { + null => throw new ArgumentNullException(nameof(vehicle)), Car c => c.Passengers switch { - 0 => 2.00m + 0.5m, - 1 => 2.0m, - 2 => 2.0m - 0.5m, - _ => 2.00m - 1.0m + 0 => CarFare + 0.50m, + 1 => CarFare, + 2 => CarFare - 0.50m, + _ => CarFare - 1.00m }, Taxi t => t.Fares switch { - 0 => 3.50m + 1.00m, - 1 => 3.50m, - 2 => 3.50m - 0.50m, - _ => 3.50m - 1.00m + 0 => TaxiFare + 1.00m, + 1 => TaxiFare, + 2 => TaxiFare - 0.50m, + _ => TaxiFare - 1.00m }, - Bus b when ((double)b.Riders / (double)b.Capacity) < 0.50 => 5.00m + 2.00m, - Bus b when ((double)b.Riders / (double)b.Capacity) > 0.90 => 5.00m - 1.00m, - Bus b => 5.00m, + Bus b => b.Riders / (double)b.Capacity < 0.5d + ? BusFare + 2.00m + : b.Riders / (double)b.Capacity > 0.9d + ? BusFare - 1.00m + : BusFare, - DeliveryTruck t when (t.GrossWeightClass > 5000) => 10.00m + 5.00m, - DeliveryTruck t when (t.GrossWeightClass < 3000) => 10.00m - 2.00m, - DeliveryTruck t => 10.00m, + DeliveryTruck t => t.GrossWeight > 5000 + ? DeliveryTruckFare + 3.50m + : t.GrossWeight < 3000 + ? DeliveryTruckFare - 2.00m + : DeliveryTruckFare, - { } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)), - null => throw new ArgumentNullException(nameof(vehicle)) + //{ } => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)), + _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)) }; - // - public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => - (IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch - { - (true, TimeBand.Overnight, _) => 0.75m, - (true, TimeBand.Daytime, _) => 1.5m, - (true, TimeBand.MorningRush, true) => 2.0m, - (true, TimeBand.EveningRush, false) => 2.0m, - (_, _, _) => 1.0m, - }; + // + public static decimal PeakTimePremium(DateTime timeOfToll, bool inbound) => + (IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch + { + (true, TimeBand.Overnight, _) => 0.75m, + (true, TimeBand.Daytime, _) => 1.5m, + (true, TimeBand.MorningRush, true) => 2.0m, + (true, TimeBand.EveningRush, false) => 2.0m, + _ => 1.0m + }; // // - public decimal PeakTimePremiumIfElse(DateTime timeOfToll, bool inbound) + public static decimal PeakTimePremiumIfElse(DateTime timeOfToll, bool inbound) { if ((timeOfToll.DayOfWeek == DayOfWeek.Saturday) || (timeOfToll.DayOfWeek == DayOfWeek.Sunday)) { return 1.0m; } - else - { - int hour = timeOfToll.Hour; - if (hour < 6) - { - return 0.75m; - } else if (hour < 10) - { - if (inbound) - { - return 2.0m; - } - else - { - return 1.0m; - } - } - else if (hour < 16) - { - return 1.5m; - } - else if (hour < 20) - { - if (inbound) - { - return 1.0m; - } - else - { - return 2.0m; - } - } - else // Overnight - { - return 0.75m; - } - } + + int hour = timeOfToll.Hour; + if (hour < 6) { return 0.75m; } + if (hour < 10) { return inbound ? 2.0m : 1.0m; } + if (hour < 16) { return 1.5m; } + if (hour < 20) { return inbound ? 1.0m : 2.0m; } + // Overnight + return 0.75m; } // // - public decimal PeakTimePremiumFull(DateTime timeOfToll, bool inbound) => + public static decimal PeakTimePremiumFull(DateTime timeOfToll, bool inbound) => (IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch { (true, TimeBand.MorningRush, true) => 2.00m, @@ -141,7 +120,7 @@ private enum TimeBand private static TimeBand GetTimeBand(DateTime timeOfToll) => timeOfToll.Hour switch { - < 6 or > 19 => TimeBand.Overnight, + < 6 or >= 20 => TimeBand.Overnight, < 10 => TimeBand.MorningRush, < 16 => TimeBand.Daytime, _ => TimeBand.EveningRush, diff --git a/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj b/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj index c9483503662..7e159c16867 100644 --- a/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj +++ b/csharp/tutorials/patterns/finished/toll-calculator/toll-calculator.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 toll_calculator enable enable diff --git a/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs b/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs index ca71e16dfbd..f00fe3a2dd0 100644 --- a/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs +++ b/csharp/tutorials/patterns/start/toll-calculator/ExternalSystems.cs @@ -1,8 +1,10 @@ -namespace ConsumerVehicleRegistration +// you are not owner, so YOU have no control over these external namespaces or definitions +// but OWNER could refactor with [primary]constructor, or from class to positional record [see ExternalSystems-*.cs examples] +namespace ConsumerVehicleRegistration { public class Car { - public int Passengers { get; set; } + public required int Passengers { get; set; } } } @@ -10,7 +12,7 @@ namespace CommercialRegistration { public class DeliveryTruck { - public int GrossWeightClass { get; set; } + public int GrossWeight { get; set; } = 4000; // default weight (hence required omitted) } } @@ -18,12 +20,12 @@ namespace LiveryRegistration { public class Taxi { - public int Fares { get; set; } + public required int Fares { get; set; } } public class Bus { - public int Capacity { get; set; } - public int Riders { get; set; } + public required int Capacity { get; set; } + public required int Riders { get; set; } } } diff --git a/csharp/tutorials/patterns/start/toll-calculator/Program.cs b/csharp/tutorials/patterns/start/toll-calculator/Program.cs index d2310bdafbd..22a5e610b2e 100644 --- a/csharp/tutorials/patterns/start/toll-calculator/Program.cs +++ b/csharp/tutorials/patterns/start/toll-calculator/Program.cs @@ -1,41 +1,37 @@ -using System; +using Calculators; using CommercialRegistration; using ConsumerVehicleRegistration; using LiveryRegistration; -var car = new Car(); -var taxi = new Taxi(); -var bus = new Bus(); -var truck = new DeliveryTruck(); +// "Implement the basic toll calculations" +var car = new Car() { Passengers = 0 }; // suppose driver only +var taxi = new Taxi() { Fares = 0 }; // not yet hired +var bus = new Bus() { Riders = 0, Capacity = 90 }; // suppose driver only +var truck = new DeliveryTruck(); // medium truck (note default GrossWeight = 4000) -/* First set of test code -var tollCalc = new toll_calculator.TollCalculator(); - -Console.WriteLine($"The toll for a car is {tollCalc.CalculateToll(car)}"); -Console.WriteLine($"The toll for a taxi is {tollCalc.CalculateToll(taxi)}"); -Console.WriteLine($"The toll for a bus is {tollCalc.CalculateToll(bus)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(truck)}"); +Console.WriteLine($"The toll for a car is {TollCalculator.CalculateToll(car)}"); +Console.WriteLine($"The toll for a taxi is {TollCalculator.CalculateToll(taxi)}"); +Console.WriteLine($"The toll for a bus is {TollCalculator.CalculateToll(bus)}"); +Console.WriteLine($"The toll for a truck is {TollCalculator.CalculateToll(truck)}"); try { - tollCalc.CalculateToll("this will fail"); + _ = TollCalculator.CalculateToll("this will fail"); } catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type"); + Console.WriteLine($"Caught an argument exception when using the wrong type for {e.ParamName}"); } try { - tollCalc.CalculateToll(null!); + _ = TollCalculator.CalculateToll(null!); } -catch (ArgumentNullException e) +catch (ArgumentNullException) { Console.WriteLine("Caught an argument exception when using null"); } -*/ -/* 2nd test (after adding for occupants) -var tollCalc = new TollCalculator(); +/* // "Add occupancy pricing" var soloDriver = new Car(); var twoRideShare = new Car { Passengers = 1 }; @@ -49,47 +45,47 @@ var normalBus = new Bus { Capacity = 90, Riders = 75 }; var fullBus = new Bus { Capacity = 90, Riders = 85 }; -var heavyTruck = new DeliveryTruck { GrossWeightClass = 7500 }; -var truck = new DeliveryTruck { GrossWeightClass = 4000 }; -var lightTruck = new DeliveryTruck { GrossWeightClass = 2500 }; +var heavyTruck = new DeliveryTruck { GrossWeight = 7500 }; +var truck = new DeliveryTruck(); // default GrossWeight = 4000 +var lightTruck = new DeliveryTruck { GrossWeight = 2500 }; -Console.WriteLine($"The toll for a solo driver is {tollCalc.CalculateToll(soloDriver)}"); -Console.WriteLine($"The toll for a two ride share is {tollCalc.CalculateToll(twoRideShare)}"); -Console.WriteLine($"The toll for a three ride share is {tollCalc.CalculateToll(threeRideShare)}"); -Console.WriteLine($"The toll for a fullVan is {tollCalc.CalculateToll(fullVan)}"); +Console.WriteLine($"The toll for a solo driver is {TollCalculator.CalculateToll(soloDriver)}"); +Console.WriteLine($"The toll for a two ride share is {TollCalculator.CalculateToll(twoRideShare)}"); +Console.WriteLine($"The toll for a three ride share is {TollCalculator.CalculateToll(threeRideShare)}"); +Console.WriteLine($"The toll for a full car is {TollCalculator.CalculateToll(fullVan)}"); -Console.WriteLine($"The toll for an empty taxi is {tollCalc.CalculateToll(emptyTaxi)}"); -Console.WriteLine($"The toll for a single fare taxi is {tollCalc.CalculateToll(singleFare)}"); -Console.WriteLine($"The toll for a double fare taxi is {tollCalc.CalculateToll(doubleFare)}"); -Console.WriteLine($"The toll for a full van taxi is {tollCalc.CalculateToll(fullVanPool)}"); +Console.WriteLine($"The toll for an empty taxi is {TollCalculator.CalculateToll(emptyTaxi)}"); +Console.WriteLine($"The toll for a single fare taxi is {TollCalculator.CalculateToll(singleFare)}"); +Console.WriteLine($"The toll for a double fare taxi is {TollCalculator.CalculateToll(doubleFare)}"); +Console.WriteLine($"The toll for a full taxi is {TollCalculator.CalculateToll(fullVanPool)}"); -Console.WriteLine($"The toll for a low-occupant bus is {tollCalc.CalculateToll(lowOccupantBus)}"); -Console.WriteLine($"The toll for a regular bus is {tollCalc.CalculateToll(normalBus)}"); -Console.WriteLine($"The toll for a bus is {tollCalc.CalculateToll(fullBus)}"); +Console.WriteLine($"The toll for a low-occupant bus is {TollCalculator.CalculateToll(lowOccupantBus)}"); +Console.WriteLine($"The toll for a regular bus is {TollCalculator.CalculateToll(normalBus)}"); +Console.WriteLine($"The toll for a bus is {TollCalculator.CalculateToll(fullBus)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(heavyTruck)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(truck)}"); -Console.WriteLine($"The toll for a truck is {tollCalc.CalculateToll(lightTruck)}"); +Console.WriteLine($"The toll for a heavy truck is {TollCalculator.CalculateToll(heavyTruck)}"); +Console.WriteLine($"The toll for a medium truck is {TollCalculator.CalculateToll(truck)}"); +Console.WriteLine($"The toll for a light truck is {TollCalculator.CalculateToll(lightTruck)}"); try { - tollCalc.CalculateToll("this will fail"); + _ = TollCalculator.CalculateToll("this will fail"); } catch (ArgumentException e) { - Console.WriteLine("Caught an argument exception when using the wrong type"); + Console.WriteLine($"Caught an argument exception when using the wrong type for {e.ParamName}"); } try { - tollCalc.CalculateToll(null); + _ = TollCalculator.CalculateToll(null!); } -catch (ArgumentNullException e) +catch (ArgumentNullException) { Console.WriteLine("Caught an argument exception when using null"); } -*/ +*/ // end of "Add occupancy pricing" -/* +/* // start of "Add peak pricing" Console.WriteLine("Testing the time premiums"); var testTimes = new DateTime[] @@ -107,13 +103,13 @@ foreach (var time in testTimes) { - Console.WriteLine($"Inbound premium at {time} is {tollCalc.PeakTimePremiumFull(time, true)}"); - Console.WriteLine($"Outbound premium at {time} is {tollCalc.PeakTimePremiumFull(time, false)}"); + Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremiumFull(time, true)}"); + Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremiumFull(time, false)}"); } Console.WriteLine("===================================================="); foreach (var time in testTimes) { - Console.WriteLine($"Inbound premium at {time} is {tollCalc.PeakTimePremium(time, true)}"); - Console.WriteLine($"Outbound premium at {time} is {tollCalc.PeakTimePremium(time, false)}"); + Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremium(time, true)}"); + Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremium(time, false)}"); } -*/ \ No newline at end of file +*/ // end of "Add peak pricing" diff --git a/csharp/tutorials/patterns/start/toll-calculator/TollCalculator.cs b/csharp/tutorials/patterns/start/toll-calculator/TollCalculator.cs new file mode 100644 index 00000000000..2f599c37c9d --- /dev/null +++ b/csharp/tutorials/patterns/start/toll-calculator/TollCalculator.cs @@ -0,0 +1,38 @@ +using CommercialRegistration; +using ConsumerVehicleRegistration; +using LiveryRegistration; + +namespace Calculators; + +public static class TollCalculator +{ + const decimal CarFare = 2.00m, TaxiFare = 3.50m, BusFare = 5.00m, DeliveryTruckFare = 10.00m; + + /// + /// simplest pure function as placeholder to make the "First set of test code" compile + /// + /// specify subject vehicle instance + /// decimal toll fare based on conditions YOU will write (hint: see below) + /// + public static decimal CalculateToll(object vehicle) => + throw new NotImplementedException("no free lunch here"); + +/* + /// + /// simple pure function to return toll based on crude vehicle type + /// + /// specify subject vehicle instance + /// decimal toll fare based on primitive vehicle type + /// so far c,t,b,t could be _ discard, but keep reading the docs! + /// + public static decimal CalculateToll(object vehicle) => + vehicle switch + { + Car c => CarFare, + Taxi t => TaxiFare, + Bus b => BusFare, + DeliveryTruck t => DeliveryTruckFare, + _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)), + }; +*/ +} diff --git a/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj b/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj index c9483503662..7e159c16867 100644 --- a/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj +++ b/csharp/tutorials/patterns/start/toll-calculator/toll-calculator.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 toll_calculator enable enable