ex-int-04-shipping-cost/ShippingCosts/Implementations/ShippingCostCalculatorBase.cs
2025-05-09 10:33:29 +02:00

35 lines
1.1 KiB
C#

using ShippingCosts.Contracts;
namespace ShippingCosts.Implementations;
/// <summary>
/// A base class for different shipping carriers
/// </summary>
public abstract class ShippingCostCalculatorBase(ICountryDistanceProvider countryDistanceProvider)
: IShippingCostCalculator
{
protected ICountryDistanceProvider CountryDistanceProvider { get; } = countryDistanceProvider;
public abstract string CarrierName { get; }
public abstract decimal? CalculateShippingCosts(string targetCountry, IMeasuredBox box);
/// <summary>
/// Calculates the sum of the longest and shortest side of a box
/// </summary>
/// <param name="box">Dimensions of the box</param>
/// <returns>Sum of the longest and shortest side</returns>
protected static int SumOfLongestAndShortestSides(IMeasuredBox box)
{
int[] sides = [box.Width, box.Height, box.Depth];
for (int i = 0; i < sides.Length; i++)
{
if (sides[i] <= 0)
{
return 0;
}
}
Array.Sort(sides);
return sides[0] + sides[2];
}
}