Laboratory Exercise – Function & Subroutine

Payroll Application

Problem Description:

Develop an application that tracks the payroll for four departments: Management, Sales, Processing, and Phone. The user of the application will be able to enter a payroll amount and the department to which the payroll should be assigned. The application will determine if the department listed is valid as well as check to ensure that the payroll is a positive value. If inappropriate input is entered, a message box will be displayed indicating the error. Otherwise, the payroll will be added to the departments payroll total and output along with the other department totals in a label displayed on the form.

Your application should look as follows:

Problem Discussion:

The layout of your application is relatively simple. Your application will require a text box to allow the user to enter a department and payroll amount. Both text boxes should be labeled accordingly. You will need a button to process the payroll entered and another button to process the budget entered. You will also need a label to indicate the title of the application as well as one to display the total payroll and budget entered so far for each department.

You will require a variable to store each department’s payroll total as well as a variable to store each department’s budget. While the problem didn’t specify a range of values for a payroll, one can assume that it will be large. Therefore, you will use a Double to store each department’s total.

The only coding required for the application could be written entirely in the Click events of the buttons, however this would lead to confusing and repeated code. With your newly acquired knowledge of functions, you should see an opportunity to improve the efficiency and readability of your code. Create three routines that will assist coding the Click events of the two buttons in your application. By coding a ProcessPayroll and ProcessBudget function, you can determine whether or not the department is valid and if so, process the amount entered. By coding an OutputResults function you can move code that would have been repeated into a separate subroutine. This will make the code easier to read and make updating the application simpler.

Code is required to process the payroll entered. The code will require validating the data entered. The first check should be to see that the payroll amount is greater than 0. While it is not incorrect to add a payroll amount of 0, this will have no effect on the result and should be excluded. Once a valid payroll amount has been entered, the department must be validated.

While there are only four departments currently in your system, checking for them can add some size and confusion to your code. Why not implement the checking and processing of the payroll for a department as a separate function? Then you could simply return True or False to indicate whether or not the department entered was correct. Your implementation should use a Select/Case statement for clarity and check for the capitalized. lowercase, and all uppercase versions of the department name. Therefore, for the management department your code should consider Management, management, and MANAGEMENT all valid departments.

If the department entered was valid, then you need to update the lblTotals label with the latest information. Again, you could code the statements directly in the button’s Click event, however when you process the addition of budget amounts, you will need to write the identical code. Instead, you should create a subroutine that will handle the outputting of information and call it from both Click events.

Finally, you will need to code the btnBudget button’s Click event in a similar manner.

 

 

Problem Solution:

Step 1: Create an Application called Payroll.

Step 2: Rename the form in the Solution Explorer to frmPayroll.vb.

Step 3: Change the Name property of the form to frmPayroll.

Step 4: Change the Text property of the form to Payroll Application.

Step 5: Add a label control to the form.

Step 6: Change the Name property of the label to lblTitle.

Step 7: Change the Text property of the label to Payroll Application.

Step 8: Change the TextAlign property of the label to MidddleCenter.

Step 9: Change the Font Size to 16 and the Bold property to True.

 

Step 10: Add a label for the department text box.

Step 11: Change the Name property of the label to lblDepartment.

Step 12: Change the Text property of the label to Department.

Step 13: Change the Font Bold property to True.

 

Step 14: Add a text box for the department.

Step 15: Change the Name property of the label to txtDepartment.

Step 16: Clear the Text property.

Step 17: Add a label for the amount text box.

Step 18: Change the Name property of the label to lblAmount.

Step 19: Change the Text property of the label to Amount.

Step 20: Change the Font Bold property to True.

 

Step 21: Add a text box for the amount.

Step 22: Change the Name property of the label to txtAmount.

Step 23: Clear the Text property.

 

Step 24: Add a button to the form for adding to the payroll.

Step 25: Change the Name property of the button to btnAddPayroll.

Step 26: Change the Text property of the button to Add Payroll.

Step 27: Add a button to the form for adding to the budget.

Step 28: Change the Name property of the button to btnAddBudget.

Step 29: Change the Text property of the button to Add Budget.

Step 30: Add a label to display the total payroll and budget for each department.

Step 31: Change the Name property of the label to lblTotals.

Step 32: Clear the Text property of the label.

Step 33: Add the declaration of the variables to store the total payrolls and budgets of each department.

Step 34: Add the code to the btnAddPayroll button’s Click event. Follow the algorithm presented in the problem discussion.

Step 35: Add the code to the btnAddBudget button’s Click event. Follow the algorithm presented in the problem discussion.

Step 36: Add the code for the function ProcessPayroll. Follow the algorithm presented in the problem discussion.

Step 37: Add the code for the function ProcessBudget. Follow the algorithm presented in the problem discussion.

Step 38: Add the code for the function OutputResults. Follow the algorithm presented in the problem discussion.