Imports System.IO

Public Class SimpleDatabase
    Private fItem As Dictionary(Of String, String)
    Private fModified As Boolean
    Private fFileName As String

    Public Sub New()
        'Preconditions: none
        'Postconditions: a SimpleDatabase object has been created with the
        'glossary initially empty, FileName set to "" and Modified set to False.
        FileName = ""
        fItem = New Dictionary(Of String, String)
        fModified = False
    End Sub

    Public Property FileName() As String
        Get
            Return fFileName
        End Get
        Set(ByVal value As String)
            fFileName = value
        End Set
    End Property

    Public ReadOnly Property Modified() As Boolean
        Get
            Return fModified
        End Get
    End Property

    Public ReadOnly Property WordList() As Dictionary(Of String, String).KeyCollection
        'Preconditions: none
        'Postconditions: The database item entries are returned as a collection of keys.
        Get
            Return fItem.Keys
        End Get
    End Property

    Public ReadOnly Property Description(ByVal word As String) As String
        'Preconditions: item is an entry in the database
        'Postconditions: The description associated with the item entry 
        'for the item is returned.
        Get
            Return fItem.Item(word)
        End Get
    End Property

    Public Sub add(ByVal word As String, ByVal description As String)
        'Preconditions: item and description are not empty strings and item is not
        'already an entry in the database.
        'Postconditions: A new entry for item with associated description is added.
        'Modified is set to True.
        fItem.Add(word, description)
        fModified = True
    End Sub


    Public Sub load()
        'Preconditions: none
        'Postconditions: If FileName is a valid path for a text file and this file is of
        'appropriate format, then the glossary is loaded from this file. Otherwise the
        'glossary is cleared and an exception is thrown. It is the responsibility of
        'client code to handle the exception. Modified is set to False in either case.
        Dim aReader As StreamReader
        fItem.Clear()
        Try
            aReader = New StreamReader(FileName)
            While Not (aReader.EndOfStream)
                fItem.Add(aReader.ReadLine(), aReader.ReadLine())
            End While
        Catch ex As ArgumentException
            fItem.Clear()
            Throw New ArgumentException("There was a problem opening the file " + FileName)
        Catch ex As IOException
            fItem.Clear()
            Throw New IOException("There was an input-output error for " + FileName)
        Catch ex As UnauthorizedAccessException
            fItem.Clear()
            Throw New UnauthorizedAccessException("There was an unauthorised access error for " + FileName)
        Finally
            fModified = False
            If aReader IsNot Nothing Then
                aReader.Close()
            End If
        End Try
    End Sub

    Public Sub save()
        'Preconditions: none
        'Postconditions: If FileName is a valid path for a text file, then the items of the
        'glossary are stored in the file and Modified is set to False. Otherwise an exception
        'is thrown. It is the responsibility of client code to handle the exception.
        Dim aWriter As StreamWriter
        Try
            aWriter = New StreamWriter(FileName)
            For Each key As String In WordList
                aWriter.WriteLine(key)
                aWriter.WriteLine(Description(key))
            Next
            fModified = False
        Catch ex As ArgumentException
            fItem.Clear()
            Throw New ArgumentException("There was a problem opening the file " + FileName)
        Catch ex As IOException
            fItem.Clear()
            Throw New IOException("There was an input-output error for " + FileName)
        Catch ex As UnauthorizedAccessException
            fItem.Clear()
            Throw New UnauthorizedAccessException("There was an unauthorised access error for " + FileName)
        Finally
            If aWriter IsNot Nothing Then
                aWriter.Close()
            End If
        End Try
    End Sub
End Class
