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:

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

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++ .Net and create a new Windows Forms 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. In Class View, expand {} DaniloPizza
  2. Right-click Form1 -> Add -> Add Function...
  3. Set the Return Type as void
  4. Set the name of the function as CalculatePrice
  5. Click Finish and implement the function as follows:
     
    void CalculatePrice(void)
    {
    	double PriceSize, 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 = txtSmall->Text->ToDouble(0);
    	if( rdoMedium->Checked == true )
    		PriceSize = txtMedium->Text->ToDouble(0);
    	if( rdoLarge->Checked == true )
    		PriceSize = txtLarge->Text->ToDouble(0);
    
    	// 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 = txtEachTopping->Text->ToDouble(0);
    PriceToppings    = (Pepperoni + Sausage + ExtraCheese + Onions + Olives) * PriceEachTopping;
    
    	// Calculate the price of the side dishes
    	// depending on the quantity entered
    	BWings = txtTotalWings->Text->ToDouble(0);
    	Bread  = txtTotalBread->Text->ToDouble(0);
                
    	// Calculate the price of the drink(s)
    	SodaCan = txtTotalCan->Text->ToDouble(0);
    	Soda20   = txtTotalSoda20->Text->ToDouble(0);
    	Soda2L   = txtTotalSoda2L->Text->ToDouble(0);
    	OJ	= txtTotalOJ->Text->ToDouble(0);
    	Water	= txtTotalWater->Text->ToDouble(0);
    			
    	TotalOrder = PriceSize + PriceToppings + BWings + Bread +
                                         SodaCan + Soda20 + Soda2L + OJ + Water;
    		     txtTotalPrice->Text = TotalOrder.ToString("C");
    }
  6. On the form double-click each radio button and each check box
  7. In their implementation, simply call the CalculatePrice() function:
     
    private: System::Void rdoSmall_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void rdoMedium_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void rdoLarge_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void chkPepperoni_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void chkSausage_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void chkExtraCheese_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void chkOlives_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
    
    private: System::Void chkOnions_CheckedChanged(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 CalculatePrice();
    		 }
  8. Test the application
  9. On the form, click the Qty text box corresponding to the Bread Sticks
  10. In the Properties window, click the Events button and, in the list of events, double-click Leave
  11. In the same way, initiate the Leave of the Qty text box of the Buffalo Wings and the drinks
  12. Implement the events as follows:
     
    private: System::Void txtQtyBread_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtyBread->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtyBread->Text->ToInt32(0);
    
    			 Price = txtPriceBread->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalBread->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    private: System::Void txtQtyWings_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtyWings->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtyWings->Text->ToInt32(0);
    
    			 Price = txtPriceWings->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalWings->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    
    private: System::Void txtQtyCan_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtyCan->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtyCan->Text->ToInt32(0);
    
    			 Price = txtPriceCan->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalCan->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    
    private: System::Void txtQtySoda20_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtySoda20->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtySoda20->Text->ToInt32(0);
    
    			 Price = txtPriceSoda20->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalSoda20->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    
    private: System::Void txtQtySoda2L_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtySoda2L->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtySoda2L->Text->ToInt32(0);
    
    			 Price = txtPriceSoda2L->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalSoda2L->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    
    private: System::Void txtQtyOJ_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtyOJ->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtyOJ->Text->ToInt32(0);
    
    			 Price = txtPriceOJ->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalOJ->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
    
    private: System::Void txtQtyWater_Leave(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 int Qty;
    			 double Price, Total;
    
    			 if( txtQtyWater->Text == "" )
    				 Qty = 0;
    			 else
    				 Qty = txtQtyWater->Text->ToInt32(0);
    
    			 Price = txtPriceWater->Text->ToDouble(0);
    
    			 Total = Qty * Price;
    			 txtTotalWater->Text = Total.ToString("F");
    
    			 CalculatePrice();
    		 }
  13. On the form, double-click the Close button and implement its Click event as follows:
     
    private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 Close();
    		 }
  14. Test the application
 
 

Home Copyright 2004 FunctionX, Inc.