-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
573 lines (497 loc) · 22.5 KB
/
Form1.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace PizzaMakerForm
{
public partial class Form1 : Form
{
private decimal basePrice = 0;
private decimal grandTotal = 0;
private decimal halfToppingPrice = 1.00m;
private decimal fullToppingPrice = 2.00m;
private decimal stuffedCrustPriceModifier = 0.20m;
private decimal TenInchPrice = 10m;
private decimal TwelveInchPrice = 12m;
private decimal FourteenInchPrice = 14m;
private enum ToppingPosition
{
LeftHalf,
RightHalf,
Full
}
public Form1()
{
InitializeComponent();
InitializePizzaSizeComboBox();
InitializeCrustTypeComboBox();
WireUpToppingRadioButtons();
// Set the PictureBox to display the combined baseline pizza image with crust, sauce, and cheese
pbPizza.Image = CreateBaselineImage();
// Set the images to indicate topping placement
filledButtonLeft.Image = CreateFilledButton(filledButtonLeft, Properties.Resources.FullRedCircle, "Left");
filledButtonCenter.Image = CreateFilledButton(filledButtonCenter, Properties.Resources.FullRedCircle, "Center");
filledButtonRight.Image = CreateFilledButton(filledButtonRight, Properties.Resources.FullRedCircle, "Right");
// Subscribe to selected index changed events to update the price total when pizza size or crust is selected
PizzaSize.SelectedIndexChanged += new EventHandler(UpdateBasePriceAndTotal);
CrustType.SelectedIndexChanged += new EventHandler(UpdateCrustAndTotal);
// Disable the radio button used for display purposes only, not for user interaction
radioButtonEmptyToppingDisplay.Enabled = false;
// Configure the ordersListBox to use custom drawing
ordersListBox.DrawMode = DrawMode.OwnerDrawFixed;
ordersListBox.DrawItem += new DrawItemEventHandler(ordersListBox_DrawItem);
// Update the list box's horizontal extent to ensure all content is visible
UpdateHorizontalExtent();
}
// Custom-draws each item in a ListBox, rendering text and adding a separator line beneath each item.
private void ordersListBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
if (e.Index >= 0)
{
// Calculate the vertical position to center the text within the bounds
SizeF textSize = e.Graphics.MeasureString(ordersListBox.Items[e.Index].ToString(), e.Font);
float textY = e.Bounds.Y + (e.Bounds.Height - textSize.Height) / 2;
e.Graphics.DrawString(ordersListBox.Items[e.Index].ToString(),
e.Font,
new SolidBrush(e.ForeColor),
e.Bounds.X,
textY); // Use the calculated Y-coordinate
// Draw a line at the bottom of the item
e.Graphics.DrawLine(Pens.Black,
e.Bounds.X,
e.Bounds.Bottom - 1, // Draw line at the bottom of the item
e.Bounds.Right,
e.Bounds.Bottom - 1);
}
}
// Method to update the horizontal extent
private void UpdateHorizontalExtent()
{
int maxWidth = 0;
foreach (var item in ordersListBox.Items)
{
int itemWidth = TextRenderer.MeasureText(item.ToString(), ordersListBox.Font).Width;
if (itemWidth > maxWidth)
{
maxWidth = itemWidth;
}
}
ordersListBox.HorizontalExtent = maxWidth;
}
private Image CreateFilledButton(PictureBox pictureBox, Image imageToDraw, string side)
{
Bitmap buttonImage = new Bitmap(pictureBox.Width, pictureBox.Height);
Rectangle destinationRect;
using (Graphics g = Graphics.FromImage(buttonImage))
{
// The source rectangle from the original image
Rectangle sourceRect = new Rectangle(0, 0, imageToDraw.Width / 2, imageToDraw.Height);
if (side == "Left")
{
destinationRect = new Rectangle(0, 0, pictureBox.Width / 2, pictureBox.Height);
}
else if (side == "Right")
{
sourceRect.X = imageToDraw.Width / 2;
destinationRect = new Rectangle(pictureBox.Width / 2, 0, pictureBox.Width / 2, pictureBox.Height);
}
else // "Center" or default
{
sourceRect = new Rectangle(0, 0, imageToDraw.Width, imageToDraw.Height);
destinationRect = new Rectangle(0, 0, pictureBox.Width, pictureBox.Height);
}
// Draw the image onto the button using the source and destination rectangles
g.DrawImage(imageToDraw, destinationRect, sourceRect, GraphicsUnit.Pixel);
// Set the drawing quality
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
}
return buttonImage;
}
private Image CreateBaselineImage()
{
var crust = Properties.Resources.crust;
var sauce = Properties.Resources.doublesauce;
var cheese = Properties.Resources.doublecheese;
Bitmap baselineImage = new Bitmap(pbPizza.Width, pbPizza.Height);
using (Graphics g = Graphics.FromImage(baselineImage))
{
// Draw the crust, scaling it to fit the new bitmap
g.DrawImage(crust, new Rectangle(0, 0, pbPizza.Width, pbPizza.Height));
// Layer the sauce on top, scaling it as well
g.DrawImage(sauce, new Rectangle(0, 0, pbPizza.Width, pbPizza.Height));
// Layer the cheese on top, scaling it as well
g.DrawImage(cheese, new Rectangle(0, 0, pbPizza.Width, pbPizza.Height));
// Set the drawing quality
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
}
return baselineImage;
}
private void UpdatePizzaWithToppings()
{
// Start with the baseline image
Bitmap pizzaWithToppings = new Bitmap(CreateBaselineImage());
using (Graphics g = Graphics.FromImage(pizzaWithToppings))
{
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int width = pizzaWithToppings.Width;
int height = pizzaWithToppings.Height;
// Pepperoni
if (!radioButtonPepperoniNone.Checked)
{
if (radioButtonPepperoniLeft.Checked)
{
DrawTopping(g, radioButtonPepperoniLeft, Properties.Resources.pepperoni, ToppingPosition.LeftHalf, width, height);
}
else if (radioButtonPepperoniRight.Checked)
{
DrawTopping(g, radioButtonPepperoniRight, Properties.Resources.pepperoni, ToppingPosition.RightHalf, width, height);
}
else if (radioButtonPepperoniCenter.Checked)
{
DrawTopping(g, radioButtonPepperoniCenter, Properties.Resources.pepperoni, ToppingPosition.Full, width, height);
}
}
// Ham
if (!radioButtonHamNone.Checked)
{
if (radioButtonHamLeft.Checked)
{
DrawTopping(g, radioButtonHamLeft, Properties.Resources.ham, ToppingPosition.LeftHalf, width, height);
}
else if (radioButtonHamRight.Checked)
{
DrawTopping(g, radioButtonHamRight, Properties.Resources.ham, ToppingPosition.RightHalf, width, height);
}
else if (radioButtonHamCenter.Checked)
{
DrawTopping(g, radioButtonHamCenter, Properties.Resources.ham, ToppingPosition.Full, width, height);
}
}
// Onions
if (!radioButtonOnionsNone.Checked)
{
if (radioButtonOnionsLeft.Checked)
{
DrawTopping(g, radioButtonOnionsLeft, Properties.Resources.onion, ToppingPosition.LeftHalf, width, height);
}
else if (radioButtonOnionsRight.Checked)
{
DrawTopping(g, radioButtonOnionsRight, Properties.Resources.onion, ToppingPosition.RightHalf, width, height);
}
else if (radioButtonOnionsCenter.Checked)
{
DrawTopping(g, radioButtonOnionsCenter, Properties.Resources.onion, ToppingPosition.Full, width, height);
}
}
// Mushrooms
if (!radioButtonMushroomsNone.Checked)
{
if (radioButtonMushroomsLeft.Checked)
{
DrawTopping(g, radioButtonMushroomsLeft, Properties.Resources.mushroom, ToppingPosition.LeftHalf, width, height);
}
else if (radioButtonMushroomsRight.Checked)
{
DrawTopping(g, radioButtonMushroomsRight, Properties.Resources.mushroom, ToppingPosition.RightHalf, width, height);
}
else if (radioButtonMushroomsCenter.Checked)
{
DrawTopping(g, radioButtonMushroomsCenter, Properties.Resources.mushroom, ToppingPosition.Full, width, height);
}
}
// Jalapenos
if (!radioButtonJalapenosNone.Checked)
{
if (radioButtonJalapenosLeft.Checked)
{
DrawTopping(g, radioButtonJalapenosLeft, Properties.Resources.jalapeno, ToppingPosition.LeftHalf, width, height);
}
else if (radioButtonJalapenosRight.Checked)
{
DrawTopping(g, radioButtonJalapenosRight, Properties.Resources.jalapeno, ToppingPosition.RightHalf, width, height);
}
else if (radioButtonJalapenosCenter.Checked)
{
DrawTopping(g, radioButtonJalapenosCenter, Properties.Resources.jalapeno, ToppingPosition.Full, width, height);
}
}
}
pbPizza.Image?.Dispose(); // Dispose of the previous image if it exists
pbPizza.Image = pizzaWithToppings; // Update Pizza image
}
private void DrawTopping(Graphics g, RadioButton radioButton, Image toppingImage, ToppingPosition toppingPosition, int width, int height)
{
if (radioButton.Checked)
{
switch (toppingPosition)
{
case ToppingPosition.Full:
g.DrawImage(toppingImage, new Rectangle(0, 0, width, height));
break;
case ToppingPosition.LeftHalf:
DrawHalfTopping(g, toppingImage, new Rectangle(0, 0, width / 2, height), new Rectangle(0, 0, toppingImage.Width / 2, toppingImage.Height));
break;
case ToppingPosition.RightHalf:
DrawHalfTopping(g, toppingImage, new Rectangle(width / 2, 0, width / 2, height), new Rectangle(toppingImage.Width / 2, 0, toppingImage.Width / 2, toppingImage.Height));
break;
default:
MessageBox.Show($"Unexpected topping position: {toppingPosition}. Please select a valid option.", "Invalid Topping Position", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
}
}
private void DrawHalfTopping(Graphics g, Image toppingImage, Rectangle destinationRect, Rectangle sourceRect)
{
g.DrawImage(toppingImage, destinationRect, sourceRect, GraphicsUnit.Pixel);
}
private void InitializePizzaSizeComboBox()
{
PizzaSize.Items.Add("10\"");
PizzaSize.Items.Add("12\"");
PizzaSize.Items.Add("14\"");
}
private void InitializeCrustTypeComboBox()
{
CrustType.Items.Add("Regular");
CrustType.Items.Add("Thin");
CrustType.Items.Add("Stuffed");
}
private void UpdateCrustAndTotal(object sender, EventArgs e)
{
CalculateTotalPrice();
}
private void ToppingRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (sender is RadioButton rb && rb.Checked)
{
CalculateTotalPrice();
UpdatePizzaWithToppings();
}
}
private void WireUpToppingRadioButtons()
{
var allToppingRadioButtons = new RadioButton[] {
radioButtonPepperoniLeft, radioButtonPepperoniCenter, radioButtonPepperoniRight, radioButtonPepperoniNone,
radioButtonHamLeft, radioButtonHamCenter, radioButtonHamRight, radioButtonHamNone,
radioButtonOnionsLeft, radioButtonOnionsCenter, radioButtonOnionsRight, radioButtonOnionsNone,
radioButtonMushroomsLeft, radioButtonMushroomsCenter, radioButtonMushroomsRight, radioButtonMushroomsNone,
radioButtonJalapenosLeft, radioButtonJalapenosCenter, radioButtonJalapenosRight, radioButtonJalapenosNone
};
foreach (RadioButton rb in allToppingRadioButtons)
{
rb.CheckedChanged += ToppingRadioButton_CheckedChanged;
}
}
private decimal GetCrustPriceAdjustment()
{
decimal updatedPrice = 0m;
if (CrustType.SelectedItem.ToString() == "Stuffed")
{
updatedPrice = basePrice * stuffedCrustPriceModifier;
}
return updatedPrice;
}
private void CalculateTotalPrice()
{
if (PizzaSize.SelectedIndex != -1 && CrustType.SelectedIndex != -1)
{
decimal crustPriceAdjustment = GetCrustPriceAdjustment();
int halfToppingCount = CountHalfToppings();
int fullToppingCount = CountFullToppings();
decimal toppingsPrice = (halfToppingCount * halfToppingPrice) + (fullToppingCount * fullToppingPrice);
decimal totalPrice = basePrice + crustPriceAdjustment + toppingsPrice;
PizzaTotalLabel.Text = $"Pizza Total: ${totalPrice.ToString("0.00")}";
}
}
private int CountHalfToppings()
{
int count = 0;
// Check left buttons
count += radioButtonPepperoniLeft.Checked ? 1 : 0;
count += radioButtonHamLeft.Checked ? 1 : 0;
count += radioButtonOnionsLeft.Checked ? 1 : 0;
count += radioButtonMushroomsLeft.Checked ? 1 : 0;
count += radioButtonJalapenosLeft.Checked ? 1 : 0;
// Check right buttons
count += radioButtonPepperoniRight.Checked ? 1 : 0;
count += radioButtonHamRight.Checked ? 1 : 0;
count += radioButtonOnionsRight.Checked ? 1 : 0;
count += radioButtonMushroomsRight.Checked ? 1 : 0;
count += radioButtonJalapenosRight.Checked ? 1 : 0;
return count;
}
private int CountFullToppings()
{
int count = 0;
count += radioButtonPepperoniCenter.Checked ? 1 : 0;
count += radioButtonHamCenter.Checked ? 1 : 0;
count += radioButtonOnionsCenter.Checked ? 1 : 0;
count += radioButtonMushroomsCenter.Checked ? 1 : 0;
count += radioButtonJalapenosCenter.Checked ? 1 : 0;
return count;
}
private void UpdateBasePriceAndTotal(object sender, EventArgs e)
{
switch (PizzaSize.SelectedItem.ToString())
{
case "10\"":
basePrice = TenInchPrice;
break;
case "12\"":
basePrice = TwelveInchPrice;
break;
case "14\"":
basePrice = FourteenInchPrice;
break;
default:
basePrice = 0m;
break;
}
CalculateTotalPrice();
}
private void addToCartButton_Click(object sender, EventArgs e)
{
bool isValidSize = true;
bool isValidCrust = true;
if (PizzaSize.SelectedIndex < 0)
{
MessageBox.Show("Please select a pizza size.", "Selection Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
isValidSize = false;
}
if (CrustType.SelectedIndex < 0)
{
MessageBox.Show("Please select a crust type.", "Selection Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
isValidCrust = false;
}
if (isValidSize && isValidCrust)
{
CalculateTotalPrice(); // Ensure the total price is up to date
string selectedSize = PizzaSize.SelectedItem.ToString();
string selectedCrust = CrustType.SelectedItem.ToString();
string orderDetails = $"Size: {selectedSize} || Crust: {selectedCrust} || ";
// Include selected toppings in the order details
orderDetails += "Toppings: " + GetSelectedToppings() + " || ";
orderDetails += PizzaTotalLabel.Text;
// Add this order detail to the ListBox
ordersListBox.Items.Add(orderDetails);
UpdateGrandTotal();
UpdateHorizontalExtent();
}
}
private string GetSelectedToppings()
{
List<string> toppings = new List<string>();
if (radioButtonPepperoniCenter.Checked)
{
toppings.Add("Pepperoni");
}
else if (radioButtonPepperoniLeft.Checked)
{
toppings.Add("Pepperoni (Left Half)");
}
else if (radioButtonPepperoniRight.Checked)
{
toppings.Add("Pepperoni (Right Half)");
}
if (radioButtonHamCenter.Checked)
{
toppings.Add("Ham");
}
else if (radioButtonHamLeft.Checked)
{
toppings.Add("Ham (Left Half)");
}
else if (radioButtonHamRight.Checked)
{
toppings.Add("Ham (Right Half)");
}
if (radioButtonOnionsCenter.Checked)
{
toppings.Add("Onions");
}
else if (radioButtonOnionsLeft.Checked)
{
toppings.Add("Onions (Left Half)");
}
else if (radioButtonOnionsRight.Checked)
{
toppings.Add("Onions (Right Half)");
}
if (radioButtonMushroomsCenter.Checked)
{
toppings.Add("Mushrooms");
}
else if (radioButtonMushroomsLeft.Checked)
{
toppings.Add("Mushrooms (Left Half)");
}
else if (radioButtonMushroomsRight.Checked)
{
toppings.Add("Mushrooms (Right Half)");
}
if (radioButtonJalapenosCenter.Checked)
{
toppings.Add("Jalapenos");
}
else if (radioButtonJalapenosLeft.Checked)
{
toppings.Add("Jalapenos (Left Half)");
}
else if (radioButtonJalapenosRight.Checked)
{
toppings.Add("Jalapenos (Right Half)");
}
if (toppings.Count == 0)
{
return "None";
}
return string.Join(", ", toppings);
}
private void UpdateGrandTotal()
{
grandTotal = 0m; // Reset grandTotal before recalculating
foreach (var item in ordersListBox.Items)
{
string itemString = item.ToString();
string costString = itemString.Substring(itemString.LastIndexOf("$") + 1);
if (decimal.TryParse(costString, out decimal itemCost))
{
grandTotal += itemCost;
}
}
GrandTotalLabel.Text = $"Grand Total: ${grandTotal:0.00}";
}
private void ordersListBox_Click(object sender, EventArgs e)
{
if (ordersListBox.SelectedIndex >= 0)
{
var result = MessageBox.Show("Would you like to remove this item from the cart?",
"Remove Item",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
ordersListBox.Items.RemoveAt(ordersListBox.SelectedIndex);
UpdateGrandTotal();
}
}
else
{
MessageBox.Show("Please select an item to remove.",
"Select Item",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}