summary refs log tree commit diff
path: root/invoice_total4/InvoiceTotal/frmInvoiceTotal.cs
blob: 0f0448fbf8cee8e6a8a8c28239d00d7be9b741aa (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InvoiceTotal
{
	public partial class frmInvoiceTotal : Form
	{
		public frmInvoiceTotal()
		{
			InitializeComponent();
		}

		// DONE: declare class variables for array and list here
		decimal[] rayInvoices = new decimal[5];
		int invoice = 0;
		List<decimal> listInvoices = new List<decimal>(5);

		private void btnCalculate_Click(object sender, EventArgs e)
		{
			try {
				if (txtSubtotal.Text == "") {
					MessageBox.Show(
						"Subtotal is a required field.", "Entry Error");
				}
				else {
					decimal subtotal = Decimal.Parse(txtSubtotal.Text);
					if (subtotal > 0 && subtotal < 10000) {
						decimal discountPercent = 0m;
						if (subtotal >= 500)
							discountPercent = .2m;
						else if (subtotal >= 250 & subtotal < 500)
							discountPercent = .15m;
						else if (subtotal >= 100 & subtotal < 250)
							discountPercent = .1m;
						decimal discountAmount = subtotal * discountPercent;
						decimal invoiceTotal = subtotal - discountAmount;

						discountAmount = Math.Round(discountAmount, 2);
						invoiceTotal = Math.Round(invoiceTotal, 2);

						rayInvoices[invoice] = invoiceTotal;
						invoice++;
						listInvoices.Add(invoiceTotal);

						txtDiscountPercent.Text = discountPercent.ToString("p1");
						txtDiscountAmount.Text = discountAmount.ToString();
						txtTotal.Text = invoiceTotal.ToString();
					}
					else {
						MessageBox.Show(
							"Subtotal must be greater than 0 and less than 10,000.",
							"Entry Error");
					}
				}
			}
			catch (FormatException) {
				MessageBox.Show(
					"Please enter a valid number for the Subtotal field.",
					"Entry Error");
			}
			catch (IndexOutOfRangeException) {
				MessageBox.Show(
					"You have already reached the maximum amount of invoices",
					"Out of Range Error");
			}
			txtSubtotal.Focus();
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			// DONE: add code that displays dialog boxes here
			if (invoice > 0) {
				string msg = "";

				Array.Sort(rayInvoices);
				foreach (decimal i in rayInvoices)
					if (i != 0)
						msg += i.ToString("c") + "\n";
				MessageBox.Show(msg, "Order Totals - Array");

				msg = "";
				listInvoices.Sort();
				foreach (decimal i in listInvoices)
					if (i != 0)
						msg += i.ToString("c") + "\n";
				MessageBox.Show(msg, "Order Totals - List");
			}
			this.Close();
		}

	}
}