-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI.cs
256 lines (242 loc) · 7.57 KB
/
UI.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
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Lab3
{
public class UI : Form
{
ComboBox fromBox;
ComboBox toBox;
RadioButton firstClass;
RadioButton secondClass;
RadioButton oneWay;
RadioButton returnWay;
RadioButton noDiscount;
RadioButton twentyDiscount;
RadioButton fortyDiscount;
ComboBox payment;
Button pay;
public UI ()
{
initializeControls ();
}
private void handlePayment(UIInfo info)
{
// Get number of tariefeenheden
int tariefeenheden = Tariefeenheden.getTariefeenheden (info.From, info.To)
// Get price
float price = PricingTable.getPrice (tariefeenheden, getColumn(info));
if (info.Way == UIWay.Return)
price *= 2;
//Initialize payment
paymethod(price, info.Payment);
}
int getColumn(UIInfo info)
{
// Compute the column in the table based on choices
int tableColumn;
// First based on class
switch (info.Class)
{
case UIClass.FirstClass:
tableColumn = 3;
break;
default:
tableColumn = 0;
break;
}
// Then, on the discount
switch (info.Discount)
{
case UIDiscount.TwentyDiscount:
tableColumn += 1;
break;
case UIDiscount.FortyDiscount:
tableColumn += 2;
break;
}
return tableColumn;
}
void paymethod(float price, UIPayment payMethod)
{
//Create a payInfo class
PayInfo payInfo = new PayInfo();
//Select the paymethod and call upon the right method in PayInfo.cs
switch (payMethod) {
case UIPayment.CreditCard:
payInfo.payCredit(price, payMethod);
break;
case UIPayment.DebitCard:
payInfo.payDebit(price, payMethod);
break;
case UIPayment.Cash:
payInfo.payCash(price, payMethod);
break;
}
}
#region Set-up -- don't look at it
private void initializeControls()
{
// Set label
this.Text = "MSO Lab Exercise III";
// this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Width = 500;
this.Height = 210;
// Set layout
var grid = new TableLayoutPanel ();
grid.Dock = DockStyle.Fill;
grid.Padding = new Padding (5);
this.Controls.Add (grid);
grid.RowCount = 4;
grid.RowStyles.Add (new RowStyle (SizeType.Absolute, 20));
grid.RowStyles.Add (new RowStyle (SizeType.Percent, 100));
grid.RowStyles.Add (new RowStyle (SizeType.Absolute, 20));
grid.RowStyles.Add (new RowStyle (SizeType.Absolute, 40));
grid.ColumnCount = 6;
for (int i = 0; i < 6; i++)
{
ColumnStyle c = new ColumnStyle (SizeType.Percent, 16.66666f);
grid.ColumnStyles.Add (c);
}
// Create From and To
var fromLabel = new Label ();
fromLabel.Text = "From:";
fromLabel.TextAlign = ContentAlignment.MiddleRight;
grid.Controls.Add (fromLabel, 0, 0);
fromLabel.Dock = DockStyle.Fill;
fromBox = new ComboBox ();
fromBox.DropDownStyle = ComboBoxStyle.DropDownList;
fromBox.Items.AddRange (Tariefeenheden.getStations ());
fromBox.SelectedIndex = 0;
grid.Controls.Add (fromBox, 1, 0);
grid.SetColumnSpan (fromBox, 2);
fromBox.Dock = DockStyle.Fill;
var toLabel = new Label ();
toLabel.Text = "To:";
toLabel.TextAlign = ContentAlignment.MiddleRight;
grid.Controls.Add (toLabel, 3, 0);
toLabel.Dock = DockStyle.Fill;
toBox = new ComboBox ();
toBox.DropDownStyle = ComboBoxStyle.DropDownList;
toBox.Items.AddRange (Tariefeenheden.getStations ());
toBox.SelectedIndex = 0;
grid.Controls.Add (toBox, 4, 0);
grid.SetColumnSpan (toBox, 2);
toBox.Dock = DockStyle.Fill;
// Create groups
GroupBox classGroup = new GroupBox ();
classGroup.Text = "Class";
classGroup.Dock = DockStyle.Fill;
grid.Controls.Add (classGroup, 0, 1);
grid.SetColumnSpan (classGroup, 2);
var classGrid = new TableLayoutPanel ();
classGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 50));
classGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 50));
classGrid.Dock = DockStyle.Fill;
classGroup.Controls.Add (classGrid);
GroupBox wayGroup = new GroupBox ();
wayGroup.Text = "Amount";
wayGroup.Dock = DockStyle.Fill;
grid.Controls.Add (wayGroup, 2, 1);
grid.SetColumnSpan (wayGroup, 2);
var wayGrid = new TableLayoutPanel ();
wayGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 50));
wayGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 50));
wayGrid.Dock = DockStyle.Fill;
wayGroup.Controls.Add (wayGrid);
GroupBox discountGroup = new GroupBox ();
discountGroup.Text = "Discount";
discountGroup.Dock = DockStyle.Fill;
grid.Controls.Add (discountGroup, 4, 1);
grid.SetColumnSpan (discountGroup, 2);
var discountGrid = new TableLayoutPanel ();
discountGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 33.33333f));
discountGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 33.33333f));
discountGrid.RowStyles.Add (new RowStyle (SizeType.Percent, 33.33333f));
discountGrid.Dock = DockStyle.Fill;
discountGroup.Controls.Add (discountGrid);
// Create radio buttons
firstClass = new RadioButton ();
firstClass.Text = "1st class";
firstClass.Checked = true;
classGrid.Controls.Add (firstClass);
secondClass = new RadioButton ();
secondClass.Text = "2nd class";
classGrid.Controls.Add (secondClass);
oneWay = new RadioButton ();
oneWay.Text = "One-way";
oneWay.Checked = true;
wayGrid.Controls.Add (oneWay);
returnWay = new RadioButton ();
returnWay.Text = "Return";
wayGrid.Controls.Add (returnWay);
noDiscount = new RadioButton ();
noDiscount.Text = "No discount";
noDiscount.Checked = true;
discountGrid.Controls.Add (noDiscount);
twentyDiscount = new RadioButton ();
twentyDiscount.Text = "20% discount";
discountGrid.Controls.Add (twentyDiscount);
fortyDiscount = new RadioButton ();
fortyDiscount.Text = "40% discount";
discountGrid.Controls.Add (fortyDiscount);
// Payment option
Label paymentLabel = new Label ();
paymentLabel.Text = "Payment by:";
paymentLabel.Dock = DockStyle.Fill;
paymentLabel.TextAlign = ContentAlignment.MiddleRight;
grid.Controls.Add (paymentLabel, 0, 2);
payment = new ComboBox ();
payment.DropDownStyle = ComboBoxStyle.DropDownList;
payment.Items.AddRange (new String[] { "Credit card", "Debit card", "Cash" });
payment.SelectedIndex = 0;
payment.Dock = DockStyle.Fill;
grid.Controls.Add (payment, 1, 2);
grid.SetColumnSpan (payment, 5);
// Pay button
pay = new Button ();
pay.Text = "Pay";
pay.Dock = DockStyle.Fill;
grid.Controls.Add (pay, 0, 3);
grid.SetColumnSpan (pay, 6);
// Set up event
pay.Click += (object sender, EventArgs e) => handlePayment(getUIInfo());
}
private UIInfo getUIInfo()
{
UIClass cls;
if (firstClass.Checked)
cls = UIClass.FirstClass;
else
cls = UIClass.SecondClass;
UIWay way;
if (oneWay.Checked)
way = UIWay.OneWay;
else
way = UIWay.Return;
UIDiscount dis;
if (noDiscount.Checked)
dis = UIDiscount.NoDiscount;
else if (twentyDiscount.Checked)
dis = UIDiscount.TwentyDiscount;
else
dis = UIDiscount.FortyDiscount;
UIPayment pment;
switch ((string)payment.SelectedItem) {
case "Credit card":
pment = UIPayment.CreditCard;
break;
case "Debit card":
pment = UIPayment.DebitCard;
break;
default:
pment = UIPayment.Cash;
break;
}
return new UIInfo ((string)fromBox.SelectedItem,
(string)toBox.SelectedItem,
cls, way, dis, pment);
}
#endregion
}
}