Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reworked Issue #6893 keeping existing subfolders for BillW #6938

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
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; }
}
}

namespace CommercialRegistration
{
public class DeliveryTruck
{
public int GrossWeightClass { get; set; }
public int GrossWeight { get; set; } = 4000; // default weight (hence required omitted)
}
}

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; }
}
}
92 changes: 44 additions & 48 deletions csharp/tutorials/patterns/finished/toll-calculator/Program.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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)}");
}
Console.WriteLine($"Inbound premium at {time} is {TollCalculator.PeakTimePremium(time, true)}");
Console.WriteLine($"Outbound premium at {time} is {TollCalculator.PeakTimePremium(time, false)}");
}
Loading
Loading