Question

Alrighty, so I have been writing a class for kitchen measurements, for now it just has volume, or liquid, measurements, and I call it like this:

ktchnmsrmnts.VolumeMeasurements(number), current_measurement, to_measurement);

and from there it's perty simple for most people to figure out the inside code, just some simple math there. Now my issue is I need to add a option that says 'hey he gots 17.50 cups, lets make it 1 gallon, and 1 1/2 cups.

I was thinking of adding a new case to 'to_measurement' that checks to see if the number is greater then the highest measurement like say is 19 cups is greater than gallons, then it makes it 1 gallon and sends the remainders down the line, but I never use pints in the kitchen so maybe add another option of what to convert it too.. I don't know I'm stuck, and yes I know the code is basic but fits my needs, halfway anyways..

Just incase you need my full code, here ya go:

class KitchenMeasurements
{
    public double VolumeMeasurements(double number, string current_measurement, string to_measurement)
    {
        double mL = 0;
        double tsp= 0;
        double tbsp = 0;
        double fluid_ounces = 0;
        double cup = 0;
        double pint = 0;
        double quart = 0;
        double gallon = 0;
        double ounce = 0;
        double gram = 0;
        double pound = 0;
        double liter = 0;

        switch (current_measurement)
        {
            case "mL":
                mL = number;
                tsp = mL * 0.202884;
                tbsp = mL * 0.067628;
                fluid_ounces = mL * 0.033814;
                cup = mL * 0.00422675;
                pint = mL * 0.00211338;
                quart = mL * 0.00105669;
                gallon = mL * 0.000264172;
                liter = mL * 0.001;
                break;

            case "tsb":
                tsp = number;
                mL = tsp * 4.92892;
                tbsp = tsp * 0.333333;
                fluid_ounces = tsp * 0.166667;
                cup = tsp * 0.0208333;
                pint = tsp * 0.0104167;
                quart = tsp * 0.00520833;
                gallon = tsp * 0.00130208;
                liter = tsp * 0.00492892;
                break;

            case "tbsp":
                tbsp = number;
                mL = tbsp * 14.7868;
                tsp = tbsp * 3;
                fluid_ounces = tbsp * 0.5;
                cup = tbsp * 0.0625;
                pint = tbsp * 0.03125;
                quart = tbsp * 0.015625;
                gallon = tbsp * 0.00390625;
                liter = tbsp * 0.0147868;
                break;

            case "fluid ounce":
                fluid_ounces = number;
                mL = fluid_ounces * 29.5735;
                tsp = fluid_ounces * 6;
                tbsp = fluid_ounces * 2;
                cup = fluid_ounces * 0.125;
                pint = fluid_ounces * 0.0625;
                quart = fluid_ounces * 0.03125;
                gallon = fluid_ounces * 0.0078125;
                liter = fluid_ounces * 0.0295735;
                break;

            case "cup":
                cup = number;
                mL = cup * 236.588;
                tsp = cup * 48;
                tbsp = cup * 16;
                fluid_ounces = cup * 8;
                pint = cup * 0.5;
                quart = cup * 0.25;
                gallon = cup * 0.0625;
                liter = cup * 0.236588;
                break;

            case "pint":
                pint = number;
                mL = pint * 473.176;
                tsp = pint * 96;
                tbsp = pint * 32;
                fluid_ounces = pint * 16;
                cup = pint * 2;
                quart = pint * 0.5;
                gallon = pint * 0.125;
                liter = pint * 0.473176;
                break;

            case "quart":
                quart = number;
                mL = quart * 946.353;
                tsp = quart * 192;
                tbsp = quart * 64;
                fluid_ounces = quart * 32;
                cup = quart * 4;
                pint = quart * 2;
                gallon = quart * 0.25;
                liter = quart * 0.946353;
                break;

            case "gallon":
                gallon = number;
                mL = gallon * 3785.41;
                tsp = gallon * 768;
                tbsp = gallon * 256;
                fluid_ounces = gallon * 128;
                cup = gallon * 16;
                pint = gallon * 8;
                quart = gallon * 4;
                liter = gallon * 3.78541;
                break;

            case "liter":
                liter = number;
                mL = liter * 1000;
                tsp = liter * 202.884;
                tbsp = liter * 67.628;
                fluid_ounces = liter * 33.814;
                cup = liter * 4.22675;
                pint = liter * 2.11338;
                quart = liter * 1.05669;
                gallon = liter * 0.264172;
                break;
        }

        switch (to_measurement)
        {
            case "mL":
                return mL;
            case "tsb":
                return tsp;
            case "tbsp":
                return tbsp;
            case "fluid ounce":
                return fluid_ounces;
            case "cup":
                return cup;
            case "pint":
                return pint;
            case "quart":
                return quart;
            case "gallon":
                return gallon;
            case "ounce":
                return ounce;
            case "gram":
                return gram;
            case "pound":
                return pound;
            case "liter":
                return liter;
        }

    }
}
Was it helpful?

Solution

It's hard to tell exactly what you were asking for, but I did see this:

Now my issue is I need to add a option that says 'hey he gots 17.50 cups, lets make it 1 gallon, and 1 1/2 cups.

You would need to take a set of measurements and loop through them recalculating the remainders. I highly advise you to extract your method code into class variables and helper methods.

Below is some example code that should help you. You'll need to fill in the blanks.

public string ConvertToDescription(double amount, string originalMeasurement, params string[] toMeasurements)
{
    StringBuilder sb = new StringBuilder();
    double valueToConvert = amount;
    string priorMeasurement = originalMeasurement;
    double displayAmount;
    for (int i = 0; i < toMeasurements.Count; i++)
    {
         if (i > 0)
             sb.Append(" ");

          double convertedAmount = VolumeMeasurements(valueToConvert, priorMeasurement, toMeasurement[i]);

          // Check if last item so we don't trim wanted decimals.
          if (i < toMeasurements.Count - 1)
              displayAmount = Math.Floor(convertedAmount)
          else
              displayAmount = convertedAmount;

          valueToConvert = convertedAmount - displayAmount;
          priorMeasurement = toMeasurements[i];

          // You will need to add logic here to display fractions if needed.
          sb.AppendFormat("{0} {1}", displayAmount, toMeasurements[i]);

          if (fixedAmount == 0)
              break;
    }

    return sb.ToString();
}

Keep in mind that none of this is tested and I really hope this is what you were asking for. This should get you on the right track. You will need to find a way to convert the decimals on the last conversion to display as fractions and implement that. This will at least get you "1 gallon 1.5 cups"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top