Beispiel #1
0
 /// <summary>
 /// The MultiplyBy method multiplies the NonNegativeFraction by a Fraction
 /// Throws an ArgumentOutOfRangeException if Fraction is less than zero
 /// </summary>
 /// <param name="fraction">Fraction by which to multiply</param>
 protected override void MultiplyBy(Fraction fraction)
 {
     if (fraction.CompareTo(0) < 0)
     {
         throw new ArgumentOutOfRangeException("fraction", "Cannot multiply NonNegativeFraction by a negative fraction.");
     }
     base.MultiplyBy(fraction);
 }
Beispiel #2
0
 /// <summary>
 /// The DivideBy method divides the NonNegativeFraction by a Fraction
 /// Throws an ArgumentOutOfRangeException if divisor is less than  zero
 /// </summary>
 /// <param name="divisor">Divisor fractionAddend</param>
 protected override void DivideBy(Fraction divisor)
 {
     if (divisor.CompareTo(0) < 0)
     {
         throw new ArgumentOutOfRangeException("fraction", "Cannot multiply NonNegativeFraction by a negative fraction.");
     }
     base.DivideBy(divisor);
 }
Beispiel #3
0
        /// <summary>
        /// The Add method adds a Fraction to the NonNegativeFraction
        /// If the passed Fraction would cause the NonNegativeFraction to be negative, throws an ArgumentOutOfRangeException
        /// </summary>
        /// <param name="fraction">Fraction to add</param>
        protected override void Add(Fraction fraction)
        {
            //if the fractionAddend is less than zero, checks if it will cause the
            if (fraction.CompareTo(0) < 0)
            {
                Fraction checkingFrction = Copy();
                checkingFrction += fraction;
                if (checkingFrction.CompareTo(0) < 0)
                {
                    throw new ArgumentOutOfRangeException("fraction", "Argument cannot make NonNegativeFraction negative.");
                }
            }

            base.Add(fraction);
        }
        /// <summary>
        /// The TeaspoonsToUserFriendlyMeasurements gets a collection of user friendly measurements
        /// from RecipeFraction of teaspoons
        /// </summary>
        /// <param name="teaspoons">RecipeFraction containing number of teaspoons in the recipe</param>
        /// <returns>Collection of user friendly measurements</returns>
        private static ICollection <Measurement> TeaspoonsToUserFriendlyMeasurements(RecipeFraction teaspoons)
        {
            //Get simplified measurements, using as many quarter cups as possible
            ICollection <Measurement> convertedMeasurements = GetMeasurementsFromQuarterCup(teaspoons.Copy());
            int wholeTeaspoons = teaspoons.WholePart();

            /**
             * If could get a third of a cup from converted recipe, get measurements
             * using third cups and then quarter cups (if applicable).
             * Then compare two Collections of Measurments to see if Collection with third cups is more user-friendly
             */
            if (wholeTeaspoons >= RecipeConstants.TSP_PER_THIRD_CUP)
            {
                ICollection <Measurement> tCupMeasurements = GetMeasurementsFromThirdCup(teaspoons);
                //If same number of Measurements are returned, use Collection that starts with greater Measurement
                if (convertedMeasurements.Count == tCupMeasurements.Count)
                {
                    /*can cast as USCookingVolumeMeasurement because was created in this class.
                     * List is of type Measurement to be usable across multiple types of measurements.
                     * Casting required to access Amount with its current access modifier and making public is undesirable*/
                    Fraction qCups = ((USCookingVolumeMeasurement)convertedMeasurements.ElementAt(0)).Amount;
                    Fraction tCups = ((USCookingVolumeMeasurement)tCupMeasurements.ElementAt(0)).Amount;
                    if (tCups.CompareTo(qCups) > 0)
                    {
                        convertedMeasurements = tCupMeasurements;
                    }
                }
                //If the Collection with third cups is shorter than collection with quarter cups, use collection with third cups
                else if (tCupMeasurements.Count < convertedMeasurements.Count)
                {
                    convertedMeasurements = tCupMeasurements;
                }
            }
            OptimizeMeasurements(convertedMeasurements);
            return(convertedMeasurements);
        }