commit 5ef1ee93671c1758b445a8eee64a5e700139c734 Author: Laura Date: Sat Sep 26 00:35:48 2026 +0200 Entrega Actividad 1.3: Tratamiento ficheros de texto - Beans 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..60114a6 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/ljl/GestorMapaMundi.java @@ -0,0 +1,113 @@ +package org.palomafp.dam2.aadd.ljl; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.palomafp.dam2.aadd.ljl.model.Continente; +import org.palomafp.dam2.aadd.ljl.model.Pais; + +public class GestorMapaMundi { + + private static final String ruta = "C:\\Users\\lauuj\\aadd\\fic03\\informacion_paises.txt"; + + public static void main(String[] args) throws FileNotFoundException { + + Pais pais = obtenerPais(105); + System.out.println(pais); + + List paises = new ArrayList<>(); + paises = obtenerListaPaisesContinente("04"); + for(Pais p: paises) { + System.out.println(p); + } + + } + + public static Pais obtenerPais(int idPais) throws FileNotFoundException { + + 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(idComparar); + pais.setNombre(partes[2].trim()); + pais.setCapital(partes[3].trim()); + + return pais; + } + } + } + + } catch (IOException e) { + + e.printStackTrace(); + } + + return null; + } + + public static List obtenerListaPaisesContinente(String codContinente) throws FileNotFoundException { + + 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()); + int idCasteado = Integer.parseInt(partes[1].trim()); + pais.setIdentificador(idCasteado); + + 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 + "]"; + } +}