VB.net Payroll Application

Option Strict On

' Payroll Application
' Author: Mark Abrams
' Date: November 20, 2005
' Class: CSC201
' Programming project to show use of Class "is a" inheritance and
' Class "has a" instantiation relationships.
' Class objects will provide data validation and data integrity
' of the Person, Address, and Employee classes
' Uses array, enums, listbox to add, modify and process
' employee payroll data to an output list for displaying the payroll
' calculations of each employee listed and on payroll salary.

Public Class payrollForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

'Create myEmployee variable and instantiate a myEmployee object
Dim myEmployee As CEmployee = New CEmployee

'Create a variable and instantiate myFunctions
Dim MyFunction As CFunctions = New CFunctions

Dim selectedEmployeeindex As Integer
Dim errorFlag As Boolean

' define arrays for displaying and selecting employee objects
Private myEmployees As ArrayList = New ArrayList ' Employees Array

' VALIDATE USER INPUT
Sub valEmpData()

' errorFlag is passed to calling sub
errorFlag = False

' First Name
If txtFName.Text.Length = 0 Or MyFunction.IsAlpha(txtFName.Text) = False Then ' Validate an alpha string
MessageBox.Show("First Name is required Alpha", "First Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtFName.Focus()
errorFlag = True
Exit Sub
Else
txtFName.Text = MyFunction.TitleCase(txtFName.Text) 'convert to title case
End If

' Last Name
If txtLName.Text.Length = 0 Or MyFunction.IsAlpha(txtLName.Text) = False Then ' Validate an alpha string
MessageBox.Show("Last Name is required Alpha", "Last Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtLName.Focus()
errorFlag = True
Exit Sub
Else
txtLName.Text = MyFunction.TitleCase(txtLName.Text) 'convert to title case
End If

' Address
If txtAddress.Text.Length = 0 Or IsNumeric(txtAddress.Text) = True Then ' Validate a non numeric
MessageBox.Show("Address is required", "Address", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtAddress.Focus()
errorFlag = True
Exit Sub
End If

' City
If txtCity.Text.Length = 0 Or MyFunction.IsAlpha(txtCity.Text) = False Then ' Validate an alpha string
MessageBox.Show("City name is required Alpha", "City", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtCity.Focus()
errorFlag = True
Exit Sub
Else
txtCity.Text = MyFunction.TitleCase(txtCity.Text) 'convert to title case
End If

' State
If MyFunction.IsUSState(UCase(txtState.Text)) = False Then ' check for valid abbrev - upper case to state string
MessageBox.Show("Invalid State Abbreviation Required", "State", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtState.Focus()
errorFlag = True
Exit Sub
End If

'Zip
If txtZip.Text.Length <> 5 Or IsNumeric(txtZip.Text) = False Then
MessageBox.Show("Invalid Zip - 5 digits required", "Zip", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtZip.Focus()
errorFlag = True
Exit Sub
End If

'Phone
If txtPhone.Text.Length = 10 And IsNumeric(txtPhone.Text) Then
Else
MessageBox.Show("Invalid Phone - 10 digits required", "Phone", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtPhone.Focus()
errorFlag = True
Exit Sub
End If

' Job Title
If txtTitle.Text.Length > 0 Then
If MyFunction.IsAlpha(txtTitle.Text) = False Then
MessageBox.Show("Invalid Title - If supplied must be all Alpha characaters", _
"Title", MessageBoxButtons.OK, MessageBoxIcon.Error)
txtTitle.Focus()
errorFlag = True
Exit Sub
Else
txtTitle.Text = MyFunction.TitleCase(txtTitle.Text) 'convert to title case
End If
End If

' Wage
If IsNumeric(txtWage.Text) = True Then ' check pay range for numeric data
If CType(txtWage.Text, Integer) < 7.75 Or CType(txtWage.Text, Integer) > 55.0 Then
errorFlag = True
Else ' Passes validation
End If
Else 'non numeric data
errorFlag = True
End If

If errorFlag Then
MessageBox.Show("Wage is required to be between $7.75 and $55.00", _
"Validation Check", MessageBoxButtons.OK, MessageBoxIcon.Warning)
txtWage.Focus()
Exit Sub
End If
End Sub

' ADD EMPLOYEE
Private Sub btnAddEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEmployee.Click
btnUpdateEmployee.Enabled = False ' Disable the update button

'Validate user data
valEmpData()

If errorFlag = True Then ' test data for errors
Exit Sub
End If

' Load myEmployee object properties with data from form controls
myEmployee = New CEmployee
With myEmployee
.FirstName = txtFName.Text
.LastName = txtLName.Text
.Address.Address = txtAddress.Text
.Address.ApartmentNo = txtApt.Text
.Address.City = txtCity.Text
.Address.State = txtState.Text
.Address.Zip = txtZip.Text
.Phone = txtPhone.Text
.JobTitle = txtTitle.Text
.Wage = CType(Val(txtWage.Text), Integer)
.OnPayroll = cbOnPayroll.Checked
End With

'Add myEmployee to array
myEmployees.Add(myEmployee)

'Add myEmployee Name (via tostring to the listbox
lstEmployeeInfo.Items.Add(myEmployee.ToString)

clearEmployee() ' clears employee data from form controls
End Sub

' CLEAR Employee Form
Private Sub clearEmployee()

' Control Array to clear employee form text and check boxs
Dim o As Object
For Each o In grpPersonnelInfo.Controls 'Clear each control object in the groupbox by datatype
If TypeOf o Is TextBox Then
CType(o, TextBox).Text = ""
ElseIf TypeOf o Is CheckBox Then
CType(o, CheckBox).Checked = False
End If
Next
End Sub

' UPDATE Employee
Private Sub btnUpdateEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateEmployee.Click

'Validate user data
valEmpData()

If errorFlag = True Then ' test data for errors
Exit Sub
End If

' set control buttons
btnUpdateEmployee.Enabled = False
btnAddEmployee.Enabled = True

'instantiate myEmployee object and assign properties
myEmployee = New CEmployee
With myEmployee
.FirstName = txtFName.Text
.LastName = txtLName.Text
.Address.Address = txtAddress.Text
.Address.ApartmentNo = txtApt.Text
.Address.City = txtCity.Text
.Address.State = txtState.Text
.Address.Zip = txtZip.Text
.Phone = txtPhone.Text
.JobTitle = txtTitle.Text
.Wage = CType(Val(txtWage.Text), Integer)
.OnPayroll = cbOnPayroll.Checked
End With

' Change array value of employee to object myEmployee
myEmployees.Item(selectedEmployeeindex) = myEmployee

'Remove selected item by index & insert new item into selected index
lstEmployeeInfo.Items.RemoveAt(selectedEmployeeindex)
lstEmployeeInfo.Items.Insert(selectedEmployeeindex, myEmployee.ToString)

clearEmployee() ' clears employee data from form controls

txtFName.Focus()
End Sub

' SELECT Employee from Listbox for UPDATE
Private Sub lstEmployeeInfo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmployeeInfo.Click

If lstEmployeeInfo.SelectedIndex < 0 Then ' Null selection - exit
Exit Sub
End If

' reset control buttons for processing
btnUpdateEmployee.Enabled = True
btnAddEmployee.Enabled = False

Dim output As String

' get selected index of employee from the employee listbox
selectedEmployeeindex = lstEmployeeInfo.SelectedIndex

' load the selected employee data from the array into the object myEmployee
myEmployee = CType(myEmployees(selectedEmployeeindex), CEmployee)

' load corresponding class properties into the form control properties
txtFName.Text = myEmployee.FirstName
txtLName.Text = myEmployee.LastName
txtAddress.Text = myEmployee.Address.Address
txtApt.Text = myEmployee.Address.ApartmentNo
txtCity.Text = myEmployee.Address.City
txtState.Text = myEmployee.Address.State
txtZip.Text = myEmployee.Address.Zip
txtPhone.Text = myEmployee.Phone
txtTitle.Text = myEmployee.JobTitle
txtWage.Text = CType(myEmployee.Wage, String)
cbOnPayroll.Checked = myEmployee.OnPayroll
End Sub

' PAY Employees
Private Sub btnPayEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayEmployee.Click
Dim myValue As String = ""
Dim pay As String = ""

lstPayInfo.Items.Clear() ' initialize the listbox content

' Output report to the PayInfo listbox
lstPayInfo.Items.Add("First Name" & vbTab & "Last Name" & vbTab & "Pay")
lstPayInfo.Items.Add("---------------------------------------------------------------------")

myEmployee = New CEmployee
Dim counter As Integer
For counter = 0 To (myEmployees.Count - 1)

' load the selected employee data from the array into the object myEmployee
myEmployee = CType(myEmployees(counter), CEmployee)

' Output Calculated pay info for each employee on the payroll
If myEmployee.OnPayroll = True Then

' Get the hours worked and set the myEmployee Hours Worked property
myValue = InputBox("Please enter the hours for " _
& myEmployee.ToString, "Payroll Hours", "0")

' Validate for numeric input
pay = "invalid hours provided"
If IsNumeric(myValue) Then
If CType(myValue, Single) >= 0 And CType(myValue, Single) <= 80 Then
myEmployee.HoursWorked = CType(myValue, Single)
pay = CType(myEmployee.CalcPay, String)
Else
MessageBox.Show("Valid input is between 0 and 80", _
"Validation Check", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End If

' Load the employee properties and pay calculation for the pay listbox
lstPayInfo.Items.Add(myEmployee.FirstName & vbTab & vbTab & _
myEmployee.LastName & vbTab & vbTab & pay)
End If
Next
End Sub

Private Sub payrollForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtFName.Focus()
End Sub
End Class