Computer programs Learn programming Visual Basic (VB.net)

Lesson:

Arrays, Structures and Strings


Exercise:

Computer programs 14


Objetive:

Create a Visual Basic (VB.net) program that can store up to 1000 records of computer programs. For each program, you must keep the following data:

* Name
* Category
* Description
* Version (is a set of 3 data: version number -text-, launch month -byte- and launch year -unsigned short-)

The program should allow the user the following operations:
1 - Add data of a new program (the name must not be empty, category must not be more than 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation).
2 - Show the names of all the stored programs. Each name must appear on one line. If there are more than 20 programs, you must pause after displaying each block of 20 programs, and wait for the user to press Enter before proceeding. The user should be notified if there is no data.
3 - View all data for a certain program (from part of its name, category or description, case sensitive). If there are several programs that contain that text, the program will display all of them, separated by a blank line. The user should be notified if there are no matches found.
4 - Update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data). He should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.
5 - Delete a record, whose number will be indicated by the user. He should be warned (but not asked again) if he enters an incorrect number.
6 - Sort the data alphabetically by name.
7 - Fix redundant spaces (turn all the sequences of two or more spaces into a single space, only in the name, for all existing records).
X - Exit the application (as we do not store the information, data will be lost).


Code:

Imports System
Public Class exercise94
    Structure versionType
        Public number As String
        Public month As Byte
        Public year As UShort
    End Structure

    Structure program
        Public name As String
        Public category As String
        Public description As String
        Public version As versionType
    End Structure

    Public Shared Sub Main()
        Dim capacity As Integer = 1000
        Dim data As program() = New program(capacity - 1) {}
        Dim repeat As Boolean = True
        Dim [option] As String
        Dim counter As Integer = 0

        Do
            Console.WriteLine()
            Console.WriteLine("Computer programas database")
            Console.WriteLine()
            Console.WriteLine("1.- Add data.")
            Console.WriteLine("2.- View names of the programs.")
            Console.WriteLine("3.- Search programs.")
            Console.WriteLine("4.- Modify program.")
            Console.WriteLine("5.- Delete Program.")
            Console.WriteLine("6.- Sort alphabetically")
            Console.WriteLine("7.- Fix redundant spaces")
            Console.WriteLine("0.-Exit.")
            Console.Write("Option: ")
            [option] = Console.ReadLine()

            Select Case [option]
                Case "1"

                    If counter > capacity - 1 Then
                        Console.WriteLine("Database full!")
                        Exit Select
                    End If

                    Do
                        Console.Write("Enter Name: ")
                        data(counter).name = Console.ReadLine()
                        If data(counter).name.Length = 0 Then Console.Write("Cannot be empty")
                    Loop While data(counter).name.Length = 0

                    Do
                        Console.Write("Enter category: ")
                        data(counter).category = Console.ReadLine()
                        If data(counter).category.Length > 30 Then Console.Write("Too long. 30 letters max, please")
                    Loop While data(counter).category.Length > 30

                    Console.Write("Enter Description: ")
                    data(counter).description = Console.ReadLine()
                    If data(counter).description.Length > 100 Then data(counter).description = data(counter).description.Substring(0, 100)
                    Console.Write("Enter the version number: ")
                    data(counter).version.number = Console.ReadLine()
                    Console.Write("Enter the version month: ")
                    data(counter).version.month = Convert.ToByte(Console.ReadLine())
                    Console.Write("Enter the version year: ")
                    data(counter).version.year = Convert.ToUInt16(Console.ReadLine())
                    counter += 1
                Case "2"

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

                        For i As Integer = 0 To counter - 1
                            Console.WriteLine("{0}: {1}", i + 1, data(i).name)

                            If i Mod 20 = 19 Then
                                Console.Write("Press Enter...")
                                Console.ReadLine()
                            End If
                        Next
                    End If

                Case "3"
                    Console.Write("Enter part of the name, description, etc... (case sensitive): ")
                    Dim search As String = Console.ReadLine()
                    Dim found As Boolean = False

                    For i As Integer = 0 To counter - 1

                        If data(i).name.Contains(search) OrElse data(i).description.Contains(search) OrElse data(i).category.Contains(search) Then
                            Console.WriteLine("{0}: {1}", i + 1, data(i).name)
                            found = True
                        End If
                    Next

                    If Not found Then Console.WriteLine("Not found!")
                Case "4"
                    Console.Write("Enter the program number: ")
                    Dim progNumber As Integer = Convert.ToInt32(Console.ReadLine()) - 1

                    If (progNumber > counter) OrElse (progNumber < 0) Then
                        Console.Write("Out of range!")
                        Exit Select
                    End If

                    Console.Write("Program name (was {0}; hit ENTER to leave as is): ", data(progNumber).name)
                    Dim newText As String = Console.ReadLine()
                    If newText <> "" Then data(progNumber).name = newText
                    Console.Write("Program category (was {0}; hit ENTER to leave as is): ", data(progNumber).category)
                    newText = Console.ReadLine()
                    If newText <> "" Then data(progNumber).category = newText
                    Console.Write("Program description (was {0}; hit ENTER to leave as is): ", data(progNumber).description)
                    newText = Console.ReadLine()
                    If newText <> "" Then data(progNumber).description = newText
                    Console.Write("Program version (number) (was {0}; hit ENTER to leave as is): ", data(progNumber).version.number)
                    newText = Console.ReadLine()
                    If newText <> "" Then data(progNumber).version.number = newText
                    Console.Write("Program version (month) (was {0}; hit ENTER to leave as is): ", data(progNumber).version.month)
                    newText = Console.ReadLine()
                    If newText <> "" Then data(progNumber).version.month = Convert.ToByte(newText)
                    Console.Write("Program version (year) (was {0}; hit ENTER to leave as is): ", data(progNumber).version.year)
                    newText = Console.ReadLine()
                    If newText <> "" Then data(progNumber).version.year = Convert.ToUInt16(newText)
                Case "5"
                    Dim position As Integer = 0
                    Console.Write("Enter the position number to delete: ")
                    position = Convert.ToInt32(Console.ReadLine()) - 1

                    If position > counter Then
                        Console.WriteLine("Error: out of range")
                    Else

                        For i As Integer = position To counter - 1
                            data(i) = data(i + 1)
                        Next

                        counter -= 1
                    End If

                Case "6"
                    Dim aux As program

                    For i As Integer = 0 To counter - 1 - 1

                        For j As Integer = i + 1 To counter - 1

                            If data(i).name.CompareTo(data(j).name) > 0 Then
                                aux = data(i)
                                data(i) = data(j)
                                data(j) = aux
                            End If
                        Next
                    Next

                Case "7"

                    For i As Integer = 0 To counter - 1

                        While data(i).name.Contains("  ")
                            data(i).name = data(i).name.Replace("  ", " ")
                        End While
                    Next

                Case "0"
                    repeat = False
                Case Else
                    Console.WriteLine("Wrong option!")
            End Select
        Loop While repeat <> False
    End Sub
End Class