commit 9b2e0b8ee704d722a47201305a74065c008cd158 Author: felipe.garcia3 Date: Tue Oct 13 17:35:21 2026 +0200 Ejercicio Hecho diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1decc8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/bin/ +*.class +.classpath +.project +.settings/ \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/fgm/GestorMapaMundi.java b/src/org/palomafp/dam2/aadd/fgm/GestorMapaMundi.java new file mode 100644 index 0000000..0af11d5 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/GestorMapaMundi.java @@ -0,0 +1,37 @@ +package org.palomafp.dam2.aadd.fgm; + +import java.util.List; + +import org.palomafp.dam2.aadd.fgm.dao.IPaisDAO; +import org.palomafp.dam2.aadd.fgm.dao.impl.PaisDaoFichero; +import org.palomafp.dam2.aadd.fgm.model.Pais; + + +public class GestorMapaMundi { + + private static IPaisDAO PAIS_DAO = new PaisDaoFichero(); + + public static void main(String[] args) { + + System.out.println("=== Buscar país por identificador ==="); + Pais pais = obtenerPaisPorIdentificador(105); + if (pais != null) { + System.out.println(pais); + } else { + System.out.println("No existe ningún país con ese identificador."); + } + + System.out.println("\n=== Mostrar países del continente ==="); + for (Pais p : obtenerListaPaisesPorContinente("04")) { + System.out.println(p); + } + } + + public static Pais obtenerPaisPorIdentificador(int identificador) { + return PAIS_DAO.obtenerPaisPorID(identificador); + } + + public static List obtenerListaPaisesPorContinente(String codContinente) { + return PAIS_DAO.obtenerPaisesPorContinente(codContinente); + } +} \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/fgm/dao/IPaisDAO.java b/src/org/palomafp/dam2/aadd/fgm/dao/IPaisDAO.java new file mode 100644 index 0000000..d2f0f7f --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/dao/IPaisDAO.java @@ -0,0 +1,12 @@ +package org.palomafp.dam2.aadd.fgm.dao; + +import java.util.List; + +import org.palomafp.dam2.aadd.fgm.model.Pais; + +public interface IPaisDAO { + + Pais obtenerPaisPorID(int idPais); + + List obtenerPaisesPorContinente(String codContinente); +} \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/fgm/dao/impl/PaisDaoFichero.java b/src/org/palomafp/dam2/aadd/fgm/dao/impl/PaisDaoFichero.java new file mode 100644 index 0000000..a3df2d1 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/dao/impl/PaisDaoFichero.java @@ -0,0 +1,64 @@ +package org.palomafp.dam2.aadd.fgm.dao.impl; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.palomafp.dam2.aadd.fgm.dao.IPaisDAO; +import org.palomafp.dam2.aadd.fgm.model.Continente; +import org.palomafp.dam2.aadd.fgm.model.Pais; + + + +public class PaisDaoFichero implements IPaisDAO { + + private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic04\\informacion_paises.txt"; + + public Pais obtenerPaisPorID(int idPais) { + for (Pais pais : leerPaises()) { + if (pais.getIdentificador() == idPais) { + return pais; + } + } + return null; + } + + public List obtenerPaisesPorContinente(String codContinente) { + List resultado = new ArrayList<>(); + for (Pais pais : leerPaises()) { + if (pais.getContinente().getCodigo().equals(codContinente)) { + resultado.add(pais); + } + } + return resultado; + } + + private List leerPaises() { + List paises = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) { + String linea; + while ((linea = br.readLine()) != null) { + if (linea.startsWith("#")) { + continue; + } + + String[] campos = linea.split(","); + + String codigoContinente = campos[0].trim().replace("\"", ""); + int identificador = Integer.parseInt(campos[1].trim()); + String nombre = campos[2].trim().replace("\"", ""); + String capital = campos[3].trim().replace("\"", ""); + + Continente continente = new Continente(codigoContinente, null); + paises.add(new Pais(identificador, nombre, capital, continente)); + } + } catch (IOException e) { + System.out.println("Error al leer el fichero: " + e.getMessage()); + } + + return paises; + } +} \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/fgm/model/Continente.java b/src/org/palomafp/dam2/aadd/fgm/model/Continente.java new file mode 100644 index 0000000..a5a1bc5 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/model/Continente.java @@ -0,0 +1,36 @@ +package org.palomafp.dam2.aadd.fgm.model; + +public class Continente { + + private String codigo; + private String nombre; + + public Continente() { + } + + public Continente(String codigo, String nombre) { + this.codigo = codigo; + this.nombre = nombre; + } + + 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/dam2/aadd/fgm/model/Pais.java b/src/org/palomafp/dam2/aadd/fgm/model/Pais.java new file mode 100644 index 0000000..4f3c410 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/fgm/model/Pais.java @@ -0,0 +1,59 @@ +package org.palomafp.dam2.aadd.fgm.model; + +public class Pais { + + private int identificador; + private String nombre; + private String capital; + private Continente continente; + + public Pais() { + } + + public Pais(int identificador, String nombre, String capital, Continente continente) { + this.identificador = identificador; + this.nombre = nombre; + this.capital = capital; + this.continente = continente; + } + + 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.getCodigo() + "]"; + } +} \ No newline at end of file