/// <summary>
        /// The OptimizeMeasurements method modifies a collection of common measurements to make it easier for kitchen use
        /// </summary>
        /// <param name="measurements">ICollection of Measurements</param>
        private static void OptimizeMeasurements(ICollection <Measurement> measurements)
        {
            int size = measurements.Count;

            if (size > 1)
            {
                //can cast as USCookingVolumeMeasurement because know it is--was created in this class. List is of type Measurement to be usable across multiple types of measurements
                USCookingVolumeMeasurement secondToLast = (USCookingVolumeMeasurement)measurements.ElementAt(size - 2);

                /* check if second-to-last element is tablespoon
                 * (if it is, the last one is teaspoons because it is the only unit less than tablespoons) */
                if (secondToLast.UnitSize == Unit.TABLESPOON)
                {
                    //simplify before checking denominator, because otherwise a denominator of 2 does not indicate any halves, as it could be 4/2
                    secondToLast.Amount.Simplify();
                    if (secondToLast.Amount.Denominator == 2)
                    {
                        //can cast as USCookingVolumeMeasurement because know it is--was created in this class. List is of type Measurement to be usable across multiple types of measurements
                        USCookingVolumeMeasurement teaspoons = (USCookingVolumeMeasurement)measurements.ElementAt(size - 1);

                        //if only 1/2 a tablespoon and has teaspoons, better to take out the tablespoons and turn have 1 1/2 more teaspoons (because more annoying to use more spoons)
                        if (secondToLast.Amount.Equals(new Fraction(1, 2))) //if tablespoons is 1/2 (don't have to use EqualsValue because it has been simplified)
                        {
                            teaspoons.Amount += new Fraction(RecipeConstants.TSP_PER_TBSP, 2);
                            teaspoons.Amount.Simplify();

                            measurements.Remove(secondToLast);
                        }

                        /*
                         * If tablespoons is not 1/2, then is greater than 1/2 (only allowed in 1/2 increments),
                         * and because it has denominator of 2 when simplifed, is X 1/2 tablespoons.
                         * If teaspoons is also in half increments, better to use bigger size spoons
                         * (less times filling them--better to add one teaspoon than do 3 1/2 tablespoons
                         * in 1/2 tablespoon increments or use both 1/2 and 1 tablespoon spoons),
                         * so becomes numberOfTablespoons - 1/2 (=whole number) and numberOfTeaspoons + 1/2 (= 2)
                         * if teaspoons denominator is 2, teaspoons is 1/2, because is simplified (so not 2/2),
                         * and any 1/2 increment greater than 1/2 would have yielded 1/2 tablespoons
                         * (because 1 1/2 teaspoons = 1 tablespoon)
                         */
                        else if (teaspoons.Amount.Denominator == 2)
                        {
                            secondToLast.Amount -= new Fraction(1, 2);
                            teaspoons.Amount    += new Fraction(3, 2);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Overloaded GetMeasurementReplacement method replaces the measurement
        /// in the given text, given a Unit
        /// </summary>
        /// <param name="amount">String containing measurement amount</param>
        /// <param name="unit">Unit of measurement</param>
        /// <returns></returns>
        private string GetMeasurementReplacement(String amount, Unit unit)
        {
            Measurement measurement;
            //gets the measurement as a fraction
            RecipeFraction fraction = GetFraction(amount);

            //perform the actual recipe conversion
            fraction *= multiplier;
            if (unit == Unit.OTHER)
            {
                measurement = new OtherMeasurement(fraction);
            }
            else
            {
                measurement = new USCookingVolumeMeasurement(fraction, unit);
            }

            //get user-friendly version of converted measurement
            ICollection <Measurement> measurements = measurement.UserFriendlyMeasurements();
            String replacement = String.Join(" + ", measurements.Select(i => i.ToHTMLFormattedString()));

            //if there was a plus after the measurement amount, indicate that in the replacement text
            if (Regex.Match(amount, PLUS_PATTERN).Success)
            {
                //if more than one measurement, need to indicate that the 'plus' at the end refers to the combined group of measurements
                if (measurements.Count > 1)
                {
                    replacement = "[" + replacement + "]<b>+</b>";
                }
                else
                {
                    replacement += "+"; //does not need to be bold if only one measurement
                }
            }

            return(replacement);
        }