commit 091c219f613e0e725eb8f1ff01d0af99fa8bfcf0 Author: Avelino Date: Tue Sep 29 12:59:13 2026 +0200 fichero_03 diff --git a/module-info.java b/module-info.java new file mode 100644 index 0000000..3496580 --- /dev/null +++ b/module-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * + */ +module Ficheros_03 { +} \ No newline at end of file diff --git a/org/palomafp/aadd/anp/model/Continente.java b/org/palomafp/aadd/anp/model/Continente.java new file mode 100644 index 0000000..0e3cd79 --- /dev/null +++ b/org/palomafp/aadd/anp/model/Continente.java @@ -0,0 +1,34 @@ +package org.palomafp.aadd.anp.model; + +public class Continente { + + private String codigoContinente; + private String nombre; + + public Continente(String codigoContinente, String nombre) { + this.codigoContinente = codigoContinente; + this.nombre = nombre; + } + + public String getCodigoContinente() { + return codigoContinente; + } + + public void setCodigoContinente(String codigoContinente) { + this.codigoContinente = codigoContinente; + } + + public String getNombre() { + return nombre; + } + + public void setNombre(String nombre) { + this.nombre = nombre; + } + + @Override + public String toString() { + return + " codigo = " + codigoContinente; + } +} diff --git a/org/palomafp/aadd/anp/model/Pais.java b/org/palomafp/aadd/anp/model/Pais.java new file mode 100644 index 0000000..7770fd1 --- /dev/null +++ b/org/palomafp/aadd/anp/model/Pais.java @@ -0,0 +1,62 @@ +package org.palomafp.aadd.anp.model; + +public class Pais { + + private int id; + private String nombre; + private String capital; + private Continente continente; + + public Pais() { + } + + public Pais(int id, String nombre, String capital, Continente continente) { + this.id = id; + this.nombre = nombre; + this.capital = capital; + this.continente = continente; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + 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{" + + "id=" + id + + ", nombre = '" + nombre + '\'' + + ", capital = '" + capital + '\'' + "," + + continente + + '}'; + } + +} \ No newline at end of file diff --git a/org/palomafp/dam2/aadd/anp/GestorMapaMundi.java b/org/palomafp/dam2/aadd/anp/GestorMapaMundi.java new file mode 100644 index 0000000..b63917a --- /dev/null +++ b/org/palomafp/dam2/aadd/anp/GestorMapaMundi.java @@ -0,0 +1,138 @@ +package org.palomafp.dam2.aadd.anp; + +import org.palomafp.aadd.anp.model.Continente; +import org.palomafp.aadd.anp.model.Pais; + +import java.io.*; +import java.util.*; + + +public class GestorMapaMundi { + + private static final String ruta = "C:\\Users\\PC GAMING\\aadd\\fic03\\informacion_paises.txt"; + public Pais obtenerPais(int identificadorPais) { + + try (BufferedReader br = + new BufferedReader(new FileReader(ruta))) { + + String linea; + + while ((linea = br.readLine()) != null) { + + if (linea.startsWith("#")) { + continue; + } + + if (linea.trim().isEmpty()) { + continue; + } + + Pais pais = convertirLineaAPais(linea); + + if (pais.getId() == identificadorPais) { + return pais; + } + } + + } catch (IOException e) { + System.out.println( + "Error al leer el fichero: " + + e.getMessage()); + } + + return null; + } + + + public List obtenerPaisesPorContinente( + String codigoContinente) { + + List paises = new ArrayList<>(); + + try (BufferedReader br = + new BufferedReader(new FileReader(ruta))) { + + String linea; + + while ((linea = br.readLine()) != null) { + + if (linea.startsWith("#")) { + continue; + } + + if (linea.trim().isEmpty()) { + continue; + } + + Pais pais = convertirLineaAPais(linea); + + if (pais.getContinente() + .getCodigoContinente() + .equals(codigoContinente)) { + + paises.add(pais); + } + } + + } catch (IOException e) { + System.out.println( + "Error al leer el fichero: " + + e.getMessage()); + } + + return paises; + } + + + + private Pais convertirLineaAPais(String linea) { + + linea = linea.replace("\"", ""); + + String[] datos = linea.split(","); + + String codigoContinente = datos[0].trim(); + + int id = Integer.parseInt(datos[1].trim()); + + String nombre = datos[2].trim(); + + String capital = datos[3].trim(); + + Continente continente = + new Continente(codigoContinente, nombre); + + return new Pais( + id, + nombre, + capital, + continente + ); + } + + public static void main(String[] args) { + + GestorMapaMundi codigo = new GestorMapaMundi(); + + System.out.println("APARTADO A"); + + Pais pais = codigo.obtenerPais(105); + + if (pais != null) { + System.out.println(pais); + } else { + System.out.println( + "No se ha encontrado el paĆ­s."); + } + + System.out.println(); + System.out.println("APARTADO B"); + + List paises = + codigo.obtenerPaisesPorContinente("04"); + + for (Pais p : paises) { + System.out.println(p); + } + } +} \ No newline at end of file