Best way to calculate the paycheck

Mattia Fanti 356 Reputation points
2022-06-27T00:41:36.427+00:00

I want to calculate my paycheck taking in consideration few paramethers like holidays/ sundays/saturday etc which are paid more than the business day. I'm trying to follow a simple logic.

The rules to follow are business

  • day - from 06am to 10 pm paid normally hourly wage
  • night - from 10 pm to 06 am paid normally hourly wage plus a 4$ added on the total. (p.s. I am eligible for the 4$ added on the total only if I work more at least 3 hours at night)
  • saturday - each hour get paid 30% more
  • sunday - each hour get paid 0.70$ more holiday - each hour get paid 35% more if I work max 7 hours - 40% from the 8th hour and forth

Other examples and why my code is not working properly...Lets say my hourly wage is 10$.
I work From 8 pm to 3am, so I add 2 hours on the day numericUpDown control ( total is now 20$) and I add 3 night hours ( total should be now 20 + (10x3)+30%)) but my code doesn't care about the 20$ already sitting in there. The issue stands after each Else, but I don't know how to make it work.. I thought I could fix it with a boolean variable, but still don't get what I want.

Also, I'm not considering sunday as a public holiday, endeed that's paid as a simple hourly wage + 0.71$ per hour. So I add 2 saturday hours 10$ x 2 + 30%, plus 5x 10.70$.
What if sunday is a holiday? well, in that case we can say that you get paid 10.71 + 35% or 35% What If I work on a holiday day which is sunday at night? well, 10.71 + 35% or 35% + 4$ on top of the total

It can happen that I work on saturday night until sunday morning so I need to get a right calculation. Also It can happen to work on a sunday holiday with get paid 35 ( or 40%) plus 0.70$ each hour. This wouldn't happen in case I work on an holiday on saturday, cause the biggest % absorb the smaller % so on holiday in saturday I only get paid 35( or 40% based on how many hours..)

I'm trying with Few numericUpDown ( 5 in total - business day, night, saturday, sunday, holidays) controls and two textboxes. First textbox contains the normal hourly wage ex 10%. The second textbox contains the total amount of the paid day.

The code I wrote so far is

 Dim filledalready As Boolean  
   Private Sub nighthours_ValueChanged(sender As Object, e As EventArgs) Handles nighthours.ValueChanged  
       If filledalready = False Then  
           If night.Value >= 3 Then  
               total.Text = CStr(CDbl(night.Value) * ((CStr(CDbl(hourlywage.Text) + CDbl(hourlywage.Text) * 30 / 100))))  
               filledalready = True  
           End If  
       Else  
           total.Text = CStr(CDbl(total.Text) + CStr(CDbl(night.Value) * ((CStr(CDbl(hourlywage.Text) + CDbl(hourlywage.Text) * 30 / 100)))))  
           filledalready = True  
       End If  
  
   End Sub  
  
   Private Sub dayhours_ValueChanged(sender As Object, e As EventArgs) Handles dayhours.ValueChanged  
  
       If filledalready = False Then  
           total.Text = CDbl(day.Value) * CStr(CDbl(hourlywage.Text))  
           filledalready = True  
       Else  
           total.Text = CStr(CDbl(total.Text) + CDbl(day.Value) * CStr(CDbl(hourlywage.Text)))  
           filledalready = False  
       End If  
  
   End Sub  

I've tried using a boolean variable because if I set the worked hours for a business day and then add few hours from the night, it would make errors on the calculation, but also It still doesn't get what I want and still it would give wrong numbers as output. I'm wondering if I'm following the right way to do it and if not, please feel free to point me out to the right path.

Lets say I m looking for a Form level variable to hold the total pay while I calculate it without making things too complicated... Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 26,506 Reputation points Microsoft Vendor
    2022-06-28T07:25:22.68+00:00

    Hi @Mattia Fanti ,
    Organized a function to calculate total.
    Please point out if my understanding is wrong.

    12 NumericUpDown controls are involved.
    daytime, night,
    saturday_daytime, saturday_night,
    sunday_daytime, sunday_night,
    holiday_daytime, holiday_night,
    holiday_saturday_daytime, holiday_saturday_night,
    holiday_sunday_daytime, holiday_sunday_night.

        Public Function paycheck() As Double  
            Dim BusinessDayPC As Double  
            Dim SaturdayPC As Double  
            Dim SundayPC As Double  
            Dim HolidayDayPC As Double  
            Dim HolidayDaySaturdayPC As Double  
            Dim HolidayDaySundayPC As Double  
            If night.Value >= 3 Then  
                BusinessDayPC = (daytime.Value + night.Value) * CDbl(hourlywage.Text) + 4  
            Else  
                BusinessDayPC = (daytime.Value + night.Value) * CDbl(hourlywage.Text)  
            End If  
            If saturday_night.Value >= 3 Then  
                SaturdayPC = (saturday_daytime.Value + saturday_night.Value) * CDbl(hourlywage.Text) * 1.3 + 4  
            Else  
                SaturdayPC = (saturday_daytime.Value + saturday_night.Value) * CDbl(hourlywage.Text) * 1.3  
            End If  
            If sunday_night.Value >= 3 Then  
                SundayPC = (sunday_daytime.Value + sunday_night.Value) * (CDbl(hourlywage.Text) + 0.7) + 4  
            Else  
                SundayPC = (sunday_daytime.Value + sunday_night.Value) * (CDbl(hourlywage.Text) + 0.7)  
            End If  
      
            If holiday_night.Value >= 3 Then  
                If holiday_daytime.Value + holiday_night.Value <= 7 Then  
                    HolidayDayPC = (holiday_daytime.Value + holiday_night.Value) * CDbl(hourlywage.Text) * 1.35 + 4  
                Else  
                    HolidayDayPC = CDbl(hourlywage.Text) * 1.35 * 7 + (holiday_daytime.Value + holiday_night.Value - 7) * CDbl(hourlywage.Text) * 1.4 + 4  
                End If  
            Else  
                If holiday_daytime.Value + holiday_night.Value <= 7 Then  
                    HolidayDayPC = (holiday_daytime.Value + holiday_night.Value) * CDbl(hourlywage.Text) * 1.35  
                Else  
                    HolidayDayPC = CDbl(hourlywage.Text) * 1.35 * 7 + (holiday_daytime.Value + holiday_night.Value - 7) * CDbl(hourlywage.Text) * 1.4  
                End If  
            End If  
      
            If saturday_night.Value >= 3 Then  
                If saturday_daytime.Value + saturday_night.Value <= 7 Then  
                    HolidayDaySaturdayPC = (saturday_daytime.Value + saturday_night.Value) * CDbl(hourlywage.Text) * 1.35 + 4  
                Else  
                    HolidayDaySaturdayPC = CDbl(hourlywage.Text) * 1.35 * 7 + (saturday_daytime.Value + saturday_night.Value - 7) * CDbl(hourlywage.Text) * 1.4 + 4  
                End If  
            Else  
                If saturday_daytime.Value + saturday_night.Value <= 7 Then  
                    HolidayDaySaturdayPC = (saturday_daytime.Value + saturday_night.Value) * CDbl(hourlywage.Text) * 1.35  
                Else  
                    HolidayDaySaturdayPC = CDbl(hourlywage.Text) * 1.35 * 7 + (saturday_daytime.Value + saturday_night.Value - 7) * CDbl(hourlywage.Text) * 1.4  
                End If  
            End If  
      
            If sunday_night.Value >= 3 Then  
                If sunday_daytime.Value + sunday_night.Value <= 7 Then  
                    HolidayDaySundayPC = (sunday_daytime.Value + sunday_night.Value) * (CDbl(hourlywage.Text) + 0.7) * 1.35 + 4  
                Else  
                    HolidayDaySundayPC = (CDbl(hourlywage.Text) + 0.7) * 1.35 * 7 + (sunday_daytime.Value + sunday_night.Value - 7) * (CDbl(hourlywage.Text) + 0.7) * 1.4 + 4  
                End If  
            Else  
                If sunday_daytime.Value + sunday_night.Value <= 7 Then  
                    HolidayDaySundayPC = (sunday_daytime.Value + sunday_night.Value) * (CDbl(hourlywage.Text) + 0.7) * 1.35  
                Else  
                    HolidayDaySundayPC = (CDbl(hourlywage.Text) + 0.7) * 1.35 * 7 + (sunday_daytime.Value + sunday_night.Value - 7) * (CDbl(hourlywage.Text) + 0.7) * 1.4  
                End If  
            End If  
      
            Return (BusinessDayPC + SaturdayPC + SundayPC + HolidayDayPC + HolidayDaySaturdayPC + HolidayDaySundayPC)  
        End Function  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.