Exemplo n.º 1
0
        private void addToListBtn_Click(object sender, EventArgs e)
        {
            string chosenFoodName             = chosenFoodNameTextBox.Text;
            double chosenFoodNumberOfServings = (double)chosenFoodNumberOfServingsNumericUpDown.Value;
            Food   chosenFood = foodDatabase.Where(t => t.Name.Equals(chosenFoodName)).FirstOrDefault();

            if (chosenFood.IsConsumed == false)
            {
                chosenFood.User             = user;
                chosenFood.NumberOfServings = chosenFoodNumberOfServings;
                chosenFood.IsConsumed       = true;
                consumedFood.Add(chosenFood);
                foodDatabase.Where(t => t.Id == chosenFood.Id).FirstOrDefault().IsConsumed = true;
                AddRowToConsumedFoodTable(chosenFood);
            }
            else
            {
                DataTable foodTable = (DataTable)consumedFoodTable.DataSource;
                foreach (DataRow row in foodTable.Rows)
                {
                    if (row["ConsumedFoodName"].ToString().Equals(chosenFoodName))
                    {
                        row["ConsumedFoodSize"] =
                            Double.Parse(row["ConsumedFoodSize"].ToString().Split(' ')[0], CultureInfo.InvariantCulture)
                            + chosenFoodNumberOfServings * chosenFood.ServingSize + " g";
                        row["ConsumedFoodCalories"] = Double.Parse(row["ConsumedFoodCalories"].ToString(), CultureInfo.InvariantCulture)
                                                      + chosenFoodNumberOfServings * chosenFood.CaloriesInServing;
                        break;
                    }
                }
                chosenFood.NumberOfServings += chosenFoodNumberOfServings;
            }
            _foodController.UpdateFood(chosenFood);
            ChangeCaloriesRatio();
        }
Exemplo n.º 2
0
        private void foodFormSaveBtn_Click(object sender, EventArgs e)
        {
            string foodName = foodFormNameTextBox.Text;
            double foodServingSize = 0, foodCalories = 0, foodProteins = 0, foodCarbs = 0, foodFats = 0;
            string foodServingSizeUnit = foodFormServingSizeUnitComboBox.Text;

            if (foodName == null || foodName == "" ||
                foodServingSizeUnit == null || foodServingSizeUnit == "")
            {
                MessageBox.Show("Niste ispunili sve podatke u formi! Provjerite unesene podatke i pokušajte ponovno.");
                return;
            }

            try
            {
                foodServingSize = Double.Parse(foodFormServingSizeTextBox.Text, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                MessageBox.Show("Vrijednost '" + foodFormServingSizeTextBox.Text + "' koju ste unijeli za veličinu porcije nije odgovarajuća. Molimo Vas pokušajte ponovno.");
                return;
            }

            try
            {
                foodCalories = Double.Parse(foodFormCaloriesTextBox.Text, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                MessageBox.Show("Vrijednost '" + foodFormCaloriesTextBox.Text + "' koju ste unijeli za kalorije nije odgovarajuća. Molimo Vas pokušajte ponovno.");
                return;
            }

            try
            {
                foodProteins = Double.Parse(foodFormProteinsTextBox.Text, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                MessageBox.Show("Vrijednost '" + foodFormProteinsTextBox.Text + "' koju ste unijeli za proteine nije odgovarajuća. Molimo Vas pokušajte ponovno.");
                return;
            }

            try
            {
                foodCarbs = Double.Parse(foodFormCarbsTextBox.Text, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                MessageBox.Show("Vrijednost '" + foodFormCarbsTextBox.Text + "' koju ste unijeli za ugljikohidrate nije odgovarajuća. Molimo Vas pokušajte ponovno.");
                return;
            }

            try
            {
                foodFats = Double.Parse(foodFormFatsTextBox.Text, CultureInfo.InvariantCulture);
            }
            catch (Exception)
            {
                MessageBox.Show("Vrijednost '" + foodFormFatsTextBox.Text + "' koju ste unijeli za masti nije odgovarajuća. Molimo Vas pokušajte ponovno.");
                return;
            }

            Food entry = foodDatabase.Where(t => t.Name.Equals(foodName)).FirstOrDefault();

            if (entry == null)
            {
                Food food = new Food()
                {
                    Name              = foodName,
                    ServingSize       = foodServingSize,
                    ServingSizeUnit   = foodServingSizeUnit,
                    CaloriesInServing = foodCalories,
                    ProteinsInServing = foodProteins,
                    CarbsInServing    = foodCarbs,
                    FatsInServing     = foodFats,
                    NumberOfServings  = 1,
                    IsConsumed        = false
                };
                _foodController.AddFood(food);
                foodDatabase.Add(food);
                AddRowToFoodDatabaseTable(food);
            }
            else
            {
                entry.ServingSize       = foodServingSize;
                entry.ServingSizeUnit   = foodServingSizeUnit;
                entry.CaloriesInServing = foodCalories;
                entry.ProteinsInServing = foodProteins;
                entry.CarbsInServing    = foodCarbs;
                entry.FatsInServing     = foodFats;
                _foodController.UpdateFood(entry);

                DataTable foodTable = (DataTable)foodDatabaseTable.DataSource;
                foreach (DataRow row in foodTable.Rows)
                {
                    if (row["FoodDatabaseName"].ToString().Equals(foodName))
                    {
                        row["FoodDatabaseServingSize"] = foodServingSize + " " + foodServingSizeUnit;
                        row["FoodDatabaseCalories"]    = foodCalories;
                        break;
                    }
                }
            }
            ClearFoodForm();
            foodFormNameTextBox.ReadOnly = false;
        }