commit 4078c6ec2b225014b9255b7850094cd99b127e9a Author: alumnoT Date: Tue Sep 29 17:33:45 2026 +0200 1 diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..4e29ea7 --- /dev/null +++ b/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98a8c43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.settings/ +bin/ \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..97928c3 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Ficheros_03 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/org/palomafp/aadd/rif/model/Continente.java b/src/org/palomafp/aadd/rif/model/Continente.java new file mode 100644 index 0000000..371932e --- /dev/null +++ b/src/org/palomafp/aadd/rif/model/Continente.java @@ -0,0 +1,25 @@ +package org.palomafp.aadd.rif.model; + +public class Continente { + private String codigo; + private String nombre; + + public Continente() {} + + public Continente(String codigo, String nombre) { + this.codigo = codigo; + this.nombre = nombre; + } + + // Getters y Setters + public String getCodigo() { return codigo; } + public void setCodigo(String codigo) { this.codigo = codigo; } + + public String getNombre() { return nombre; } + public void setNombre(String nombre) { this.nombre = nombre; } + + @Override + public String toString() { + return "Continente{" + "codigo='" + codigo + '\'' + ", nombre='" + nombre + '\'' + '}'; + } +} \ No newline at end of file diff --git a/src/org/palomafp/aadd/rif/model/Pais.java b/src/org/palomafp/aadd/rif/model/Pais.java new file mode 100644 index 0000000..c8e8456 --- /dev/null +++ b/src/org/palomafp/aadd/rif/model/Pais.java @@ -0,0 +1,40 @@ +package org.palomafp.aadd.rif.model; + +public class Pais { + private int identificador; + private String nombre; + private String capital; + private Continente continente; // Relación reflejada en el diagrama + + public Pais() {} + + public Pais(int identificador, String nombre, String capital, Continente continente) { + this.identificador = identificador; + this.nombre = nombre; + this.capital = capital; + this.continente = continente; + } + + // Getters y Setters + public int getIdentificador() { return identificador; } + public void setIdentificador(int identificador) { this.identificador = identificador; } + + public String getNombre() { return nombre; } + public void setNombre(String nombre) { this.nombre = nombre; } + + public String getCapital() { return capital; } + public void setCapital(String capital) { this.capital = capital; } + + public Continente getContinente() { return continente; } + public void setContinente(Continente continente) { this.continente = continente; } + + @Override + public String toString() { + return "Pais{" + + "identificador=" + identificador + + ", nombre='" + nombre + '\'' + + ", capital='" + capital + '\'' + + ", continente=" + (continente != null ? continente.getNombre() : "null") + + '}'; + } +} \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/rif/GestorMapaMundi.java b/src/org/palomafp/dam2/aadd/rif/GestorMapaMundi.java new file mode 100644 index 0000000..60fa9b2 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/rif/GestorMapaMundi.java @@ -0,0 +1,106 @@ +package org.palomafp.dam2.aadd.rif; + +import org.palomafp.aadd.rif.model.Continente; +import org.palomafp.aadd.rif.model.Pais; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class GestorMapaMundi { + + // Ruta del fichero según especificaciones del ejercicio + private static final String RUTA_FICHERO = "\"C:\\Users\\alumnoT\\aadd\\fic03.txt\""; + + public static void main(String[] []args) { + GestorMapaMundi gestor = new GestorMapaMundi(); + + // REQUISITO A: Obtener país con identificador 105 + System.out.println("--- Buscando País con ID 105 ---"); + Pais pais = gestor.obtenerPaisPorId(105); + if (pais != null) { + System.out.println(pais); + } else { + System.out.println("País no encontrado."); + } + + // REQUISITO B: Obtener países asociados al código de continente "04" + System.out.println("\n--- Buscando Países del Continente '04' ---"); + List paisesContinente = gestor.obtenerPaisesPorContinente("04"); + if (!paisesContinente.isEmpty()) { + for (Pais p : paisesContinente) { + System.out.println(p); + } + } else { + System.out.println("No se encontraron países para este continente."); + } + } + + /** + * Método para el apartado A + */ + public Pais obtenerPaisPorId(int idBusqueda) { + try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) { + String linea; + while ((linea = br.readLine()) != null) { + if (linea.trim().isEmpty()) continue; + + Pais pais = mapearLineaAPais(linea); + if (pais != null && pais.getIdentificador() == idBusqueda) { + return pais; + } + } + } catch (IOException e) { + System.err.println("Error al leer el fichero: " + e.getMessage()); + } + return null; + } + + /** + * Método para el apartado B + */ + public List obtenerPaisesPorContinente(String codigoContinenteBusqueda) { + List resultado = new ArrayList<>(); + try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) { + String linea; + while ((linea = br.readLine()) != null) { + if (linea.trim().isEmpty()) continue; + + Pais pais = mapearLineaAPais(linea); + if (pais != null && pais.getContinente() != null && + pais.getContinente().getCodigo().equals(codigoContinenteBusqueda)) { + resultado.add(pais); + } + } + } catch (IOException e) { + System.err.println("Error al leer el fichero: " + e.getMessage()); + } + return resultado; + } + + /** + * Método auxiliar para transformar una línea de texto plano a objetos Java + * Asumiendo un formato: codContinente;nomContinente;idPais;nomPais;capitalPais + */ + private Pais mapearLineaAPais(String linea) { + String[] datos = linea.split(";"); + if (datos.length >= 5) { + try { + String codCont = datos[0].trim(); + String nomCont = datos[1].trim(); + int idPais = Integer.parseInt(datos[2].trim()); + String nomPais = datos[3].trim(); + String capitalPais = datos[4].trim(); + + Continente continente = new Continente(codCont, nomCont); + return new Pais(idPais, nomPais, capitalPais, continente); + } catch (NumberFormatException e) { + // Manejar error de parseo si una fila tiene formato incorrecto + return null; + } + } + return null; + } +} \ No newline at end of file