commit 3e0d4152159ce7b59fe7a279a6c8e463c1f051f6 Author: marcos.alcalde1 Date: Tue Sep 29 14:45:19 2026 +0200 Actividad 1.3 Tratamiento de ficheros III uso de Beans diff --git a/Continente.java b/Continente.java new file mode 100644 index 0000000..0a953dd --- /dev/null +++ b/Continente.java @@ -0,0 +1,35 @@ +package org.palomafp.aadd.mag.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/GestorMapaMundi.java b/GestorMapaMundi.java new file mode 100644 index 0000000..a9d9c9c --- /dev/null +++ b/GestorMapaMundi.java @@ -0,0 +1,85 @@ +package org.palomafp.dam2.aadd.mag; + +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.aadd.mag.model.Continente; +import org.palomafp.aadd.mag.model.Pais; + +public class GestorMapaMundi { + + private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic03\\informacion_paises.txt"; + + public static void main(String[] args) { + GestorMapaMundi gestor = new GestorMapaMundi(); + + System.out.println("=== APARTADO A: PAÍS 105 ==="); + Pais p = gestor.getPaisPorIdentificador(105); // ¡Esta línea es la que faltaba! + System.out.println(p); + + System.out.println("\n=== APARTADO B: PAÍSES DEL CONTINENTE 04 ==="); + List lista = gestor.getPaisesPorContinente("04"); + for (Pais pais : lista) { + System.out.println(pais); + } + } + public Pais getPaisPorIdentificador(int idBuscado) { + File fichero = new File(RUTA_FICHERO); + try (BufferedReader reader = new BufferedReader(new FileReader(fichero))) { + String linea; + while ((linea = reader.readLine()) != null) { + linea = linea.trim(); + if (!linea.isEmpty() && !linea.startsWith("#")) { + String[] datos = linea.split(","); + if (datos.length >= 4) { + String cod = datos[0].trim().replace("\"", ""); + int id = Integer.parseInt(datos[1].trim()); + String nombre = datos[2].trim().replace("\"", ""); + String capital = datos[3].trim().replace("\"", ""); + + if (id == idBuscado) { + Continente c = new Continente(cod, "Continente " + cod); + return new Pais(id, nombre, capital, c); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public List getPaisesPorContinente(String codigoBuscado) { + List resultados = new ArrayList<>(); + File fichero = new File(RUTA_FICHERO); + try (BufferedReader reader = new BufferedReader(new FileReader(fichero))) { + String linea; + while ((linea = reader.readLine()) != null) { + linea = linea.trim(); + if (!linea.isEmpty() && !linea.startsWith("#")) { + String[] datos = linea.split(","); + if (datos.length >= 4) { + String cod = datos[0].trim().replace("\"", ""); + int id = Integer.parseInt(datos[1].trim()); + String nombre = datos[2].trim().replace("\"", ""); + String capital = datos[3].trim().replace("\"", ""); + + if (cod.equals(codigoBuscado)) { + Continente c = new Continente(cod, "Continente " + cod); + Pais p = new Pais(id, nombre, capital, c); + resultados.add(p); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + return resultados; + } +} \ No newline at end of file diff --git a/Pais.java b/Pais.java new file mode 100644 index 0000000..8c56a1b --- /dev/null +++ b/Pais.java @@ -0,0 +1,55 @@ +package org.palomafp.aadd.mag.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 + "]"; + } +} \ No newline at end of file diff --git a/module-info.java b/module-info.java new file mode 100644 index 0000000..4b606fe --- /dev/null +++ b/module-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * + */ +module Ficheros_03 { +} \ No newline at end of file