Base de datos de ciudades Aprende programación con ejercicios C# Sharp

Lección:

Matrices, Estructuras y Cadenas


Ejercicio:

Base de datos de ciudades 74


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:

using System;
public class exercise86
{

    struct city
    {
        public string name;
        public uint inhabitants;
    }

    public static void Main()
    {
        int maxCities= 500;
        city[] cities = new city[maxCities];
        int amount = 0;
        int currentCityNumber;        
        string option;                
        string textToSearch;
        bool found;
        string textToModify;
        bool finished = 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();

            switch (option)
            {
                case "0":
                    finished = true;
                    break;

                case "1":
                    if (amount > maxCities- 1)
                        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++;
                    }
                    break;

                case "2":
                    for (int i = 0; i < amount; i++)
                    {
                        Console.WriteLine("{0}: {1}, {2} inhabitants",
                        i + 1, cities[i].name, cities[i].inhabitants);
                    }
                    Console.WriteLine();
                    break;

                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 != "")
                        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 != "")
                            cities[currentCityNumber - 1].inhabitants = Convert.ToUInt32(textToModify);
                            Console.WriteLine();
                            break;
                case "4":
                    if (amount > maxCities- 1)
                        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++;
                        for (int i = (int)amount; i > currentCityNumber - 1; i--)
                        {
                            cities[i] = cities[i - 1];
                        }
                        Console.Write("City name: ");
                        cities[currentCityNumber - 1].name = Console.ReadLine();
                        Console.Write("Inhabitants: ");
                        cities[currentCityNumber - 1].inhabitants = Convert.ToUInt32(Console.ReadLine());
                    }
                    break;

                case "5":
                    Console.Write("Enter the city number for delete: ");
                    currentCityNumber = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Deleting the number {0}", currentCityNumber);
                    for (int i = currentCityNumber - 1; i < amount; i++)
                    {
                        cities[i] = cities[i + 1];
                    }
                    amount--;
                    break;

                case "6":
                    Console.Write("Enter the text to search: ");
                    textToSearch = Console.ReadLine();
                    found = false;
                    for (int i = 0; i < amount; i++)
                    {
                        if (cities[i].name.ToUpper().IndexOf(textToSearch.ToUpper()) >= 0)
                        {
                            Console.WriteLine("{0} found in {1}",
                            textToSearch, cities[i].name);
                            found = true;
                        }
                    }
                    if (!found)
                        Console.WriteLine("Not found.");
                        break;

                case "7":
                    for(int i = 0;i < amount;i++)  
                    {
                        string lowerCaseName = cities[i].name.ToLower();
                        string correctedName = lowerCaseName.Substring(0, 1).ToUpper()
                        + lowerCaseName.Substring(1);
                        for (int j = 1; j < correctedName.Length - 2; j++)
                        {
                            if (correctedName[j] == ' ')
                            correctedName = correctedName.Substring(0, j) + " " +
                            correctedName.Substring(j + 1, 1).ToUpper() +
                            correctedName.Substring(j + 2);
                        }
                        cities[i].name = correctedName;
                    }
                    break;

                default:
                    Console.WriteLine("Wrong option ");
                    break;

            }

        } 
        while (!finished);
    }
}