-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBooksPage.xaml.cs
452 lines (361 loc) · 10.7 KB
/
AddBooksPage.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using Microsoft.Maui;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using LibraryOne.DataBase;
using LibraryOne.BookClass;
namespace LibraryOne;
public partial class AddBooksPage : ContentPage
{
//Add Database functionality
public SqlDatabase Database { get; set; }
//Stores all Science Books
public List<Science> AllScienceBooks { get; set; }
//Stores all Children Books
public List<Children> AllChildrenBooks { get; set; }
//Store all Mystery Books
public List<Mystery> AllMysteryBooks { get; set; }
//Store all Romance Books
public List<Romance> AllRomanceBooks { get; set; }
//Create an observational list of book categories for Category Picker
public ObservableCollection<string> BookCategories { get; set; }
//Create a observable collection of the Bookentry status
public ObservableCollection<bool> EntryIsEnabled { get; set; }
//Get full path of the CSV path
public string BookCategoriesCSVFilePath
{
get {
string csvBookCategoriesFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data\\categories.csv");
return csvBookCategoriesFilePath;
}
}
public AddBooksPage()
{
InitializeComponent();
//CREATE ITEMS
this.AllScienceBooks = new List<Science>();
this.AllChildrenBooks = new List<Children>();
this.AllMysteryBooks = new List<Mystery>();
this.AllRomanceBooks = new List<Romance>();
this.BookCategories = new ObservableCollection<string>();
this.EntryIsEnabled = new ObservableCollection<bool>();
//BIND CONTENT TO XAML
this.BindingContext = this;
// Create connection to database
this.Database = new SqlDatabase();
this.Database.Open();
//CALL METHOD
ListBookCategories();
//ApplyEntryBoxStates(expectedSelectedCategory);
}
private void ListBookCategories()
{
this.BookCategories.Clear();
//Create an array of Book Categories
string[] lines = File.ReadAllLines(this.BookCategoriesCSVFilePath);
foreach (string line in lines)
{
string[] columns = line.Split();
if (columns.Length > 0)
{
string bookCategory = columns[0];
this.BookCategories.Add(bookCategory);
}
}
}
//METHOD TO DYNAMTICALL UPDATE UI DEPENDING ON SELECTED CATEGORY
private void BookCategoryPickerChanged(object sender, EventArgs e)
{
//Expected selected input category
string expectedSelectedCategory;
expectedSelectedCategory = (string)findBookCategoryFromPicker.SelectedItem;
//Default the selection is false
bool findSelectedBookCategory = false;
//Verify that user selects a category
if (expectedSelectedCategory != null)
{
findSelectedBookCategory = true;
}
else
{
DisplayAlert("Error", "Please select a Book Category.", "OK");
}
// Update UI based on the selected category
UpdateUIForCategory(expectedSelectedCategory);
}
private void UpdateUIForCategory(string expectedSelectedCategory)
{
this.EntryIsEnabled.Clear(); // Clear the previous state
if (expectedSelectedCategory == "Science")
{
// Enable the entry boxes for Science category
this.EntryIsEnabled.Add(true); // Enable Subject
this.EntryIsEnabled.Add(true); // Enable Scientific Level
this.EntryIsEnabled.Add(true); // Enable Type of Book
}
else if (expectedSelectedCategory == "Children")
{
// Enable the entry boxes for Children category
this.EntryIsEnabled.Add(true); // Enable Age Group
this.EntryIsEnabled.Add(true); // Enable Learning Level
this.EntryIsEnabled.Add(true); // Enable Message
}
else if (expectedSelectedCategory == "Mystery")
{
// Enable the entry boxes for Mystery category
this.EntryIsEnabled.Add(true); // Enable Suspense Level
this.EntryIsEnabled.Add(true); // Enable Literature Type
}
else if (expectedSelectedCategory == "Romance")
{
// Enable the entry boxes for Mystery category
this.EntryIsEnabled.Add(true); // Enable Suspense Level
this.EntryIsEnabled.Add(true); // Enable Literature Type
}
// Apply the entry box states to your UI controls
ApplyEntryBoxStates(expectedSelectedCategory);
}
// Implement this method to apply the boolean values to your entry boxes in the UI
private void ApplyEntryBoxStates(string expectedSelectedCategory)
{
//By default, entry boxes are disabled
this.addSubject.IsEnabled = false;
this.addScientificLevel.IsEnabled = false;
this.addTypeOfBook.IsEnabled = false;
this.addAgeGroup.IsEnabled = false;
this.addLearningLevel.IsEnabled = false;
this.addMessage.IsEnabled = false;
this.addSuspenseLevel.IsEnabled = false;
this.addLiteratureType.IsEnabled = false;
this.addTone.IsEnabled = false;
this.addSetting.IsEnabled = false;
if (expectedSelectedCategory == "Science" && EntryIsEnabled.Count >= 3)
{
this.addSubject.IsEnabled = EntryIsEnabled[0];
this.addScientificLevel.IsEnabled = EntryIsEnabled[1];
this.addTypeOfBook.IsEnabled = EntryIsEnabled[2];
}
else if (expectedSelectedCategory == "Children" && EntryIsEnabled.Count >= 3)
{
this.addAgeGroup.IsEnabled = EntryIsEnabled[0];
this.addLearningLevel.IsEnabled = EntryIsEnabled[1];
this.addMessage.IsEnabled = EntryIsEnabled[2];
}
else if (expectedSelectedCategory == "Mystery" && EntryIsEnabled.Count >= 2)
{
this.addSuspenseLevel.IsEnabled = EntryIsEnabled[0];
this.addLiteratureType.IsEnabled = EntryIsEnabled[1];
}
else if (expectedSelectedCategory == "Romance" && EntryIsEnabled.Count >= 2)
{
this.addTone.IsEnabled = EntryIsEnabled[0];
this.addSetting.IsEnabled = EntryIsEnabled[1];
}
}
private void ClickAddBook(object sender, EventArgs e)
{
//Input values
string authorFirstName;
string authorLastName;
string isbn;
string title;
bool isCheckedOut;
string checkOutDate = null;
string returnDate = null;
//string bookCategory;
//Input values for Science
string subject; // e.g., Biology, Chemistry, Physics
int scientificLevel; // beginner, intermediate, advanced
string typeOfBook; // textbook, journal, report
//Input values for Children
int ageGroup; //preschool (age: 2-5), early readers (ages 6-8), middle grade (ages: 9-12), teens (ages: 13-17)
int learningLevel; //beginner, intermediate, advanced
string message; // alphabets, being kind, bullying, puberty
////Input values for Mystery book:
int suspenseLevel; //low, medium, high
string literatureType; // fiction or non - fiction
string tone; // romantic, dramatic, humorous
string setting; //location/time period
if (string.IsNullOrEmpty(this.addAuthorLastName.Text) == false)
{
authorLastName = this.addAuthorLastName.Text;
}
else
{
authorLastName = "";
}
if (string.IsNullOrEmpty(this.addAuthorFirstName.Text) == false)
{
authorFirstName = this.addAuthorFirstName.Text;
}
else
{
authorFirstName = "";
}
if (string.IsNullOrEmpty(this.addBookTitle.Text) == false)
{
title = this.addBookTitle.Text;
}
else
{
title = "";
}
if (string.IsNullOrEmpty(this.addBookISBN.Text) == false)
{
isbn = this.addBookISBN.Text;
}
else
{
isbn = "";
}
if (string.IsNullOrEmpty(this.addCheckOutStatus.Text) == false)
{
isCheckedOut = true;
}
else
{
isCheckedOut = false;
}
//if (string.IsNullOrEmpty(this.addCheckOutDate.Text) == false)
//{
// checkOutDate = this.addCheckOutDate.Text;
//}
//else
//{
// checkOutDate = null;
//}
//if (string.IsNullOrEmpty(this.addReturnDate.Text) == false)
//{
// returnDate = this.addReturnDate.Text;
//}
//else
//{
// returnDate = "";
//}
if (string.IsNullOrEmpty(this.addSubject.Text) == false)
{
subject = this.addSubject.Text;
}
else
{
subject = "";
}
if (string.IsNullOrEmpty(this.addScientificLevel.Text) == false)
{
scientificLevel = int.Parse(this.addScientificLevel.Text);
}
else
{
scientificLevel = -1;
}
if (string.IsNullOrEmpty(this.addTypeOfBook.Text) == false)
{
typeOfBook = this.addTypeOfBook.Text;
}
else
{
typeOfBook = "";
}
//Add Children Book Inputs
if (string.IsNullOrEmpty(this.addAgeGroup.Text) == false)
{
ageGroup = int.Parse(this.addAgeGroup.Text);
}
else
{
ageGroup = -1;
}
if (string.IsNullOrEmpty(this.addLearningLevel.Text) == false)
{
learningLevel = int.Parse(this.addLearningLevel.Text);
}
else
{
learningLevel = -1;
}
if (string.IsNullOrEmpty(this.addMessage.Text) == false)
{
message = this.addMessage.Text;
}
else
{
message= "";
}
//Inputs for mystery book
if (string.IsNullOrEmpty(this.addSuspenseLevel.Text) == false)
{
suspenseLevel = int.Parse(this.addSuspenseLevel.Text);
}
else
{
suspenseLevel = -1;
}
if (string.IsNullOrEmpty(this.addLiteratureType.Text) == false)
{
literatureType= this.addLiteratureType.Text;
}
else
{
literatureType = "";
}
//Inputs for Romance
if (string.IsNullOrEmpty(this.addTone.Text) == false)
{
tone = this.addTone.Text;
}
else
{
tone = "";
}
if (string.IsNullOrEmpty(this.addSetting.Text) == false)
{
setting= this.addSetting.Text;
}
else
{
setting = "";
}
//Validate input values
// Check if a name was entered.
if (string.IsNullOrEmpty(authorFirstName))
{
DisplayAlert("Error", "Author first name is empty. Please try again.", "Ok");
return;
}
if (string.IsNullOrEmpty(authorLastName))
{
DisplayAlert("Error", "Author last name is empty. Please try again.", "Ok");
return;
}
if (string.IsNullOrEmpty(title))
{
DisplayAlert("Error", "Book title is empty. Please try again.", "Ok");
return;
}
if (string.IsNullOrEmpty(isbn))
{
DisplayAlert("Error", "Book ISBN is empty. Please try again.", "Ok");
return;
}
if (!isCheckedOut)
{
DisplayAlert("Error", "Book status is empty. Please try again.", "Ok");
return;
}
Science science = new Science(isbn, title, authorFirstName, authorLastName, isCheckedOut, checkOutDate, returnDate, subject, scientificLevel, typeOfBook);
Children children = new Children(isbn, title, authorFirstName, authorLastName, isCheckedOut, checkOutDate, returnDate, ageGroup, learningLevel, message); // Add to database
Mystery mystery = new Mystery(isbn, title, authorFirstName, authorLastName, isCheckedOut, checkOutDate, returnDate, suspenseLevel, literatureType); // Add to database
Romance romance = new Romance(isbn, title, authorFirstName, authorLastName, isCheckedOut, checkOutDate, returnDate, tone, setting);
//Add to database
this.Database.Add(science);
this.Database.Add(children);
this.Database.Add(mystery);
this.Database.Add(romance);
// Add to existing list
this.AllScienceBooks.Add(science);
this.AllChildrenBooks.Add(children);
this.AllMysteryBooks.Add(mystery);
this.AllRomanceBooks.Add(romance);
// Tell user employee was added
this.DisplayAlert("Success!", "Book was added.", "OK");
}
}