Computer programs VB.Net Exercise - Visual Basic Programming Course


 Exercise

Computer programs

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

More VB.Net Exercises of Arrays, Structures and Strings

 Reverse array
Create a Visual Basic (VB.Net) program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Create a Visual Basic (VB.Net) program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user ho...
 Array of even numbers
Write a Visual Basic (VB.Net) program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Create a Visual Basic (VB.Net) program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negativ...
 Many numbers and sum
Create a program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it must d...
 Two dimensional array
Write a Visual Basic (VB.Net) program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average...
 Statistics V2
reate a statistical program which will allow the user to: - Add new data - See all data entered - Find an item, to see whether it has been entere...
 Struct
Create a "struct" to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte) ...
 Array of struct
Expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the user for data for the first...
 Array of struct and menu
Expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data for one point - Display al...
 Books database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
 Triangle V2
Write a Visual Basic (VB.Net) program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has...
 Rectangle V3
Write a Visual Basic (VB.Net) program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Ent...
 Centered triangle
Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Create a Visual Basic (VB.Net) program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Create a Visual Basic (VB.Net) program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Create a Visual Basic (VB.Net) program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded w...
 Nested structs
Create a struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month and ...
 Sort data
Create a Visual Basic (VB.Net) program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Create a Visual Basic (VB.Net) program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positi...
 Two dimensional array 2: circunference on screen
Create a Visual Basic (VB.Net) program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside i...
 Exercise tasks
Create a Visual Basic (VB.Net) program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 ...
 Household accounts
Create a program in Visual Basic (VB.Net) that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expens...


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