-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
360 lines (336 loc) · 13.5 KB
/
MainWindow.xaml.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Hazem Kudaimi November 2022
//A Subscriber class (a listerner)
using SR_BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Staff_Registry
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// This class creates a search window for a search auction
/// but does not save the info on the search
/// Receives search information and show it in the datagrid.
/// This class is mainly a subscriber of the events fired by
/// the search window class.
/// </summary>
public partial class MainWindow : Window
{
private readonly StaffManager staffManager;
private readonly string[] gender = Enum.GetNames(typeof(GenderType));
private readonly string[] rating = Enum.GetNames(typeof(RatingType));
private int selectedDGIndex;
private const string file = @"DB.csv";
//private const string file = @"C:\Users\hazem\Desktop\C# III\assignment_3\SR_PL\DB.csv";
// Using Action delegate with lambda expression
private readonly Action<string> WriteToMessageBox = str =>
{
MessageBox.Show("" + str);
};
public MainWindow()
{
InitializeComponent();
staffManager = new StaffManager(file);
InitalizeCBComponents();
InitalizeComponentIndex();
GetStaffListFromDB();
selectedDGIndex = -1;
}
#region Initalizing GUI
private void InitalizeCBComponents()
{
CBGender.ItemsSource = gender;
CBCommunication.ItemsSource = rating;
CBDecisionMaking.ItemsSource = rating;
CBProblemSolving.ItemsSource = rating;
CBListening.ItemsSource = rating;
CBLeadership.ItemsSource = rating;
CBAccuracy.ItemsSource = rating;
}
private void InitalizeComponentIndex()
{
TBID.Text = "";
TBFirstName.Text = "";
TBLastName.Text = "";
CBGender.SelectedIndex = 1;
CBCommunication.SelectedIndex = 3;
CBDecisionMaking.SelectedIndex = 3;
CBProblemSolving.SelectedIndex = 3;
CBListening.SelectedIndex = 3;
CBLeadership.SelectedIndex = 3;
CBAccuracy.SelectedIndex = 3;
}
#endregion
#region New, Add, Change, Delete, Search Clicked
private void NewClick(object sender, RoutedEventArgs e)
{
selectedDGIndex = -1;
InitalizeComponentIndex();
GenerateID();
}
private void AddClick(object sender, RoutedEventArgs e)
{
selectedDGIndex = -1;
if (TBFirstName.Text.Trim() == "" || TBLastName.Text.Trim() == "")
{
WriteToMessageBox("You must write first- and lastname!");
}
else
{
GenerateID();
Employee employee = new (
int.Parse(TBID.Text),
TBFirstName.Text,
TBLastName.Text,
(GenderType)CBGender.SelectedIndex,
(RatingType)CBCommunication.SelectedIndex,
(RatingType)CBDecisionMaking.SelectedIndex,
(RatingType)CBProblemSolving.SelectedIndex,
(RatingType)CBListening.SelectedIndex,
(RatingType)CBLeadership.SelectedIndex,
(RatingType)CBAccuracy.SelectedIndex
);
staffManager.StaffList.Add(employee);
UppdateDG();
AddEmployeeToDB(employee);
}
}
private void ChangeClick(object sender, RoutedEventArgs e)
{
if (selectedDGIndex >= 0)
{
Employee employee = new (
int.Parse(TBID.Text),
TBFirstName.Text,
TBLastName.Text,
(GenderType)CBGender.SelectedIndex,
(RatingType)CBCommunication.SelectedIndex,
(RatingType)CBDecisionMaking.SelectedIndex,
(RatingType)CBProblemSolving.SelectedIndex,
(RatingType)CBListening.SelectedIndex,
(RatingType)CBLeadership.SelectedIndex,
(RatingType)CBAccuracy.SelectedIndex);
staffManager.StaffList[DG.SelectedIndex] = employee;
UppdateDG();
SaveStaffToDB();
InitalizeComponentIndex();
}
else
{
WriteToMessageBox("You must select an employee!");
}
}
private void DeleteClick(object sender, RoutedEventArgs e)
{
if (selectedDGIndex >= 0)
{
var result = MessageBox.Show("Are you sure?", "Delet employee",
MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
if (result == MessageBoxResult.Yes)
{
staffManager.StaffList.RemoveAt(selectedDGIndex);
UppdateDG();
SaveStaffToDB();
InitalizeComponentIndex();
}
}
else
{
WriteToMessageBox("You must select an employee!");
}
}
private void SearchClick(object sender, RoutedEventArgs e)
{
SearchWindow searchWindow = new ();
searchWindow.Show();
InputPanel.Visibility = Visibility.Hidden;
MainPanel.Visibility = Visibility.Hidden;
searchWindow.EmployeeProcessorEvent += StaffFilter;
searchWindow.CancelClicked += SearchWindowCancelClicked;
}
private void SearchWindowCancelClicked()
{
InputPanel.Visibility = Visibility.Visible;
MainPanel.Visibility = Visibility.Visible;
UppdateDG();
}
private void StaffFilter(object? sender, EmployeeArgs e)
{
Employee employee = e.Employee;
List<Employee> staffLis = new();
// Using lambda expressions with LINQ
if (employee.FirstName != "")
{
var staffList = staffManager.StaffList.Where(x => x.FirstName.ToLower().Contains(employee.FirstName.ToLower()));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.LastName != "")
{
var staffList = staffManager.StaffList.Where(x => x.LastName.ToLower().Contains(employee.LastName.ToLower()));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Gender != GenderType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Gender.Equals(employee.Gender));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Communication != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Communication.Equals(employee.Communication));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Decision_Making != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Decision_Making.Equals(employee.Decision_Making));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Problem_Solving != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Problem_Solving.Equals(employee.Problem_Solving));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Listening != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Listening.Equals(employee.Listening));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Leadership != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Leadership.Equals(employee.Leadership));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (employee.Accuracy != RatingType.Empty)
{
var staffList = staffManager.StaffList.Where(x => x.Accuracy.Equals(employee.Accuracy));
foreach (Employee em in staffList)
{
staffLis.Add(em);
}
}
if (staffLis.Count > 0)
{
var staffList = staffLis.Distinct();
DG.ItemsSource = null;
DG.ItemsSource = staffList;
}
else
{
MessageBox.Show("No result matched!");
UppdateDG();
}
}
#endregion
#region Help Methods
private void GenerateID()
{
Employee employee = staffManager.StaffList.Last();
int newID = employee.ID + 1;
TBID.Text = "" + newID;
}
private void DGSelectedChange(object sender, SelectedCellsChangedEventArgs e)
{
selectedDGIndex = DG.SelectedIndex;
if (selectedDGIndex >= 0)
{
TBID.Text = "" + staffManager.StaffList[selectedDGIndex].ID;
TBFirstName.Text = staffManager.StaffList[selectedDGIndex].FirstName;
TBLastName.Text = staffManager.StaffList[selectedDGIndex].LastName;
CBGender.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Gender;
CBCommunication.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Communication;
CBDecisionMaking.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Decision_Making;
CBProblemSolving.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Problem_Solving;
CBListening.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Listening;
CBLeadership.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Leadership;
CBAccuracy.SelectedIndex = (int)staffManager.StaffList[selectedDGIndex].Accuracy;
// Count of the rating score of the empolyee competence:
//string str = string.Format("{0:0.##}", staffManager.StaffList[selectedDGIndex].CompetenceDegree());
// Count of the rating score of the empolyee competence: But with another way, by using lambda expressions with LINQ
List<int> competenceListAll = new ()
{
(int)staffManager.StaffList[selectedDGIndex].Communication,
(int)staffManager.StaffList[selectedDGIndex].Decision_Making,
(int)staffManager.StaffList[selectedDGIndex].Problem_Solving,
(int)staffManager.StaffList[selectedDGIndex].Listening,
(int)staffManager.StaffList[selectedDGIndex].Leadership,
(int)staffManager.StaffList[selectedDGIndex].Accuracy
};
var competenceList = competenceListAll.Where(x => x > 0).ToList();
double CompetenceSum = competenceList.Sum(x => x);
// Using Func delegate with lambda statement
Func<double, double, double> average = (num1, num2) =>
{
double result = num1 / num2;
return result;
};
double result = average(CompetenceSum, competenceList.Count);
TBRating.Text = string.Format("{0:0.##}", result);
}
}
private void UppdateDG()
{
DG.ItemsSource = null;
DG.ItemsSource = staffManager.StaffList;
}
#endregion
#region Data Base
private void GetStaffListFromDB()
{
staffManager.StaffList.Clear();
bool result = staffManager.GetStaffListFromDatabase();
UppdateDG();
if (!result)
{
MessageBox.Show("There is with getting the data from the file, check the file path!");
}
}
private void SaveStaffToDB()
{
staffManager.SaveStaffListToDatabase();
}
private void AddEmployeeToDB(Employee employee)
{
if (staffManager.AddEmployeeToDatabase(employee))
{
MessageBox.Show("Add Employee to data base is succeed");
}
else
MessageBox.Show("There is a problem with the data base, the employee is not added!");
}
#endregion
}
}