- In Class View, expand DaniloPizza and expand {} DaniloPizza
- Right-click Form1 -> Add -> Add Method...
- Accept the Return Type as void
- Set the name of the method as CalculatePrice
- Click Finish and implement the function as follows:
public void CalculatePrice()
{
double PriceSize = 0.00;
double PriceEachTopping, BWings, Bread,
SodaCan, Soda20, Soda2L, OJ, Water, PriceToppings, TotalOrder;
int Pepperoni, Sausage, ExtraCheese, Onions, Olives;
// Get the price of pizza depending on the selected size
if( rdoSmall.Checked == true )
PriceSize = double.Parse(txtSmall.Text);
if( rdoMedium.Checked == true )
PriceSize = double.Parse(txtMedium.Text);
if( rdoLarge.Checked == true )
PriceSize = double.Parse(txtLarge.Text);
// Get the price of a topping if it was selected
if( chkPepperoni.Checked == true )
Pepperoni = 1;
else
Pepperoni = 0;
if( chkSausage.Checked == true )
Sausage = 1;
else
Sausage = 0;
if( chkExtraCheese.Checked == true )
ExtraCheese = 1;
else
ExtraCheese = 0;
if( chkOnions.Checked == true )
Onions = 1;
else
Onions = 0;
if( chkOlives.Checked == true )
Olives = 1;
else
Olives = 0;
PriceEachTopping = double.Parse(txtEachTopping.Text);
PriceToppings = (Pepperoni + Sausage + ExtraCheese + Onions + Olives) * PriceEachTopping;
// Calculate the price of the side dishes
// depending on the quantity entered
BWings = double.Parse(txtTotalWings.Text);
Bread = double.Parse(txtTotalBread.Text);
// Calculate the price of the drink(s)
SodaCan = double.Parse(txtTotalCan.Text);
Soda20 = double.Parse(txtTotalSoda20.Text);
Soda2L = double.Parse(txtTotalSoda2L.Text);
OJ = double.Parse(txtTotalOJ.Text);
Water = double.Parse(txtTotalWater.Text);
TotalOrder = PriceSize + PriceToppings + BWings + Bread +
SodaCan + Soda20 + Soda2L + OJ + Water;
txtTotalPrice.Text = TotalOrder.ToString("C");
}
|
- On the form double-click each radio button and each check box
- In their implementation, simply call the CalculatePrice()
function:
private void rdoSmall_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void rdoMedium_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void rdoLarge_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void chkPepperoni_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void chkSausage_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void chkExtraCheese_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void chkOlives_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
private void chkOnions_CheckedChanged(object sender, System.EventArgs e)
{
CalculatePrice();
}
|
- Test the application
- On the form, click the Qty text box corresponding to the Bread
Sticks
- In the Properties window, click the Events button
and, in the list of events, double-click Leave
- In the same way, initiate the Leave of the Qty text box of the
Buffalo Wings and the drinks
- Implement the events as follows:
private void txtQtyBread_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtyBread.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtyBread.Text);
Price = double.Parse(txtPriceBread.Text);
Total = Qty * Price;
txtTotalBread.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtyWings_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtyWings.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtyWings.Text);
Price = double.Parse(txtPriceWings.Text);
Total = Qty * Price;
txtTotalWings.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtyCan_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtyCan.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtyCan.Text);
Price = double.Parse(txtPriceCan.Text);
Total = Qty * Price;
txtTotalCan.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtySoda20_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtySoda20.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtySoda20.Text);
Price = double.Parse(txtPriceSoda20.Text);
Total = Qty * Price;
txtTotalSoda20.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtySoda2L_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtySoda2L.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtySoda2L.Text);
Price = double.Parse(txtPriceSoda2L.Text);
Total = Qty * Price;
txtTotalSoda2L.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtyOJ_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtyOJ.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtyOJ.Text);
Price = double.Parse(txtPriceOJ.Text);
Total = Qty * Price;
txtTotalOJ.Text = Total.ToString("F");
CalculatePrice();
}
private void txtQtyWater_Leave(object sender, System.EventArgs e)
{
int Qty;
double Price, Total;
if( txtQtyWater.Text == "" )
Qty = 0;
else
Qty = Int32.Parse(txtQtyWater.Text);
Price = double.Parse(txtPriceWater.Text);
Total = Qty * Price;
txtTotalWater.Text = Total.ToString("F");
CalculatePrice();
}
|
- On the form, double-click the Close button and implement its Click
event as follows:
private void button1_Click(object sender, System.EventArgs e)
{
Close();
}
|
- Test the application
|