public void Given12InchesAdd2FeetShouldEqal3Feet() { var twelveInches = new Quantity(12, LengthUnit.INCH); var oneFeet = new Quantity(2, LengthUnit.FEET); var threeFeet = new Quantity(3, LengthUnit.FEET); Assert.AreEqual(threeFeet, twelveInches.Add(oneFeet)); }
public Quantity Add(Quantity quantity) { if (IsUnitTypeDifferentFrom(quantity)) throw new ArgumentException(); var thisAmountFactor = UnitConversionFactor.Get(unit); var otherAmountFactor = UnitConversionFactor.Get(quantity.unit); return new Quantity(thisAmountFactor*amount+otherAmountFactor*quantity.amount, LengthUnit.INCH); }
public bool Equals(Quantity other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; if (IsUnitTypeDifferentFrom(other)) return false; var thisAmountFactor = UnitConversionFactor.Get(unit); var otherAmountFactor = UnitConversionFactor.Get(other.unit); return thisAmountFactor*amount == other.amount*otherAmountFactor; }
public void ShouldThrowExceptionWhenAddWithDifferentUnits() { var twelveInches = new Quantity(12, LengthUnit.INCH); var twoTBSP = new Quantity(2, VolumeUnit.TBSP); twelveInches.Add(twoTBSP); }
private bool IsUnitTypeDifferentFrom(Quantity quantity) { return unit.GetType().Name != quantity.unit.GetType().Name; }