Visual C++ Examples: Ice Cream Shop

 

Introduction

This is an example that applies some features of the combo box control. We will simulate a small business that sells ice cream. An ice cream order is represented by a flavor, a type of container, an ingredient if any, and the number of scoops.

When a customer places an order, the clerk will select the date and the time the order was processed. Then the composition of the order as specified by the customer will also be selected. After making these selections, the employee will calculate the price of the order.

Prerequisites:

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++
  2. Create a new project named IceCream1
  3. Create the project as a Dialog Based and set the Dialog Title as Clarksville Ice Cream
     
  4. Click Finish and design the dialog box as follows:
     
    Clarksville Ice Cream - Dialog Design
    Control ID Caption Additional Properties
    Group Box        
    Static Text     Order Date:  
    DateTimePicker IDC_ORDERDATE   Format: Short Date
    Static Text     Order Time:  
    DateTimePicker   IDC_ORDERTIME   Format: Time
    Static Text     Flavor:  
    Combo Box   IDC_FLAVORS   Data: Vanilla;Cream of Cocoa; Chocolate Chip; Butter Pecan; Chocolate Cookie; Chunky Butter; Organic Strawberry;Chocolate Brownies; Caramel Au Lait
    Static Text     Container:  
    Combo Box   IDC_CONTAINERS   Data: Cone;Cup;Bowl
    Static Text     Ingredient:  
    Combo Box   IDC_INGREDIENTS   Data: None; Peanuts; Mixed Nuts; M & M; Cookies
    Static Text     Scoops:  
    Edit Control   IDC_SCOOPS   Align Text: Right
    Number: True
    Static Text     Order Total:  
    Edit Control   IDC_ORDERTOTAL   Align Text: Right
    Button   IDC_CALCULATE Calculate  
    Button   IDC_NEWORDER New Order  
    Button   IDCANCEL Close  
  5. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls:
     
    ID Value Variable Control Variable
    Type Name
    IDC_ORDERDATE   m_OrderDate
    IDC_ORDERTIME   m_OrderTime
    IDC_FLAVORS   m_Flavor
    IDC_CONTAINERS CString m_Container m_ctlContainer
    IDC_INGREDIENTS   m_Ingredient
    IDC_SCOOPS int m_Scoops  
    IDC_ORDERTOTAL double m_TotalOrder  
  6. On the dialog box, double-click the Calculate button and implement its event as follows:
     
    void CIceCream1Dlg::OnBnClickedCalculate()
    {
    	// TODO: Add your control notification handler code here
    	// The prices of the components of an ice cream order
    	double PriceContainer  = 0.00,
    		   PriceIngredient = 0.00,
    		   PriceScoops     = 0.00;
    	// The total price of the order
    	double OrderTotal      = 0.00;
    
    	// Process an order only if the user had selected a flavor
    	if( this->m_Flavor.GetCurSel() != -1 )
    	{
    		// Start the price of an ice cream based on the type of container
    		if( this->m_Container == "Cone" )
    			PriceContainer = 0.55;
    		else if( this->m_Container == "Cup" )
    			PriceContainer = 0.75;
    		else
    			PriceContainer = 1.15;
    
    		// Find out if the user selected an ingredient other than None
    		if( this->m_Ingredient.GetCurSel() > 0 )
    			PriceIngredient = 0.95;
    
    		// The number of scoops of this order
    		// Set the default to 1 because a valid must have at least one
    		int Scoops = 1;
    
    		Scoops = this->m_Scoops;
    
    		// Increase the price of the Ice Cream depending on the number of scoops
    		if( Scoops == 2 )
    			PriceScoops = 2.25;
    		else if( Scoops == 3 )
    			PriceScoops = 3.25;
    		else
    			PriceScoops = 1.85;
    
    		// Calculate the total order
    		OrderTotal = PriceScoops + PriceContainer + PriceIngredient;
    
    		this->m_OrderTotal = OrderTotal;
    	}
    	else
    		AfxMessageBox("You must select a flavor in order to process an order");
    
    	UpdateData(FALSE);
    }
  7. Return to the dialog box. Double-click the New Order button and implement its event as follows:
     
    void CIceCream1Dlg::OnBnClickedNeworder()
    {
    	// TODO: Add your control notification handler code here
    	this->m_Flavor.SetCurSel(-1);
    	this->m_ctlContainer.SetCurSel(-1);
    	this->m_Ingredient.SetCurSel(-1);
    	this->m_Scoops = 0;
    	this->m_OrderTotal = 0.00;
    	UpdateData(FALSE);
    }
  8. Test the application
  9. After using it, close it and return to your programming environment

 

 
 

Home Copyright © 2004-2014 FunctionX, Inc.