Public Class frmHockeyStats
#Region "Constant Declarations"
'declare constants to the upper and lower limits for the goals and assists
Private Const GOAL_LOWER_LIMIT As Integer = 0
Private Const GOAL_UPPER_LIMIT As Integer = 60
Private Const ASSIST_LOWER_LIMIT As Integer = 0
Private Const ASSIST_UPPER_LIMIT As Integer = 60
Private Const SEASONS_LOWER_LIMIT As Integer = 1
Private Const SEASONS_UPPER_LIMIT As Integer = 20
Private Const PLAYER_MIN_AGE As Integer = 18
Private Const PLAYER_MAX_AGE As Integer = 30
#End Region
#Region "Form Level Variable Declarations"
Private seasons As Integer = 0
Private age As Integer = 0
Private totalGoals As Integer = 0
Private totalAssists As Integer = 0
Private totalPoints As Integer = 0
#End Region
#Region "Form Level Event Handlers"
Private Sub frmHockeyStats_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ClearFields()
End Sub
#End Region
#Region "Validating Event Handlers"
Private Sub NameValidatingEvent(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
txtFirstName.Validating, _
txtLastName.Validating
Dim txtbox As TextBox = CType(sender, TextBox)
Dim datadescription As String = String.Empty
If sender Is txtFirstName Then
datadescription = "First Name"
ElseIf sender Is txtLastName Then
datadescription = "Last Name"
End If
e.Cancel = ValidateStringInput(datadescription, txtbox.Text)
End Sub
Private Sub NumberValidatingEvent(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
txtNumSeasons.Validating, _
txtAge.Validating
Dim txtBox As TextBox = CType(sender, TextBox)
If sender Is txtNumSeasons Then
e.Cancel = ValidateNumberInput("Seasons", txtBox.Text, SEASONS_LOWER_LIMIT, SEASONS_UPPER_LIMIT, seasons)
ElseIf sender Is txtAge Then
e.Cancel = ValidateNumberInput("Age", txtBox.Text, PLAYER_MIN_AGE, PLAYER_MAX_AGE, age)
btnGetStats.Enabled = Not (e.Cancel)
End If
If e.Cancel Then
txtBox.Clear()
End If
End Sub
#End Region
#Region "Button Event Handlers"
Private Sub btnGetStats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetStats.Click
CollectStatistics()
DisplaySummaryData()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
ClearFields()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
#End Region
#Region "Utility Modules"
Private Function ValidateStringInput(ByVal dataDescription As String, ByVal strInput As String) As Boolean
Dim invalid As Boolean = True
If Not String.IsNullOrEmpty(strInput) Then
invalid = False 'good value
Else
MessageBox.Show(dataDescription & " must be provided", "Empty " & dataDescription, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
invalid = True 'bad value
End If
Return invalid
End Function
Private Function ValidateNumberInput(ByVal dataType As String, ByVal strInput As String, _
ByVal min As Integer, ByVal max As Integer, _
ByRef value As Integer) As Boolean
Dim invalid As Boolean = True
If IsNumeric(strInput) Then
value = Integer.Parse(strInput)
If value >= min AndAlso value <= max Then
invalid = False
Else
MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
invalid = True
End If
Else
MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
invalid = True
End If
Return invalid
End Function
Private Sub ClearFields()
txtFirstName.Clear()
txtLastName.Clear()
txtNumSeasons.Clear()
txtAge.Clear()
lstSeasons.Items.Clear()
lblTotal.Visible = False
btnGetStats.Enabled = False
End Sub
Private Sub CollectStatistics()
Dim goals As Integer = -1
Dim assists As Integer = -1
Dim points As Integer
'add a header to the list of statistics
lstSeasons.Items.Add("Season" & vbTab & "Goals" & vbTab & "Assists" & vbTab & "Points")
'gather the statistics for each season
'cannot get to this point unless there is a valid number of seasons
For count As Integer = 1 To seasons
goals = GetData("goals", GOAL_LOWER_LIMIT, GOAL_UPPER_LIMIT)
assists = GetData("assists", ASSIST_LOWER_LIMIT, ASSIST_UPPER_LIMIT)
points = AddToTotals(goals, assists)
DisplayRunningTotals(count, goals, assists, points)
Next
End Sub
Private Function GetData(ByVal dataDescription As String, ByVal lowerLimit As Integer, ByVal upperLimit As Integer) As Integer
'use an input validation loop to get the data value
'check againist the provide upper and lower limit
Dim value As Integer
Dim response As String
Do
response = InputBox("Enter the number of " & dataDescription & "(" & lowerLimit.ToString & " to " & upperLimit.ToString & "): ")
ValidateNumberInput(dataDescription, response, lowerLimit, upperLimit, value)
Loop Until (value >= lowerLimit AndAlso value <= upperLimit)
Return value
End Function
Private Function AddToTotals(ByVal goals As Integer, ByVal assists As Integer) As Integer
Dim points As Integer = 0
'keep the running totals
points = assists + goals
totalGoals += goals
totalAssists += assists
totalPoints += points
Return points
End Function
Private Sub DisplayRunningTotals(ByVal season As Integer, ByVal goals As Integer, ByVal assists As Integer, ByVal points As Integer)
'set the season totals in the list box
lstSeasons.Items.Add(season.ToString & vbTab _
& goals.ToString & vbTab _
& assists.ToString & vbTab _
& points.ToString)
End Sub
Private Sub DisplaySummaryData()
lblTotal.Visible = True
lblTotal.Text = txtFirstName.Text & " " & txtLastName.Text & " career statistics" & vbNewLine _
& " Seasons: " & seasons.ToString & vbNewLine _
& " Age: " & age.ToString & vbNewLine _
& " Goals: " & totalGoals.ToString & vbNewLine _
& " Assists: " & totalAssists.ToString & vbNewLine _
& " Points: " & totalPoints.ToString
End Sub
#End Region
Private Sub grpOperations_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpOperations.Enter
End Sub
End Class