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