Lee Wyatt
Lee Wyatt

Reputation: 9

VB.net - Time and a half

I'm stuck on how I would modify this so that it pays the employee time and one half for any hours worked over 40. I understand it would be an if statement, but I'm unsure as to how I would go about it specifically.

Example: If the user enters 35.5 hours and 9.56 as rate, net would be $237.56.

Input: Hours worked and pay rate.

Output: Different taxes and net pay.

Here is what I have so far:

    Const FWT_ As Decimal = 0.2
    Const FICA_ As Decimal = 0.08
    Const STAT As Decimal = 0.02

    Dim Hrs As Decimal
    Dim Rate As Decimal
    Dim Gros As Decimal
    Dim FWT As Decimal
    Dim Fi As Decimal
    Dim stat As Decimal
    Dim NetPay As Decimal


    Decimal.TryParse(txtHours.Text, Hrs)
    Decimal.TryParse(txtRate.Text, Rate)


    Gros = Math.Round(Hrs * Rate, 2)
    FWT = Math.Round(Gros * FWT_, 2)
    Fi = Math.Round(Gros * FICA_, 2)
    stat = Math.Round(Gros * STAT, 2)
    NetPay = Gros - FWT - Fi - stat

    lblGr.Text = Gros.ToString("N2")
    lblF.Text = Fi.ToString("N2")
    lblF.Text = Fi.ToString("N2")
    lblSt.Text = stat.ToString("N2")
    lblN.Text = NetPay.ToString("C2")

Thank you.

Upvotes: 1

Views: 327

Answers (2)

I don't know how specifically to write the answer such that it will compile in your visual basic program but i can give you the algorithmic answer

if(hours > 40)
    payment = 40 * rate + ((hours-40)) * (rate*1.5);
else
    payment = hours * rate;

or if VB supports the ternary operator

payment = hours > 40 ? (40 * rate + ((hours-40)) * (rate*1.5)) : (hours * rate);

If i were to be given this code to edit the first thing i'd try is :

Gros = Math.Round(Hrs * Rate, 2)

to

Gros = Math.Round(hours > 40 ? (40 * rate + ((hours-40)) * (rate*1.5)) : (hours * rate), 2)

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102783

You could just add 0.5 * Rate pay for any hours worked over 40 to your existing calculation:

If Hrs > 40 Then
    Gros += Math.Round( (Hrs-40) * Rate * 0.5 )
End If

Upvotes: 1

Related Questions