Date Class – Java

A part of a university assignment, I was asked to create a Date Class using Java which processes date and time data information in various ways including counting days until, days between dates, how many days in a certain year (inc leap years) and more. While processing the data I was NOT allowed to use any of the already existing Java Date class.

 

Code Snippet

Public Class Form1

    '================================================================
    '== Sub:      btnExit_Click(...)
    '== Descript: Processes the "Click" event for the "Exit" button.
    '==           Stops the program
    '==    
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub  'btnExit_Click(...)



    '================================================================
    '== Sub:      btnCalculate_Click(...)
    '== Descript: Processes the "Click" event for the "Calculate" button.
    '==           Reads in the employee's details and calculates payroll
    '==           details.  
    '==    
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim hoursWorked, hourlyRate As Single 'input values
        Dim hoursNormal, hoursTimeAndHalf, hoursDoubleTime As Single
        Dim payNormal, payTimeAndHalf, PayDoubleTime As Single
        Dim grossPay, takeHomePay As Single
        Dim taxAt25perCent, taxAt50perCent As Single
        Dim natInsurance, socialClub, unionFee As Single
        Dim totalDeductions, totalTax As Single

        '1. Get the input data values from the form
        hoursWorked = Val(txtHoursWorked.Text)
        hourlyRate = Val(txtHourlyRate.Text)

        '2. Calculate hours of overtime etc
        If (hoursWorked > 50) Then
            hoursNormal = 40
            hoursTimeAndHalf = 10
            hoursDoubleTime = hoursWorked - 50
        ElseIf (hoursWorked > 40) Then
            hoursNormal = 40
            hoursTimeAndHalf = hoursWorked - 40
            hoursDoubleTime = 0
        Else
            hoursNormal = hoursWorked
            hoursTimeAndHalf = 0
            hoursDoubleTime = 0
        End If

        '3. Calculate gross pay (before deductions)
        payNormal = hoursNormal * hourlyRate
        payTimeAndHalf = hoursTimeAndHalf * hourlyRate * 1.5
        PayDoubleTime = hoursDoubleTime * hourlyRate * 2

        grossPay = payNormal + payTimeAndHalf + PayDoubleTime

        '4. Calculate tax 
        '  4.1 by tax band
        If (grossPay > 300) Then
            'high rate tax
            taxAt50perCent = (grossPay - 300) / 2
            'medium tax rate
            taxAt25perCent = 25
        ElseIf (grossPay > 200) Then
            'high rate tax = 0
            taxAt50perCent = 0
            'medium tax rate 
            taxAt25perCent = (grossPay - 200) / 4
        Else
            'no tax to pay
            taxAt50percent = 0
            taxAt25perCent = 0
        End If

        '  4.2 hence total tax
        totalTax = taxAt50percent + taxAt25perCent

        '5. Calculate other deductions
        '  5.1 Nat Insurance
        natInsurance = 20.5

        '  5.2 Social Club?
        If cbxMemberOfSocialClub.Checked Then
            socialClub = 5.0
        Else
            socialClub = 0
        End If

        '  5.3 Union?
        If cbxMemberOfUnion.Checked Then
            unionFee = 2.5
        Else
            unionFee = 0
        End If

        '6. Calculate take home pay
        totalDeductions = totalTax + natInsurance + socialClub + unionFee
        takeHomePay = grossPay - totalDeductions

        '7. Display the results
        lblGrosspay.Text = FormatCurrency(grossPay, 2)
        lblTotalTax.Text = FormatCurrency(totalTax, 2)
        lblNatInsurance.Text = FormatCurrency(natInsurance, 2)
        lblSocialClub.Text = FormatCurrency(socialClub, 2)
        lblUnion.Text = FormatCurrency(unionFee, 2)
        lblTotalDeductions.Text = FormatCurrency(totalDeductions, 2)

        lblTakeHome.Text = FormatCurrency(takeHomePay, 2)

    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class




Leave a comment

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: