This commit is contained in:
2026-09-29 18:09:32 +02:00
commit 0b37e6ee51
8 changed files with 219 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-27">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
+2
View File
@@ -0,0 +1,2 @@
.settings/
bin/
+17
View File
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Ficheros_04</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,49 @@
package org.palomafp.aadd.rif;
import java.util.List;
import org.palomafp.aadd.rif.dao.IPaisDAO;
import org.palomafp.aadd.rif.dao.impl.PaisDAO;
import org.palomafp.aadd.rif.model.Pais;
public class GestorMapaMundi {
// El gestor usa la abstracción (Interfaz)
private final IPaisDAO paisDAO;
// CORREGIDO: Se arregló la errata en el nombre del constructor (Gestro -> Gestor)
public GestorMapaMundi() {
// Se instancia la implementación concreta por fichero
this.paisDAO = new PaisDAO();
}
public static void main(String[] args) {
GestorMapaMundi gestor = new GestorMapaMundi();
// Prueba A: ID 105
System.out.println("--- Buscando País con ID 105 ---");
Pais pais = gestor.obtenerPaisesPorIdentificador(105);
if (pais != null) {
System.out.println(pais);
} else {
System.out.println("País no encontrado.");
}
// Prueba B: Continente "04"
System.out.println("\n--- Buscando Países del Continente '04' ---");
List<Pais> lista = gestor.obtenerListaPaisesPorContinente("04");
if (!lista.isEmpty()) {
// CORREGIDO: Se sustituyó el punto por los dos puntos dobles (::)
lista.forEach(System.out::println);
} else {
System.out.println("No se encontraron países.");
}
}
public Pais obtenerPaisesPorIdentificador(int identificador) {
return paisDAO.obtenerPaisPorID(identificador);
}
public List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
return paisDAO.obtenerPaisesPorContinente(codContinente);
}
} // CORREGIDO: Se añadió la llave de cierre de la clase
@@ -0,0 +1,9 @@
package org.palomafp.aadd.rif.dao;
import java.util.List;
import org.palomafp.aadd.rif.model.Pais;
public interface IPaisDAO {
Pais obtenerPaisPorID(int idPais);
List<Pais> obtenerPaisesPorContinente(String codContinente);
}
@@ -0,0 +1,72 @@
package org.palomafp.aadd.rif.dao.impl;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.palomafp.aadd.rif.dao.IPaisDAO;
import org.palomafp.aadd.rif.model.Continente;
import org.palomafp.aadd.rif.model.Pais;
public class PaisDAO implements IPaisDAO {
// Ruta de la actividad fic04 según especificaciones
private static final String RUTA_FICHERO = "C:\\Users\\alumnoT\\aadd\\fic03.txt";
@Override
public Pais obtenerPaisPorID(int idPais) {
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
String linea;
while ((linea = br.readLine()) != null) {
if (linea.trim().isEmpty()) continue;
Pais pais = mapearLinea(linea);
if (pais != null && pais.getIdentificador() == idPais) {
return pais;
}
}
} catch (IOException e) {
System.err.println("Error al acceder al fichero: " + e.getMessage());
}
return null;
}
@Override
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
List<Pais> lista = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
String linea;
while ((linea = br.readLine()) != null) {
if (linea.trim().isEmpty()) continue;
Pais pais = mapearLinea(linea);
if (pais != null && pais.getContinente() != null &&
pais.getContinente().getCodigo().equals(codContinente)) {
lista.add(pais);
}
}
} catch (IOException e) {
System.err.println("Error al acceder al fichero: " + e.getMessage());
}
return lista;
}
private Pais mapearLinea(String linea) {
String[] datos = linea.split(";");
if (datos.length >= 5) {
try {
String codCont = datos[0].trim();
String nomCont = datos[1].trim();
int idPais = Integer.parseInt(datos[2].trim());
String nomPais = datos[3].trim();
String capitalPais = datos[4].trim();
Continente con = new Continente(codCont, nomCont);
return new Pais(idPais, nomPais, capitalPais, con);
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
}
@@ -0,0 +1,24 @@
package org.palomafp.aadd.rif.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 + "]";
}
}
+36
View File
@@ -0,0 +1,36 @@
package org.palomafp.aadd.rif.model;
public class Pais {
private int identificador;
private String nombre;
private String nombreCapital;
private Continente continente;
public Pais() {}
public Pais(int identificador, String nombre, String nombreCapital, Continente continente) {
this.identificador = identificador;
this.nombre = nombre;
this.nombreCapital = nombreCapital;
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 getNombreCapital() { return nombreCapital; }
public void setNombreCapital(String nombreCapital) { this.nombreCapital = nombreCapital; }
public Continente getContinente() { return continente; }
public void setContinente(Continente continente) { this.continente = continente; }
@Override
public String toString() {
return "Pais [identificador=" + identificador + ", nombre=" + nombre +
", nombreCapital=" + nombreCapital + ", continente=" +
(continente != null ? continente.getNombre() : "null") + "]";
}
}