Net Price Calculation

Introduction

Items in a department store or any other store usually display the price of an item. Customers take such an item to the cashier who rings it, applies the tax rate and present the total price to the customer.

The marked price, the price presented on an item, is in currency value. In this example, it would be $120.95

The tax rate is expressed as a percentage value. An example would be 5.75%

The computer used to evaluate the price use a formula such as:

Tax Amount = Marked Price / Tax Rate

The net price, the actual price a customer should pay, is calculated as:

Net Price = Marked Price + Tax Amount

In this exercise, we will simulate such a calculation

Starting the Application

  1. Start Microsoft Visual Studio or Microsoft Visual C++.
  2. On the main menu, click File -> New…
  3. To create the project, from the New dialog box, click the Projects property sheet
  4. Click MFC AppWizard (exe)
  5. In the Project Name edit box, type NetPrice and click OK
  6. In the MFC AppWizard – Step 1, click the Dialog Based radio button and click Next
  7. In the MFC AppWizard – Step 2 of 4, in the edit box, type Net Price and click Next
  8. In the MFC AppWizard – Step 3 of 4, click the No Thanks radio button and click Finish and click OK
 
 

Designing the Application

  1. On the dialog box, click the TODO line and press Delete
  2. On the Dialog toolbar, click the Toggle Grid button .
  3. Design the dialog box as follows:
     
  4. Set the IDs of the Edit controls to
    1 IDC_MARKED_PRICE
    2 IDC_TAX_RATE
    3 IDC_TAX_AMOUNT
    4 IDC_NET_PRICE
  5. Remember to add the Calculate button
  6. Drag save your application.

Programming the Application

  1. Create a CString variable for each Edit control
     
     
  2. Create a BN_CLICKED event for the Calculate button and implement it as follows:
     
    void CNetPrice1Dlg::OnBtnCalculate() 
    {
    	// TODO: Add your control notification handler code here
    	double MarkedPrice, TaxRate, TaxAmount, NetPrice;
    
    	MarkedPrice = atof(m_MarkedPrice);
    	TaxRate		= atof(m_TaxRate) / 100;
    
    	TaxAmount	= MarkedPrice * TaxRate;
    	NetPrice	= MarkedPrice + TaxAmount;
    
    	m_TaxAmount.Format("$%.2f", TaxAmount);
    	m_NetPrice.Format("$%.2f", NetPrice);
    
    	UpdateData(FALSE);
    }
  3. Test your program.
 

Home Copyright © 2003-2015, FunctionX, Inc.