CNSA-212-Personal/MidtermProject/Midterm.cs

269 lines
7.1 KiB
C#
Raw Permalink Normal View History

2024-02-02 07:31:09 -08:00
using System;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace MidtermProject
{
public partial class Midterm : Form
{
// Tax Rates
private const decimal STANDARD_TAX_RATE = 0.06m;
private const decimal INSTATE_TAX_RATE = 0.04m;
// Prices
private const decimal BLUE_COST = 24.99m;
private const decimal YELLOW_COST = 15.97m;
// Running totals
private int transactions;
private int yellowSold;
private int blueSold;
private decimal totalSum;
// Integer Limit Checks
private double dubBlue = 0;
private double dubYellow = 0;
private int track;
private decimal tax = STANDARD_TAX_RATE;
private readonly ErrorProvider ep1 = new ErrorProvider();
public Midterm()
{
InitializeComponent();
LoadForm();
}
private void LoadForm()
{
lblSumBlue.Text = "0";
lblSumYellow.Text = "0";
lblAvg.Text = "0";
lblTransactions.Text = "0";
lblSumGrandTotal.Text = "0";
grpSummary.Visible = false;
btnNext.Enabled = false;
btnCalc.Enabled = false;
btnReset.Enabled = false;
}
private void lblDisAvg_Click(object sender, EventArgs e)
{
// throw new System.NotImplementedException();
}
private void chkSummary_CheckedChanged(object sender, EventArgs e)
{
if (chkSummary.Checked)
grpSummary.Visible = true;
else
grpSummary.Visible = false;
}
private void chkTax_CheckedChanged(object sender, EventArgs e)
{
if (chkTax.Checked)
tax = INSTATE_TAX_RATE;
else
tax = STANDARD_TAX_RATE;
}
private decimal Calculate(int qBlue, int qYellow)
{
decimal cost;
transactions++;
yellowSold += qYellow;
blueSold += qBlue;
dubYellow += qYellow;
dubBlue += qBlue;
cost = qBlue * BLUE_COST * (1 + tax) + qYellow * YELLOW_COST * (1 + tax);
totalSum += cost;
return cost;
}
private void btnCalc_Click(object sender, EventArgs e)
{
Int32 qBlue = 0;
Int32 qYellow = 0;
try
{
try
{
qBlue = int.Parse(txtBlue.Text);
}
catch (Exception exception)
{
ep1.SetError(txtBlue, "Invalid Value");
throw new Exception();
}
try
{
qYellow = int.Parse(txtYellow.Text);
}
catch (Exception exception)
{
ep1.SetError(txtYellow, "Invalid Value");
throw new Exception();
}
// Check to make sure sales dont surpass integer limits
try
{
if (blueSold + qBlue != dubBlue + qBlue)
{
throw new Exception();
}
}
catch (Exception exception)
{
ep1.SetError(txtBlue, "We Can't Sell That Many");
throw;
}
try
{
if (yellowSold + qYellow != dubYellow + qYellow)
{
throw new Exception();
}
}
catch (Exception exception)
{
ep1.SetError(txtYellow, "We Can't Sell That Many");
throw;
}
if (txtBlue.Text.Length > 0 || txtYellow.Text.Length > 0)
try
{
lblTotal.Text = Calculate(qBlue, qYellow).ToString("C");
}
catch (Exception exception)
{
ep1.SetError(btnCalc, "Calculation Error");
}
else
MessageBox.Show("Whoops!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
track++;
}
catch (Exception exception)
{
// Dont Run Calculations
}
UpdateForm();
}
private void UpdateForm()
{
lblSumBlue.Text = blueSold.ToString("N0");
lblSumYellow.Text = yellowSold.ToString("N0");
if (transactions > 0)
{
lblAvg.Text = (totalSum / transactions).ToString("C");
btnReset.Enabled = true;
}
else
{
lblAvg.Text = "0";
btnReset.Enabled = false;
}
lblTransactions.Text = transactions.ToString("N0");
lblSumGrandTotal.Text = totalSum.ToString("C");
if (track > 0)
btnNext.Enabled = true;
else
btnNext.Enabled = false;
if (txtBlue.Text.Length > 0 && txtYellow.Text.Length > 0)
{
btnCalc.Enabled = true;
}
else
{
btnCalc.Enabled = false;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
private void txtBlue_TextChanged(object sender, EventArgs e)
{
UpdateForm();
ep1.Clear();
}
private void btnNext_Click(object sender, EventArgs e)
{
track = 0;
txtBlue.Text = "";
txtYellow.Text = "";
lblTotal.Text = "";
chkTax.Checked = false;
txtBlue.Focus();
UpdateForm();
}
private void txtYellow_TextChanged(object sender, EventArgs e)
{
UpdateForm();
ep1.Clear();
}
private void btnReset_Click(object sender, EventArgs e)
{
DialogResult btnDialog;
string strResponse;
strResponse = "Do you really want to reset all statistics?";
btnDialog = MessageBox.Show(strResponse, "Midterm", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (btnDialog == DialogResult.Yes)
{
transactions = 0;
yellowSold = 0;
blueSold = 0;
totalSum = 0;
track = 0;
txtBlue.Text = "";
txtYellow.Text = "";
lblTotal.Text = "";
}
UpdateForm();
}
}
}