I am learning about arrays, and I need to figure out how to enter data from a form into the array. The user enters a subtotal, then the form calculates a discount percentage and amount, and give a final total. I need to have that total loaded into the array each time. The array needs to store 5 totals. Eventually I need to create a display box that will show the last five totals. Any general guidance would be greatly appreciated. I have read and re-read this in my book so many times and just not getting it. I'm thinking I would use a foreach loop here but not sure.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace InvoiceTotal
{
// This is the starting point for exercise 8-1 from
// "Murach's C# 2010" by Joel Murach
// (c) 2010 by Mike Murach & Associates, Inc.
// www.murach.com
public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}
// TODO: declare class variables for array and list here
decimal[] totals = new decimal[5];
private void btnCalculate_Click(object sender, EventArgs e)
{
totals[0] = 0.0m;
totals[1] = 0.0m;
totals[3] = 0.0m;
totals[4] = 0.0m;
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);
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");
}
txtSubtotal.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
// TODO: add code that displays dialog boxes here
this.Close();
}
}
}