Sunteți pe pagina 1din 7

Employee Payroll

In this example, we will calculate the overtime worked by an employee of a company. To do this, we will consider each week day with 8 regular hours. If an employee works more than 8 hours in one day, any time over that is considered overtime. In our payroll simulation, we will cover two weeks but each week has its separate calculation. After collecting the time for both weeks and performing the calculation, we will display the number of regular hours, the number of hours worked overtime, the amount pay for the regular hours, and the amount pay for overtime, if any. At the end, we will display the total net pay.

Practical Learning: Introducing the Date Picker


1. Start a new Windows Application named Payroll1 2. Design the form as follows:

Control GroupBox Label TextBox Label TextBox GroupBox Label

Name

Text Employee Identification Employee #:

Other Properties

txtEmployeeNbr Hourly Salary: txtHourlySalary Time Period Starting Date:

DateTimePicker Label DateTimePicker GroupBox Label Label Label Label Label Label Label Label TextBox TextBox TextBox TextBox TextBox TextBox TextBox Label TextBox TextBox TextBox TextBox TextBox TextBox

dtpStartingDate End Date: dtpEndDate Time Sheet Monday Tuesday Wednesday Thursday Friday Saturday Sunday First Week: txtMonday1 txtTuesday1 txtWednesday1 txtThursday1 txtFriday1 txtSaturday1 txtSunday1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 Second Week: txtMonday2 txtTuesday2 txtWednesday2 txtThursday2 txtFriday2 txtSaturday2 0.00 0.00 0.00 0.00 0.00 0.00 TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right TextAlign: Right

TextBox GroupBox Label Label Button Label TextBox TextBox Label TextBox Label TextBox TextBox Button

txtSunday2

0.00 Payroll Processing Hours Amount

TextAlign: Right

btnProcessIt

Process It Regular

txtRegularHours txtRegularAmount

0.00 0.00 Total Earnings

TextAlign: Right TextAlign: Right

txtNetPay

0.00 Overtime

TextAlign: Right

txtOvertimeHours

0.00

TextAlign: Right TextAlign: Right

txtOvertiimeAmount 0.00 btnClose

3. Double-click the Close button and implement its Click event as follows: Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnClose.Click End End Sub 4. While still in the Code Editor, in the Class Name combo box, make sure (Form1 Events) is selected Click the arrow of the Method Name combo box and select Load 5. Implement the Load event as follows: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim todayDate As DateTime = DateTime.Today Dim twoWeeksAgo As DateTime = todayDate.AddDays(-14) Me.dtpStartingDate.Value = twoWeeksAgo End Sub 6. In the Class Name combo box, select the dtpStartingDate 7. Click the arrow of the Method Name combo box and select CloseUp 8. Implement the event as follows:

Private Sub dtpStartingDate_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles dtpStartingDate.CloseUp ' Get the starting date Dim dteStart As DateTime = Me.dtpStartingDate.Value ' Find out if the user selected a day that is not Monday If dteStart.DayOfWeek <> DayOfWeek.Monday Then MsgBox("The date you selected in invalid\n" & _ "The time period should start on a Monday") End If Me.dtpStartingDate.Focus() End Sub 9. In the Class Name combo box, select the dtpEndDate 10. Click the arrow of the Method Name combo box and select CloseUp 11. Implement the event as follows: Private Sub dtpEndDate_CloseUp(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles dtpEndDate.CloseUp ' Get the starting date Dim dteStart As DateTime = Me.dtpStartingDate.Value ' Get the ending date Dim dteEnd As DateTime = Me.dtpEndDate.Value ' Make sure the first day of the period is Monday If dteStart.DayOfWeek <> DayOfWeek.Monday Then MsgBox("The starting date you selected in invalid\n" & _ "The time period should start on a Monday") End If Me.dtpStartingDate.Focus() ' Find the number of days that separate the start and end Dim timeDifference As TimeSpan = dteEnd.Subtract(dteStart) Dim fourteenDaysLater As Double = timeDifference.Days If (dteEnd.DayOfWeek <> DayOfWeek.Sunday) Or (fourteenDaysLater <> 13) Then MsgBox("The ending date you selected in invalid\n" & _ "The time period should span 2 weeks and end on a Sunday") End If Me.dtpEndDate.Focus() End Sub 12. In the Class Name combo box, select the btnProcessIt 13. Click the arrow of the Method Name combo box and select Click 14. Implement the Click event as follows: Private Sub btnProcessIt_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnProcessIt.Click Dim monday1 As Double Dim tuesday1 As Double Dim wednesday1 As Double Dim thursday1 As Double

Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim Dim

friday1 As Double saturday1 As Double sunday1 As Double monday2 As Double tuesday2 As Double wednesday2 As Double thursday2 As Double friday2 As Double saturday2 As Double sunday2 As Double totalHoursWeek1 As Double totalHoursWeek2 As Double regHours1 As Double regHours2 As Double ovtHours1 As Double ovtHours2 As Double regAmount1 As Double regAmount2 As Double ovtAmount1 As Double ovtAmount2 As Double regularHours As Double overtimeHours As Double regularAmount As Double overtimeAmount As Double totalEarnings As Double

Dim hourlySalary As Double ' Retrieve the hourly salary hourlySalary = CDbl(Me.txtHourlySalary.Text) ' Retrieve the time for each day ' First Week monday1 = CDbl(Me.txtMonday1.Text) tuesday1 = CDbl(Me.txtTuesday1.Text) wednesday1 = CDbl(Me.txtWednesday1.Text) thursday1 = CDbl(Me.txtThursday1.Text) friday1 = CDbl(Me.txtFriday1.Text) saturday1 = CDbl(Me.txtSaturday1.Text) sunday1 = CDbl(Me.txtSunday1.Text) ' Second Week monday2 = CDbl(Me.txtMonday2.Text) tuesday2 = CDbl(Me.txtTuesday2.Text) wednesday2 = CDbl(Me.txtWednesday2.Text) thursday2 = CDbl(Me.txtThursday2.Text) friday2 = CDbl(Me.txtFriday2.Text) saturday2 = CDbl(Me.txtSaturday2.Text) sunday2 = CDbl(Me.txtSunday2.Text) ' 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 Dim ovtSalary As Double ovtSalary = hourlySalary * 1.5 ' If the employee worked under 40 hours, there is no overtime

If totalHoursWeek1 < 40 Then regHours1 = totalHoursWeek1 regAmount1 = hourlySalary * regHours1 ovtHours1 = 0 ovtAmount1 = 0 ' If the employee worked over 40 hours, calculate the overtime ElseIf totalHoursWeek1 >= 40 Then regHours1 = 40 regAmount1 = hourlySalary * 40 ovtHours1 = totalHoursWeek1 - 40 ovtAmount1 = ovtHours1 * ovtSalary End If If totalHoursWeek2 < 40 Then regHours2 = totalHoursWeek2 regAmount2 = hourlySalary * regHours2 ovtHours2 = 0 ovtAmount2 = 0 ElseIf totalHoursWeek2 >= 40 Then regHours2 = 40 regAmount2 = hourlySalary * 40 ovtHours2 = totalHoursWeek2 - 40 ovtAmount2 = ovtHours2 * ovtSalary End If regularHours = regHours1 + regHours2 overtimeHours = ovtHours1 + ovtHours2 regularAmount = regAmount1 + regAmount2 overtimeAmount = ovtAmount1 + ovtAmount2 totalEarnings = regularAmount + overtimeAmount Me.txtRegularHours.Text = regularHours.ToString("F") Me.txtOvertimeHours.Text = overtimeHours.ToString("F") Me.txtRegularAmount.Text = regularAmount.ToString("C") Me.txtOvertimeAmount.Text = overtimeAmount.ToString("C") Me.txtNetPay.Text = totalEarnings.ToString("C") End Sub 15. Execute the application to test it

16. After using it, close the form and return to your programming environment

S-ar putea să vă placă și