Esempio n. 1
0
        public static Amount operator +(Amount a1, Amount a2)
        {
            if (a1.Unit == a2.Unit)                             //Just add
            {
                if (a1.SizeLow.HasValue && a2.SizeLow.HasValue) //Combine the lows, combine the highs
                {
                    return(new Amount(a1.SizeLow + a2.SizeLow, a1.SizeHigh + a2.SizeHigh, a1.Unit));
                }

                if (a1.SizeLow.HasValue) //(1-2) + 1 = (2-3)
                {
                    return(new Amount(a1.SizeLow + a2.SizeHigh, a1.SizeHigh + a2.SizeHigh, a1.Unit));
                }

                if (a2.SizeLow.HasValue) //1 + (1-2) = (2-3)
                {
                    return(new Amount(a1.SizeHigh + a2.SizeLow, a1.SizeHigh + a2.SizeHigh, a1.Unit));
                }

                //just combine the highs
                return(new Amount(a1.SizeHigh + a2.SizeHigh, a1.Unit));
            }

            if (UnitConverter.CanConvert(a1.Unit, a2.Unit))
            {
                //TODO: Handle range + nonrange
                var newLow  = a2.SizeLow.HasValue ? (float?)UnitConverter.Convert(a2.SizeLow.Value, a2.Unit, a1.Unit) : null;
                var newHigh = a1.SizeHigh + UnitConverter.Convert(a2.SizeHigh, a2.Unit, a1.Unit);
                return(new Amount(newLow, newHigh, a1.Unit));
            }

            throw new IncompatibleAmountException();
        }
Esempio n. 2
0
        public static Amount operator +(Amount firstAmount, Amount secondAmount)
        {
            if (firstAmount.Unit == secondAmount.Unit)                             //Just add
            {
                if (firstAmount.SizeLow.HasValue && secondAmount.SizeLow.HasValue) //Combine the lows, combine the highs
                {
                    return(new Amount(firstAmount.SizeLow + secondAmount.SizeLow, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit));
                }

                if (firstAmount.SizeLow.HasValue) //(1-2) + 1 = (2-3)
                {
                    return(new Amount(firstAmount.SizeLow + secondAmount.SizeHigh, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit));
                }

                if (secondAmount.SizeLow.HasValue) //1 + (1-2) = (2-3)
                {
                    return(new Amount(firstAmount.SizeHigh + secondAmount.SizeLow, firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit));
                }

                //just combine the highs
                return(new Amount(firstAmount.SizeHigh + secondAmount.SizeHigh, firstAmount.Unit));
            }

            if (UnitConverter.CanConvert(firstAmount.Unit, secondAmount.Unit))
            {
                //TODO: Handle range + nonrange
                var newLow = secondAmount.SizeLow.HasValue
                                 ? (float?)UnitConverter.Convert(secondAmount.SizeLow.Value, secondAmount.Unit, firstAmount.Unit)
                                 : null;
                var newHigh = firstAmount.SizeHigh + UnitConverter.Convert(secondAmount.SizeHigh, secondAmount.Unit, firstAmount.Unit);
                return(new Amount(newLow, newHigh, firstAmount.Unit));
            }

            throw new IncompatibleAmountException();
        }