-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* UnitsSystem introduced * UPS provider to detect units system * PackageKgCm
- Loading branch information
1 parent
442cc56
commit 7b2e779
Showing
24 changed files
with
708 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using NUnit.Framework; | ||
using ShippingRates.Models; | ||
|
||
namespace ShippingRates.Tests.Units | ||
{ | ||
[TestFixture] | ||
public class PackageWeightTests | ||
{ | ||
[Test()] | ||
public void LbsToKgs() | ||
{ | ||
var weight1 = new PackageWeight(UnitsSystem.USCustomary, 5); | ||
Assert.AreEqual(5, weight1.Get()); | ||
Assert.AreEqual(5, weight1.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(2.26796185m, weight1.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(5, weight1.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(3, weight1.GetRounded(UnitsSystem.Metric)); | ||
|
||
var weight2 = new PackageWeight(UnitsSystem.USCustomary, 0.3m); | ||
Assert.AreEqual(0.3m, weight2.Get()); | ||
Assert.AreEqual(0.3m, weight2.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(0.136077711m, weight2.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.Metric)); | ||
} | ||
|
||
[Test()] | ||
public void KgsToLbs() | ||
{ | ||
var weight1 = new PackageWeight(UnitsSystem.Metric, 5); | ||
Assert.AreEqual(5, weight1.Get()); | ||
Assert.AreEqual(11.0231m, weight1.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(5, weight1.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(12, weight1.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(5, weight1.GetRounded(UnitsSystem.Metric)); | ||
|
||
var weight2 = new PackageWeight(UnitsSystem.Metric, 0.5m); | ||
Assert.AreEqual(0.5m, weight2.Get()); | ||
Assert.AreEqual(1.10231m, weight2.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(0.5m, weight2.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(2, weight2.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(1, weight2.GetRounded(UnitsSystem.Metric)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using NUnit.Framework; | ||
using ShippingRates.Models; | ||
|
||
namespace ShippingRates.Tests.Units | ||
{ | ||
[TestFixture] | ||
public class PackageKgCmTests | ||
{ | ||
[Test()] | ||
public void TestDimensions() | ||
{ | ||
var packageLbsInches = new Package(10, 20, 30, 40, 50); | ||
var packageKgCm = new PackageKgCm(10, 20, 30, 40, 50); | ||
|
||
Assert.AreNotEqual(packageKgCm.GetHeight(UnitsSystem.Metric), packageLbsInches.GetHeight(UnitsSystem.Metric)); | ||
Assert.AreNotEqual(packageKgCm.GetLength(UnitsSystem.Metric), packageLbsInches.GetLength(UnitsSystem.Metric)); | ||
Assert.AreNotEqual(packageKgCm.GetWidth(UnitsSystem.USCustomary), packageLbsInches.GetWidth(UnitsSystem.USCustomary)); | ||
Assert.AreNotEqual(packageKgCm.GetHeight(UnitsSystem.USCustomary), packageLbsInches.GetHeight(UnitsSystem.USCustomary)); | ||
|
||
Assert.AreEqual(40, packageLbsInches.GetWeight(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(18.1436948m, packageLbsInches.GetWeight(UnitsSystem.Metric)); | ||
Assert.AreEqual(88.1848m, packageKgCm.GetWeight(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(40, packageKgCm.GetWeight(UnitsSystem.Metric)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using NUnit.Framework; | ||
using ShippingRates.Models; | ||
|
||
namespace ShippingRates.Tests.Units | ||
{ | ||
[TestFixture] | ||
public class PackageDimensionTests | ||
{ | ||
[Test()] | ||
public void InchesToCm() | ||
{ | ||
var dimension1 = new PackageDimension(UnitsSystem.USCustomary, 5); | ||
Assert.AreEqual(5, dimension1.Get()); | ||
Assert.AreEqual(5, dimension1.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(12.7m, dimension1.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(5, dimension1.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(13, dimension1.GetRounded(UnitsSystem.Metric)); | ||
|
||
var dimension2 = new PackageDimension(UnitsSystem.USCustomary, 0.4m); | ||
Assert.AreEqual(0.4m, dimension2.Get()); | ||
Assert.AreEqual(0.4m, dimension2.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(1.016m, dimension2.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(1, dimension2.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(2, dimension2.GetRounded(UnitsSystem.Metric)); | ||
} | ||
|
||
[Test()] | ||
public void CmToInches() | ||
{ | ||
var dimension1 = new PackageDimension(UnitsSystem.Metric, 6); | ||
Assert.AreEqual(6, dimension1.Get()); | ||
Assert.AreEqual(2.362206m, dimension1.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(6, dimension1.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(3, dimension1.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(6, dimension1.GetRounded(UnitsSystem.Metric)); | ||
|
||
var dimension2 = new PackageDimension(UnitsSystem.Metric, 2.6m); | ||
Assert.AreEqual(2.6m, dimension2.Get()); | ||
Assert.AreEqual(1.0236226m, dimension2.Get(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(2.6m, dimension2.Get(UnitsSystem.Metric)); | ||
Assert.AreEqual(2, dimension2.GetRounded(UnitsSystem.USCustomary)); | ||
Assert.AreEqual(3, dimension2.GetRounded(UnitsSystem.Metric)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ShippingRates.Models | ||
{ | ||
internal class PackageDimension | ||
{ | ||
readonly UnitsSystem _unitsSystem; | ||
readonly decimal _value; | ||
|
||
public PackageDimension(UnitsSystem unitsSystem, decimal value) | ||
{ | ||
_unitsSystem = unitsSystem; | ||
_value = value; | ||
} | ||
|
||
public decimal Get() => _value; | ||
|
||
public decimal Get(UnitsSystem unitsSystem) | ||
{ | ||
if (unitsSystem == _unitsSystem) | ||
{ | ||
return _value; | ||
} | ||
else if (unitsSystem == UnitsSystem.Metric && _unitsSystem == UnitsSystem.USCustomary) | ||
{ | ||
return _value * 2.54m; | ||
} | ||
else if (unitsSystem == UnitsSystem.USCustomary && _unitsSystem == UnitsSystem.Metric) | ||
{ | ||
return _value * 0.393701m; | ||
} | ||
throw new Exception($"Unsupported size conversion from {_unitsSystem} to {unitsSystem}"); | ||
} | ||
|
||
public decimal GetRounded(UnitsSystem unitsSystem) => Math.Ceiling(Get(unitsSystem)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
namespace ShippingRates.Models | ||
{ | ||
/// <summary> | ||
/// Package object with dimensions in kgs and cm | ||
/// </summary> | ||
public class PackageKgCm : Package | ||
{ | ||
/// <summary> | ||
/// Creates a new package object with dimensions in kgs and cm | ||
/// </summary> | ||
/// <param name="length">The length of the package, in cm.</param> | ||
/// <param name="width">The width of the package, in cm.</param> | ||
/// <param name="height">The height of the package, in cm.</param> | ||
/// <param name="weight">The weight of the package, in kgs.</param> | ||
/// <param name="insuredValue">The insured-value of the package, in dollars.</param> | ||
/// <param name="container">A specific packaging from a shipping provider. E.g. "LG FLAT RATE BOX" for USPS</param> | ||
/// <param name="signatureRequiredOnDelivery">If true, will attempt to send this to the appropriate rate provider.</param> | ||
public PackageKgCm(int length, int width, int height, int weight, decimal insuredValue, string container = null, bool signatureRequiredOnDelivery = false) | ||
: this(length, width, height, (decimal)weight, insuredValue, container, signatureRequiredOnDelivery) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new package object with dimensions in kgs and cm | ||
/// </summary> | ||
/// <param name="length">The length of the package, in cm.</param> | ||
/// <param name="width">The width of the package, in cm.</param> | ||
/// <param name="height">The height of the package, in cm.</param> | ||
/// <param name="weight">The weight of the package, in kgs.</param> | ||
/// <param name="insuredValue">The insured-value of the package, in dollars.</param> | ||
/// <param name="container">A specific packaging from a shipping provider. E.g. "LG FLAT RATE BOX" for USPS</param> | ||
/// <param name="signatureRequiredOnDelivery">If true, will attempt to send this to the appropriate rate provider.</param> | ||
public PackageKgCm(decimal length, decimal width, decimal height, decimal weight, decimal insuredValue, string container = null, bool signatureRequiredOnDelivery = false) | ||
: base(UnitsSystem.Metric, length, width, height, weight) | ||
|
||
{ | ||
InsuredValue = insuredValue; | ||
Container = container; | ||
SignatureRequiredOnDelivery = signatureRequiredOnDelivery; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
|
||
namespace ShippingRates.Models | ||
{ | ||
internal class PackageWeight | ||
{ | ||
readonly UnitsSystem _unitsSystem; | ||
readonly decimal _value; | ||
|
||
public PackageWeight(UnitsSystem unitsSystem, decimal value) | ||
{ | ||
_unitsSystem = unitsSystem; | ||
_value = value; | ||
} | ||
|
||
public decimal Get() => _value; | ||
|
||
public decimal Get(UnitsSystem unitsSystem) | ||
{ | ||
if (unitsSystem == _unitsSystem) | ||
{ | ||
return _value; | ||
} | ||
else if (unitsSystem == UnitsSystem.Metric && _unitsSystem == UnitsSystem.USCustomary) | ||
{ | ||
return _value * 0.45359237m; | ||
} | ||
else if (unitsSystem == UnitsSystem.USCustomary && _unitsSystem == UnitsSystem.Metric) | ||
{ | ||
return _value * 2.20462m; | ||
} | ||
throw new Exception($"Unsupported weight conversion from {_unitsSystem} to {unitsSystem}"); | ||
} | ||
|
||
public decimal GetRounded(UnitsSystem unitsSystem) => Math.Ceiling(Get(unitsSystem)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.