コード例 #1
0
        public void button_ConfirmIngredients_Click(object sender, RoutedEventArgs e)
        {
            currentRecipe = new Recipe();

            bool   abortCalculation = false, validIngredient = false, servingsSpecified = true, measuredByVolume = false;
            double portion, ingrQtyVal = 0; // portion represents how much the ingredient used is compared to one serving size of that ingredient
            double n_servingsMade = 0;


            if (textBox_NumberServingsMade.Text == "")
            {
                servingsSpecified = false;
            }
            else
            {
                try
                {
                    n_servingsMade = Double.Parse(textBox_NumberServingsMade.Text);

                    if (n_servingsMade <= 0)
                    {
                        abortCalculation = true;
                        MessageBox.Show("A number <= 0 was detected.");
                    }
                }
                catch
                {
                    MessageBox.Show("Alpha input detected for number of servings.");
                    abortCalculation = true;
                }
            }

            double totalCalories = 0, totalFat = 0, totalSatFat = 0, totalTransFat = 0, totalCholesterol = 0,
                   totalSodium = 0, totalCarbs = 0, totalFiber = 0, totalSugar = 0, totalProtein = 0, totalPrice = 0;

            // add nutrition from each ingredient entered
            foreach (Grid g in recipeIngredientList)
            {
                if (abortCalculation == false) // an ingredient containing invalid fields has not been found yet
                {
                    UIElementCollection gridChildren   = g.Children;
                    TextBox             ingrQty        = (TextBox)gridChildren[0];
                    ComboBox            ingrMsr        = (ComboBox)gridChildren[1];
                    ComboBox            ingrIngredient = (ComboBox)gridChildren[3];

                    if (ingrQty.Text == "" && ingrMsr.Text == "" && ingrIngredient.Text == "")
                    {
                        // skip adding this ingredient since it was left blank but do not abort
                    }
                    else if (ingrQty.Text == "" || ingrMsr.Text == "" || ingrIngredient.Text == "")
                    {
                        // not all fields were left blank so user forgot a field, abort the calculation and show message
                        abortCalculation = true;

                        String msg = "An empty field was detected for an ingredient.  Either\n"
                                     + "occupy all fields or leave them all blank.";
                        MessageBox.Show(msg);
                    }
                    else
                    {
                        // all fields were entered for the ingredient, now check if they are valid
                        try
                        {
                            ingrQtyVal = Double.Parse(ingrQty.Text);

                            if (ingrQtyVal <= 0)
                            {
                                abortCalculation = true;
                                MessageBox.Show("A number <= 0 was detected.");
                            }
                        }
                        catch (Exception)
                        {
                            abortCalculation = true;
                            MessageBox.Show("Alpha input detected for number of servings.");
                        }


                        String     ingrMsr_AsString = ingrMsr.Text;
                        Ingredient ingredient       = (Ingredient)ingrIngredient.SelectedItem;

                        if (ingrMsr_AsString == "oz" || ingrMsr_AsString == "mg" || ingrMsr_AsString == "g" || ingrMsr_AsString == "kg")
                        {
                            measuredByVolume = false;
                        }
                        else
                        {
                            measuredByVolume = true;
                        }

                        if (measuredByVolume != ingredient.measuredByVolume)
                        {
                            String msg = "Invalid Measurement Type Conversion\n\n";

                            msg += "Recipe calls for " + ingrQty.Text + " " + ingrMsr.Text + " of " + ingredient.ToString() +
                                   " but " + ingredient.ToString() + " is currently only known to be measured by " + ingredient.ServingMsr + ".\n";

                            msg += "Cannot convert a ";
                            if (measuredByVolume == true)
                            {
                                msg += "volume";
                            }
                            else
                            {
                                msg += "mass";
                            }

                            msg += " measurement to a ";
                            if (ingredient.measuredByVolume == true)
                            {
                                msg += "volume";
                            }
                            else
                            {
                                msg += "mass";
                            }

                            msg += " measurement.";

                            MessageBox.Show(msg);

                            abortCalculation = true;
                        }

                        // All fields have been gathered and tested for validity, calculate if we can
                        if (abortCalculation == false)
                        {
                            validIngredient = true; // atleast one ingredient was valid

                            portion = GetPortionSize(ingrQtyVal, ingrMsr_AsString, ingredient);

                            totalCalories    += ingredient.Calories * portion;
                            totalFat         += ingredient.Fat * portion;
                            totalSatFat      += ingredient.SatFat * portion;
                            totalTransFat    += ingredient.TransFat * portion;
                            totalCholesterol += ingredient.Cholesterol * portion;
                            totalSodium      += ingredient.Sodium * portion;
                            totalCarbs       += ingredient.Carbs * portion;
                            totalFiber       += ingredient.Fiber * portion;
                            totalSugar       += ingredient.Sugar * portion;
                            totalProtein     += ingredient.Protein * portion;

                            double pricePerServing = ingredient.Price / ingredient.ServingPerContainer;
                            totalPrice += pricePerServing * portion;

                            // now add the ingredient to the recipe
                            RecipeIngredient rIngredient = new RecipeIngredient(ingrQtyVal, ingrMsr_AsString, ingredient);
                            currentRecipe.ingredients.Add(rIngredient);
                        }
                    }
                }
            }

            label_ServingCalories.Content    = "-";
            label_ServingFat.Content         = "-";
            label_ServingSatFat.Content      = "-";
            label_ServingTransFat.Content    = "-";
            label_ServingCholesterol.Content = "-";
            label_ServingSodium.Content      = "-";
            label_ServingCarbs.Content       = "-";
            label_ServingFiber.Content       = "-";
            label_ServingSugar.Content       = "-";
            label_ServingProtein.Content     = "-";
            label_ServingPrice.Content       = "-";

            label_RecipeCalories.Content    = "-";
            label_RecipeFat.Content         = "-";
            label_RecipeSatFat.Content      = "-";
            label_RecipeTransFat.Content    = "-";
            label_RecipeCholesterol.Content = "-";
            label_RecipeSodium.Content      = "-";
            label_RecipeCarbs.Content       = "-";
            label_RecipeFiber.Content       = "-";
            label_RecipeSugar.Content       = "-";
            label_RecipeProtein.Content     = "-";
            label_RecipePrice.Content       = "-";

            textBox_RecipeName.Text        = "Add a Recipe Name (Click me)";
            textBox_RecipeIngredients.Text = "Ingredients will be added here";
            textBox_RecipeDirections.Text  = "Add directions here.";
            textBox_RecipeNotes.Text       = "Add notes here.";

            if (validIngredient && abortCalculation == false)
            {
                // update nutrition facts label to hold the values
                label_RecipeCalories.Content    = String.Format("{0:0.#}", totalCalories);
                label_RecipeFat.Content         = String.Format("{0:0.#}", totalFat);
                label_RecipeSatFat.Content      = String.Format("{0:0.#}", totalSatFat);
                label_RecipeTransFat.Content    = String.Format("{0:0.#}", totalTransFat);
                label_RecipeCholesterol.Content = String.Format("{0:0.#}", totalCholesterol);
                label_RecipeSodium.Content      = String.Format("{0:0.#}", totalSodium);
                label_RecipeCarbs.Content       = String.Format("{0:0.#}", totalCarbs);
                label_RecipeFiber.Content       = String.Format("{0:0.#}", totalFiber);
                label_RecipeSugar.Content       = String.Format("{0:0.#}", totalSugar);
                label_RecipeProtein.Content     = String.Format("{0:0.#}", totalProtein);
                label_RecipePrice.Content       = "$" + String.Format("{0:0.00}", totalPrice);

                if (servingsSpecified == true) // user did specify how many servings the recipe made (and it is > 0)
                {
                    label_ServingCalories.Content    = String.Format("{0:0.#}", totalCalories / n_servingsMade);
                    label_ServingFat.Content         = String.Format("{0:0.#}", totalFat / n_servingsMade);
                    label_ServingSatFat.Content      = String.Format("{0:0.#}", totalSatFat / n_servingsMade);
                    label_ServingTransFat.Content    = String.Format("{0:0.#}", totalTransFat / n_servingsMade);
                    label_ServingCholesterol.Content = String.Format("{0:0.#}", totalCholesterol / n_servingsMade);
                    label_ServingSodium.Content      = String.Format("{0:0.#}", totalSodium / n_servingsMade);
                    label_ServingCarbs.Content       = String.Format("{0:0.#}", totalCarbs / n_servingsMade);
                    label_ServingFiber.Content       = String.Format("{0:0.#}", totalFiber / n_servingsMade);
                    label_ServingSugar.Content       = String.Format("{0:0.#}", totalSugar / n_servingsMade);
                    label_ServingProtein.Content     = String.Format("{0:0.#}", totalProtein / n_servingsMade);
                    label_ServingPrice.Content       = "$" + String.Format("{0:0.00}", totalPrice / n_servingsMade);
                }
                else // user did not specify a number of servings so just show them the recipe amount automatically
                {
                    if (displayingNutrition == true)
                    {
                        // execute TogglePerRecipe_MouseDown event
                        if (displayingPerServing == true)
                        {
                            // want to show per recipe
                            displayingPerServing = false;

                            label_TogglePerServing.Opacity = 0.6;
                            label_TogglePerRecipe.Opacity  = 1;

                            stackPanel_NutritionLabelPerServing.Visibility = Visibility.Collapsed;
                            stackPanel_NutritionLabelPerRecipe.Visibility  = Visibility.Visible;
                        }
                    }
                }

                // update recipe panel
                String text = "";
                foreach (RecipeIngredient rIngredient in currentRecipe.ingredients)
                {
                    text += " - " + rIngredient.Quantity.ToString() + " " + rIngredient.MeasureType + " " + rIngredient.Ingredient.ToString() + "\n";
                }
                textBox_RecipeIngredients.Text = text.Substring(0, text.Length - 1); // remove last \n

                if (servingsSpecified)
                {
                    textBox_RecipeNotes.Text = "Makes " + n_servingsMade.ToString() + " servings.";
                    currentRecipe.Notes      = "Makes " + n_servingsMade.ToString() + " servings.";
                }
            }
            else
            {
                currentRecipe = new Recipe();
            }
        }
コード例 #2
0
        private double GetPortionSize(double ingrQty, String ingrMsr, Ingredient ingredient)
        {
            bool   measuredByVolume = false;
            double amt_InGrams = 0, amt_InCups = 0;
            double ratio = 0, conversionFactor = 0;

            if (ingrMsr == "oz" || ingrMsr == "mg" || ingrMsr == "g" || ingrMsr == "kg")
            {
                measuredByVolume = false;
            }
            else
            {
                measuredByVolume = true;
            }

            if (measuredByVolume == true)
            {
                switch (ingrMsr) // get the conversion factor for converting the type to cups (for example, 16 tbsp = 1 cup)
                {
                case "tsp.":
                    conversionFactor = 48;
                    break;

                case "tbsp.":
                    conversionFactor = 16;
                    break;

                case "fl oz":
                    conversionFactor = 8;
                    break;

                case "cup":
                    conversionFactor = 1;
                    break;

                case "pint":
                    conversionFactor = 0.5;
                    break;

                case "quart":
                    conversionFactor = 0.25;
                    break;

                case "gallon":
                    conversionFactor = 0.0625;
                    break;

                case "ml":
                    conversionFactor = 236.588237;
                    break;

                case "liter":
                    conversionFactor = 0.23658824;
                    break;
                }

                amt_InCups = ingrQty / conversionFactor;
                ratio      = amt_InCups / ingredient.ServingQty_True;
            }
            else // measured by mass
            {
                switch (ingrMsr) // get conversion factor for the type to grams
                {
                case "oz":
                    conversionFactor = 0.03527396;
                    break;

                case "mg":
                    conversionFactor = 1000;
                    break;

                case "g":
                    conversionFactor = 1;
                    break;

                case "kg":
                    conversionFactor = 0.001;
                    break;
                }

                amt_InGrams = ingrQty / conversionFactor;
                ratio       = amt_InGrams / ingredient.ServingQty_True;
            }

            return(ratio);
        }
コード例 #3
0
        private void menuItem_LoadFromCSV_Click(object sender, RoutedEventArgs e)
        {
            StreamReader   stream         = null;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = "c:\\";
            openFileDialog.Filter           = "CSV files (*.csv)|*.csv";
            openFileDialog.RestoreDirectory = true;

            Nullable <bool> result = openFileDialog.ShowDialog();

            if (result == true)
            {
                String fileName = openFileDialog.FileName;

                List <Ingredient> list = new List <Ingredient>();
                list.Add(new Ingredient());

                try
                {
                    stream = new StreamReader(fileName);

                    int currentLine = 1; // use non-zero based indexing when reporting errors

                    String text = "";
                    while (stream.Peek() > -1) // while there is another line in the csv file
                    {
                        text = stream.ReadLine();

                        String[] subStrings = text.Split(',');

                        // there are 15 total columns for each ingredient
                        if (subStrings.Length == 15)
                        {
                            String Name = "", ServingMsr = "";
                            double ServingQty = 0, ServingPerContainer = 0, Calories = 0, Fat = 0, SatFat = 0, TransFat = 0, Cholesterol = 0,
                                   Sodium = 0, Carbs = 0, Fiber = 0, Sugar = 0, Protein = 0, Price = 0;

                            Name                = subStrings[0];
                            ServingQty          = Double.Parse(subStrings[1]);
                            ServingMsr          = subStrings[2];
                            ServingPerContainer = Double.Parse(subStrings[3]);
                            Calories            = Double.Parse(subStrings[4]);
                            Fat         = Double.Parse(subStrings[5]);
                            SatFat      = Double.Parse(subStrings[6]);
                            TransFat    = Double.Parse(subStrings[7]);
                            Cholesterol = Double.Parse(subStrings[8]);
                            Sodium      = Double.Parse(subStrings[9]);
                            Carbs       = Double.Parse(subStrings[10]);
                            Fiber       = Double.Parse(subStrings[11]);
                            Sugar       = Double.Parse(subStrings[12]);
                            Protein     = Double.Parse(subStrings[13]);
                            Price       = Double.Parse(subStrings[14]);

                            if (Name.Length == 0 || ServingQty < 0 || (ServingMsr != "tsp." && ServingMsr != "tbsp." && ServingMsr != "fl oz" && ServingMsr != "cup" && ServingMsr != "pint" && ServingMsr != "quart" && ServingMsr != "gallon" && ServingMsr != "ml" && ServingMsr != "liter" && ServingMsr != "oz" && ServingMsr != "mg" && ServingMsr != "g" && ServingMsr != "kg") || ServingPerContainer < 0 || Calories < 0 || Fat < 0 || SatFat < 0 ||
                                TransFat < 0 || Cholesterol < 0 || Sodium < 0 || Carbs < 0 || Fiber < 0 || Sugar < 0 || Protein < 0 || Price < 0)
                            {
                                MessageBox.Show("Either a field was empty, Serving Measurement Type was invalid, or a field contained a negative number");
                                throw new Exception();
                            }

                            Ingredient i = new Ingredient(Name, ServingQty, ServingMsr, ServingPerContainer, Calories, Fat, SatFat,
                                                          TransFat, Cholesterol, Sodium, Carbs, Fiber, Sugar, Protein, Price);

                            list.Add(i);
                        }
                        else
                        {
                            MessageBox.Show("The ingredient on line " + currentLine + " does not contain 15 values.");
                            throw new Exception();
                        }
                        currentLine++;
                    }

                    stream.Close();

                    // make ingredientDatabaseList point to the newly loaded in list
                    ingredientDatabaseList = list;
                    RefreshIngredientDropdowns();
                }
                catch
                {
                    MessageBox.Show("Error in loading from CSV.  CSV file is formatted improperly.");
                }
            }
        }
        private void button_SaveChanges_Click(object sender, RoutedEventArgs e)
        {
            bool emptyField = false, invalidField = false;

            String Name = "", ServingMsr = "";
            double ServingQty = 0, ServingPerContainer = 0, Calories = 0, Fat = 0, SatFat = 0, TransFat = 0, Cholesterol = 0,
                   Sodium = 0, Carbs = 0, Fiber = 0, Sugar = 0, Protein = 0, Price = 0;

            if (textBox_Name.Text != "")
            {
                Name = textBox_Name.Text;
            }
            else
            {
                emptyField = true;
            }

            if (textBox_ServingQty.Text != "")
            {
                try
                {
                    ServingQty = Double.Parse(textBox_ServingQty.Text);
                    if (ServingQty <= 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (comboBox_ServingMsr.Text != "")
            {
                ServingMsr = comboBox_ServingMsr.Text;
            }
            else
            {
                emptyField = true;
            }

            if (textBox_ServingPerContainer.Text != "")
            {
                try
                {
                    ServingPerContainer = Double.Parse(textBox_ServingPerContainer.Text);
                    if (ServingPerContainer <= 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Calories.Text != "")
            {
                try
                {
                    Calories = Double.Parse(textBox_Calories.Text);
                    if (Calories < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Fat.Text != "")
            {
                try
                {
                    Fat = Double.Parse(textBox_Fat.Text);
                    if (Fat < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_SatFat.Text != "")
            {
                try
                {
                    SatFat = Double.Parse(textBox_SatFat.Text);
                    if (SatFat < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_TransFat.Text != "")
            {
                try
                {
                    TransFat = Double.Parse(textBox_TransFat.Text);
                    if (TransFat < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Cholesterol.Text != "")
            {
                try
                {
                    Cholesterol = Double.Parse(textBox_Cholesterol.Text);
                    if (Cholesterol < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Sodium.Text != "")
            {
                try
                {
                    Sodium = Double.Parse(textBox_Sodium.Text);
                    if (Sodium < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Carbs.Text != "")
            {
                try
                {
                    Carbs = Double.Parse(textBox_Carbs.Text);
                    if (Carbs < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Fiber.Text != "")
            {
                try
                {
                    Fiber = Double.Parse(textBox_Fiber.Text);
                    if (Fiber < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Sugar.Text != "")
            {
                try
                {
                    Sugar = Double.Parse(textBox_Sugar.Text);
                    if (Sugar < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Protein.Text != "")
            {
                try
                {
                    Protein = Double.Parse(textBox_Protein.Text);
                    if (Protein < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (textBox_Price.Text != "")
            {
                try
                {
                    Price = Double.Parse(textBox_Price.Text);
                    if (Price < 0)
                    {
                        invalidField = true;
                    }
                }
                catch (Exception)
                {
                    invalidField = true;
                }
            }
            else
            {
                emptyField = true;
            }

            if (emptyField == true)
            {
                MessageBox.Show("At least one of the fields was left empty!\nPlease ensure all fields are occupied.");
                ingredient = null;
            }
            else if (invalidField == true)
            {
                MessageBox.Show("Alpha input or a negative number was detected!\nPlease enter only positive numbers.");
                ingredient = null;
            }
            else
            {
                ingredient = new Ingredient(Name, ServingQty, ServingMsr, ServingPerContainer, Calories, Fat, SatFat, TransFat,
                                            Cholesterol, Sodium, Carbs, Fiber, Sugar, Protein, Price);

                mainWindow.ingredientDatabaseList.Remove(selectedIngredient);
                mainWindow.ingredientDatabaseList.Add(ingredient);
                mainWindow.ingredientDatabaseList.Sort();

                button_ApplySearch_Click(sender, e);
            }
        }
コード例 #5
0
 public RecipeIngredient(double q, String m, Ingredient i)
 {
     Quantity    = q;
     MeasureType = m;
     Ingredient  = i;
 }
コード例 #6
0
 public RecipeIngredient()
 {
     Quantity    = 0;
     MeasureType = "";
     Ingredient  = new Ingredient();
 }
コード例 #7
0
 private void button_Cancel_Click(object sender, RoutedEventArgs e)
 {
     ingredient = null;
     this.Close();
 }