/// <summary> /// method to load food items /// </summary> private void LoadFoods() { //declare food item variable and set it as a string with no text as default string foodItem = ""; //string array to hold tokens as they are split string[] splitFoodArray; //local variable to assign food items from file to array index int index = 0; //declare streamreader variable StreamReader inputFile; //open file with streamreader inputFile = File.OpenText(foodFileName); //validate reading file try { //put file contents into array while (!inputFile.EndOfStream) { //loop through file and save each food item in variable foodItem = inputFile.ReadLine(); //split string using comma splitFoodArray = foodItem.Split(','); //add only the name of the food from the array lstBoxFoodItems.Items.Add(splitFoodArray[0]); //add the food item myFoods[index] = new FoodItem(splitFoodArray); //increment index index++; } //close file inputFile.Close(); } //error if reading failed catch(Exception ex) { //display error if reading didn't succeed MessageBox.Show("Sorry, couldn't read the file. \n\nCode: " + ex.Message + "\n\nPerhaps you haven't made a list of food on the Food Log page?", "Reading Error"); } }
/// <summary> /// click event for the add food button, adds food item to list box /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddFood_Click(object sender, EventArgs e) { //create new instance of FoodItem class FoodItem myFood = new FoodItem(); //call validation method if (CheckFood(myFood)) { //adds items if valid, using ToString method from FoodItem class lstBoxNutrition.Items.Add(myFood.ToString()); //clear textboxes txtBoxCalories.Clear(); txtBoxFiber.Clear(); txtBoxFoodName.Clear(); txtBoxProtein.Clear(); txtBoxServings.Clear(); txtBoxSugars.Clear(); txtBoxSodium.Clear(); } }
/// <summary> /// method to validate user input /// </summary> /// <param name="myFood"></param> /// <returns></returns> private bool CheckFood(FoodItem myFood) { //create local variables for validation int calorie; double serving; double sugar; double protein; double fiber; double sodium; //ensure a name has been input if (txtBoxFoodName.Text != "") { //store food name in FoodItem class myFood.FoodName = txtBoxFoodName.Text; //ensure calories is a valid number and between a set value if (int.TryParse(txtBoxCalories.Text, out calorie) && (calorie <= 1000 && calorie >= 0)) { //store calories in FoodItem class myFood.Calories = calorie; //ensure servings is a valid number and between a set value if (double.TryParse(txtBoxServings.Text, out serving) && (serving <= 5 && serving > 0)) { //store servings in FoodItem class myFood.Servings = serving; //ensure sugars is a valid number and between a set value if (double.TryParse(txtBoxSugars.Text, out sugar) && (sugar <= 100 && sugar >= 0)) { //store sugars in FoodItem class myFood.Sugars = sugar; //ensure protein is a valid number and between a set value if (double.TryParse(txtBoxProtein.Text, out protein) && (protein <= 60 && protein >= 0)) { //store protein in FoodItem class myFood.Protein = protein; //ensure fiber is a valid number and between a set value if (double.TryParse(txtBoxFiber.Text, out fiber) && (fiber <= 10 && fiber > 0)) { //store fiber in FoodItem class myFood.Fiber = fiber; //ensure sodium is a valid number and between a set value if (double.TryParse(txtBoxSodium.Text, out sodium) && (sodium <= 5 && sodium > 0)) { //store sodium in FoodItem class and return true if all fields are valid myFood.Sodium = sodium; return true; } //display error if sodium is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for sodium, between 0 and 5 grams"); txtBoxSodium.Clear(); txtBoxSodium.Focus(); return false; } } //display error if fiber is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for fiber, between 0 and 10 grams"); txtBoxFiber.Clear(); txtBoxFiber.Focus(); return false; } } //display error if protein is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for protein, between 0 and 60 grams"); txtBoxProtein.Clear(); txtBoxProtein.Focus(); return false; } } //display error if sugars is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for sugar, between 0 and 100 grams"); txtBoxSugars.Clear(); txtBoxSugars.Focus(); return false; } } //display error if serving size is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for serving size, between 0 and 5"); txtBoxServings.Clear(); txtBoxServings.Focus(); return false; } } //display error if calories is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for calories, between 0 and 1000"); txtBoxCalories.Clear(); txtBoxCalories.Focus(); return false; } } //display error if food name is left blank else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a food name"); txtBoxFoodName.Clear(); txtBoxFoodName.Focus(); return false; } }
/// <summary> /// method to validate user input /// </summary> /// <param name="myFood"></param> /// <returns></returns> private bool CheckFood(FoodItem myFood) { //create local variables for validation int calorie; double serving; double sugar; double protein; double fiber; double sodium; //ensure a name has been input if (txtBoxFoodName.Text != "") { //store food name in FoodItem class myFood.FoodName = txtBoxFoodName.Text; //ensure calories is a valid number and between a set value if (int.TryParse(txtBoxCalories.Text, out calorie) && (calorie <= 1000 && calorie >= 0)) { //store calories in FoodItem class myFood.Calories = calorie; //ensure servings is a valid number and between a set value if (double.TryParse(txtBoxServings.Text, out serving) && (serving <= 5 && serving > 0)) { //store servings in FoodItem class myFood.Servings = serving; //ensure sugars is a valid number and between a set value if (double.TryParse(txtBoxSugars.Text, out sugar) && (sugar <= 100 && sugar >= 0)) { //store sugars in FoodItem class myFood.Sugars = sugar; //ensure protein is a valid number and between a set value if (double.TryParse(txtBoxProtein.Text, out protein) && (protein <= 60 && protein >= 0)) { //store protein in FoodItem class myFood.Protein = protein; //ensure fiber is a valid number and between a set value if (double.TryParse(txtBoxFiber.Text, out fiber) && (fiber <= 10 && fiber > 0)) { //store fiber in FoodItem class myFood.Fiber = fiber; //ensure sodium is a valid number and between a set value if (double.TryParse(txtBoxSodium.Text, out sodium) && (sodium <= 5 && sodium > 0)) { //store sodium in FoodItem class and return true if all fields are valid myFood.Sodium = sodium; return(true); } //display error if sodium is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for sodium, between 0 and 5 grams"); txtBoxSodium.Clear(); txtBoxSodium.Focus(); return(false); } } //display error if fiber is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for fiber, between 0 and 10 grams"); txtBoxFiber.Clear(); txtBoxFiber.Focus(); return(false); } } //display error if protein is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for protein, between 0 and 60 grams"); txtBoxProtein.Clear(); txtBoxProtein.Focus(); return(false); } } //display error if sugars is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for sugar, between 0 and 100 grams"); txtBoxSugars.Clear(); txtBoxSugars.Focus(); return(false); } } //display error if serving size is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for serving size, between 0 and 5"); txtBoxServings.Clear(); txtBoxServings.Focus(); return(false); } } //display error if calories is incorrect else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a valid value for calories, between 0 and 1000"); txtBoxCalories.Clear(); txtBoxCalories.Focus(); return(false); } } //display error if food name is left blank else { //display error message, clear field, set focus, and return false MessageBox.Show("Enter a food name"); txtBoxFoodName.Clear(); txtBoxFoodName.Focus(); return(false); } }