-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddRecipe.xaml.cs
More file actions
95 lines (83 loc) · 3.2 KB
/
Copy pathAddRecipe.xaml.cs
File metadata and controls
95 lines (83 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using RecipeManagementApp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace RecipeManagementAppWPF
{
/// <summary>
/// Interaction logic for AddRecipe.xaml
/// </summary>
public partial class AddRecipe : Window, IDataErrorInfo
{
public Recipe NewRecipe { get; private set; }
private List<Ingredients> ingredients = new List<Ingredients>();
private List<RecipeSteps> steps = new List<RecipeSteps>();
public AddRecipe()
{
InitializeComponent();
IngredientsDataGrid.ItemsSource = ingredients;
StepsDataGrid.ItemsSource = steps;
}
private void SaveRecipeBtn_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrWhiteSpace(RecipeNameTextBox.Text) && ingredients.All(IngredientIsValid) && steps.All(StepIsValid))
{
NewRecipe = new Recipe
{
Name = RecipeNameTextBox.Text,
Ingredients = new List<Ingredients>(ingredients),
Steps = new List<RecipeSteps>(steps),
OriginalQuantities = new List<double>(ingredients.Select(i => i.Quantity)),
OriginalCalories = new List<double>(ingredients.Select(i => i.Calories))
};
double totalCalories = Recipe.CalculateTotalCalories(NewRecipe.Ingredients);
if (totalCalories > 300)
{
MessageBox.Show("Warning: Total calories exceed 300 Kcal!");
}
this.DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("Please complete all fields correctly before saving the recipe.");
}
}
// Validation logic for Quantity and Calories
private bool IngredientIsValid(Ingredients ingredient)
{
if (string.IsNullOrWhiteSpace(ingredient.Name) || string.IsNullOrWhiteSpace(ingredient.Unit) || string.IsNullOrWhiteSpace(ingredient.FoodGroup))
return false;
if (ingredient.Quantity <= 0)
return false;
if (ingredient.Calories < 0) // Allow 0 or positive calories
return false;
return true;
}
// Validation logic for Step description
private bool StepIsValid(RecipeSteps step)
{
return !string.IsNullOrWhiteSpace(step?.StepsDescription);
}
// IDataErrorInfo interface implementation for data grid validation
public string Error => null;
public string this[string columnName]
{
get
{
if (columnName == "Quantity")
{
foreach (var ingredient in ingredients)
{
if (!double.TryParse(ingredient.Quantity.ToString(), out double quantity) || quantity <= 0)
return "Quantity must be a positive number.";
}
}
return null;
}
}
}
}