Base de datos de ciudades Curso de Programación Visual Basic

Lección:

Matrices, Estructuras y Cadenas


Ejercicio:

Base de datos de ciudades 25


Objetivo:

Cree una base de datos para almacenar información sobre las ciudades.

En un primer acercamiento, almacenaremos solo el nombre de cada ciudad y el número de habitantes, y asignaremos espacio para hasta 500 ciudades.

El menú debe incluir las siguientes opciones:
1.- Añadir una nueva ciudad (al final de los datos existentes)
2.- Ver todas las ciudades (nombre y habitantes)
3.- Modificar un registro (renombrar y/o cambiar número de habitantes)
4.- Insertar un nuevo registro (en una posición especificada, moviendo los siguientes a la derecha)
5.- Eliminar un registro (moviendo los siguientes a la izquierda para que no queden espacios vacíos)
6.- Buscar en los registros (mostrar los que contienen un determinado texto en su nombre, ya sea en mayúsculas o minúsculas, mediante búsqueda parcial)
7.- Corregir la mayúscula de los nombres (convertir en mayúscula la primera letra y las siguientes después de un espacio, y hacer el resto en minúsculas).
0.- Salida


Código:

Imports System
Public Class exercise86
    Structure city
        Public name As String
        Public inhabitants As UInteger
    End Structure

    Public Shared Sub Main()
        Dim maxCities As Integer = 500
        Dim cities As city() = New city(maxCities - 1) {}
        Dim amount As Integer = 0
        Dim currentCityNumber As Integer
        Dim [option] As String
        Dim textToSearch As String
        Dim found As Boolean
        Dim textToModify As String
        Dim finished As Boolean = False

        Do
            Console.WriteLine()
            Console.WriteLine("Cities database")
            Console.WriteLine()
            Console.WriteLine("1.- Add a new city")
            Console.WriteLine("2.- View all cities")
            Console.WriteLine("3.- Modify a record")
            Console.WriteLine("4.- Insert a new record")
            Console.WriteLine("5.- Delete a record")
            Console.WriteLine("6.- Search in the records")
            Console.WriteLine("7.- Correct the capitalization of the names")
            Console.WriteLine("0.- Exit")
            Console.WriteLine()
            Console.Write("Choose an option: ")
            [option] = Console.ReadLine()

            Select Case [option]
                Case "0"
                    finished = True
                Case "1"

                    If amount > maxCities - 1 Then
                        Console.WriteLine("the database is full")
                    Else
                        Console.WriteLine("Entering data for city number {0}", amount + 1)
                        Console.Write("Enter the city name: ")
                        cities(amount).name = Console.ReadLine()
                        Console.Write("Enter the inhabitants numbers: ")
                        cities(amount).inhabitants = Convert.ToUInt32(Console.ReadLine())
                        Console.WriteLine("The data was entered correctly")
                        amount += 1
                    End If

                Case "2"

                    For i As Integer = 0 To amount - 1
                        Console.WriteLine("{0}: {1}, {2} inhabitants", i + 1, cities(i).name, cities(i).inhabitants)
                    Next

                    Console.WriteLine()
                Case "3"
                    Console.Write("Enter the city number: ")
                    currentCityNumber = Convert.ToInt32(Console.ReadLine())
                    Console.WriteLine("Enter a new data for a city number: {0}", currentCityNumber)
                    Console.Write("City name (was {0}; hit ENTER to leave as is): ", cities(currentCityNumber - 1).name)
                    textToModify = Console.ReadLine()
                    If textToModify <> "" Then cities(currentCityNumber - 1).name = textToModify
                    Console.Write("Inhabitants (was {0}; hit ENTER to leave as is): ", cities(currentCityNumber - 1).inhabitants)
                    textToModify = Console.ReadLine()
                    If textToModify <> "" Then cities(currentCityNumber - 1).inhabitants = Convert.ToUInt32(textToModify)
                    Console.WriteLine()
                Case "4"

                    If amount > maxCities - 1 Then
                        Console.WriteLine("The database is full")
                    Else
                        Console.Write("Enter the number of the city to modify: ")
                        currentCityNumber = Convert.ToInt32(Console.ReadLine())
                        Console.WriteLine("Insert a new data at {0} position: ", currentCityNumber)
                        amount += 1

                        For i As Integer = CInt(amount) To currentCityNumber - 1 + 1
                            cities(i) = cities(i - 1)
                        Next

                        Console.Write("City name: ")
                        cities(currentCityNumber - 1).name = Console.ReadLine()
                        Console.Write("Inhabitants: ")
                        cities(currentCityNumber - 1).inhabitants = Convert.ToUInt32(Console.ReadLine())
                    End If

                Case "5"
                    Console.Write("Enter the city number for delete: ")
                    currentCityNumber = Convert.ToInt32(Console.ReadLine())
                    Console.WriteLine("Deleting the number {0}", currentCityNumber)

                    For i As Integer = currentCityNumber - 1 To amount - 1
                        cities(i) = cities(i + 1)
                    Next

                    amount -= 1
                Case "6"
                    Console.Write("Enter the text to search: ")
                    textToSearch = Console.ReadLine()
                    found = False

                    For i As Integer = 0 To amount - 1

                        If cities(i).name.ToUpper().IndexOf(textToSearch.ToUpper()) >= 0 Then
                            Console.WriteLine("{0} found in {1}", textToSearch, cities(i).name)
                            found = True
                        End If
                    Next

                    If Not found Then Console.WriteLine("Not found.")
                Case "7"

                    For i As Integer = 0 To amount - 1
                        Dim lowerCaseName As String = cities(i).name.ToLower()
                        Dim correctedName As String = lowerCaseName.Substring(0, 1).ToUpper() & lowerCaseName.Substring(1)

                        For j As Integer = 1 To correctedName.Length - 2 - 1
                            If correctedName(j) = " "c Then correctedName = correctedName.Substring(0, j) & " " & correctedName.Substring(j + 1, 1).ToUpper() & correctedName.Substring(j + 2)
                        Next

                        cities(i).name = correctedName
                    Next

                Case Else
                    Console.WriteLine("Wrong option ")
            End Select
        Loop While Not finished
    End Sub
End Class