/
PopupProperty.cs
517 lines (473 loc) · 16.4 KB
/
PopupProperty.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SOFT152Assignment
{
public partial class PopupProperty : Form
{
public TextBox[] textBoxes;
public string level;
public string action;
public int districtIndex;
public int neighborhoodIndex;
public int propertyIndex;
// For editing or viewing.
public PopupProperty(string accessLevel, string desiredAction, int indexDistrict, int indexNeighborhood, int indexProperty)
{
InitializeComponent();
this.level = accessLevel.ToLower();
this.action = desiredAction.ToLower();
this.districtIndex = indexDistrict;
this.neighborhoodIndex = indexNeighborhood;
this.propertyIndex = indexProperty;
SetTitle();
GetTextBoxes();
// For editing and viewing, this sets/autofills the values of the TextBoxes.
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
Property property = neighborhood.Properties[propertyIndex];
this.inputDistrictName.Text = district.Name;
this.inputNeighborhoodName.Text = neighborhood.Name;
this.inputPropertyID.Text = property.Id.ToString();
this.inputPropertyName.Text = property.Name.ToString();
this.inputHostID.Text = property.HostID.ToString();
this.inputHostName.Text = property.HostName.ToString();
this.inputHostPropertyCount.Text = property.Count.ToString();
this.inputRoomType.Text = property.RoomType.ToString();
this.inputRoomPrice.Text = property.RoomPrice.ToString();
this.inputLongitude.Text = property.Longitude.ToString();
this.inputLatitude.Text = property.Latitude.ToString();
this.inputRoomNights.Text = property.RoomNights.ToString();
this.inputRoomAvailability.Text = property.RoomAvailability.ToString();
// The delete button is only visible when the user is modifying a property.
if(this.action == "edit")
{
buttonDelete.Visible = true;
}
// If the user only wants to view the item, then the TextBox components are set to read-only and are disabled so the user can't trigger any focus events on them.
else if(this.action == "view")
{
foreach(TextBox input in textBoxes)
{
input.ReadOnly = true;
input.Enabled = false;
}
this.buttonConfirm.Hide();
}
}
// For adding, but with the district name and neighborhood name autofilled.
public PopupProperty(string accessLevel, string desiredAction, int indexDistrict, int indexNeighborhood)
{
InitializeComponent();
this.level = accessLevel.ToLower();
this.action = desiredAction.ToLower();
this.districtIndex = indexDistrict;
this.neighborhoodIndex = indexNeighborhood;
SetTitle();
GetTextBoxes();
// For editing and viewing, this sets/autofills the values of the TextBoxes.
this.inputDistrictName.Text = Data.districts[districtIndex].Name;
this.inputNeighborhoodName.Text = Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Name;
// If the user only wants to view the item, then the TextBox components are set to read-only and are disabled so the user can't trigger any focus events on them.
if(this.action == "view")
{
foreach(TextBox input in textBoxes)
{
input.ReadOnly = true;
input.Enabled = false;
}
this.buttonConfirm.Hide();
}
}
private void SetTitle()
{
// When a "popup" form is opened, the action the user wants to perform is passed to the popup form's class, and is used to determine what the window title of the form should be.
if(action == "edit")
{
this.Text = "Edit Property";
}
else if(action == "add")
{
this.Text = "Add Property";
}
else if(action == "view")
{
this.Text = "View Property";
}
}
private void GetTextBoxes()
{
this.textBoxes = new TextBox[13] { inputDistrictName, inputNeighborhoodName, inputPropertyID, inputPropertyName, inputHostID, inputHostName, inputHostPropertyCount, inputRoomType, inputRoomPrice, inputLongitude, inputLatitude, inputRoomNights, inputRoomAvailability };
}
private void InputDistrictName_Enter(object sender, EventArgs e)
{
if(inputDistrictName.Text == "District Name...")
{
inputDistrictName.ForeColor = Color.FromArgb(250, 250, 250);
inputDistrictName.Text = "";
}
}
private void InputNeighborhoodName_Enter(object sender, EventArgs e)
{
if(inputNeighborhoodName.Text == "Neighborhood Name...")
{
inputNeighborhoodName.ForeColor = Color.FromArgb(250, 250, 250);
inputNeighborhoodName.Text = "";
}
}
private void InputPropertyID_Enter(object sender, EventArgs e)
{
if(inputPropertyID.Text == "Property ID...")
{
inputPropertyID.Text = "";
inputPropertyID.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputPropertyName_Enter(object sender, EventArgs e)
{
if(inputPropertyName.Text == "Property Name...")
{
inputPropertyName.Text = "";
inputPropertyName.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputHostID_Enter(object sender, EventArgs e)
{
if(inputHostID.Text == "Host ID...")
{
inputHostID.Text = "";
inputHostID.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputHostName_Enter(object sender, EventArgs e)
{
if(inputHostName.Text == "Host Name...")
{
inputHostName.Text = "";
inputHostName.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputHostPropertyCount_Enter(object sender, EventArgs e)
{
if(inputHostPropertyCount.Text == "Number of Properties Owned by Host...")
{
inputHostPropertyCount.Text = "";
inputHostPropertyCount.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputRoomType_Enter(object sender, EventArgs e)
{
if(inputRoomType.Text == "Room Type...")
{
inputRoomType.Text = "";
inputRoomType.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputRoomPrice_Enter(object sender, EventArgs e)
{
if(inputRoomPrice.Text == "Room Price...")
{
inputRoomPrice.Text = "";
inputRoomPrice.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputLongitude_Enter(object sender, EventArgs e)
{
if(inputLongitude.Text == "Longitude...")
{
inputLongitude.Text = "";
inputLongitude.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputLatitude_Enter(object sender, EventArgs e)
{
if(inputLatitude.Text == "Latitude...")
{
inputLatitude.Text = "";
inputLatitude.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputRoomNights_Enter(object sender, EventArgs e)
{
if(inputRoomNights.Text == "Minimum Nights...")
{
inputRoomNights.Text = "";
inputRoomNights.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputRoomAvailability_Enter(object sender, EventArgs e)
{
if(inputRoomAvailability.Text == "Availability Days...")
{
inputRoomAvailability.Text = "";
inputRoomAvailability.ForeColor = Color.FromArgb(250, 250, 250);
}
}
private void InputDistrictName_Leave(object sender, EventArgs e)
{
if(inputDistrictName.Text.Trim() == "")
{
inputDistrictName.Text = "District Name...";
}
}
private void InputNeighborhoodName_Leave(object sender, EventArgs e)
{
if(inputNeighborhoodName.Text.Trim() == "")
{
inputNeighborhoodName.Text = "Neighborhood Name...";
}
}
private void InputPropertyID_Leave(object sender, EventArgs e)
{
if(inputPropertyID.Text.Trim() == "")
{
inputPropertyID.Text = "Property ID...";
}
}
private void InputPropertyName_Leave(object sender, EventArgs e)
{
if(inputPropertyName.Text.Trim() == "")
{
inputPropertyName.Text = "Property Name...";
}
}
private void InputHostID_Leave(object sender, EventArgs e)
{
if(inputHostID.Text.Trim() == "")
{
inputHostID.Text = "Host ID...";
}
}
private void InputHostName_Leave(object sender, EventArgs e)
{
if(inputHostName.Text.Trim() == "")
{
inputHostName.Text = "Host Name...";
}
}
private void InputHostPropertyCount_Leave(object sender, EventArgs e)
{
if(inputHostPropertyCount.Text.Trim() == "")
{
inputHostPropertyCount.Text = "Number of Properties Owned by Host...";
}
}
private void InputRoomType_Leave(object sender, EventArgs e)
{
if(inputRoomType.Text.Trim() == "")
{
inputRoomType.Text = "Room Type...";
}
}
private void InputRoomPrice_Leave(object sender, EventArgs e)
{
if(inputRoomPrice.Text.Trim() == "")
{
inputRoomPrice.Text = "Room Price...";
}
}
private void InputLongitude_Leave(object sender, EventArgs e)
{
if(inputLongitude.Text.Trim() == "")
{
inputLongitude.Text = "Longitude...";
}
}
private void InputLatitude_Leave(object sender, EventArgs e)
{
if(inputLatitude.Text.Trim() == "")
{
inputLatitude.Text = "Latitude...";
}
}
private void InputRoomNights_Leave(object sender, EventArgs e)
{
if(inputRoomNights.Text.Trim() == "")
{
inputRoomNights.Text = "Minimum Nights...";
}
}
private void InputRoomAvailability_Leave(object sender, EventArgs e)
{
if(inputRoomAvailability.Text.Trim() == "")
{
inputRoomAvailability.Text = "Availability Days...";
}
}
private void ButtonDelete_Click(object sender, EventArgs e)
{
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
neighborhood.Properties[propertyIndex] = null;
Property[] properties = new Property[0];
foreach(Property property in neighborhood.Properties)
{
if(property != null)
{
int numberOfProperties = properties.Length;
Array.Resize(ref properties, numberOfProperties + 1);
properties[numberOfProperties] = property;
}
}
Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].PropertyCount -= 1;
Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Properties = properties;
Data.changed = true;
Data.deletedProperty = true;
this.Hide();
}
private void ButtonNext_Click(object sender, EventArgs e)
{
ProcessInput();
}
private void InputFields_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
ProcessInput();
}
}
private void ProcessInput()
{
// The boolean variable "valid" is used to determine whether or not the TextBoxes have been filled out. If they have, then the next button does what's it's actually meant to do.
bool valid = true;
long longResult;
double doubleResult;
try
{
inputDistrictName.ForeColor = Color.FromArgb(250, 250, 250);
inputNeighborhoodName.ForeColor = Color.FromArgb(250, 250, 250);
inputPropertyID.ForeColor = Color.FromArgb(250, 250, 250);
inputPropertyName.ForeColor = Color.FromArgb(250, 250, 250);
inputHostID.ForeColor = Color.FromArgb(250, 250, 250);
inputHostName.ForeColor = Color.FromArgb(250, 250, 250);
inputHostPropertyCount.ForeColor = Color.FromArgb(250, 250, 250);
inputRoomType.ForeColor = Color.FromArgb(250, 250, 250);
inputRoomPrice.ForeColor = Color.FromArgb(250, 250, 250);
inputLongitude.ForeColor = Color.FromArgb(250, 250, 250);
inputLatitude.ForeColor = Color.FromArgb(250, 250, 250);
inputRoomNights.ForeColor = Color.FromArgb(250, 250, 250);
inputRoomAvailability.ForeColor = Color.FromArgb(250, 250, 250);
if(inputDistrictName.Text == "District Name..." || inputDistrictName.Text == "")
{
inputDistrictName.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputNeighborhoodName.Text == "Neighborhood Name..." || inputNeighborhoodName.Text == "")
{
inputNeighborhoodName.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputPropertyID.Text == "Property ID..." || inputPropertyID.Text == "" || !long.TryParse(inputPropertyID.Text, out longResult))
{
inputPropertyID.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputPropertyName.Text == "Property Name..." || inputPropertyName.Text == "")
{
inputPropertyName.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputHostID.Text == "Host ID..." || inputHostID.Text == "" || !long.TryParse(inputHostID.Text, out longResult))
{
inputHostID.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputHostName.Text == "Host Name..." || inputHostName.Text == "")
{
inputHostName.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputHostPropertyCount.Text == "Number of Properties Owned by Host..." || inputHostPropertyCount.Text == "" || !long.TryParse(inputHostPropertyCount.Text, out longResult))
{
inputHostPropertyCount.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputRoomType.Text == "Room Type..." || inputRoomType.Text == "")
{
inputRoomType.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputRoomPrice.Text == "Room Price..." || inputRoomPrice.Text == "")
{
inputRoomPrice.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputLongitude.Text == "Longitude..." || inputLongitude.Text == "" || !double.TryParse(inputLongitude.Text, out doubleResult))
{
inputLongitude.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputLatitude.Text == "Latitude..." || inputLatitude.Text == "" || !double.TryParse(inputLatitude.Text, out doubleResult))
{
inputLatitude.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputRoomNights.Text == "Minimum Nights..." || inputRoomNights.Text == "" || !long.TryParse(inputRoomNights.Text, out longResult))
{
inputRoomNights.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
if(inputRoomAvailability.Text == "Availability Days..." || inputRoomAvailability.Text == "")
{
inputRoomAvailability.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
else
{
if(!long.TryParse(inputRoomAvailability.Text, out longResult))
{
inputRoomAvailability.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
else if(Convert.ToInt32(inputRoomAvailability.Text) > 365 || Convert.ToInt32(inputRoomAvailability.Text) < 0)
{
inputRoomAvailability.ForeColor = Color.FromArgb(240, 100, 50);
valid = false;
}
}
if(valid)
{
if(action == "add")
{
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
Property property = new Property(Convert.ToInt32(this.inputPropertyID.Text), this.inputPropertyName.Text, Convert.ToInt32(this.inputHostID.Text), this.inputHostName.Text, Convert.ToInt32(this.inputHostPropertyCount.Text), Convert.ToDouble(this.inputLongitude.Text), Convert.ToDouble(this.inputLatitude.Text), this.inputRoomType.Text, Convert.ToDouble(this.inputRoomPrice.Text), Convert.ToInt32(this.inputRoomNights.Text), Convert.ToInt32(this.inputRoomAvailability.Text));
neighborhood.AddProperty(property);
Data.districts[districtIndex].Neighborhoods[neighborhoodIndex] = neighborhood;
}
else if(action == "edit")
{
District district = Data.districts[districtIndex];
Neighborhood neighborhood = district.Neighborhoods[neighborhoodIndex];
Property property = neighborhood.Properties[propertyIndex];
property.Id = Convert.ToInt32(this.inputPropertyID.Text);
property.Name = this.inputPropertyName.Text;
property.HostID = Convert.ToInt32(this.inputHostID.Text);
property.HostName = this.inputHostName.Text;
property.Count = Convert.ToInt32(this.inputHostPropertyCount.Text);
property.RoomType = this.inputRoomType.Text;
property.RoomPrice = Convert.ToDouble(this.inputRoomPrice.Text);
property.Longitude = Convert.ToDouble(this.inputLongitude.Text);
property.Latitude = Convert.ToDouble(this.inputLatitude.Text);
property.RoomNights = Convert.ToInt32(this.inputRoomNights.Text);
property.RoomAvailability = Convert.ToInt32(this.inputRoomAvailability.Text);
Data.districts[districtIndex].Neighborhoods[neighborhoodIndex].Properties[propertyIndex] = property;
}
Data.changed = true;
this.Hide();
}
}
catch(Exception e)
{
MessageBox.Show(e.Message, "Error");
}
}
}
}