Ejercicio
Base de datos de ciudades
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 de Ejemplo
import java.util.*;
public class Main
{
private final static class city
{
public String name;
public int inhabitants;
public city clone()
{
city varCopy = new city();
varCopy.name = this.name;
varCopy.inhabitants = this.inhabitants;
return varCopy;
}
}
public static void main(String[] args)
{
int maxCities = 500;
city[] cities = new city[maxCities];
int amount = 0;
int currentCityNumber;
String option;
String textToSearch;
boolean found;
String textToModify;
boolean finished = false;
do
{
System.out.println();
System.out.println("Cities database");
System.out.println();
System.out.println("1.- Add a new city");
System.out.println("2.- View all cities");
System.out.println("3.- Modify a record");
System.out.println("4.- Insert a new record");
System.out.println("5.- Delete a record");
System.out.println("6.- Search in the records");
System.out.println("7.- Correct the capitalization of the names");
System.out.println("0.- Exit");
System.out.println();
System.out.print("Choose an option: ");
option = new Scanner(System.in).nextLine();
switch (option)
{
case "0":
finished = true;
break;
case "1":
if (amount > maxCities - 1)
{
System.out.println("the database is full");
}
else
{
System.out.printf("Entering data for city number %1$s" + "\r\n", amount + 1);
System.out.print("Enter the city name: ");
cities[amount].name = new Scanner(System.in).nextLine();
System.out.print("Enter the inhabitants numbers: ");
cities[amount].inhabitants = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.println("The data was entered correctly");
amount++;
}
break;
case "2":
for (int i = 0; i < amount; i++)
{
System.out.printf("%1$s: %2$s, %3$s inhabitants" + "\r\n", i + 1, cities[i].name, cities[i].inhabitants);
}
System.out.println();
break;
case "3":
System.out.print("Enter the city number: ");
currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.printf("Enter a new data for a city number: %1$s" + "\r\n", currentCityNumber);
System.out.printf("City name (was %1$s; hit ENTER to leave as is): ", cities[currentCityNumber - 1].name);
textToModify = new Scanner(System.in).nextLine();
if (!textToModify.equals(""))
{
cities[currentCityNumber - 1].name = textToModify;
}
System.out.printf("Inhabitants (was %1$s; hit ENTER to leave as is): ", cities[currentCityNumber - 1].inhabitants);
textToModify = new Scanner(System.in).nextLine();
if (!textToModify.equals(""))
{
cities[currentCityNumber - 1].inhabitants = Integer.parseInt(textToModify);
}
System.out.println();
break;
case "4":
if (amount > maxCities - 1)
{
System.out.println("The database is full");
}
else
{
System.out.print("Enter the number of the city to modify: ");
currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.printf("Insert a new data at %1$s position: " + "\r\n",currentCityNumber);
amount++;
for (int i = (int)amount; i > currentCityNumber - 1; i--)
{
cities[i] = cities[i - 1].clone();
}
System.out.print("City name: ");
cities[currentCityNumber - 1].name = new Scanner(System.in).nextLine();
System.out.print("Inhabitants: ");
cities[currentCityNumber - 1].inhabitants = Integer.parseInt(new Scanner(System.in).nextLine());
}
break;
case "5":
System.out.print("Enter the city number for delete: ");
currentCityNumber = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.printf("Deleting the number %1$s" + "\r\n", currentCityNumber);
for (int i = currentCityNumber - 1; i < amount; i++)
{
cities[i] = cities[i + 1].clone();
}
amount--;
break;
case "6":
System.out.print("Enter the text to search: ");
textToSearch = new Scanner(System.in).nextLine();
found = false;
for (int i = 0; i < amount; i++)
{
if (cities[i].name.toUpperCase().indexOf(textToSearch.toUpperCase()) >= 0)
{
System.out.printf("%1$s found in %2$s" + "\r\n", textToSearch, cities[i].name);
found = true;
}
}
if (!found)
{
System.out.println("Not found.");
}
break;
case "7":
for (int i = 0;i < amount;i++)
{
String lowerCaseName = cities[i].name.toLowerCase();
String correctedName = lowerCaseName.substring(0, 1).toUpperCase() + lowerCaseName.substring(1);
for (int j = 1; j < correctedName.length() - 2; j++)
{
if (correctedName.charAt(j) == ' ')
{
correctedName = correctedName.substring(0, j) + " " + correctedName.substring(j + 1, j + 1 + 1).toUpperCase() + correctedName.substring(j + 2);
}
}
cities[i].name = correctedName;
}
break;
default:
System.out.println("Wrong option ");
break;
}
} while (!finished);
}
}