예제 #1
0
 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));
 }
예제 #2
0
        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);
        }
예제 #3
0
        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;
        }
예제 #4
0
 public void ShouldThrowExceptionWhenAddWithDifferentUnits()
 {
     var twelveInches = new Quantity(12, LengthUnit.INCH);
     var twoTBSP = new Quantity(2, VolumeUnit.TBSP);
     twelveInches.Add(twoTBSP);
 }
예제 #5
0
 private bool IsUnitTypeDifferentFrom(Quantity quantity)
 {
     return unit.GetType().Name != quantity.unit.GetType().Name;
 }