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(); } }
private List <Recipe> BuildRecipeDatabaseList() { bool abortRead = false; List <Recipe> list = new List <Recipe>(); String name = "", ingredients = "", directions = "", notes = ""; // read from table, create Recipe object, add to list SQL_cmd.CommandText = "SELECT * FROM Recipes;"; SQL_datareader = SQL_cmd.ExecuteReader(); while (SQL_datareader.Read()) { // there is a table entry to read name = (String)SQL_datareader["Name"]; ingredients = (String)SQL_datareader["Ingredients"]; directions = (String)SQL_datareader["Directions"]; notes = (String)SQL_datareader["Notes"]; Recipe recipe = new Recipe(); recipe.Name = name; recipe.Directions = directions; recipe.Notes = notes; // add the ingredients to the recipe object String[] subStrings = ingredients.Split('~'); if (subStrings.Length % 3 == 0) // each ingredient will have quantity, measure type and ingredient, so each ingredient should have 3 strings associated with it { for (int i = 0; i < subStrings.Length; i++) { if (!abortRead) { RecipeIngredient rIngredient = new RecipeIngredient(); // read quantity if (!Double.TryParse(subStrings[i], out rIngredient.Quantity)) // if failed { abortRead = true; } i++; // read measure type rIngredient.MeasureType = subStrings[i]; i++; // find the ingredient object bool ingredientFound = false; foreach (Ingredient dbIngredient in ingredientDatabaseList) { if (dbIngredient.Name == subStrings[i]) { rIngredient.Ingredient = dbIngredient; ingredientFound = true; } } if (!ingredientFound) { abortRead = true; MessageBox.Show("A Recipe in the database calls for an ingredient not currently saved in the archive."); } recipe.ingredients.Add(rIngredient); } } } else { abortRead = true; } list.Add(recipe); } SQL_datareader.Close(); if (abortRead) { list = new List <Recipe>(); MessageBox.Show("Error when reading from Recipe Database.\nLoading with a blank recipe list..."); } return(list); }