This repository was archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaleWindow.cs
504 lines (407 loc) · 19 KB
/
SaleWindow.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cyclone.Objects;
using Gtk;
using Newtonsoft.Json;
namespace Cyclone {
public class SaleWindow : Assistant {
private Entry nameEntry;
private Entry surnameEntry;
private Entry emailEntry;
private Entry phoneEntry;
protected ComboBox paymentCombo;
private List<SpinButton> bikePrices = new List<SpinButton>();
private ComboBox countryCombo;
private List<string> countryList;
private Entry regionEntry;
private Entry cityEntry;
private Entry postCodeEntry;
private Entry addressOneEntry;
private Entry addressTwoEntry;
protected List<Bike> soldBikes;
protected string[] paymentOptions = { "Cash", "Card", "Paypal" };
public const int WIN_W = 180;
public const int WIN_H = 120;
public const uint defPadding = 20;
public const int ICON_SIDE = 32;
public const int TOOL_SIDE = 24;
protected const uint EDITOR_BORDER_WIDTH = 4;
protected const uint CELL_PAD = 4;
protected const float V_MIDDLE = 0.5f;
protected const float H_START = 0;
public SaleWindow(List<Bike> saleBikes) {
soldBikes = saleBikes;
WindowProperties();
Widget basicInfoPage = CustomerInfo;
AppendPage(basicInfoPage);
SetPageTitle(basicInfoPage, "Customer Information");
SetPageType (basicInfoPage, AssistantPageType.Content);
Widget paymentPage = PaymentInfo;
AppendPage(paymentPage);
SetPageTitle(paymentPage, bikeCostsTitle);
SetPageType (paymentPage, AssistantPageType.Content);
Widget addressPage = CustomerAddress;
AppendPage(addressPage);
SetPageTitle(addressPage, "Customer Address");
SetPageType (addressPage, AssistantPageType.Content);
Widget completeSale = ReviewSale;
AppendPage (completeSale);
SetPageTitle (completeSale, reviewTitle);
SetPageType (completeSale, AssistantPageType.Confirm);
SetPageComplete(completeSale, true);
}
/// <summary>
/// Sets the window properties: size and icon.
/// </summary>
private void WindowProperties() {
SetDefaultSize(WIN_W, WIN_H);
SetSizeRequest(WIN_W, WIN_H);
//Resizable = false;
Title = "Sale Assistant";
// Set window icon
Gdk.Pixbuf windowIcon = new Gdk.Pixbuf(System.Reflection.Assembly.GetEntryAssembly(),
iconName, ICON_SIDE, ICON_SIDE);
Icon = windowIcon;
Cancel += AssistantCancel;
Close += AssistantClose;
}
private void AssistantClose(object sender, EventArgs e) {
Destroy();
}
protected Widget CustomerInfo {
get {
uint customerRows = 5;
uint customerColumns = 2;
// First Column
uint fStart = 0;
uint fSpan = 1;
uint fEnd = fStart + fSpan;
// Second Column
uint sStart = 1;
uint sSpan = 1;
uint sEnd = sStart + sSpan;
Table basicTable = new Table(customerRows, customerColumns, true);
basicTable.BorderWidth = EDITOR_BORDER_WIDTH;
basicTable.Halign = Align.Center;
basicTable.Expand = true;
Label customerLabel = new Label("<b>CUSTOMER INFORMATION:</b>");
customerLabel.UseMarkup = true;
customerLabel.SetAlignment(H_START, V_MIDDLE);
Label nameLabel = new Label("Name*");
nameLabel.SetAlignment(H_START, V_MIDDLE);
Label surnameLabel = new Label("Surname*");
surnameLabel.SetAlignment(H_START, V_MIDDLE);
Label emailLabel = new Label("E-Mail");
emailLabel.SetAlignment(H_START, V_MIDDLE);
Label phoneLabel = new Label("Phone Number*");
phoneLabel.SetAlignment(H_START, V_MIDDLE);
nameEntry = new Entry();
nameEntry.Changed += ValidateBasic;
surnameEntry = new Entry();
surnameEntry.Changed += ValidateBasic;
emailEntry = new Entry();
emailEntry.Changed += ValidateBasic;
phoneEntry = new Entry();
phoneEntry.Changed += ValidateBasic;
List<Widget> basicWgtList = new List<Widget> { null, null, nameLabel, surnameLabel,
nameEntry, surnameEntry, emailLabel, phoneLabel, emailEntry, phoneEntry};
uint row = 0;
basicTable.Attach(customerLabel, fStart, sEnd, row, ++row, AttachOptions.Expand, AttachOptions.Fill, CELL_PAD, CELL_PAD);
for (; row < customerRows; row++) {
basicTable.Attach(basicWgtList[(int)row*2], fStart, fEnd, row, row + 1, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
basicTable.Attach(basicWgtList[((int)row*2)+1], sStart, sEnd, row, row + 1, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
}
return basicTable;
}
}
protected Widget CustomerAddress {
get {
uint addressRows= 9;
uint addressColumns = 2;
// First Column
uint genStart = 0;
uint genSpan = 1;
uint genEnd = genStart + genSpan;
// Second Column
uint regionStart = 1;
uint regionSpan = 1;
uint regionEnd = regionStart + regionSpan;
Table addressTable = new Table(addressRows, addressColumns, true);
addressTable.BorderWidth = EDITOR_BORDER_WIDTH;
addressTable.Halign = Align.Center;
addressTable.Expand = true;
Label addressLabel = new Label("<b>CUSTOMER ADDRESS:</b>");
addressLabel.UseMarkup = true;
addressLabel.SetAlignment(H_START, V_MIDDLE);
Label countryLabel = new Label("Country*");
countryLabel.SetAlignment(H_START, V_MIDDLE);
Label regionLabel = new Label("County*");
regionLabel.SetAlignment(H_START, V_MIDDLE);
Label cityLabel = new Label("City*");
cityLabel.SetAlignment(H_START, V_MIDDLE);
Label postCodeLabel = new Label("Post Code*");
postCodeLabel.SetAlignment(H_START, V_MIDDLE);
Label addressOneLabel = new Label("Street and House No.*");
addressOneLabel.SetAlignment(H_START, V_MIDDLE);
Label addressTwoLabel = new Label("Apartment, Suite, Unit, Floor");
addressTwoLabel.SetAlignment(H_START, V_MIDDLE);
countryList = GetCountries();
countryCombo = new ComboBox(countryList.ToArray());
countryCombo.Active = 240;
regionEntry = new Entry();
regionEntry.Changed += ValidateAddress;
cityEntry = new Entry();
cityEntry.Changed += ValidateAddress;
postCodeEntry = new Entry();
postCodeEntry.Changed += ValidateAddress;
addressOneEntry = new Entry();
addressOneEntry.Changed += ValidateAddress;
addressTwoEntry = new Entry();
uint row = 0;
uint wgtSpan = 1;
int areaRows = 5;
int rowWgts = 2;
addressTable.Attach(addressLabel, genStart, regionEnd, row, ++row, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
List<Widget> addressWgts = new List<Widget> { null, null, countryLabel, regionLabel, countryCombo, regionEntry,
cityLabel, postCodeLabel, cityEntry, postCodeEntry};
for (; row < areaRows; row++) {
addressTable.Attach(addressWgts[(int)row * rowWgts], genStart, genEnd, row, row+ wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
addressTable.Attach(addressWgts[(int)((row * rowWgts) + wgtSpan)], regionStart, regionEnd, row, row + wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
}
addressTable.Attach(addressOneLabel, genStart, genEnd, row, row + wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
row++;
addressTable.Attach(addressOneEntry, genStart, regionEnd, row, row + wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
row++;
addressTable.Attach(addressTwoLabel, genStart, genEnd, row, row + wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
row++;
addressTable.Attach(addressTwoEntry, genStart, regionEnd, row, row + wgtSpan, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
return addressTable;
}
}
protected virtual Widget PaymentInfo {
get {
// First Column
uint modelStart = 0;
uint modelSpan = 1;
uint modelEnd = modelStart + modelSpan;
// Second Column
uint costStart = 1;
uint costSpan = 1;
uint costEnd = costStart + costSpan;
uint paymentRows = 3 + (uint) soldBikes.Count;
uint paymentColumns = 2;
ScrolledWindow bikeCostScroll = new ScrolledWindow();
Table paymentTable = new Table(paymentRows, paymentColumns, true);
Label paymentLabel = new Label("<b>PAYMENT VIA:</b>");
paymentLabel.UseMarkup = true;
paymentLabel.SetAlignment(H_START, V_MIDDLE);
paymentCombo = new ComboBox(paymentOptions);
paymentCombo.Changed += ValidatePayment;
paymentTable.Attach(paymentLabel, modelStart, modelEnd, 0, 1, AttachOptions.Fill, AttachOptions.Fill, 60, 12);
paymentTable.Attach(paymentCombo, modelStart, costEnd, 1, 2, AttachOptions.Fill, AttachOptions.Fill, 60, 12);
Label bikeTitle = new Label("<b>BIKE MODEL:</b>");
bikeTitle.UseMarkup = true;
Label priceTitle = new Label("<b>COST (GBP):</b>");
priceTitle.UseMarkup = true;
paymentTable.Attach(bikeTitle, modelStart, modelEnd, 2, 3, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
paymentTable.Attach(priceTitle, costStart, costEnd, 2, 3, AttachOptions.Fill, AttachOptions.Fill, CELL_PAD, CELL_PAD);
uint tableRowCounter = 3;
uint labelSpan = 1;
foreach (Bike soldBike in soldBikes) {
Label bikeName = new Label(string.Format("{0}, {1} ({2})", soldBike.Model, soldBike.Make, soldBike.SecurityCode));
bikeName.Selectable = true;
Adjustment priceAdj = new Adjustment(399, 0, 1000000, 1, 10, 10);
SpinButton costSpin = new SpinButton(priceAdj, 50, 2);
bikePrices.Add(costSpin);
paymentTable.Attach(bikeName, modelStart, modelEnd, tableRowCounter, tableRowCounter + labelSpan, AttachOptions.Expand, AttachOptions.Fill, CELL_PAD, CELL_PAD);
paymentTable.Attach(costSpin, costStart, costEnd, tableRowCounter, tableRowCounter + labelSpan, AttachOptions.Expand, AttachOptions.Fill, CELL_PAD, CELL_PAD);
tableRowCounter++;
}
bikeCostScroll.Add(paymentTable);
return bikeCostScroll;
}
}
protected virtual Widget ReviewSale {
get {
VBox reviewBox = new VBox();
string pluralBikes = "";
if (soldBikes.Count > 1) {
pluralBikes = "s";
}
Label completeSale = new Label (string.Format("Proceed with the sale of {0} bike{1}?", soldBikes.Count, pluralBikes));
reviewBox.PackStart(completeSale, true, true, defPadding);
return reviewBox;
}
}
private List<string> GetCountries() {
Dictionary<string, string> countryDict = new Dictionary<string, string>();
try {
countryDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText("countries.json"));
} catch (FileNotFoundException) {
Console.WriteLine("Could not get list of countries");
}
return countryDict.Values.ToList();
}
public Sale ParseSale() {
Address clientAddress = new Address(AddressCountry, AddressRegion, AddressCity, AddressOne, AddressTwo, AddressCode);
ClientData clientData = new ClientData(CustomerName, CustomerSurname, CustomerPhone, clientAddress);
clientData.Mail = CustomerEmail;
Sale newSale = new Sale(soldBikes, BikeEarnings, clientData, PaymentMethod);
return newSale;
}
protected void ValidateBasic (object o, EventArgs args) {
bool basicEmpty = string.IsNullOrWhiteSpace(nameEntry.Text);
basicEmpty = basicEmpty || string.IsNullOrWhiteSpace(surnameEntry.Text);
basicEmpty = basicEmpty || string.IsNullOrWhiteSpace(phoneEntry.Text);
bool emailValid = true;
if (!string.IsNullOrWhiteSpace(emailEntry.Text)) {
emailValid = emailEntry.Text.Contains('@');
}
bool phoneValid = phoneEntry.Text.Any(char.IsDigit);
bool basicValid = !basicEmpty && emailValid && phoneValid;
SetPageComplete(GetNthPage (CurrentPage), basicValid);
}
protected void ValidatePayment(object o, EventArgs args) {
bool paymentEmpty = paymentCombo.Active == -1;
SetPageComplete(GetNthPage (CurrentPage), !paymentEmpty);
}
protected void ValidateAddress(object o, EventArgs args) {
bool addressEmpty = countryCombo.Active == -1;
addressEmpty = addressEmpty || string.IsNullOrWhiteSpace(regionEntry.Text);
addressEmpty = addressEmpty || string.IsNullOrWhiteSpace(cityEntry.Text);
addressEmpty = addressEmpty || string.IsNullOrWhiteSpace(postCodeEntry.Text);
addressEmpty = addressEmpty || string.IsNullOrWhiteSpace(addressOneEntry.Text);
SetPageComplete(GetNthPage (CurrentPage), !addressEmpty);
}
protected string AddressCountry {
get {
if (countryCombo.Active == -1) {
throw new IndexOutOfRangeException();
}
return countryList[countryCombo.Active];
}
}
protected int PaymentMethod {
get {
if (paymentCombo.Active == -1) {
throw new IndexOutOfRangeException();
}
return paymentCombo.Active;
}
}
public string CustomerName {
get {
if (string.IsNullOrWhiteSpace(nameEntry.Text)) {
throw new FormatException();
}
return nameEntry.Text;
} set {
nameEntry.Text = value;
}
}
public string CustomerSurname {
get {
if (string.IsNullOrWhiteSpace(surnameEntry.Text)) {
throw new FormatException();
}
return surnameEntry.Text;
} set {
surnameEntry.Text = value;
}
}
public string CustomerPhone {
get {
if (string.IsNullOrWhiteSpace(phoneEntry.Text)) {
throw new FormatException();
}
return phoneEntry.Text;
} set {
phoneEntry.Text = value;
}
}
public string CustomerEmail {
get {
return emailEntry.Text;
} set {
emailEntry.Text = value;
}
}
public string AddressRegion {
get {
if (string.IsNullOrWhiteSpace(regionEntry.Text)) {
throw new FormatException();
}
return regionEntry.Text;
} set {
regionEntry.Text = value;
}
}
public string AddressCity {
get {
if (string.IsNullOrWhiteSpace(cityEntry.Text)) {
throw new FormatException();
}
return cityEntry.Text;
} set {
cityEntry.Text = value;
}
}
public string AddressCode {
get {
if (string.IsNullOrWhiteSpace(postCodeEntry.Text)) {
throw new FormatException();
}
return postCodeEntry.Text;
} set {
postCodeEntry.Text = value;
}
}
public string AddressOne {
get {
if (string.IsNullOrWhiteSpace(addressOneEntry.Text)) {
throw new FormatException();
}
return addressOneEntry.Text;
} set {
addressOneEntry.Text = value;
}
}
public string AddressTwo {
get {
return addressTwoEntry.Text;
} set {
addressTwoEntry.Text = value;
}
}
public List<int> BikeEarnings {
get {
List<int> bikeEarnings = new List<int>();
foreach (SpinButton costSelector in bikePrices) {
bikeEarnings.Add(costSelector.ValueAsInt);
}
return bikeEarnings;
}
}
void AssistantCancel (object o, EventArgs args) {
Console.WriteLine ("Sale assistant cancelled.");
Destroy();
}
protected virtual string iconName {
get {
return "Cyclone.Assets.Money.png";
}
}
protected virtual string reviewTitle {
get {
return "Review Sale";
}
}
protected virtual string bikeCostsTitle {
get {
return "Bike Costs And Payment";
}
}
}
}