'Program Name:   Exercise 5.1

'Programmer:     Bradley Shedd

'Date:           August 2006

'Description:    This project uses menus and a function procedure.

'                The amount due is calculated

'                based on the number of pieces completed

'                and  summary data is accumulated for the total

'                number of pieces, the total pay and the average pay

'                per person. 

'Folder:         EX0501

 

Option Strict On

 

Public Class pieceworkForm

 

    Friend workerCountInteger, totalPiecesInteger As Integer

    Friend totalPayDecimal, averagePayDecimal As Decimal

    Const RATE1_TO_199_Decimal As Decimal = 0.5D

    Const RATE200_TO_399_Decimal As Decimal = 0.55D

    Const RATE400_TO_599_Decimal As Decimal = 0.6D

    Const RATE600_PLUS_Decimal As Decimal = 0.65D

 

    Private Function AmountEarned(ByVal quantityInteger As Integer) As Decimal

        'Calculate the amount earned

        Dim payRateDecimal As Decimal

 

        Select Case quantityInteger

            Case 1 To 199

                payRateDecimal = RATE1_TO_199_Decimal

                Return quantityInteger * payRateDecimal

            Case 200 To 399

                payRateDecimal = RATE200_TO_399_Decimal

                Return quantityInteger * payRateDecimal

            Case 400 To 599

                payRateDecimal = RATE400_TO_599_Decimal

                Return quantityInteger * payRateDecimal

            Case Is > 599

                payRateDecimal = RATE600_PLUS_Decimal

                Return quantityInteger * payRateDecimal

        End Select

    End Function

 

    Private Sub CalculatePayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculatePayToolStripMenuItem.Click

        'Calculate the amount earned

        Dim messageString As String

        Dim quantityInteger As Integer

        Dim amountEarnedDecimal As Decimal

 

        'Calculate pay

        With Me

            If .nameTextBox.Text <> "" Then 'Name has been entered

                Try

                    'Convert the quantity

                    quantityInteger = Integer.Parse(.quantityTextBox.Text)

 

                    'Number of pieces must be greater than zero

                    If quantityInteger > 0 Then

                        'Determine the correct pay rate

                        amountEarnedDecimal = AmountEarned(quantityInteger)

                        totalPayDecimal += amountEarnedDecimal

                        totalPiecesInteger += quantityInteger

                        workerCountInteger += 1

 

                        'Display information

                        .amountEarnedTextBox.Text = amountEarnedDecimal.ToString("C")

                    Else

                        'Zero was entered

                        messageString = "Please enter a number greater than zero."

                        MessageBox.Show(messageString, "Data Entry Error", _

                            MessageBoxButtons.OK, MessageBoxIcon.Information)

                        With quantityTextBox

                            .Focus()

                            .SelectAll()

                        End With

                    End If

                Catch quantityException As FormatException

                    MessageBox.Show("Enter the quantity.", "Data entry error", _

                        MessageBoxButtons.OK, MessageBoxIcon.Information)

                    With .quantityTextBox

                        .Focus()

                        .SelectAll()

                    End With

                End Try

            Else

                messageString = "Please enter worker name."

                MessageBox.Show(messageString, "Missing Data", _

                    MessageBoxButtons.OK, MessageBoxIcon.Information)

                .nameTextBox.Focus()

            End If

        End With

    End Sub

 

    Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click

        'Calculate the average pay and Display the totals

        If workerCountInteger > 0 Then

 

            'Clear textboxes and labels

            ClearToolStripMenuItem_Click(sender, e)

 

            'Calculate average pay

            averagePayDecimal = totalPayDecimal / workerCountInteger

 

            SummaryForm.ShowDialog()

        End If

 

    End Sub

 

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        'Terminate the project

 

        Me.Close()

    End Sub

 

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click

        'Clear textboxes for a new worker

 

        With Me

            .amountEarnedTextBox.Clear()

            .quantityTextBox.Clear()

            With .nameTextBox

                .Clear()

                .Focus()

            End With

        End With

    End Sub

 

    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click

        'Change the font of amountEarnedLabel

 

        With Me.FontDialog1

            .Font = Me.amountEarnedTextBox.Font

            .ShowDialog()

            Me.amountEarnedTextBox.Font = .Font

        End With

    End Sub

 

    Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click

        'Change the color of amountEarnedLabel

 

        With Me.ColorDialog1

            .Color = Me.amountEarnedTextBox.ForeColor

            .ShowDialog()

            Me.amountEarnedTextBox.ForeColor = .Color

            Me.amountEarnedLabel.ForeColor = .Color

        End With

    End Sub

 

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        'Display program information

        AboutBox1.ShowDialog()

    End Sub

End Class

 

 

Homepage