1
This commit is contained in:
+10
@@ -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>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.settings/
|
||||||
|
bin/
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Ficheros_05</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,45 @@
|
|||||||
|
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 {
|
||||||
|
|
||||||
|
private final IPaisDAO paisDAO;
|
||||||
|
|
||||||
|
public GestorMapaMundi() {
|
||||||
|
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()) {
|
||||||
|
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,7 @@
|
|||||||
|
package org.palomafp.aadd.rif.dao;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rif.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
Continente obtenerContinentePorCodigo(String codigo);
|
||||||
|
}
|
||||||
@@ -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,38 @@
|
|||||||
|
package org.palomafp.aadd.rif.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import org.palomafp.aadd.rif.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rif.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDAO implements IContinenteDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_CSV = "C:\\Users\\alumnoT\\aadd\\fic05\\informacion_continentes.csv";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigoBusqueda) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_CSV))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
linea = linea.trim();
|
||||||
|
// Ignorar la cabecera del CSV
|
||||||
|
if (linea.isEmpty() || linea.startsWith("anyo")) continue;
|
||||||
|
|
||||||
|
String[] datos = linea.split("#");
|
||||||
|
if (datos.length >= 3) {
|
||||||
|
// Limpiamos los espacios en blanco que vienen en tu fichero (ej: " 04")
|
||||||
|
String codigo = datos[1].trim();
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
|
||||||
|
if (codigo.equals(codigoBusqueda.trim())) {
|
||||||
|
return new Continente(codigo, nombre);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el CSV de continentes: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
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.IContinenteDAO;
|
||||||
|
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_TXT = "C:\\Users\\alumnoT\\aadd\\fic05\\informacion_paises.txt";
|
||||||
|
private final IContinenteDAO continenteDAO;
|
||||||
|
|
||||||
|
public PaisDAO() {
|
||||||
|
this.continenteDAO = new ContinenteDAO();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) {
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_TXT))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
linea = linea.trim();
|
||||||
|
// Saltarse líneas vacías o comentarios que empiecen con '#'
|
||||||
|
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 leer el TXT de países: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> lista = new ArrayList<>();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_TXT))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
linea = linea.trim();
|
||||||
|
// Saltarse líneas vacías o comentarios que empiecen con '#'
|
||||||
|
if (linea.isEmpty() || linea.startsWith("#")) continue;
|
||||||
|
|
||||||
|
Pais pais = mapearLinea(linea);
|
||||||
|
if (pais != null && pais.getContinente() != null &&
|
||||||
|
pais.getContinente().getCodigo().equals(codContinente.trim())) {
|
||||||
|
lista.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el TXT de países: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return lista;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pais mapearLinea(String linea) {
|
||||||
|
// Separamos por coma tal como muestra tu ejemplo
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
if (datos.length >= 4) {
|
||||||
|
try {
|
||||||
|
// Quitamos comillas dobles y espacios en blanco de cada columna
|
||||||
|
String codCont = datos[0].replace("\"", "").trim();
|
||||||
|
int idPais = Integer.parseInt(datos[1].trim());
|
||||||
|
String nomPais = datos[2].replace("\"", "").trim();
|
||||||
|
String capitalPais = datos[3].replace("\"", "").trim();
|
||||||
|
|
||||||
|
// Buscamos la entidad Continente completa en el CSV
|
||||||
|
Continente con = continenteDAO.obtenerContinentePorCodigo(codCont);
|
||||||
|
if (con == null) {
|
||||||
|
con = new Continente(codCont, "Desconocido");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Pais(idPais, nomPais, capitalPais, con);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return null; // Salta filas con errores de formato de número
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
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; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
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; }
|
||||||
|
|
||||||
|
// Requisito de impresión: "Nombre_de_país (identificador_de_país), con capital en capital_de_pais pertenece a nombre_continente (código_de_continente)"
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String nomCont = (continente != null) ? continente.getNombre() : "Desconocido";
|
||||||
|
String codCont = (continente != null) ? continente.getCodigo() : "XX";
|
||||||
|
return String.format("%s (%d), con capital en %s pertenece a %s (%s)",
|
||||||
|
nombre, identificador, nombreCapital, nomCont, codCont);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user