-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
168 lines (150 loc) · 5.53 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
168 lines (150 loc) · 5.53 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using RecipeManagementApp;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace RecipeManagementAppWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private List<Recipe> recipes = new List<Recipe>();
public MainWindow()
{
InitializeComponent();
}
private void AddRecipeBtn_Click(object sender, RoutedEventArgs e)
{
var addRecipeWindow = new AddRecipe();
if (addRecipeWindow.ShowDialog() == true)
{
var newRecipe = addRecipeWindow.NewRecipe;
if (newRecipe != null)
{
recipes.Add(newRecipe);
MessageBox.Show("Recipe added successfully!");
}
}
}
private void DisplayRecipeBtn_Click(object sender, RoutedEventArgs e)
{
if (recipes.Count > 0)
{
int indexVal = GetRecipeIndex("Select a recipe to display:", recipes);
if (indexVal >= 0)
{
var displayRecipeWindow = new DisplayRecipe(recipes[indexVal]);
displayRecipeWindow.ShowDialog();
}
}
else
{
MessageBox.Show("No recipes added yet.");
}
}
private void ChangeScaleBtn_Click(object sender, RoutedEventArgs e)
{
if (recipes.Count > 0)
{
int indexVal = GetRecipeIndex("Select a recipe to change scale:", recipes);
if (indexVal >= 0)
{
var selectedRecipe = recipes[indexVal];
var changeScaleWindow = new ChangeScale(selectedRecipe);
bool? dialogResult = changeScaleWindow.ShowDialog();
if (dialogResult == true)
{
// Optionally update UI or display a message after scale change
}
}
}
else
{
MessageBox.Show("No recipes added yet.");
}
}
private void ResetBtn_Click(object sender, RoutedEventArgs e)
{
if (recipes.Count > 0)
{
int indexVal = GetRecipeIndex("Select a recipe to reset to original values:", recipes);
if (indexVal >= 0)
{
var selectedRecipe = recipes[indexVal];
var resetRecipeWindow = new ResetRecipe(selectedRecipe);
bool? dialogResult = resetRecipeWindow.ShowDialog();
if (dialogResult == true)
{
// Optionally update UI or display a message after reset
}
}
}
else
{
MessageBox.Show("No recipes added yet.");
}
}
private void ExitBtn_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private int GetRecipeIndex(string prompt, List<Recipe> recipes)
{
// Show a simple dialog to select a recipe
var sortedRecipes = recipes.OrderBy(r => r.Name).ToList();
string message = prompt + "\n";
for (int i = 0; i < sortedRecipes.Count; i++)
{
message += $"{i + 1}. {sortedRecipes[i].Name}\n";
}
string input = Microsoft.VisualBasic.Interaction.InputBox(message, "Select Recipe", "1");
if (int.TryParse(input, out int recipeIndex) && recipeIndex >= 1 && recipeIndex <= sortedRecipes.Count)
{
return recipeIndex - 1;
}
MessageBox.Show("Invalid input. Please enter a valid recipe number.");
return -1;
}
private void CreateMenuBtn_Click(object sender, RoutedEventArgs e)
{
if (recipes.Count > 0)
{
var menuWindow = new MenuSelection(recipes);
if (menuWindow.ShowDialog() == true)
{
var selectedRecipes = menuWindow.SelectedRecipes;
DisplayPieChart(selectedRecipes);
}
}
else
{
MessageBox.Show("No recipes added yet.");
}
}
private void DisplayPieChart(List<Recipe> selectedRecipes)
{
// Calculate the food group percentages
var foodGroupTotals = new Dictionary<string, double>();
foreach (var recipe in selectedRecipes)
{
foreach (var ingredient in recipe.Ingredients)
{
if (foodGroupTotals.ContainsKey(ingredient.FoodGroup))
{
foodGroupTotals[ingredient.FoodGroup] += ingredient.Quantity;
}
else
{
foodGroupTotals[ingredient.FoodGroup] = ingredient.Quantity;
}
}
}
double totalQuantity = foodGroupTotals.Values.Sum();
var foodGroupPercentages = foodGroupTotals.ToDictionary(kvp => kvp.Key, kvp => kvp.Value / totalQuantity * 100);
// Create a pie chart window
var pieChartWindow = new PieChart(foodGroupPercentages);
pieChartWindow.ShowDialog();
}
}
}