1
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# Ruta completa de la clase que implementa la interfaz IPaisDAO
|
||||||
|
dao.implementacion=org.palomafp.aadd.rif.dao.impl.PaisDAO
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package org.palomafp.aadd.rif;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
import org.palomafp.aadd.rif.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.rif.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
// El gestor usa la abstracción (Interfaz)
|
||||||
|
private final IPaisDAO paisDAO;
|
||||||
|
|
||||||
|
// MODIFICADO: Ahora se instancia dinámicamente mediante reflexión
|
||||||
|
public GestorMapaMundi() {
|
||||||
|
Properties propiedades = new Properties();
|
||||||
|
String nombreClase = "";
|
||||||
|
|
||||||
|
try (InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties")) {
|
||||||
|
if (input == null) {
|
||||||
|
throw new RuntimeException("No se encontró el archivo config.properties en el src/classpath.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cargamos el fichero de propiedades
|
||||||
|
propiedades.load(input);
|
||||||
|
nombreClase = propiedades.getProperty("dao.implementacion");
|
||||||
|
|
||||||
|
// INTROSPECCIÓN: Cargamos la clase y creamos la instancia dinámicamente
|
||||||
|
Class<?> claseDAO = Class.forName(nombreClase);
|
||||||
|
this.paisDAO = (IPaisDAO) claseDAO.getDeclaredConstructor().newInstance();
|
||||||
|
|
||||||
|
System.out.println("=== Éxito: Objeto construido mediante introspección (" + nombreClase + ") ===");
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Error fatal al inicializar el DAO por reflexión [" + nombreClase + "]: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
GestorMapaMundi gestor = new GestorMapaMundi();
|
||||||
|
|
||||||
|
// Prueba A: ID 105
|
||||||
|
System.out.println("\n--- 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()) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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,79 @@
|
|||||||
|
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 {
|
||||||
|
|
||||||
|
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) {
|
||||||
|
linea = linea.trim();
|
||||||
|
// Omitir líneas vacías y comentarios que empiecen por #
|
||||||
|
if (linea.isEmpty() || linea.startsWith("#")) 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) {
|
||||||
|
linea = linea.trim();
|
||||||
|
// Omitir líneas vacías y comentarios que empiecen por #
|
||||||
|
if (linea.isEmpty() || linea.startsWith("#")) 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CORREGIDO: Ajuste de índices del array y limpieza de comillas dobles (replace("\"", ""))
|
||||||
|
private Pais mapearLinea(String linea) {
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
// Según tu .txt hay 4 campos por línea ("codCont",idPais,"nomPais","capital")
|
||||||
|
if (datos.length >= 4) {
|
||||||
|
try {
|
||||||
|
// Quitamos espacios y eliminamos comillas dobles sobrantes
|
||||||
|
String codCont = datos[0].trim().replace("\"", "");
|
||||||
|
int idPais = Integer.parseInt(datos[1].trim());
|
||||||
|
String nomPais = datos[2].trim().replace("\"", "");
|
||||||
|
String capitalPais = datos[3].trim().replace("\"", "");
|
||||||
|
|
||||||
|
// Como el .txt no trae nombre de continente, pasamos el código como nombre temporal o vacío
|
||||||
|
Continente con = new Continente(codCont, codCont);
|
||||||
|
return new Pais(idPais, nomPais, capitalPais, con);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return null; // Si falla el parseInt por alguna cabecera o línea corrupta, la salta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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 + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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") + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user