internal void CalculateTotal()
{
decimal unitPriceShirts = 0.00M, unitPricePants = 0.00M, unitPriceItem1 = 0.00M,
unitPriceItem2 = 0.00M, unitPriceItem3 = 0.00M, unitPriceItem4 = 0.00M;
decimal subTotalShirts = 0.00M, subTotalPants = 0.00M, subTotalItem1 = 0.00M,
subTotalItem2 = 0.00M, subTotalItem3 = 0.00M, subTotalItem4 = 0.00M;
int qtyShirts = 1, qtyPants = 1, qtyItem1 = 1,
qtyItem2 = 1, qtyItem3 = 1, qtyItem4 = 4;
decimal cleaningTotal = 0.00M, taxRate = 0.00M, taxAmount = 0.00M, netPrice = 0.00M;
// Retrieve the unit price of this item
// Just in case the user types an invalid value, we are using a try...catch
try
{
unitPriceShirts = decimal.Parse(this.txtShirtsUnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price of shirts is not valid" +
"\nPlease try again");
}
// Retrieve the number of this item
// Just in case the user types an invalid value, we are using a try...catch
try
{
qtyShirts = int.Parse(this.txtShirtsQuantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the number of shirts is not valid" +
"\nPlease try again");
}
try
{
unitPricePants = decimal.Parse(this.txtPantsUnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price of pants is not valid" +
"\nPlease try again");
}
try
{
qtyPants = int.Parse(this.txtPantsQuantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the number of pants is not valid" +
"\nPlease try again");
}
try
{
unitPriceItem1 = decimal.Parse(this.txtItem1UnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
}
try
{
qtyItem1 = int.Parse(this.txtItem1Quantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
}
try
{
unitPriceItem2 = decimal.Parse(this.txtItem2UnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
}
try
{
qtyItem2 = int.Parse(this.txtItem2Quantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
}
try
{
unitPriceItem3 = decimal.Parse(this.txtItem3UnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
}
try
{
qtyItem3 = int.Parse(this.txtItem3Quantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
}
try
{
unitPriceItem4 = decimal.Parse(this.txtItem4UnitPrice.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered for the price is not valid" +
"\nPlease try again");
}
try
{
qtyItem4 = int.Parse(this.txtItem4Quantity.Text);
}
catch(FormatException )
{
MessageBox.Show("The value you entered is not valid" +
"\nPlease try again");
}
// Calculate the sub-total for this item
subTotalShirts = qtyShirts * unitPriceShirts;
subTotalPants = qtyPants * unitPricePants;
subTotalItem1 = qtyItem1 * unitPriceItem1;
subTotalItem2 = qtyItem2 * unitPriceItem2;
subTotalItem3 = qtyItem3 * unitPriceItem3;
subTotalItem4 = qtyItem4 * unitPriceItem4;
// Calculate the total based on sub-totals
cleaningTotal = subTotalShirts + subTotalPants + subTotalItem1 +
subTotalItem2 + subTotalItem3 + subTotalItem4;
taxRate = decimal.Parse(this.txtTaxRate.Text);
// Calculate the amount owed for the taxes
taxAmount = cleaningTotal * taxRate / 100;
// Add the tax amount to the total order
netPrice = cleaningTotal + taxAmount;
// Display the sub-total in the corresponding text box
this.txtShirtsSubTotal.Text = subTotalShirts.ToString("F");
this.txtPantsSubTotal.Text = subTotalPants.ToString("F");
this.txtItem1SubTotal.Text = subTotalItem1.ToString("F");
this.txtItem2SubTotal.Text = subTotalItem2.ToString("F");
this.txtItem3SubTotal.Text = subTotalItem3.ToString("F");
this.txtItem4SubTotal.Text = subTotalItem4.ToString("F");
this.txtCleaningTotal.Text = cleaningTotal.ToString("F");
this.txtTaxAmount.Text = taxAmount.ToString("F");
this.txtNetPrice.Text = netPrice.ToString("F");
}
|