Win32 Examples: Payroll Simulation

Introduction

This example simulates the calculation of overtime hours for employees of a company. It considers that a work day accounts for 8 regular hours. Any time above that for the same day qualifies as overtime.

To create this type of application, we will use Borland C++BuilderX because it is freely available from Borland. We will create the application as dialog-based.

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 Payroll
  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_CONTROLS_DLG                101
    #define IDC_EMPLOYEE_NAME               1002
    #define IDC_HOURLY_SALARY               1003
    #define IDC_MONDAY1                     1004
    #define IDC_TUESDAY1                    1005
    #define IDC_WEDNESDAY1                  1006
    #define IDC_THURSDAY1                   1007
    #define IDC_FRIDAY1                     1008
    #define IDC_SATURDAY1                   1009
    #define IDC_SUNDAY1                     1010
    #define IDC_MONDAY2                     1011
    #define IDC_TUESDAY2                    1012
    #define IDC_WEDNESDAY2                  1013
    #define IDC_THURSDAY2                   1014
    #define IDC_FRIDAY2                     1015
    #define IDC_SATURDAY2                   1016
    #define IDC_SUNDAY2                     1017
    #define IDC_PROCESS_BTN                 1018
    #define IDC_REGULAR_HOURS               1019
    #define IDC_REGULAR_AMOUNT              1020
    #define IDC_OVERTIME_HOURS              1021
    #define IDC_OVERTIME_AMOUNT             1022
    #define IDC_NET_PAY                     1023
  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_CONTROLS_DLG DIALOG DISCARDABLE  0, 0, 407, 222
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Employee Payroll"
    FONT 8, "MS Sans Serif"
    BEGIN
        PUSHBUTTON      "Close",IDCANCEL,290,185,50,14
        GROUPBOX        "Employee Identification",IDC_STATIC,10,10,385,40
        LTEXT           "Employee Name:",IDC_STATIC,20,27,54,8
        EDITTEXT        IDC_EMPLOYEE_NAME,85,25,115,12,ES_AUTOHSCROLL
        LTEXT           "Hourly Salary:",IDC_STATIC,210,27,44,8
        EDITTEXT        IDC_HOURLY_SALARY,260,25,40,12,ES_RIGHT | ES_AUTOHSCROLL
        GROUPBOX        "Time Sheet",IDC_STATIC,10,55,385,75
        LTEXT           "Monday",IDC_STATIC,71,71,26,8
        LTEXT           "Tuesday",IDC_STATIC,120,70,28,8
        LTEXT           "Wednesday",IDC_STATIC,160,70,39,8
        LTEXT           "Thursday",IDC_STATIC,210,70,30,8
        LTEXT           "Friday",IDC_STATIC,260,70,20,8
        LTEXT           "Saturday",IDC_STATIC,300,70,29,8
        LTEXT           "Sunday",IDC_STATIC,345,70,25,8
        LTEXT           "First Week:",IDC_STATIC,20,86,37,8
        EDITTEXT        IDC_MONDAY1,70,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_TUESDAY1,115,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_WEDNESDAY1,160,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_THURSDAY1,205,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_FRIDAY1,250,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_SATURDAY1,295,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_SUNDAY1,340,85,40,12,ES_RIGHT | ES_AUTOHSCROLL
        LTEXT           "Second Week:",IDC_STATIC,20,106,49,8
        EDITTEXT        IDC_MONDAY2,70,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_TUESDAY2,115,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_WEDNESDAY2,160,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_THURSDAY2,205,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_FRIDAY2,250,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_SATURDAY2,295,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        EDITTEXT        IDC_SUNDAY2,340,105,40,12,ES_RIGHT | ES_AUTOHSCROLL
        GROUPBOX        "Payroll Processing",IDC_STATIC,10,135,385,75
        PUSHBUTTON      "Process It",IDC_PROCESS_BTN,25,160,50,35
        LTEXT           "Hours",IDC_STATIC,140,150,20,8
        LTEXT           "Amount",IDC_STATIC,185,150,25,8
        LTEXT           "Regular:",IDC_STATIC,99,166,28,8
        EDITTEXT        IDC_REGULAR_HOURS,135,165,40,12,ES_RIGHT | 
                        ES_AUTOHSCROLL
        EDITTEXT        IDC_REGULAR_AMOUNT,180,165,40,12,ES_RIGHT | 
                        ES_AUTOHSCROLL
        LTEXT           "Overtime:",IDC_STATIC,100,182,31,8
        EDITTEXT        IDC_OVERTIME_HOURS,136,180,40,12,ES_RIGHT | 
                        ES_AUTOHSCROLL
        EDITTEXT        IDC_OVERTIME_AMOUNT,180,180,40,12,ES_RIGHT | 
                        ES_AUTOHSCROLL
        LTEXT           "Net Pay:",IDC_STATIC,255,167,28,8
        EDITTEXT        IDC_NET_PAY,295,165,40,12,ES_RIGHT | ES_AUTOHSCROLL
    END
  18. Display the Exercise.cpp file and change it as follows:
     
    #include <windows.h>
    #include <cstdio>
    #include "resource.h"
    
    using namespace std;
    
    #ifdef __BORLANDC__
      #pragma argsused
    #endif
    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_CONTROLS_DLG),
                      hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
            hInst = hInstance;
    
      return 0;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    
        char strHourlySalary[20], strMonday1[10], strTuesday1[10], strWednesday1[10],
              strThursday1[10],  strFriday1[10],  strSaturday1[10],  strSunday1[10],
              strMonday2[10], strTuesday2[10], strWednesday2[10],
              strThursday2[10],  strFriday2[10],  strSaturday2[10],  strSunday2[10];
    
        double monday1 = 0.00, tuesday1 = 0.00, wednesday1 = 0.00, thursday1 = 0.00,
               friday1 = 0.00, saturday1 = 0.00, sunday1 = 0.00,
               monday2 = 0.00, tuesday2 = 0.00, wednesday2 = 0.00, thursday2 = 0.00,
               friday2 = 0.00, saturday2 = 0.00, sunday2 = 0.00;
    
        HWND hWndHourlySalary, hWndMonday1, hWndTuesday1, hWndWednesday1,
             hWndThursday1, hWndFriday1, hWndSaturday1, hWndSunday1,
             hWndMonday2, hWndTuesday2, hWndWednesday2,
             hWndThursday2, hWndFriday2, hWndSaturday2, hWndSunday2,
             hWndRegularHours, hWndOvertimeHours,
             hWndRegularAmount, hWndOvertimeAmount,
             hWndNetPay;
    
        double totalHoursWeek1, totalHoursWeek2, ovtSalary;
        double regHours1 = 0.00, regHours2 = 0.00, ovtHours1 = 0.00, ovtHours2 = 0.00;
        double regAmount1 = 0.00, regAmount2 = 0.00, ovtAmount1 = 0.00, ovtAmount2 = 0.00;
    
        char   strRegularHours[20], strOvertimeHours[20];
        double regularHours, overtimeHours;
        char   strRegularAmount[20], strOvertimeAmount[20], strNetPay[20];
        double regularAmount, overtimeAmount, netPay;
    
        double hourlySalary = 0.00;
    
        hWndHourlySalary = GetDlgItem(hWndDlg, IDC_HOURLY_SALARY);
        hWndMonday1      = GetDlgItem(hWndDlg, IDC_MONDAY1);
        hWndTuesday1     = GetDlgItem(hWndDlg, IDC_TUESDAY1);
        hWndWednesday1   = GetDlgItem(hWndDlg, IDC_WEDNESDAY1);
        hWndThursday1    = GetDlgItem(hWndDlg, IDC_THURSDAY1);
        hWndFriday1      = GetDlgItem(hWndDlg, IDC_FRIDAY1);
        hWndSaturday1    = GetDlgItem(hWndDlg, IDC_SATURDAY1);
        hWndSunday1      = GetDlgItem(hWndDlg, IDC_SUNDAY1);
    
        hWndMonday2      = GetDlgItem(hWndDlg, IDC_MONDAY2);
        hWndTuesday2     = GetDlgItem(hWndDlg, IDC_TUESDAY2);
        hWndWednesday2   = GetDlgItem(hWndDlg, IDC_WEDNESDAY2);
        hWndThursday2    = GetDlgItem(hWndDlg, IDC_THURSDAY2);
        hWndFriday2      = GetDlgItem(hWndDlg, IDC_FRIDAY2);
        hWndSaturday2    = GetDlgItem(hWndDlg, IDC_SATURDAY2);
        hWndSunday2      = GetDlgItem(hWndDlg, IDC_SUNDAY2);
    
        hWndRegularHours   = GetDlgItem(hWndDlg, IDC_REGULAR_HOURS);
        hWndOvertimeHours  = GetDlgItem(hWndDlg, IDC_OVERTIME_HOURS);
        hWndRegularAmount  = GetDlgItem(hWndDlg, IDC_REGULAR_AMOUNT);
        hWndOvertimeAmount = GetDlgItem(hWndDlg, IDC_OVERTIME_AMOUNT);
        hWndNetPay         = GetDlgItem(hWndDlg, IDC_NET_PAY);
    
            switch(Msg)
            {
            case WM_INITDIALOG:
                SetWindowText(hWndHourlySalary, "0.00");
    
                SetWindowText(hWndMonday1, "0.00");
                SetWindowText(hWndTuesday1, "0.00");
                SetWindowText(hWndWednesday1, "0.00");
                SetWindowText(hWndThursday1, "0.00");
                SetWindowText(hWndFriday1, "0.00");
                SetWindowText(hWndSaturday1, "0.00");
                SetWindowText(hWndSunday1, "0.00");
    
                SetWindowText(hWndMonday2, "0.00");
                SetWindowText(hWndTuesday2, "0.00");
                SetWindowText(hWndWednesday2, "0.00");
                SetWindowText(hWndThursday2, "0.00");
                SetWindowText(hWndFriday2, "0.00");
                SetWindowText(hWndSaturday2, "0.00");
                SetWindowText(hWndSunday2, "0.00");
    
                return TRUE;
    
            case WM_COMMAND:
                    switch(wParam)
                    {
                    case IDC_PROCESS_BTN:
                        // Retrieve the hourly salary
                        GetWindowText(hWndHourlySalary, strHourlySalary, 20);
                        hourlySalary = atof(strHourlySalary);
    
                        // Get the time for each day
                        GetWindowText(hWndMonday1, strMonday1, 10);
                        monday1 = atof(strMonday1);
                        GetWindowText(hWndTuesday1, strTuesday1, 10);
                        tuesday1 = atof(strTuesday1);
                        GetWindowText(hWndWednesday1, strWednesday1, 10);
                        wednesday1 = atof(strWednesday1);
                        GetWindowText(hWndThursday1, strThursday1, 10);
                        thursday1 = atof(strThursday1);
                        GetWindowText(hWndFriday1, strFriday1, 10);
                        friday1 = atof(strFriday1);
                        GetWindowText(hWndSaturday1, strSaturday1, 10);
                        saturday1 = atof(strSaturday1);
                        GetWindowText(hWndSunday1, strSunday1, 10);
                        sunday1 = atof(strSunday1);
    
                        GetWindowText(hWndMonday2, strMonday2, 10);
                        monday2 = atof(strMonday2);
                        GetWindowText(hWndTuesday2, strTuesday2, 10);
                        tuesday2 = atof(strTuesday2);
                        GetWindowText(hWndWednesday2, strWednesday2, 10);
                        wednesday2 = atof(strWednesday2);
                        GetWindowText(hWndThursday2, strThursday2, 10);
                        thursday2 = atof(strThursday2);
                        GetWindowText(hWndFriday2, strFriday2, 10);
                        friday2 = atof(strFriday2);
                        GetWindowText(hWndSaturday2, strSaturday2, 10);
                        saturday2 = atof(strSaturday2);
                        GetWindowText(hWndSunday2, strSunday2, 10);
                        sunday2 = atof(strSunday2);
    
                        // Calculate the total number of hours for each week
                        totalHoursWeek1 = monday1 + tuesday1 + wednesday1 + thursday1 +
                                           friday1 + saturday1 + sunday1;
                        totalHoursWeek2 = monday2 + tuesday2 + wednesday2 + thursday2 +
                                           friday2 + saturday2 + sunday2;
    
                         // The overtime is paid time and half
                         ovtSalary = hourlySalary * 1.5;
    
                        // If the employee worked under 40 hours, there is no overtime
                        if( totalHoursWeek1 < 40 )
                        {
                            regHours1  = totalHoursWeek1;
                            regAmount1 = hourlySalary * regHours1;
                            ovtHours1  = 0.00;
                            ovtAmount1 = 0.00;
                        } // If the employee worked over 40 hours, calculate the overtime
                        else if( totalHoursWeek1 >= 40 )
                        {
                            regHours1  = 40;
                            regAmount1 = hourlySalary * 40;
                            ovtHours1  = totalHoursWeek1 - 40;
                            ovtAmount1 = ovtHours1 * ovtSalary;
                        }
    
                        if( totalHoursWeek2 < 40 )
                        {
                            regHours2  = totalHoursWeek2;
                            regAmount2 = hourlySalary * regHours2;
                            ovtHours2  = 0.00;
                            ovtAmount2 = 0.00;
                        }
                        else if( totalHoursWeek2 >= 40 )
                        {
                            regHours2  = 40;
                            regAmount2 = hourlySalary * 40;
                            ovtHours2  = totalHoursWeek2 - 40;
                            ovtAmount2 = ovtHours2 * ovtSalary;
                        }
    
                        regularHours   = regHours1  + regHours2;
                        overtimeHours  = ovtHours1  + ovtHours2;
                        regularAmount  = regAmount1 + regAmount2;
                        overtimeAmount = ovtAmount1 + ovtAmount2;
                        netPay         = regularAmount + overtimeAmount;
    
                        sprintf(strRegularHours, "%.2f", regularHours);
                        sprintf(strOvertimeHours, "%.2f", overtimeHours);
                        sprintf(strRegularAmount, "%.2f", regularAmount);
                        sprintf(strOvertimeAmount, "%.2f", overtimeAmount);
                        sprintf(strNetPay, "%.2f", netPay);
    
                        SetWindowText(hWndRegularHours, strRegularHours);
                        SetWindowText(hWndRegularAmount, strRegularAmount);
                        SetWindowText(hWndOvertimeHours, strOvertimeHours);
                        SetWindowText(hWndOvertimeAmount, strOvertimeAmount);
    
                        SetWindowText(hWndNetPay, strNetPay);
    
                          return TRUE;
    
                    case IDCANCEL:
                            EndDialog(hWndDlg, 0);
                            return TRUE;
                    }
                    break;
            }
    
            return FALSE;
    }
    //---------------------------------------------------------------------------
  19. Test the application then close the dialog box

Home Copyright © 2004-2015 FunctionX, Inc.