예제 #1
0
 private bool testFoodDupMacro(FoodItem newf, ref FoodItem dbf)  //function used to compare macro values of foods with same name
 {
     foreach (FoodItem f in Foods)
     {
         if (newf == f)
         {
             if (f._fat != newf._fat || f._carbs != newf._carbs || f._protein != newf._protein)
             {
                 dbf = f;
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     return(false);
 }
예제 #2
0
        public FoodEntry(Match match)   //init food entry by a Regex match(from any food entry text) with several groups for respective values
        {
            FoodItem ftemp = new FoodItem(match);

            MainForm.insertOrRefFood(ref ftemp);    //if food given is not part of Global Foods list Adds it and Refereces it here. Else it only refereces it from the global list.
            _food = ftemp;

            String str = new String(match.Groups["Amount"].Value.TakeWhile(c => !Char.IsLetter(c) && c != ' ').ToArray());  //grabs only number from format: 11g or 1/2

            if (str.Contains('/'))
            {
                _amount = Math.Round(Convert.ToDouble(str.Split('/')[0]) / Convert.ToDouble(str.Split('/')[1]), 2);     //in case of fraction format get an actual number
            }
            else
            {
                _amount = Convert.ToDouble(str);
            }

            setCalculatedValues();  //replace current macro with calculated
        }
예제 #3
0
 public FoodEntry(FoodItem food, double amount, double fat, double carbs, double protein) : this(food, amount)
 {
     _fatEntry     = fat;
     _carbsEntry   = carbs;
     _proteinEntry = protein;
 }
예제 #4
0
        private void button_addFood_Click(object sender, EventArgs e)   //adds a new food(or references from Foods through the process of FoodEntry init) to selected meal
        {
            MealEntry meal = comboBox_meals.SelectedItem as MealEntry;

            if (meal == null)
            {
                MessageBox.Show("No Meal Selected!");
                return;
            }
            if (textBox_amount.Text == "")      //checks amount
            {
                MessageBox.Show("Needs Amount!");
                return;
            }
            FoodItem food = null;

            if (radio_list.Checked)             //adds food from the list of all foods
            {
                food = (FoodItem)listBox_foodItems.SelectedItem;
                if (food != null)
                {
                    meal.addFoodEntry(new FoodEntry(food, Convert.ToDouble(textBox_amount.Text)));
                }
                else
                {
                    MessageBox.Show("Invalid Selection for Food!");
                }
                textBox_amount.Clear();
                textBox_searchFood.Clear();
                textBox_searchFood.Focus();
            }
            else if (radio_pattern.Checked)     //adds food by matching a string(similar to one written file format)
            {
                if (MainForm.regexEntry.Match(textBox_pattern.Text).Success)
                {
                    meal.addFoodEntry(new FoodEntry(textBox_pattern.Text));
                    food = _day._mealEntries[comboBox_meals.SelectedIndex]._foodEntries.Last()._food;
                }
                else
                {
                    MessageBox.Show("Invalid Pattern for Food!");
                }
                textBox_pattern.Clear();
                textBox_pattern.Focus();
            }
            else
            {                                   //adds new food by creating it with values given by user through custom textboxes
                try
                {
                    food = new FoodItem(textBox_foodName.Text, textBox_brand.Text,
                                        Convert.ToDouble(textBox_fat.Text), Convert.ToDouble(textBox_carbs.Text), Convert.ToDouble(textBox_protein.Text),
                                        (Measure)comboBox_measure.SelectedIndex);
                    meal.addFoodEntry(new FoodEntry(food, Convert.ToDouble(textBox_amount.Text)));
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Invalid Entry Values! {ex.Message}");
                }
                textBox_foodName.Clear();
                textBox_brand.Clear();
                textBox_fat.Clear();
                textBox_carbs.Clear();
                textBox_protein.Clear();
                textBox_foodName.Focus();
            }

            if (food != null)                  //updates MealEntry and FoodEntry calculated values(does the math for macros)
            {
                ((MealEntry)comboBox_meals.SelectedItem).setCalculatedValues();
                _day.setCalculatedValues();
            }

            updateDay();
        }