JunkieHF

Hockey Statistics

Dec 15th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Week 7
  2. '10/20/2013
  3.  
  4. Public Class Form1
  5.  
  6. #Region "Constant Declarations"
  7.  
  8.     'declare constants to the upper and lower limits for the goals and assists
  9.    Private Const GOAL_LOWER_LIMIT As Integer = 0
  10.     Private Const GOAL_UPPER_LIMIT As Integer = 70
  11.     Private Const ASSIST_LOWER_LIMIT As Integer = 0
  12.     Private Const ASSIST_UPPER_LIMIT As Integer = 75
  13.     Private Const SEASONS_LOWER_LIMIT As Integer = 1
  14.     Private Const SEASONS_UPPER_LIMIT As Integer = 25
  15.     Private Const PLAYER_MIN_AGE As Integer = 18
  16.     Private Const PLAYER_MAX_AGE As Integer = 30
  17.  
  18. #End Region
  19. #Region "Form Level Variable Declarations"
  20.  
  21.     Private seasons As Integer = 0
  22.     Private age As Integer = 0
  23.     Private totalGoals As Integer = 0
  24.     Private totalAssists As Integer = 0
  25.     Private totalPoints As Integer = 0
  26.  
  27. #End Region
  28. #Region "Form Level Event Handlers"
  29.  
  30.     Private Sub frmHockeyStats_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  31.         ClearFields()
  32.     End Sub
  33.  
  34. #End Region
  35. #Region "Validating Event Handlers"
  36.     'validating the text boxes
  37.    Private Sub NameValidatingEvent(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
  38.                                                                                                                txtFirstName.Validating, _
  39.                                                                                                                txtLastName.Validating
  40.         Dim txtbox As TextBox = CType(sender, TextBox)
  41.         Dim datadescription As String = String.Empty
  42.         If sender Is txtFirstName Then
  43.             datadescription = "First Name"
  44.         ElseIf sender Is txtLastName Then
  45.             datadescription = "Last Name"
  46.         End If
  47.         e.Cancel = ValidateStringInput(datadescription, txtbox.Text)
  48.     End Sub
  49.  
  50.     Private Sub NumberValidatingEvent(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
  51.                                                                                                             txtSeasons.Validating, _
  52.                                                                                                             txtAge.Validating
  53.         Dim txtBox As TextBox = CType(sender, TextBox)
  54.         If sender Is txtSeasons Then
  55.             e.Cancel = ValidateNumberInput("Seasons", txtBox.Text, SEASONS_LOWER_LIMIT, SEASONS_UPPER_LIMIT, seasons)
  56.         ElseIf sender Is txtAge Then
  57.             e.Cancel = ValidateNumberInput("Age", txtBox.Text, PLAYER_MIN_AGE, PLAYER_MAX_AGE, age)
  58.             btnStatistics.Enabled = Not (e.Cancel)
  59.         End If
  60.  
  61.         If e.Cancel Then
  62.             txtBox.Clear()
  63.         End If
  64.     End Sub
  65.  
  66. #End Region
  67.  
  68. #Region "Button Event Handlers"
  69.  
  70.     Private Sub btnGetStats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
  71.         CollectStatistics()
  72.         DisplaySummaryData()
  73.     End Sub
  74.  
  75.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
  76.         ClearFields()
  77.     End Sub
  78.  
  79.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  80.         Me.Close()
  81.     End Sub
  82. #End Region
  83.  
  84. #Region "Menu Event Handlers"
  85.     Private Sub GetStatsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GetStatsToolStripMenuItem.Click
  86.         CollectStatistics()
  87.         DisplaySummaryData()
  88.     End Sub
  89.     Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearToolStripMenuItem.Click
  90.         ClearFields()
  91.     End Sub
  92.     Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
  93.         Me.Close()
  94.     End Sub
  95. #End Region
  96. #Region "Utility Modules"
  97.  
  98.     Private Function ValidateStringInput(ByVal dataDescription As String, ByVal strInput As String) As Boolean
  99.         Dim invalid As Boolean = True
  100.         If Not String.IsNullOrEmpty(strInput) Then
  101.             invalid = False 'good value
  102.        Else
  103.             MessageBox.Show(dataDescription & " must be provided", "Empty " & dataDescription, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  104.             invalid = True 'bad value
  105.        End If
  106.         Return invalid
  107.     End Function
  108.  
  109.     Private Function ValidateNumberInput(ByVal dataType As String, ByVal strInput As String, _
  110.                                          ByVal min As Integer, ByVal max As Integer, _
  111.                                          ByRef value As Integer) As Boolean
  112.         Dim invalid As Boolean = True
  113.  
  114.         If IsNumeric(strInput) Then
  115.             value = Integer.Parse(strInput)
  116.             If value >= min AndAlso value <= max Then
  117.                 invalid = False
  118.             Else
  119.                 MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  120.                 invalid = True
  121.             End If
  122.         Else
  123.             MessageBox.Show(dataType & " must be between " & min & " and " & max & "--try again", "Invalid " & dataType, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  124.             invalid = True
  125.         End If
  126.         Return invalid
  127.     End Function
  128.  
  129.     Private Sub ClearFields()
  130.         txtFirstName.Clear()
  131.         txtLastName.Clear()
  132.         txtSeasons.Clear()
  133.         txtAge.Clear()
  134.         lstResults.Items.Clear()
  135.         lblResults.Visible = False
  136.         btnStatistics.Enabled = False
  137.     End Sub
  138.  
  139.     Private Sub CollectStatistics()
  140.         Dim goals As Integer = -1
  141.         Dim assists As Integer = -1
  142.         Dim points As Integer
  143.  
  144.         'add a header to the list of statistics
  145.        lstResults.Items.Add("Season" & vbTab & "Goals" & vbTab & "Assists" & vbTab & "Points")
  146.  
  147.  
  148.         For count As Integer = 1 To seasons
  149.             goals = GetData("goals", GOAL_LOWER_LIMIT, GOAL_UPPER_LIMIT)
  150.             assists = GetData("assists", ASSIST_LOWER_LIMIT, ASSIST_UPPER_LIMIT)
  151.             points = AddToTotals(goals, assists)
  152.             DisplayRunningTotals(count, goals, assists, points)
  153.         Next
  154.     End Sub
  155.  
  156.     Private Function GetData(ByVal dataDescription As String, ByVal lowerLimit As Integer, ByVal upperLimit As Integer) As Integer
  157.        
  158.  
  159.         Dim value As Integer
  160.         Dim response As String
  161.         Do
  162.             response = InputBox("Enter the number of " & dataDescription & "(" & lowerLimit.ToString & " to " & upperLimit.ToString & "): ")
  163.             ValidateNumberInput(dataDescription, response, lowerLimit, upperLimit, value)
  164.         Loop Until (value >= lowerLimit AndAlso value <= upperLimit)
  165.         Return value
  166.     End Function
  167.  
  168.     Private Sub DisplaySummaryData()
  169.         Dim objStreamWriter As System.IO.StreamWriter
  170.         Dim strLine As String
  171.         lblResults.Visible = True
  172.         lblResults.Text = txtFirstName.Text & " " & txtLastName.Text & " career statistics" & vbNewLine _
  173.                       & "    Seasons: " & seasons.ToString & vbNewLine _
  174.                       & "         Age: " & age.ToString & vbNewLine _
  175.                       & "       Goals: " & totalGoals.ToString & vbNewLine _
  176.                       & "     Assists: " & totalAssists.ToString & vbNewLine _
  177.                       & "      Points: " & totalPoints.ToString
  178.         Try
  179.             objStreamWriter = System.IO.File.AppendText("SummaryData.txt")
  180.         Catch ex As Exception
  181.             MessageBox.Show("File could not be opened", "Error", _
  182.                             MessageBoxButtons.OK, MessageBoxIcon.Error)
  183.             Exit Sub
  184.         End Try
  185.  
  186.  
  187.  
  188.         strLine = txtFirstName.Text & "," & txtLastName.Text & "," & _
  189.             seasons.ToString() & "," & _
  190.             age.ToString() & "," & _
  191.             totalGoals.ToString() & "," & _
  192.             totalAssists.ToString() & "," & _
  193.             totalPoints.ToString()
  194.  
  195.  
  196.         'Write the data to the file
  197.        Try
  198.             objStreamWriter.WriteLine(strLine)
  199.         Catch ex As Exception
  200.             MessageBox.Show("File could not be written", "Error", _
  201.                             MessageBoxButtons.OK, MessageBoxIcon.Error)
  202.             Exit Sub
  203.         End Try
  204.  
  205.         'Close the file
  206.        Try
  207.             objStreamWriter.Close()
  208.         Catch ex As Exception
  209.             MessageBox.Show("File could not be closed", "Error", _
  210.                             MessageBoxButtons.OK, MessageBoxIcon.Error)
  211.             Exit Sub
  212.         End Try
  213.     End Sub
  214.  
  215.  
  216.  
  217.  
  218.     Private Function AddToTotals(ByVal goals As Integer, ByVal assists As Integer) As Integer
  219.         Dim points As Integer = 0
  220.  
  221.         'keep the running totals
  222.        points = assists + goals
  223.         totalGoals += goals
  224.         totalAssists += assists
  225.         totalPoints += points
  226.  
  227.         Return points
  228.     End Function
  229.  
  230.     Private Sub DisplayRunningTotals(ByVal season As Integer, ByVal goals As Integer, ByVal assists As Integer, ByVal points As Integer)
  231.         'set the season totals in the list box
  232.        lstResults.Items.Add(season.ToString & vbTab _
  233.                              & goals.ToString & vbTab _
  234.                              & assists.ToString & vbTab _
  235.                              & points.ToString)
  236.     End Sub
  237.  
  238.  
  239. #End Region
  240.  
  241.  
  242.  
  243.  
  244.  
  245.     Private Sub btnSummary_Click(sender As Object, e As EventArgs) Handles btnSummary.Click
  246.         Form2.ShowDialog()
  247.     End Sub
  248. End Class
  249.  
  250.  
  251. Private Sub btnGetStats_Click(sender As Object, e As EventArgs) Handles btnGetStats.Click
  252.  
  253.  
  254.  
  255.  
  256.         Dim strLine As String
  257.  
  258.  
  259.  
  260.         Try
  261.  
  262.             objStreamReader = System.IO.File.OpenText("SummaryData.txt")
  263.  
  264.  
  265.  
  266.         Catch ex As Exception
  267.  
  268.  
  269.  
  270.             MessageBox.Show("File could not be opened", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  271.  
  272.  
  273.  
  274.             Exit Sub
  275.  
  276.         End Try
  277.  
  278.         dgvStats.DataSource = Nothing
  279.  
  280.  
  281.  
  282.         Do While objStreamReader.Peek() <> -1
  283.  
  284.  
  285.  
  286.             Try
  287.  
  288.                 strLine = objStreamReader.ReadLine()
  289.  
  290.  
  291.  
  292.             Catch ex As Exception
  293.  
  294.  
  295.  
  296.                 MessageBox.Show("File 'SummaryData.txt' could not be read", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  297.  
  298.  
  299.  
  300.                 Exit Sub
  301.  
  302.  
  303.  
  304.             End Try
  305.             Dim results() As String = IO.File.ReadAllLines("SummaryData.txt")
  306.             Dim query = From line In results Let
  307.             Data = line.Split(","c)
  308.            Let Name = Data(0)
  309.            Let lastName = Data(1)
  310.            Let seasons = Data(2)
  311.            Let age = Data(3)
  312.           Let totalGoals = Data(4)
  313.           Let totalAssists = Data(5)
  314.           Let totalPoints = Data(6)
  315.             Select Name, lastName, seasons, age, totalAssists, totalGoals, totalPoints
  316.             dgvStats.DataSource = query.ToList
  317.  
  318.  
  319.             dgvStats.CurrentCell = Nothing
  320.  
  321.             dgvStats.Columns("name").HeaderText = "First Name"
  322.             dgvStats.Columns("lastName").HeaderText = "Last Name"
  323.             dgvStats.Columns("seasons").HeaderText = "Number of Seasons"
  324.             dgvStats.Columns("TotalGoals").HeaderText = "Total Goals"
  325.             dgvStats.Columns("TotalAssists").HeaderText = "Total Assists"
  326.             dgvStats.Columns("totalPoints").HeaderText = "Total Points"
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.             Me.dgvStats.Columns("Name").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.             Me.dgvStats.Columns("lastName").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  342.  
  343.             Me.dgvStats.Columns("seasons").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  344.  
  345.             Me.dgvStats.Columns("age").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  346.  
  347.  
  348.  
  349.             Me.dgvStats.Columns("TotalGoals").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  350.  
  351.  
  352.  
  353.             Me.dgvStats.Columns("TotalAssists").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  354.  
  355.             Me.dgvStats.Columns("totalPoints").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.         Loop
  364.  
  365.  
  366.  
  367.         'Close the file
  368.  
  369.  
  370.  
  371.         Try
  372.             objStreamReader.Close()
  373.  
  374.  
  375.  
  376.         Catch ex As Exception
  377.  
  378.  
  379.  
  380.             MessageBox.Show("File could not be closed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  381.  
  382.  
  383.  
  384.             Exit Sub
  385.  
  386.  
  387.  
  388.         End Try
  389.  
  390.  
  391.  
  392.     End Sub
  393.     Friend WithEvents Button1 As System.Windows.Forms.Button
  394.  
  395.  
  396.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  397.         System.IO.File.Delete("SummaryData.txt")
  398.  
  399.  
  400.  
  401.  
  402.        
  403.  
  404.  
  405.  
  406.  
  407.             Exit Sub
  408.  
  409.    
  410.     End Sub
  411. End Class
Add Comment
Please, Sign In to add comment