Friends database, using files Learn programming Visual Basic (VB.net)

Lesson:

File Management


Exercise:

Friends database, using files


Objetive:

Expand the "friends database", so that it loads data from file at the beginning of each session (if the file exists) and saves the data to file when the session ends. Therefore, the data entered in a session must be available for the next session.


Code:

Imports System
Imports System.IO
Public Class FriendsDatabase
    Structure people
        Public name As String
        Public email As String
        Public address As String
        Public year As UShort
    End Structure

    Public Shared Sub Main()
        Dim total As Integer = 275
        Dim p As people() = New people(total - 1) {}
        Dim amount As Integer = 0
        Dim [option] As Char
        Dim found As Boolean
        Dim textSearch As String
        Dim name As String = "friends.dat"

        If File.Exists(name) Then
            Dim file As StreamReader = File.OpenText(name)
            amount = Convert.ToInt32(file.ReadLine())
            file.ReadLine()

            For i As Integer = 0 To amount - 1

                If i < total Then
                    p(i).name = file.ReadLine()
                    p(i).email = file.ReadLine()
                    p(i).address = file.ReadLine()
                    p(i).year = Convert.ToUInt16(file.ReadLine())
                    file.ReadLine()
                End If
            Next

            file.Close()
        End If

        Do
            Console.WriteLine("1- Add data")
            Console.WriteLine("2- Show")
            Console.WriteLine("3- View all data")
            Console.WriteLine("4- Show between dates")
            Console.WriteLine("5- Show oldest")
            Console.WriteLine("6- Show fields match")
            Console.WriteLine("0- Exit")
            Console.Write("Enter a option: ")
            [option] = Convert.ToChar(Console.ReadLine())

            Select Case [option]
                Case "1"c

                    If amount < total - 1 Then

                        Do
                            Console.Write("Name: ")
                            p(amount).name = Console.ReadLine()
                            If p(amount).name.Length > 40 Then Console.WriteLine("Max 40 letters")
                        Loop While p(amount).name.Length > 40

                        Do
                            Console.Write("Email: ")
                            p(amount).email = Console.ReadLine()
                            If p(amount).email.Length > 30 Then Console.WriteLine("Max 30 letters")
                        Loop While p(amount).email.Length > 30

                        Do
                            Console.Write("Address: ")
                            p(amount).address = Console.ReadLine()
                            If p(amount).address.Length > 150 Then Console.WriteLine("Max 150 letters")
                        Loop While p(amount).address.Length > 150

                        Do
                            Console.Write("Year: ")
                            p(amount).year = Convert.ToUInt16(Console.ReadLine())
                            If p(amount).year < 1850 OrElse p(amount).year > 2100 Then Console.WriteLine("1850-2100")
                        Loop While p(amount).year < 1850 OrElse p(amount).year > 2100

                        amount += 1
                        Console.WriteLine()
                    Else
                        Console.WriteLine("Full")
                    End If

                Case "2"c

                    If amount = 0 Then
                        Console.WriteLine("No data")
                    Else

                        For i As Integer = 0 To amount - 1

                            If p(i).name.Length <= 30 Then
                                Console.WriteLine("{0}: Name = {1}", i + 1, p(i).name)
                            Else
                                Console.WriteLine("{0}: Name = {1}", i + 1, p(i).name.Substring(0, 30) & "...")
                            End If

                            If i Mod 20 = 19 Then Console.ReadLine()
                        Next
                    End If

                Case "3"c
                    Console.Write("Enter the person: ")
                    textSearch = Console.ReadLine()
                    found = False

                    For i As Integer = 0 To amount - 1

                        If textSearch.ToLower() = p(i).name.ToLower() Then
                            found = True
                            Console.WriteLine("{0}: Year = {1}, Email = {2}, Address = {3}", i + 1, p(i).year, p(i).email, p(i).address)
                        End If
                    Next

                    If Not found Then Console.WriteLine("Not exists")
                Case "4"c
                    Console.Write("Enter the first year: ")
                    Dim year1 As Integer = Convert.ToUInt16(Console.ReadLine())
                    Console.Write("Enter the second year: ")
                    Dim year2 As Integer = Convert.ToUInt16(Console.ReadLine())

                    If year1 > year2 Then
                        Dim aux As Integer = year2
                        year2 = year1
                        year1 = aux
                    End If

                    found = False

                    For i As Integer = 0 To amount - 1

                        If p(i).year >= year1 AndAlso p(i).year <= year2 Then
                            Console.Write(p(i).name & " - ")
                            found = True
                        End If
                    Next

                    If Not found Then Console.WriteLine("Not found")
                Case "5"c

                    If amount = 0 Then
                        Console.WriteLine("No data")
                    Else
                        Dim firstYear As Integer = p(0).year
                        found = False

                        For i As Integer = 1 To amount - 1
                            If p(i).year < firstYear Then firstYear = p(i).year
                        Next

                        For i As Integer = 0 To amount - 1
                            Console.WriteLine("{0}: Address = {2}, Year = {3}", i + 1, p(i).name, p(i).address, p(i).year)
                            If Console.ReadLine().ToLower() = "end" Then Exit For
                        Next
                    End If

                Case "0"c
                Case Else
                    Console.WriteLine("Wrong option")
            End Select
        Loop While [option] <> "0"c

        Dim dataFile As StreamWriter = File.CreateText(name)
        dataFile.WriteLine(amount)
        dataFile.WriteLine()

        For i As Integer = 0 To amount - 1
            dataFile.WriteLine(p(i).name)
            dataFile.WriteLine(p(i).email)
            dataFile.WriteLine(p(i).address)
            dataFile.WriteLine(p(i).year)
            dataFile.WriteLine()
        Next

        dataFile.Close()
    End Sub
End Class

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.