public MealPlannerForm() { InitializeComponent(); //constant for food size to limit the array int FOOD_SIZE = 1000; //array limit for the food size myfoods = new FoodItemClass[FOOD_SIZE]; }
/// <summary> /// calls the methods from the class to run multiple functions /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddFoodItem_Click(object sender, EventArgs e) { //new instance of the food item class so we can pull from it. FoodItemClass fooditems = new FoodItemClass(); //call the chack food method CheckFood1(fooditems); //use the fooditem tostring method to populate the listbox accordingly lstBoxFoodChoices.Items.Add(fooditems.ToStringFood()); }
private void LoadFoods() { //string object to hold food items string foodItem = ""; //string array for the token results of the split method string[] splitFoodArray; //local variable to help assign each food item read from file to the food item array int indexCount = 0; //streamreader object StreamReader streamFoodFile; //try block to loop throught the file untill end of stream, split them at the commas to add them to the array try { //open the food items file streamFoodFile = File.OpenText(foodFileName); //end of stream loop while (!streamFoodFile.EndOfStream) { //read a line from the file and assign tif to the food item string foodItem = streamFoodFile.ReadLine(); //split the string using the ',' character splitFoodArray = foodItem.Split(','); //add the name of the food to the list box lstBoxSelectFoodItem.Items.Add(splitFoodArray[0]); myfoods[indexCount] = new FoodItemClass(splitFoodArray); indexCount++; } //close the file for reading streamFoodFile.Close(); } catch (Exception) { //error message MessageBox.Show("There was a problem reading the food items from the file"); } }
/// <summary> /// validates the user input and displays an error message if they get it wrong. /// </summary> /// <returns></returns> private bool CheckFood1(FoodItemClass foodz) { //constant for realistic range of input values to make sure user is within reason. const int REALISTIC_RANGE = 10000; //temp variables for method double calories, servings; int totalfat, saturatedfat, sugar, protein; //validate all the user input if (txtBoxFoodName.Text != "" && double.TryParse(txtBoxCalPerServ.Text, out calories) && calories < REALISTIC_RANGE && double.TryParse(txtBoxServings.Text, out servings) && servings < REALISTIC_RANGE && int.TryParse(txtBoxTotalFat.Text, out totalfat) && totalfat < REALISTIC_RANGE && int.TryParse(txtBoxSatFat.Text, out saturatedfat) && saturatedfat < REALISTIC_RANGE && int.TryParse(txtBoxSugar.Text, out sugar) && sugar < REALISTIC_RANGE && int.TryParse(txtBoxProtein.Text, out protein) && protein < REALISTIC_RANGE) { //set the values from this end to the backing spaces in the class foodz.Calories = calories; foodz.Servings = servings; foodz.TotalFat = totalfat; foodz.SaturatedFat = saturatedfat; foodz.Sugar = sugar; foodz.Protein = protein; foodz.FoodName = txtBoxFoodName.Text; //call my clearboxes method CLearBoxes(); //return true for the sake of the method return(true); } else { //call my clear boxes method CLearBoxes(); //return false and error message MessageBox.Show("you have inputed your info incorrectly. Food name is text \n and the rest are to be intergers \n with exception to servings which can be a decimal number \n no numbers are alloud to be above 5000 either."); //return false so that the values aren't changed in the back of the class by error return(false); } }