commit 3374a6f05da8002f7d04c7f0b390868b17336d38 Author: Laura Date: Tue Sep 29 17:54:09 2026 +0200 Entrega Actividad 1.4: Tratamiento de ficheros con patrón DAO diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4281832 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ignorar la carpeta de compilados +bin/ +*.class + +# Ignorar archivos de configuración de Eclipse +.settings/ +.project +.classpath \ No newline at end of file diff --git a/src/module-info.java b/src/module-info.java new file mode 100644 index 0000000..3496580 --- /dev/null +++ b/src/module-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * + */ +module Ficheros_03 { +} \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/ljl/GestorMapaMundi.java b/src/org/palomafp/dam2/aadd/ljl/GestorMapaMundi.java new file mode 100644 index 0000000..1532d91 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/GestorMapaMundi.java @@ -0,0 +1,49 @@ +package org.palomafp.dam2.aadd.ljl; + +import java.util.List; + +import org.palomafp.dam2.aadd.ljl.dao.IPaisDAO; +import org.palomafp.dam2.aadd.ljl.dao.impl.PaisDaoFichero; +import org.palomafp.dam2.aadd.ljl.model.Pais; + +public class GestorMapaMundi { + + public static void main(String[] args) { + + // Instanciamos el gestor como marca el diagrama + GestorMapaMundi gestor = new GestorMapaMundi(); + + System.out.println("----------------------------------------------------"); + + Pais pais = gestor.obtenerPaisesPorIdentificador(105); + System.out.println(pais); + + System.out.println("----------------------------------------------------"); + + List paises = gestor.obtenerListaPaisesPorContinente("04"); + for(Pais p : paises) { + System.out.println(p); + } + + System.out.println("----------------------------------------------------"); + } + + public Pais obtenerPaisesPorIdentificador(int identificador) { + + // Usamos la interfaz IPaisDAO para instanciar la implementación PaisDaoFichero() + // Cuando haya otra implemetación solo tendremos que cambiar la instancia + IPaisDAO dao = new PaisDaoFichero(); + + return dao.obtenerPaisPorId(identificador); + + } + + public List obtenerListaPaisesPorContinente(String codContinente) { + + // Usamos la interfaz IPaisDAO para instanciar la implementación PaisDaoFichero() + // Cuando haya otra implemetación solo tendremos que cambiar la instancia + IPaisDAO dao = new PaisDaoFichero(); + + return dao.obtenerPaisesPorContinente(codContinente); + } +} diff --git a/src/org/palomafp/dam2/aadd/ljl/dao/IPaisDAO.java b/src/org/palomafp/dam2/aadd/ljl/dao/IPaisDAO.java new file mode 100644 index 0000000..2244602 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/dao/IPaisDAO.java @@ -0,0 +1,12 @@ +package org.palomafp.dam2.aadd.ljl.dao; + +import java.util.List; + +import org.palomafp.dam2.aadd.ljl.model.Pais; + +public interface IPaisDAO { + + public Pais obtenerPaisPorId(int idPais); + + public List obtenerPaisesPorContinente(String codContinente); +} diff --git a/src/org/palomafp/dam2/aadd/ljl/dao/impl/PaisDaoFichero.java b/src/org/palomafp/dam2/aadd/ljl/dao/impl/PaisDaoFichero.java new file mode 100644 index 0000000..e09d019 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/dao/impl/PaisDaoFichero.java @@ -0,0 +1,89 @@ +package org.palomafp.dam2.aadd.ljl.dao.impl; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.palomafp.dam2.aadd.ljl.dao.IPaisDAO; +import org.palomafp.dam2.aadd.ljl.model.Continente; +import org.palomafp.dam2.aadd.ljl.model.Pais; + +public class PaisDaoFichero implements IPaisDAO { + + // Movemos la ruta y los métodos de GetsorMapaMundi a la implementación que lee del fichero + private static final String RUTA = "C:\\Users\\lauuj\\aadd\\fic04\\informacion_paises.txt"; + + @Override + public Pais obtenerPaisPorId(int idPais) { + + File fichero = new File(RUTA); + + try (BufferedReader br = new BufferedReader(new FileReader(fichero))) { + String linea; + + while ((linea = br.readLine()) != null) { + if (!linea.startsWith("#")) { + + String[] partes = linea.replace("\"", "").split(","); + + int idComparar = Integer.parseInt(partes[1]); + + if (idComparar == idPais) { + Continente continente = new Continente(); + continente.setCodigo(partes[0].trim()); + + Pais pais = new Pais(); + pais.setContinente(continente); + pais.setIdentificador(Integer.parseInt(partes[1].trim())); + pais.setNombre(partes[2].trim()); + pais.setCapital(partes[3].trim()); + + return pais; + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + public List obtenerPaisesPorContinente(String codContinente) { + + File fichero = new File(RUTA); + + List lpaises = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(fichero))) { + String linea; + + while ((linea = br.readLine()) != null) { + if (!linea.startsWith("#")) { + + String[] partes = linea.replace("\"", "").split(","); + + if (partes[0].trim().equals(codContinente)) { + + Continente continente = new Continente(); + continente.setCodigo(codContinente); + + Pais pais = new Pais(); + pais.setContinente(continente); + pais.setNombre(partes[2].trim()); + pais.setCapital(partes[3].trim()); + pais.setIdentificador(Integer.parseInt(partes[1].trim())); + + lpaises.add(pais); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return lpaises; + } +} diff --git a/src/org/palomafp/dam2/aadd/ljl/model/Continente.java b/src/org/palomafp/dam2/aadd/ljl/model/Continente.java new file mode 100644 index 0000000..19db905 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/model/Continente.java @@ -0,0 +1,38 @@ +package org.palomafp.dam2.aadd.ljl.model; + +public class Continente { + + private String codigo; + private String nombre; + + // Constructores + public Continente() { + + } + public Continente(String codigo) { + super(); + this.codigo = codigo; + } + + + // Getters y setters + public String getCodigo() { + return codigo; + } + public String getNombre() { + return nombre; + } + + public void setCodigo(String codigo) { + this.codigo = codigo; + } + public void setNombre(String nombre) { + this.nombre = nombre; + } + + // Generamos el método toString + @Override + public String toString() { + return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]"; + } +} diff --git a/src/org/palomafp/dam2/aadd/ljl/model/Pais.java b/src/org/palomafp/dam2/aadd/ljl/model/Pais.java new file mode 100644 index 0000000..3c22d49 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/model/Pais.java @@ -0,0 +1,55 @@ +package org.palomafp.dam2.aadd.ljl.model; + +public class Pais { + + private int identificador; + private String nombre; + private String capital; + private Continente continente; + + // Constructores + public Pais() { + + } + public Pais(int identificador, String nombre, String capital, Continente continente) { + super(); + this.identificador = identificador; + this.nombre = nombre; + this.capital = capital; + this.continente = continente; + } + + // Getters y Setters + public int getIdentificador() { + return identificador; + } + public String getNombre() { + return nombre; + } + public String getCapital() { + return capital; + } + public Continente getContinente() { + return continente; + } + + public void setIdentificador(int identificador) { + this.identificador = identificador; + } + public void setNombre(String nombre) { + this.nombre = nombre; + } + public void setCapital(String capital) { + this.capital = capital; + } + public void setContinente(Continente continente) { + this.continente = continente; + } + + // Método toString + @Override + public String toString() { + return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente=" + + continente + "]"; + } +}