Option Explicit On
Option Strict On
 
Imports System.Convert
Imports System.IO
Imports System.Text.RegularExpressions
 
 
 
Public Class Form1
    Dim CurrentWeather() As Weather
    Dim DateIndex() As _DateIndex
    Dim DropDown() As String
    Dim Read As String
    Dim Write As String
    Dim _Match As String
 
 
 
    Structure Weather
        Dim strDate As String
        Dim Temp As Double
    End Structure
 
    Structure _DateIndex
        Dim _strDate As String
        Dim _Junk As String
    End Structure
 
    Private Sub ExitButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButt.Click
        Me.Close()
    End Sub
 
    Private Sub OpenButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButt.Click
 
        OpenFileDialog1.InitialDirectory = "C:\"
        OpenFileDialog1.FileName = "HandsOnProject1Data.txt"
        OpenFileDialog1.Filter = "Open Data File *.txt) | *.txt"
        OpenFileDialog1.ShowDialog()
 
        If OpenFileDialog1.FileName <> Nothing Then
            Read = OpenFileDialog1.FileName
            ReadFile2(Read)
        End If
 
        For i = 0 To DropDown.GetUpperBound(0)
            ComboDateBox.Items.Add(DropDown(i))
        Next
 
    End Sub
 
    Public Sub ReadFile(ByVal Path As String)
        Dim strReader As StreamReader
        Dim chrDelimiter() As Char = {ToChar(",")}
        Dim strLine As String
        Dim strFields() As String
        Dim index As Integer = 0
        Dim index2 As Integer = 0
        Dim t1 As String
        Dim t2 As String
        Dim MathAr() As Double
        Dim TempStrAr As New ArrayList
 
        strReader = New StreamReader(Path)
        strLine = strReader.ReadLine()
        t1 = strLine.ToUpper
        t2 = _Match.ToUpper
 
        Do Until strLine = Nothing
 
            t1 = strLine.ToUpper
            If t1.Contains(t2) = True Then
 
                If CurrentWeather Is Nothing Then
                    ReDim CurrentWeather(index)
                Else
                    index = CurrentWeather.GetUpperBound(0) + 1
                    ReDim Preserve CurrentWeather(index)
                End If
 
                strFields = strLine.Split(chrDelimiter)
 
                CurrentWeather(index).strDate = strFields(0)
                CurrentWeather(index).Temp = CDbl(strFields(1))
 
                index += 1
            End If
 
            strLine = strReader.ReadLine()
 
        Loop
 
        'DateBox.Items.Add(_Match) 'debugging tool
        'TempBox.Items.Add(t1.Contains(t2)) 'debugging tool
 
        For i = 0 To UBound(CurrentWeather)
            DateBox.Items.Add(CurrentWeather(i).strDate)
            TempBox.Items.Add(CurrentWeather(i).Temp)
        Next
 
        For index2 = 0 To CurrentWeather.Count - 1
            TempStrAr.Add(CurrentWeather(index2).Temp)
        Next
 
        index2 = 0
 
        ReDim MathAr(TempStrAr.Count - 1)
 
        For index2 = 0 To TempStrAr.Count - 1
            MathAr(index2) = CType(TempStrAr.Item(index2), Double)
        Next
 
        AvgBox.Text = CType(Math.Round(MathAr.Average, 4), String)
        MinBox.Text = CType(MathAr.Min, String)
        MaxBox.Text = CType(MathAr.Max, String)
 
 
 
    End Sub
 
 
    Public Sub ReadFile2(ByVal Path As String)
        Dim _strReader As StreamReader
        Dim _chrDelimiter() As Char = {ToChar(" ")}
        Dim _strLine As String
        Dim _strFields() As String
        Dim _index As Integer = 0
        Dim index2 As Integer = 0
        Dim TempAr As New ArrayList
        Dim TempAr2 As New ArrayList
 
        _strReader = New StreamReader(Path)
        _strLine = _strReader.ReadLine()
 
        Do Until _strLine = Nothing
 
            If DateIndex Is Nothing Then
                ReDim DateIndex(_index)
            Else
                _index = DateIndex.GetUpperBound(0) + 1
                ReDim Preserve DateIndex(_index)
            End If
 
            _strFields = _strLine.Split(_chrDelimiter)
 
            DateIndex(_index)._strDate = _strFields(0)
            DateIndex(_index)._Junk = _strFields(1)
 
            _strLine = _strReader.ReadLine()
            _index += 1
 
        Loop
 
        For index2 = 0 To DateIndex.Count - 1
            TempAr.Add(DateIndex(index2)._strDate)
        Next
 
        index2 = 0
 
        For index2 = 0 To TempAr.Count - 1
            If Not TempAr2.Contains(TempAr(index2)) Then
                TempAr2.Add(TempAr(index2))
            End If
        Next
 
        index2 = 0
 
        ReDim DropDown(TempAr2.Count - 1)
 
        For index2 = 0 To TempAr2.Count - 1
            DropDown(index2) = CType(TempAr2.Item(index2), String)
        Next
 
 
    End Sub
 
    Private Sub ComboDateBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboDateBox.SelectedValueChanged
 
        DateBox.Items.Clear()
        TempBox.Items.Clear()
        Dim iclear As Integer = 0
 
        If CurrentWeather Is Nothing Then
        Else
            Erase CurrentWeather
        End If
 
        _Match = CType(ComboDateBox.SelectedItem, String)
        ReadFile(Read)
 
    End Sub
 
    Private Sub WriteButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WriteButt.Click
 
        SaveFileDialog1.InitialDirectory = "C\:"
        SaveFileDialog1.FileName = "WeatherData.txt"
        SaveFileDialog1.Filter = "Save Data File *.txt) | *.txt"
        SaveFileDialog1.ShowDialog()
 
        If SaveFileDialog1.FileName <> Nothing Then
            Write = SaveFileDialog1.FileName
            WriteFile(Write)
        End If
 
    End Sub
 
    Public Sub WriteFile(ByVal Path As String)
 
        Dim WLine As StreamWriter
 
        WLine = New StreamWriter(Path)
 
        For index = 0 To CurrentWeather.Count - 1
            WLine.WriteLine(CurrentWeather(index).strDate & "," & CurrentWeather(index).Temp)
        Next
 
        WLine.Close()
 
    End Sub
End Class