commit 07201b8bee0c76558eee17e5e0bbd794b46ead17 Author: sheila-mb28 Date: Tue Sep 29 12:27:14 2026 +0200 Primer commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0e29e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Compilados de Java +*.class +bin/ + +# Configuración de Eclipse +.project +.classpath +.settings/ \ No newline at end of file diff --git a/src/org/palomafp/dam2/aadd/smmb/GestorMapaMundi.java b/src/org/palomafp/dam2/aadd/smmb/GestorMapaMundi.java new file mode 100644 index 0000000..a28acaf --- /dev/null +++ b/src/org/palomafp/dam2/aadd/smmb/GestorMapaMundi.java @@ -0,0 +1,141 @@ +package org.palomafp.dam2.aadd.smmb; + +import java.io.File; +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.smmb.model.Continente; +import org.palomafp.dam2.aadd.smmb.model.Pais; + +public class GestorMapaMundi { + + private static final File FICHERO_PAISES = new File( + "D:/Documentos/DAM 2/AC - Acceso a Datos/Practicas/aadd/fic03/informacion_paises.txt"); + + public static void main(String[] args) { + + GestorMapaMundi gmm = new GestorMapaMundi(); + + Pais pais = gmm.obtenerPaisPorIdentificador(105); + + if (pais != null) { + System.out.println(pais); + } else { + System.out.println("No se encontraron coincidencias"); + } + + + + + List paises = gmm.obtenerPaisesPorContinente("04"); + + if (!paises.isEmpty()) { + for(Pais p: paises) { + System.out.println(p); + } + } else { + System.out.println("No se encontraron coincidencias"); + } + + + } + + + public Pais obtenerPaisPorIdentificador(int id) { + + try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))) { + + String linea = ""; + + while ((linea = br.readLine()) != null) { + + if (!linea.trim().startsWith("#") && !linea.trim().isEmpty()) { + + String[] campos = linea.split(","); + + String codigoContinente = campos[0].trim().replace("\"", ""); + int idPais = Integer.parseInt(campos[1].trim()); + String nombrePais = campos[2].trim().replace("\"", ""); + String capitalPais = campos[3].trim().replace("\"", ""); + + if (idPais == id) { + + Continente continente = new Continente(); + continente.setCodigo(codigoContinente); + + Pais pais = new Pais(); + pais.setIdentificador(idPais); + pais.setNombre(nombrePais); + pais.setCapital(capitalPais); + pais.setContinente(continente); + + return pais; + } + + } + + } + + } catch (IOException e) { + System.err.println("Error leyendo el fichero: " + e.getMessage()); + } + + return null; + + } + + public List obtenerPaisesPorContinente(String id) { + + List paises = new ArrayList<>(); + + try (BufferedReader br = new BufferedReader(new FileReader(FICHERO_PAISES))) { + + String linea = ""; + + + while ((linea = br.readLine()) != null) { + + if (!linea.trim().startsWith("#") && !linea.trim().isEmpty()) { + + String[] campos = linea.split(","); + + String codigoContinente = campos[0].trim().replace("\"", ""); + int idPais = Integer.parseInt(campos[1].trim()); + String nombrePais = campos[2].trim().replace("\"", ""); + String capitalPais = campos[3].trim().replace("\"", ""); + + + if (codigoContinente.equals(id)) { + + Continente continente = new Continente(); + Pais pais = new Pais(); + + continente.setCodigo(codigoContinente); + + pais.setIdentificador(idPais); + pais.setNombre(nombrePais); + pais.setCapital(capitalPais); + pais.setContinente(continente); + + paises.add(pais); + + } + + } + + } + + + + } catch (IOException e) { + System.err.println("Error leyendo el fichero: " + e.getMessage()); + } + + return paises; + + } + +} diff --git a/src/org/palomafp/dam2/aadd/smmb/model/Continente.java b/src/org/palomafp/dam2/aadd/smmb/model/Continente.java new file mode 100644 index 0000000..14cde5a --- /dev/null +++ b/src/org/palomafp/dam2/aadd/smmb/model/Continente.java @@ -0,0 +1,35 @@ +package org.palomafp.dam2.aadd.smmb.model; + +import java.io.Serializable; + +public class Continente implements Serializable{ + + private static final long serialVersionUID = 1L; + private String codigo; + private String nombre; + + public Continente() { + } + + + 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 + "]"; + } + + +} diff --git a/src/org/palomafp/dam2/aadd/smmb/model/Pais.java b/src/org/palomafp/dam2/aadd/smmb/model/Pais.java new file mode 100644 index 0000000..4ef0ca6 --- /dev/null +++ b/src/org/palomafp/dam2/aadd/smmb/model/Pais.java @@ -0,0 +1,51 @@ +package org.palomafp.dam2.aadd.smmb.model; + +import java.io.Serializable; + +public class Pais implements Serializable{ + + private static final long serialVersionUID = 1L; + private int identificador; + private String nombre; + private String capital; + private Continente continente; + + public Pais() { + } + + + + 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 void setContinente(Continente continente) { + + } + + + @Override + public String toString() { + return "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + ", continente=" + + continente + "]"; + } + + + + +}