'********************************************************************* ' pieceWorkCalculator.vb ' -------------------- 'Folder: pieceWorkCalculator 'Project: Assignment 4.6 - Piece Work Calculator 'Programmer: Adam Reyher (adam@adamreyher.com) 'Date: Wednesday, October 15, 2007 'Description: This program will calculate the amount earned for ' a specified number of pieces completed and keep ' running totals which can then be summariezed. '********************************************************************* Option Strict On Option Explicit On Public Class pieceWorkCalculatorForm 'Declare the summary values we'll need to access later on at the module level Dim totalPiecesCompletedDecimal As Decimal = 0 Dim totalAmountEarnedDecimal As Decimal = 0 Dim numberOfEmployeesDecimal As Decimal = 0 Dim averageEarnedDecimal As Decimal = 0 Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click 'Confirm and exit the program Dim exitConfirmDialogResult As DialogResult exitConfirmDialogResult = MessageBox.Show("Are you sure you wish to exit?", "Exit Program", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) If exitConfirmDialogResult = Windows.Forms.DialogResult.Yes Then Me.Close() End If End Sub Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click 'Declare all of the variables we'll need for this procedure Dim errorThrownBoolean As Boolean = False Dim errorMessageString As String = "" Dim pricePaidPerPieceDecimal As Decimal Dim piecesCompletedThisInteger As Integer = 0 Dim amountEarnedCalculatedDecimal As Decimal 'Let's do some error checking to be sure data has been entered. If not, toggle the error message to TRUE If Me.employeeNameTextBox.Text = "" Then errorThrownBoolean = True errorMessageString &= "Name is required" & ControlChars.NewLine End If If Me.piecesCompletedThisTextBox.Text = "" Then errorThrownBoolean = True errorMessageString &= "Pieces completed is required" End If 'Display the error message(s) if it has been toggled to TRUE If errorThrownBoolean = True Then MessageBox.Show(errorMessageString, "Error entering data", _ MessageBoxButtons.OK, MessageBoxIcon.Information) 'No errors? Let's proceed with the program. Else Try 'Convert string to decimal piecesCompletedThisInteger = Integer.Parse(Me.piecesCompletedThisTextBox.Text) Try 'I can't remember what this does. Oh God please let it work! Select Case piecesCompletedThisInteger Case 1I To 199I pricePaidPerPieceDecimal = 0.5D Case 200I To 399I pricePaidPerPieceDecimal = 0.55D Case 400I To 599I pricePaidPerPieceDecimal = 0.6D Case Is >= 600I pricePaidPerPieceDecimal = 0.65D End Select 'Oh, heh. Now I remember. 'Check for negative values If piecesCompletedThisInteger < 0 Then MessageBox.Show("Pieces completed cannot be less than 0", "Data entry error", _ MessageBoxButtons.OK, MessageBoxIcon.Information) Else 'Calculate and display the amount earned for this entry amountEarnedCalculatedDecimal = (piecesCompletedThisInteger * pricePaidPerPieceDecimal) Me.amountEarnedTextBox.Text = amountEarnedCalculatedDecimal.ToString("C") 'See totals. See totals run. Run totals, run! Me.summaryButton.Enabled = True totalPiecesCompletedDecimal = (totalPiecesCompletedDecimal + piecesCompletedThisInteger) totalAmountEarnedDecimal = (totalAmountEarnedDecimal + amountEarnedCalculatedDecimal) numberOfEmployeesDecimal = (numberOfEmployeesDecimal + 1) averageEarnedDecimal = (totalAmountEarnedDecimal / numberOfEmployeesDecimal) End If Catch ex As Exception 'I swear! My code is perfect! It can't possibly break ... but it did. I blame Visual Studio. MessageBox.Show("Error when calculating value", "Data calculation error", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try Catch ex As Exception 'If this catch fires, the data type entered is text or a decimal MessageBox.Show("Pieces completed must be an integer", "Data entry error", _ MessageBoxButtons.OK, MessageBoxIcon.Information) With piecesCompletedThisTextBox .Focus() .Select() End With End Try End If End Sub Private Sub clearFormButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearFormButton.Click 'Clear the CURRENT form and focus With Me.employeeNameTextBox .Text = "" .Focus() End With Me.piecesCompletedThisTextBox.Text = "" Me.amountEarnedTextBox.Text = "" End Sub Private Sub clearAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearAllButton.Click 'Display a message box confirming the clear ALL Dim clearAllConfirmDialogResult As DialogResult clearAllConfirmDialogResult = MessageBox.Show("Are you sure you wish to clear all summary data?", "Clear Summary Data", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) If clearAllConfirmDialogResult = Windows.Forms.DialogResult.Yes Then 'Call to the absolutely awesome procedure that Adam created already clearFormButton_Click(sender, e) 'Reset the summary values totalPiecesCompletedDecimal = 0D totalAmountEarnedDecimal = 0D numberOfEmployeesDecimal = 0D averageEarnedDecimal = 0D Me.summaryButton.Enabled = False End If End Sub Private Sub summaryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles summaryButton.Click 'Display the summary box. This is it, folks. The program stops here. MessageBox.Show("Total Pieces Completed: " & totalPiecesCompletedDecimal & ControlChars.NewLine _ & "Total Amount Earned: " & totalAmountEarnedDecimal.ToString("C") & ControlChars.NewLine _ & "Average Amount Earned: " & averageEarnedDecimal.ToString("C"), _ "Piece Work Summary Data", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub End Class 'EoF