Danilo Pizza

Creating the Application

This application is used to process orders for a pizza business. It will allow us to view examples of validating radio buttons and check boxes

Prerequisites:

  • Radio Buttons
  • Check Boxes
  • Dialog Boxes

This is a classic application that is created from a dialog box.

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual J# and create a new Windows Application named Danilo Pizza
     
  2. Design the form as follows:
     
     
    Control Text Name Additional Properties
    GroupBox Pizza Size    
    RadioButton Small rdoSmall  
    TextBox 8.95 txtSmall  
    RadioButton Medium rdoMedium Checked: True
    TextBox 10.75 txtMedium  
    RadioButton Large rdoLarge  
    TextBox 12.95 txtLarge  
    GroupBox Side Orders    
    Label Qty    
    Label Unit Price    
    Label Sub Total    
    Label Bread Sticks    
    TextBox 0 txtQtyBread AlignText: Right
    TextBox 3.25 txtPriceBread AlignText: Right
    TextBox 0.00 txtTotalBread AlignText: Right
    Label Buffalo Wings    
    TextBox 0 txtQtyWings AlignText: Right
    TextBox 2.15 txtPriceWings AlignText: Right
    TextBox 0.00 txtTotalWings AlignText: Right
    GroupBox Toppings    
    CheckBox Pepperoni chkPepperoni CheckAlign: MiddleRight
    CheckBox Sausage chkSausage CheckAlign: MiddleRight
    CheckBox Extra Cheese chkExtraCheese CheckAlign: MiddleRight
    CheckBox Olives chkOlives CheckAlign: MiddleRight
    CheckBox Onions chkOnions CheckAlign: MiddleRight
    Label Each Topping    
    TextBox 0.45 txtEachTopping AlignText: Right
    GroupBox Drinks    
    Label Qty    
    Label Unit Price    
    Label Sub Total    
    Label Soda Can    
    TextBox 0 txtQtyCan AlignText: Right
    TextBox 1.45 txtPriceCan AlignText: Right
    TextBox 0.00 txtTotalCan AlignText: Right
    Label Soda 20 Oz.    
    TextBox 0 txtQtySoda20 AlignText: Right
    TextBox 1.45 txtPriceSoda20 AlignText: Right
    TextBox 0.00 txtTotalSoda20 AlignText: Right
    Label Soda 2L Bottle    
    TextBox 0 txtQtySoda2L AlignText: Right
    TextBox 1.45 txtPriceSoda2L AlignText: Right
    TextBox 0.00 txtTotalSoda2L AlignText: Right
    Label Orange Juice    
    TextBox 0 txtQtyOJ AlignText: Right
    TextBox 2.25 txtPriceOJ AlignText: Right
    TextBox 0.00 txtTotalOJ AlignText: Right
    Label Water    
    TextBox 0 txtQtyWater AlignText: Right
    TextBox 1.25 txtPriceWater AlignText: Right
    TextBox 0.00 txtTotalWater AlignText: Right
    Button Close btnClose  
    Label Total Price    
    Text Box 0.00 txtTotalPrice AlignRight: Right
  3. Save everything
 

Coding the Application

  1. Right-click the form and click View Code
  2. Just under #endregion, define the following function:
     
    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.get_Checked() == true )
    			PriceSize = System.Convert.ToDouble(txtSmall.get_Text());
    		if( rdoMedium.get_Checked() == true )
    			PriceSize = System.Convert.ToDouble(txtMedium.get_Text());
    		if( rdoLarge.get_Checked() == true )
    			PriceSize = System.Convert.ToDouble(txtLarge.get_Text());
    
    		// Get the price of a topping if it was selected
    		if( chkPepperoni.get_Checked() == true )
    			Pepperoni = 1;
    		else
    			Pepperoni = 0;
    
    		if( chkSausage.get_Checked() == true )
    			Sausage = 1;
    		else
    			Sausage = 0;
    
    		if( chkExtraCheese.get_Checked() == true )
    			ExtraCheese = 1;
    		else
    			ExtraCheese = 0;
    
    		if( chkOnions.get_Checked() == true )
    			Onions = 1;
    		else
    			Onions = 0;
    
    		if( chkOlives.get_Checked() == true )
    			Olives = 1;
    		else
    			Olives = 0;
    
    		PriceEachTopping = System.Convert.ToDouble(txtEachTopping.get_Text());
    	PriceToppings    = (Pepperoni + Sausage + ExtraCheese + Onions + Olives) * PriceEachTopping;
    
    		// Calculate the price of the side dishes
    		// depending on the quantity entered
    		BWings = System.Convert.ToDouble(txtTotalWings.get_Text());
    		Bread  = System.Convert.ToDouble(txtTotalBread.get_Text());
                
    		// Calculate the price of the drink(s)
    		SodaCan = System.Convert.ToDouble(txtTotalCan.get_Text());
    		Soda20  = System.Convert.ToDouble(txtTotalSoda20.get_Text());
    		Soda2L  = System.Convert.ToDouble(txtTotalSoda2L.get_Text());
    		OJ		= System.Convert.ToDouble(txtTotalOJ.get_Text());
    		Water	= System.Convert.ToDouble(txtTotalWater.get_Text());
    			
    		TotalOrder = PriceSize + PriceToppings + BWings + Bread +
    			SodaCan + Soda20 + Soda2L + OJ + Water;
    		txtTotalPrice.set_Text(String.Format("{0:C}", (System.Double)TotalOrder));
    	}
  3. On the form double-click each radio button and each check box
  4. 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();
    	}
  5. Test the application
  6. On the form, click the Qty text box corresponding to the Bread Sticks
  7. In the Properties window, click the Events button and, in the list of events, double-click Leave
  8. In the same way, initiate the Leave of the Qty text box of the Buffalo Wings and the drinks
  9. Implement the events as follows:
     
    private void txtQtyBread_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtyBread.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtyBread.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceBread.get_Text());
    
    	Total = Qty * Price;
    	txtTotalBread.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtyWings_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtyWings.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtyWings.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceWings.get_Text());
    
    	Total = Qty * Price;
    	txtTotalWings.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtyCan_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtyCan.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtyCan.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceCan.get_Text());
    
    	Total = Qty * Price;
    	txtTotalCan.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtySoda20_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtySoda20.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtySoda20.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceSoda20.get_Text());
    
    	Total = Qty * Price;
    	txtTotalSoda20.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtySoda2L_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtySoda2L.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtySoda2L.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceSoda2L.get_Text());
    
    	Total = Qty * Price;
    	txtTotalSoda2L.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtyOJ_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtyOJ.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtyOJ.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceOJ.get_Text());
    
    	Total = Qty * Price;
    	txtTotalOJ.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
    
    private void txtQtyWater_Leave (Object sender, System.EventArgs e)
    {
    	int Qty;
    	double Price, Total;
    
    	if( txtQtyWater.get_Text() == "" )
    		Qty = 0;
    	else
    		Qty = System.Convert.ToInt32(txtQtyWater.get_Text());
    
    	Price = System.Convert.ToDouble(txtPriceWater.get_Text());
    
    	Total = Qty * Price;
    	txtTotalWater.set_Text(String.Format("{0:F}", (System.Double)Total));
    
    	CalculatePrice();
    }
  10. 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();
    	}
  11. Test the application
 

 


Home Copyright 2004 FunctionX, Inc.