1 // Student: Pat Moss
2 // Instructor: Dave Hammer
3 // CIS162AD C# 5832 - Quiz #2 - Order Form Calculator
4 // 04/09/2006 Rev. 01a (includes try/catch blocks and Help)
5
6 // III. Programming Exercises: Write source code for the following:
7 // Create an order form that allows bags to be purchased. There
8 // are six different types: full decorative, beaded, needlepoint
9 // design, fringed, fringed beaded, and plain. Create a ListBox
10 // object for the different styles. After the user makes a
11 // selection, display a message indicating which selection was
12 // made. Add to the application by including a control that
13 // allows the user to enter the quantity desired. Include a set
14 // of radio buttons that contain shipping options of overnight,
15 // three day, and standard. The price for each bag is as follows:
16 // full decorative $50, beaded $45, needlepoint $40, fringed $25,
17 // fringed beaded $50, and plain $20. The shipping charges are
18 // based on the total purchase. The following percentages are
19 // used: overnight 10%, three day 7%, and standard 5%. Display
20 // in a message box the shipping charge along with the selection,
21 // quantity, and total cost.
22 // Turn in your source code for this program.
23
24 using System;
25 using System.Drawing;
26 using System.Collections;
27 using System.ComponentModel;
28 using System.Windows.Forms;
29 using System.Data;
30
31 namespace orderformcalculator
32 {
33 ///
34 /// Summary description for OrderFormCalculator form
35 ///
36 public class OrderFormCalculator : System.Windows.Forms.Form
37 {
38 private System.Windows.Forms.Label label1;
39 private System.Windows.Forms.Label label2;
40 private System.Windows.Forms.Button btnCalculate;
41 private System.Windows.Forms.Button btnExit;
42 private System.Windows.Forms.Label label3;
43 private System.Windows.Forms.Label label5;
44 private System.Windows.Forms.Button btnClear;
45 private System.Windows.Forms.Button btnHelp;
46 private System.Windows.Forms.Label label4;
47 private System.Windows.Forms.TextBox textShipPcnt;
48 private System.Windows.Forms.TextBox textShipCost;
49 private System.Windows.Forms.Label label6;
50 private System.Windows.Forms.TextBox textItmQty;
51 private System.Windows.Forms.TextBox textPurchTotal;
52 private System.Windows.Forms.GroupBox groupBox1;
53 private System.Windows.Forms.RadioButton radBtn1;
54 private System.Windows.Forms.RadioButton radBtn2;
55 private System.Windows.Forms.RadioButton radBtn3;
56 private System.Windows.Forms.Label label7;
57 private System.Windows.Forms.TextBox textItmEach;
58 private System.Windows.Forms.TextBox textItmTotal;
59 private System.Windows.Forms.ComboBox cboItmPrices;
60
61 ///
62 /// Required designer variable
63 ///
64 private System.ComponentModel.Container components = null;
65
66 public OrderFormCalculator()
67 {
68 //
69 // Required for Windows Form Designer support
70 //
71 InitializeComponent();
72
73 //
74 // TODO: Add any constructor code after InitializeComponent call
75 //
76 }
77
78 ///
79 /// Clean up any resources being used
80 ///
81 protected override void Dispose( bool disposing )
82 {
83 if( disposing )
84 {
85 if (components != null)
86 {
87 components.Dispose();
88 }
89 }
90 base.Dispose( disposing );
91 }
92
93 #region Windows Form Designer generated code
94 ///
95 /// Required method for Designer support - do not modify
96 /// the contents of this method with the code editor
97 ///
98 private void InitializeComponent()
99 {
100 this.cboItmPrices = new System.Windows.Forms.ComboBox();
101 this.textItmQty = new System.Windows.Forms.TextBox();
102 this.textItmEach = new System.Windows.Forms.TextBox();
103 this.label1 = new System.Windows.Forms.Label();
104 this.label2 = new System.Windows.Forms.Label();
105 this.btnCalculate = new System.Windows.Forms.Button();
106 this.btnExit = new System.Windows.Forms.Button();
107 this.label3 = new System.Windows.Forms.Label();
108 this.textItmTotal = new System.Windows.Forms.TextBox();
109 this.label5 = new System.Windows.Forms.Label();
110 this.btnClear = new System.Windows.Forms.Button();
111 this.btnHelp = new System.Windows.Forms.Button();
112 this.label4 = new System.Windows.Forms.Label();
113 this.textShipPcnt = new System.Windows.Forms.TextBox();
114 this.textShipCost = new System.Windows.Forms.TextBox();
115 this.label6 = new System.Windows.Forms.Label();
116 this.textPurchTotal = new System.Windows.Forms.TextBox();
117 this.groupBox1 = new System.Windows.Forms.GroupBox();
118 this.label7 = new System.Windows.Forms.Label();
119 this.radBtn3 = new System.Windows.Forms.RadioButton();
120 this.radBtn2 = new System.Windows.Forms.RadioButton();
121 this.radBtn1 = new System.Windows.Forms.RadioButton();
122 this.groupBox1.SuspendLayout();
123 this.SuspendLayout();
124 //
125 // cboItmPrices
126 //
127 this.cboItmPrices.Items.AddRange(new object[] {
128 "Choose a Bag Type:",
129 "Full Decorative $50",
130 "Beaded $45",
131 "Needlepoint $40",
132 "Fringed $25",
133 "Fringed Beaded $50",
134 "Plain $20"});
135 this.cboItmPrices.Location = new System.Drawing.Point(16, 34);
136 this.cboItmPrices.Name = "cboItmPrices";
137 this.cboItmPrices.Size = new System.Drawing.Size(128, 21);
138 this.cboItmPrices.TabIndex = 0;
139 this.cboItmPrices.SelectedIndexChanged += new System.EventHandler(this.cboItmPrices_SelectedIndexChanged);
140 //
141 // textItmQty
142 //
143 this.textItmQty.Location = new System.Drawing.Point(168, 34);
144 this.textItmQty.Name = "textItmQty";
145 this.textItmQty.Size = new System.Drawing.Size(48, 20);
146 this.textItmQty.TabIndex = 1;
147 this.textItmQty.Text = "";
148 this.textItmQty.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
149 //
150 // textItmEach
151 //
152 this.textItmEach.Location = new System.Drawing.Point(232, 34);
153 this.textItmEach.Name = "textItmEach";
154 this.textItmEach.ReadOnly = true;
155 this.textItmEach.Size = new System.Drawing.Size(72, 20);
156 this.textItmEach.TabIndex = 2;
157 this.textItmEach.Text = "";
158 this.textItmEach.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
159 //
160 // label1
161 //
162 this.label1.Location = new System.Drawing.Point(160, 8);
163 this.label1.Name = "label1";
164 this.label1.Size = new System.Drawing.Size(64, 16);
165 this.label1.TabIndex = 3;
166 this.label1.Text = "Quantity";
167 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
168 //
169 // label2
170 //
171 this.label2.Location = new System.Drawing.Point(232, 8);
172 this.label2.Name = "label2";
173 this.label2.Size = new System.Drawing.Size(64, 16);
174 this.label2.TabIndex = 4;
175 this.label2.Text = "Each";
176 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
177 //
178 // btnCalculate
179 //
180 this.btnCalculate.Location = new System.Drawing.Point(216, 144);
181 this.btnCalculate.Name = "btnCalculate";
182 this.btnCalculate.Size = new System.Drawing.Size(80, 24);
183 this.btnCalculate.TabIndex = 5;
184 this.btnCalculate.Text = "Calculate";
185 this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
186 //
187 // btnExit
188 //
189 this.btnExit.Location = new System.Drawing.Point(312, 144);
190 this.btnExit.Name = "btnExit";
191 this.btnExit.Size = new System.Drawing.Size(80, 24);
192 this.btnExit.TabIndex = 6;
193 this.btnExit.Text = "Exit";
194 this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
195 //
196 // label3
197 //
198 this.label3.Location = new System.Drawing.Point(320, 8);
199 this.label3.Name = "label3";
200 this.label3.Size = new System.Drawing.Size(64, 16);
201 this.label3.TabIndex = 8;
202 this.label3.Text = "Total";
203 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
204 //
205 // textItmTotal
206 //
207 this.textItmTotal.Location = new System.Drawing.Point(320, 34);
208 this.textItmTotal.Name = "textItmTotal";
209 this.textItmTotal.ReadOnly = true;
210 this.textItmTotal.Size = new System.Drawing.Size(72, 20);
211 this.textItmTotal.TabIndex = 9;
212 this.textItmTotal.TabStop = false;
213 this.textItmTotal.Text = "";
214 this.textItmTotal.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
215 //
216 // label5
217 //
218 this.label5.Location = new System.Drawing.Point(24, 8);
219 this.label5.Name = "label5";
220 this.label5.Size = new System.Drawing.Size(88, 16);
221 this.label5.TabIndex = 12;
222 this.label5.Text = "Bag Type";
223 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
224 //
225 // btnClear
226 //
227 this.btnClear.Location = new System.Drawing.Point(120, 144);
228 this.btnClear.Name = "btnClear";
229 this.btnClear.Size = new System.Drawing.Size(80, 24);
230 this.btnClear.TabIndex = 13;
231 this.btnClear.Text = "Clear";
232 this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
233 //
234 // btnHelp
235 //
236 this.btnHelp.Location = new System.Drawing.Point(24, 144);
237 this.btnHelp.Name = "btnHelp";
238 this.btnHelp.Size = new System.Drawing.Size(80, 24);
239 this.btnHelp.TabIndex = 14;
240 this.btnHelp.Text = "Help";
241 this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
242 //
243 // label4
244 //
245 this.label4.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
246 this.label4.Location = new System.Drawing.Point(176, 66);
247 this.label4.Name = "label4";
248 this.label4.Size = new System.Drawing.Size(80, 16);
249 this.label4.TabIndex = 16;
250 this.label4.Text = "Shipping Cost";
251 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
252 //
253 // textShipPcnt
254 //
255 this.textShipPcnt.Location = new System.Drawing.Point(264, 64);
256 this.textShipPcnt.Name = "textShipPcnt";
257 this.textShipPcnt.ReadOnly = true;
258 this.textShipPcnt.Size = new System.Drawing.Size(40, 20);
259 this.textShipPcnt.TabIndex = 17;
260 this.textShipPcnt.TabStop = false;
261 this.textShipPcnt.Text = "";
262 this.textShipPcnt.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
263 //
264 // textShipCost
265 //
266 this.textShipCost.Location = new System.Drawing.Point(320, 64);
267 this.textShipCost.Name = "textShipCost";
268 this.textShipCost.ReadOnly = true;
269 this.textShipCost.Size = new System.Drawing.Size(72, 20);
270 this.textShipCost.TabIndex = 18;
271 this.textShipCost.TabStop = false;
272 this.textShipCost.Text = "";
273 this.textShipCost.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
274 //
275 // label6
276 //
277 this.label6.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
278 this.label6.Location = new System.Drawing.Point(232, 98);
279 this.label6.Name = "label6";
280 this.label6.Size = new System.Drawing.Size(80, 16);
281 this.label6.TabIndex = 19;
282 this.label6.Text = "Total Purchase";
283 this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
284 //
285 // textPurchTotal
286 //
287 this.textPurchTotal.Location = new System.Drawing.Point(320, 96);
288 this.textPurchTotal.Name = "textPurchTotal";
289 this.textPurchTotal.ReadOnly = true;
290 this.textPurchTotal.Size = new System.Drawing.Size(72, 20);
291 this.textPurchTotal.TabIndex = 20;
292 this.textPurchTotal.TabStop = false;
293 this.textPurchTotal.Text = "";
294 this.textPurchTotal.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
295 //
296 // groupBox1
297 //
298 this.groupBox1.Controls.Add(this.label7);
299 this.groupBox1.Controls.Add(this.radBtn3);
300 this.groupBox1.Controls.Add(this.radBtn2);
301 this.groupBox1.Controls.Add(this.radBtn1);
302 this.groupBox1.Location = new System.Drawing.Point(8, 56);
303 this.groupBox1.Name = "groupBox1";
304 this.groupBox1.Size = new System.Drawing.Size(136, 80);
305 this.groupBox1.TabIndex = 22;
306 this.groupBox1.TabStop = false;
307 //
308 // label7
309 //
310 this.label7.Location = new System.Drawing.Point(18, 8);
311 this.label7.Name = "label7";
312 this.label7.Size = new System.Drawing.Size(100, 18);
313 this.label7.TabIndex = 3;
314 this.label7.Text = "Shipping Type:";
315 this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
316 //
317 // radBtn3
318 //
319 this.radBtn3.Checked = true;
320 this.radBtn3.Location = new System.Drawing.Point(16, 56);
321 this.radBtn3.Name = "radBtn3";
322 this.radBtn3.Size = new System.Drawing.Size(104, 18);
323 this.radBtn3.TabIndex = 2;
324 this.radBtn3.TabStop = true;
325 this.radBtn3.Text = "Standard (5%)";
326 //
327 // radBtn2
328 //
329 this.radBtn2.Location = new System.Drawing.Point(16, 40);
330 this.radBtn2.Name = "radBtn2";
331 this.radBtn2.Size = new System.Drawing.Size(104, 18);
332 this.radBtn2.TabIndex = 1;
333 this.radBtn2.Text = "3 Day (7%)";
334 //
335 // radBtn1
336 //
337 this.radBtn1.Location = new System.Drawing.Point(16, 24);
338 this.radBtn1.Name = "radBtn1";
339 this.radBtn1.Size = new System.Drawing.Size(112, 18);
340 this.radBtn1.TabIndex = 0;
341 this.radBtn1.Text = "Overnight (10%)";
342 //
343 // OrderFormCalculator
344 //
345 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
346 this.ClientSize = new System.Drawing.Size(416, 190);
347 this.Controls.Add(this.groupBox1);
348 this.Controls.Add(this.textPurchTotal);
349 this.Controls.Add(this.label6);
350 this.Controls.Add(this.textShipCost);
351 this.Controls.Add(this.textShipPcnt);
352 this.Controls.Add(this.label4);
353 this.Controls.Add(this.btnHelp);
354 this.Controls.Add(this.btnClear);
355 this.Controls.Add(this.label5);
356 this.Controls.Add(this.textItmTotal);
357 this.Controls.Add(this.label3);
358 this.Controls.Add(this.btnExit);
359 this.Controls.Add(this.btnCalculate);
360 this.Controls.Add(this.label2);
361 this.Controls.Add(this.label1);
362 this.Controls.Add(this.textItmEach);
363 this.Controls.Add(this.textItmQty);
364 this.Controls.Add(this.cboItmPrices);
365 this.Name = "OrderFormCalculator";
366 this.Text = "Quiz 02 - Order Form Calculator - by Pat Moss";
367 this.Load += new System.EventHandler(this.OrderFormCalculator_Load);
368 this.groupBox1.ResumeLayout(false);
369 this.ResumeLayout(false);
370
371 }
372 #endregion
373
374 ///
375 /// The main entry point for the application
376 ///
377 [STAThread]
378 static void Main()
379 {
380 Application.Run(new OrderFormCalculator());
381 }
382
383 // Form load event
384 // Initialize the screen controls
385 private void OrderFormCalculator_Load(object sender, System.EventArgs e)
386 {
387 InitScreenControls();
388 }
389
390 // "Help" button event
391 // The user has clicked on the "Help" button
392 // Display user help information
393 private void btnHelp_Click(object sender, System.EventArgs e)
394 {
395 string helpinfo = "";
396 helpinfo += " User Instructions:\n\n";
397 helpinfo += "1. Choose a Bag Type.\n";
398 helpinfo += "2. Enter a Quantity, 1 to 100 (if other than 1).\n";
399 helpinfo += "3. Click on Calculate.\n";
400 helpinfo += "4. View Shipping Cost and Total Purchase.\n";
401 helpinfo += "5. If Quantity is changed, click on Calculate again.\n";
402 helpinfo += "6. If Shipping Type is changed, click on Calculate again.\n\n";
403 helpinfo += " Thank You for Your Purchase!\n";
404 MessageBox.Show(helpinfo, "Order Form Calculator Help",
405 MessageBoxButtons.OK, MessageBoxIcon.Information,
406 MessageBoxDefaultButton.Button1);
407 }
408
409 // "Clear" button event
410 // The user has clicked on the "Clear" button
411 // Initialize the screen controls
412 private void btnClear_Click(object sender, System.EventArgs e)
413 {
414 InitScreenControls();
415 }
416
417 // Initialize the screen controls
418 private void InitScreenControls()
419 {
420 cboItmPrices.SelectedIndex = 0; // set combo box = "Choose a Bag Type:"
421 textItmQty.Text = ""; // initialize "item qty" to a null string
422 InitScreenControls2(); // initialize text box controls to null strings
423 radBtn3.Checked = true; // set default "shipping type" = standard
424 }
425
426 // Initialize text box controls
427 private void InitScreenControls2()
428 {
429 textItmEach.Text = "";
430 textItmTotal.Text = "";
431 textShipPcnt.Text = "";
432 textShipCost.Text = "";
433 textPurchTotal.Text = "";
434 }
435
436 // "Calculate" button event
437 // The user has clicked on the "Calculate" button
438 // so calculate the Each, Total extended item values, and
439 // calculate the Shipping Pcnt and Shipping Cost values, and
440 // calculate the Total Purchase value.
441 // Then display the calculated values on the form
442 // and also in a MessageBox.
443 private void btnCalculate_Click(object sender, System.EventArgs e)
444 {
445 InitScreenControls2();
446
447 string strItmPrice = "";
448 string strItmQty = "";
449 string strShipPcnt = "";
450
451 double dblItmPrice = 0;
452 int intItmQty = 0;
453 double dblItmEach = 0;
454 double dblItmTotal = 0;
455 double dblShipPcnt = 0;
456 double dblShipCost = 0;
457 double dblPurchTotal = 0;
458 int intShipIndex = 0;
459
460 bool inperror = false;
461
462 // extract the "item price" value as right-most two characters
463 // of selected combo box item, such as "20" from "Plain $20", etc.
464 string strItm = cboItmPrices.SelectedItem.ToString();
465 int lenItm = strItm.Length;
466 strItmPrice = strItm.Substring(lenItm - 2, 2);
467
468 // fetch the "item quantity" value from input text box
469 strItmQty = textItmQty.Text;
470
471 // convert the "item price" string to double
472 try
473 {
474 dblItmPrice = double.Parse(strItmPrice);
475 dblItmEach = dblItmPrice;
476 }
477 catch
478 {
479 string helpinfo = "Bag Type: Please choose an item.";
480 MessageBox.Show(helpinfo, "Bad Input Data",
481 MessageBoxButtons.OK, MessageBoxIcon.Error,
482 MessageBoxDefaultButton.Button1);
483 cboItmPrices.SelectedIndex = 0;
484 cboItmPrices.Focus();
485 inperror = true;
486 }
487
488 // convert the "item quantity" string to an integer
489 try
490 {
491 intItmQty = int.Parse(strItmQty);
492 }
493 catch
494 {
495 if (!inperror)
496 {
497 string helpinfo = "Quantity: Please enter an integer value (1 to 100).";
498 MessageBox.Show(helpinfo, "Bad Input Data",
499 MessageBoxButtons.OK, MessageBoxIcon.Error,
500 MessageBoxDefaultButton.Button1);
501 textItmQty.Focus();
502 }
503
504 inperror = true;
505 }
506
507 // check for "item quantity" value in the integer range 1 to 100
508 if (!inperror)
509 {
510 if((intItmQty < 1) || (intItmQty > 100))
511 {
512 string helpinfo = "Quantity: Please enter an integer value (1 to 100).";
513 MessageBox.Show(helpinfo, "Bad Input Data",
514 MessageBoxButtons.OK, MessageBoxIcon.Error,
515 MessageBoxDefaultButton.Button1);
516 textItmQty.Focus();
517 inperror = true;
518 }
519 }
520
521 // determine which "shipping type" radio button is checked, where
522 // radBtn1 = Overnight (10%)
523 // radBtn2 = 3 Day (7%)
524 // radBtn3 = Standard (5%)
525 intShipIndex = 0;
526 if (radBtn1.Checked)
527 {
528 intShipIndex = 1;
529 strShipPcnt = "10%";
530 dblShipPcnt = 0.10;
531 }
532 if (radBtn2.Checked)
533 {
534 intShipIndex = 2;
535 strShipPcnt = "7%";
536 dblShipPcnt = 0.07;
537 }
538 if (radBtn3.Checked)
539 {
540 intShipIndex = 3;
541 strShipPcnt = "5%";
542 dblShipPcnt = 0.05;
543 }
544
545 if (intShipIndex == 0)
546 {
547 if (!inperror)
548 {
549 string helpinfo = "Shipping: Please choose a Shipping Type.";
550 MessageBox.Show(helpinfo, "Bad Input Data",
551 MessageBoxButtons.OK, MessageBoxIcon.Error,
552 MessageBoxDefaultButton.Button1);
553 }
554
555 inperror = true;
556 }
557
558 // if input data is ok, then calculate and display
559 // the Order Form results now
560 if (!inperror)
561 {
562 dblItmTotal = dblItmEach * intItmQty;
563 dblItmTotal = Math.Round(dblItmTotal, 2);
564
565 dblShipCost = dblShipPcnt * dblItmTotal;
566 dblShipCost = Math.Round(dblShipCost, 2);
567
568 dblPurchTotal = dblItmTotal + dblShipCost;
569 dblPurchTotal = Math.Round(dblPurchTotal, 2);
570
571 textItmQty.Text = intItmQty.ToString();
572 textItmEach.Text = dblItmEach.ToString("c");
573 textItmTotal.Text = dblItmTotal.ToString("c");
574
575 textShipPcnt.Text = strShipPcnt;
576 textShipCost.Text = dblShipCost.ToString("c");
577
578 textPurchTotal.Text = dblPurchTotal.ToString("c");
579
580 // Display the Order Form results in a MessageBox
581 string msginfo = "";
582 msginfo += "Bag Type: " + strItm + "\n";
583 msginfo += "Quantity: " + strItmQty + "\n";
584 msginfo += "Extended Total: " + textItmTotal.Text + "\n";
585 msginfo += "Shipping Cost: " + textShipCost.Text + "\n";
586 msginfo += "Total Purchase: " + textPurchTotal.Text + "\n";
587 MessageBox.Show(msginfo, "Order Form Results",
588 MessageBoxButtons.OK, MessageBoxIcon.Information,
589 MessageBoxDefaultButton.Button1);
590 }
591 }
592
593 // "ItmPrices" combo-box item selected event
594 // When the user selects a new Bag Type, set the form "item quantity" value = 1
595 private void cboItmPrices_SelectedIndexChanged(object sender, System.EventArgs e)
596 {
597 textItmQty.Text = "1";
598 }
599
600 // "Exit" button event
601 // The user has clicked on the "Exit" button
602 // so exit from this application now.
603 private void btnExit_Click(object sender, System.EventArgs e)
604 {
605 this.Close();
606 }
607 }
608 }
609
610