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 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
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.
CodePudding user response:
Going to be completely honest, this is very off the top of my head and more focused on the how rather than implementing your rules and logic but hope this might serve as a starter for you. There are a lot more elegant solutions that this as well.
Public Class Form1
Const StdRate = 17.45
Property StandardHours As Integer
Property OvertimeHours As Integer
Property WeekendHours As Integer
Property PublicHolidayHours As Integer
ReadOnly Property TotalPay As Double
Get
Return (StandardHours * StdRate) (OvertimeHours * (StdRate 4))
(WeekendHours * (StdRate * 1.7)) (PublicHolidayHours * (StdRate (StdRate * 0.35)))
End Get
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtStdHours.DataBindings.Add("Value", Me, "StandardHours", True, DataSourceUpdateMode.OnPropertyChanged)
txtOvertimeHrs.DataBindings.Add("Value", Me, "OvertimeHours", True, DataSourceUpdateMode.OnPropertyChanged)
txtWeekendHrs.DataBindings.Add("Value", Me, "WeekendHours", True, DataSourceUpdateMode.OnPropertyChanged)
txtPublichHolidayHours.DataBindings.Add("Value", Me, "PublicHolidayHours", True, DataSourceUpdateMode.OnPropertyChanged)
txtTotal.DataBindings.Add("Text", Me, "TotalPay", True, DataSourceUpdateMode.OnPropertyChanged)
End Sub
End Class
Also note I've used a Constant for the standard pay rate, you might have another textbox or something around that holds that. You can apply the same principles to it by adding it's own property and bindings etc