Win32 Examples: 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

Prerequisites:

  • Dialog Boxes
  • Static Text Control

 

Creating the Application

To create this type of application, you can start with a dialog box. We will use Borland C++ Builder because it is freely available from Borland.

Practical Learning: Starting the Exercise

  1. Start Borland C++BuilderX and, on the main menu, click File -> New...
     
  2. In the Object Gallery dialog box, click New GUI Application and click OK
  3. In the New GUI Application Project Wizard - Step 1 of 3, in the Directory edit box of the Project Settings section, type the path you want. Otherwise, type
    C:\Programs\Win32 Programming
  4. In the Name edit box, type NetPrice
  5. Click Next
  6. In the New GUI Application Project Wizard - Step 2 of 3, accept the defaults and click Next
  7. In the New GUI Application Project Wizard - Step 3 of 3, click the check box under Create
  8. Select Untitled under the Name column header. Type Exercise to replace the name and press Tab
  9. Click Finish
  10. To create a resource header file, on the main menu, click File -> New File...
  11. In the Create New File dialog box, in the Name, type resource
  12. In the Type combo box, select h, and click OK
  13. In the file, type:
     
    #define IDD_CONTROLSDLG   101
    #define IDC_MARKED_PRICE  104
    #define IDC_TAX_RATE      106
    #define IDC_CALCULATE_BTN 107
    #define IDC_TAX_AMOUNT    109
    #define IDC_NET_PRICE     110
  14. To create a resource script, on the main menu, click File -> New File...
  15. In the Create New File dialog box, in the Name, type NetPrice
  16. In the Type combo box, select rc, and click OK
  17. In the file, type:
     
    #include "resource.h"
    
    IDD_CONTROLSDLG DIALOG 260, 200, 188, 100
    STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
    EXSTYLE WS_EX_APPWINDOW
    CAPTION "Net Price Calculation"
    FONT 8, "MS Shell Dlg"
    BEGIN
        LTEXT           "Marked Price:",IDC_STATIC,10,11,45,8
        EDITTEXT        IDC_MARKED_PRICE,65,9,55,12,ES_RIGHT | ES_AUTOHSCROLL
        LTEXT           "Tax Rate:",IDC_STATIC,10,32,32,8
        EDITTEXT        IDC_TAX_RATE,65,30,45,12,ES_RIGHT | ES_AUTOHSCROLL
        LTEXT           "%",IDC_STATIC,112,32,10,8
        PUSHBUTTON      "Calculate",IDC_CALCULATE_BTN,130,30,50,14
        PUSHBUTTON      "Close",IDCANCEL,130,70,50,14
        LTEXT           "Tax Amount:",IDC_STATIC,10,52,41,8
        EDITTEXT        IDC_TAX_AMOUNT,65,50,55,12,ES_RIGHT | ES_AUTOHSCROLL
        LTEXT           "Net Price:",IDC_STATIC,10,73,32,8
        EDITTEXT        IDC_NET_PRICE,65,71,55,12,ES_RIGHT | ES_AUTOHSCROLL
    END
  18. Display the Exercise.cpp file and change it as follows:
     
    #include <windows.h>
    #include <cstdio>
    using namespace std;
    
    #ifdef __BORLANDC__
      #pragma argsused
    #endif
    
    #include "resource.h"
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInst;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                          LPSTR lpCmdLine, int nCmdShow )
    {
            DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLSDLG),
                      hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
            hInst = hInstance;
    
      return 0;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    
        char strMarkedPrice[20], strTaxRate[20], strTaxAmount[20], strNetPrice[20];
        HWND hWndMarkedPrice, hWndTaxRate, hWndTaxAmount, hWndNetPrice;
        double MarkedPrice, TaxRate, TaxAmount, NetPrice;
    
        hWndMarkedPrice = GetDlgItem(hWndDlg, IDC_MARKED_PRICE);
        hWndTaxRate     = GetDlgItem(hWndDlg, IDC_TAX_RATE);
        hWndTaxAmount   = GetDlgItem(hWndDlg, IDC_TAX_AMOUNT);
        hWndNetPrice    = GetDlgItem(hWndDlg, IDC_NET_PRICE);
    
            switch(Msg)
            {
            case WM_INITDIALOG:
                SetWindowText(hWndMarkedPrice, "0.00");
                SetWindowText(hWndTaxRate, "5.75");
                SetWindowText(hWndTaxAmount, "0.00");
                SetWindowText(hWndNetPrice, "0.00");
                return TRUE;
    
            case WM_COMMAND:
                    switch(wParam)
                    {
                    case IDC_CALCULATE_BTN:
                        GetWindowText(hWndMarkedPrice, strMarkedPrice, 20);
                        GetWindowText(hWndTaxRate, strTaxRate, 20);
    
                        MarkedPrice = atof(strMarkedPrice);
                        TaxRate	= atof(strTaxRate) / 100;
    
                        TaxAmount	= MarkedPrice * TaxRate;
                        NetPrice	= MarkedPrice + TaxAmount;
    
                        sprintf(strTaxAmount, "%.2f", TaxAmount);
                        sprintf(strNetPrice, "%.2f", NetPrice);
    
                        SetWindowText(hWndTaxAmount, strTaxAmount);
                        SetWindowText(hWndNetPrice, strNetPrice);
    
                          return TRUE;
                    case IDCANCEL:
                            EndDialog(hWndDlg, 0);
                            return TRUE;
                    }
                    break;
            }
    
            return FALSE;
    }
    //---------------------------------------------------------------------------
  19. Test the application then close the dialog box
 

Home Copyright 2004 FunctionX, Inc.