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