Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'Week 7
- '10/20/2013
- Public Class Form1
- #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 = 70
- Private Const ASSIST_LOWER_LIMIT As Integer = 0
- Private Const ASSIST_UPPER_LIMIT As Integer = 75
- Private Const SEASONS_LOWER_LIMIT As Integer = 1
- Private Const SEASONS_UPPER_LIMIT As Integer = 25
- 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"
- 'validating the text boxes
- 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 _
- txtSeasons.Validating, _
- txtAge.Validating
- Dim txtBox As TextBox = CType(sender, TextBox)
- If sender Is txtSeasons 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)
- btnStatistics.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 btnStatistics.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 "Menu Event Handlers"
- Private Sub GetStatsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetStatsToolStripMenuItem.Click
- CollectStatistics()
- DisplaySummaryData()
- End Sub
- Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearToolStripMenuItem.Click
- ClearFields()
- End Sub
- Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.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()
- txtSeasons.Clear()
- txtAge.Clear()
- lstResults.Items.Clear()
- lblResults.Visible = False
- btnStatistics.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
- lstResults.Items.Add("Season" & vbTab & "Goals" & vbTab & "Assists" & vbTab & "Points")
- 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
- 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 Sub DisplaySummaryData()
- Dim objStreamWriter As System.IO.StreamWriter
- Dim strLine As String
- lblResults.Visible = True
- lblResults.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
- Try
- objStreamWriter = System.IO.File.AppendText("SummaryData.txt")
- Catch ex As Exception
- MessageBox.Show("File could not be opened", "Error", _
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- strLine = txtFirstName.Text & "," & txtLastName.Text & "," & _
- seasons.ToString() & "," & _
- age.ToString() & "," & _
- totalGoals.ToString() & "," & _
- totalAssists.ToString() & "," & _
- totalPoints.ToString()
- 'Write the data to the file
- Try
- objStreamWriter.WriteLine(strLine)
- Catch ex As Exception
- MessageBox.Show("File could not be written", "Error", _
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- 'Close the file
- Try
- objStreamWriter.Close()
- Catch ex As Exception
- MessageBox.Show("File could not be closed", "Error", _
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- End Sub
- 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
- lstResults.Items.Add(season.ToString & vbTab _
- & goals.ToString & vbTab _
- & assists.ToString & vbTab _
- & points.ToString)
- End Sub
- #End Region
- Private Sub btnSummary_Click(sender As Object, e As EventArgs) Handles btnSummary.Click
- Form2.ShowDialog()
- End Sub
- End Class
- Private Sub btnGetStats_Click(sender As Object, e As EventArgs) Handles btnGetStats.Click
- Dim strLine As String
- Try
- objStreamReader = System.IO.File.OpenText("SummaryData.txt")
- Catch ex As Exception
- MessageBox.Show("File could not be opened", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- dgvStats.DataSource = Nothing
- Do While objStreamReader.Peek() <> -1
- Try
- strLine = objStreamReader.ReadLine()
- Catch ex As Exception
- MessageBox.Show("File 'SummaryData.txt' could not be read", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- Dim results() As String = IO.File.ReadAllLines("SummaryData.txt")
- Dim query = From line In results Let
- Data = line.Split(","c)
- Let Name = Data(0)
- Let lastName = Data(1)
- Let seasons = Data(2)
- Let age = Data(3)
- Let totalGoals = Data(4)
- Let totalAssists = Data(5)
- Let totalPoints = Data(6)
- Select Name, lastName, seasons, age, totalAssists, totalGoals, totalPoints
- dgvStats.DataSource = query.ToList
- dgvStats.CurrentCell = Nothing
- dgvStats.Columns("name").HeaderText = "First Name"
- dgvStats.Columns("lastName").HeaderText = "Last Name"
- dgvStats.Columns("seasons").HeaderText = "Number of Seasons"
- dgvStats.Columns("TotalGoals").HeaderText = "Total Goals"
- dgvStats.Columns("TotalAssists").HeaderText = "Total Assists"
- dgvStats.Columns("totalPoints").HeaderText = "Total Points"
- Me.dgvStats.Columns("Name").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("lastName").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("seasons").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("age").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("TotalGoals").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("TotalAssists").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Me.dgvStats.Columns("totalPoints").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
- Loop
- 'Close the file
- Try
- objStreamReader.Close()
- Catch ex As Exception
- MessageBox.Show("File could not be closed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- End Sub
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- System.IO.File.Delete("SummaryData.txt")
- Exit Sub
- End Sub
- End Class
Add Comment
Please, Sign In to add comment