Actividad 1.9 Acceso a datos Mapamundi con cliente Swing
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,13 @@
|
|||||||
|
package org.palomafp.aadd.gdo.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorID(int idPais);
|
||||||
|
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package org.palomafp.aadd.gdo.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.gdo.model.Continente;
|
||||||
|
import org.palomafp.aadd.gdo.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO =
|
||||||
|
"C:\\Users\\gonza\\aadd\\fic05\\informacion_paises.txt";
|
||||||
|
|
||||||
|
private final IContinenteDAO continenteDao;
|
||||||
|
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDao) {
|
||||||
|
this.continenteDao = continenteDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorID(int idPais) {
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getIdentificador() == idPais) {
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> resultado = new ArrayList<>();
|
||||||
|
for (Pais pais : leerPaises()) {
|
||||||
|
if (pais.getContinente().getCodigo().equals(codContinente)) {
|
||||||
|
resultado.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> leerPaises() {
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
Map<String, Continente> continentes = new HashMap<>();
|
||||||
|
for (Continente continente : continenteDao.obtenerContinentes()) {
|
||||||
|
continentes.put(continente.getCodigo(), continente);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String[] campos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = campos[0].trim().replace("\"", "");
|
||||||
|
int identificador = Integer.parseInt(campos[1].trim());
|
||||||
|
String nombre = campos[2].trim().replace("\"", "");
|
||||||
|
String nombreCapital = campos[3].trim().replace("\"", "");
|
||||||
|
|
||||||
|
Continente continente = continentes.get(codigoContinente);
|
||||||
|
paises.add(new Pais(identificador, nombre, nombreCapital, continente));
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,17 +2,22 @@ package org.palomafp.aadd.gdo.gestores;
|
|||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class GestorConfiguracion {
|
public class GestorConfiguracion {
|
||||||
private static final String RUTA_FICHERO_CONFIGURACION = "config/application.properties";
|
private static final String FICHERO_CONFIGURACION = "/application.properties";
|
||||||
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
|
||||||
private GestorConfiguracion() { }
|
private GestorConfiguracion() { }
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try (FileInputStream fichero = new FileInputStream(RUTA_FICHERO_CONFIGURACION)) {
|
try (InputStream fichero = GestorConfiguracion.class
|
||||||
|
.getResourceAsStream(FICHERO_CONFIGURACION)) {
|
||||||
|
|
||||||
|
if (fichero == null) {
|
||||||
|
throw new IOException("No se encuentra " + FICHERO_CONFIGURACION + " en el classpath");
|
||||||
|
}
|
||||||
PROPIEDADES_CONFIGURACION.load(fichero);
|
PROPIEDADES_CONFIGURACION.load(fichero);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.palomafp.aadd.gdo.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 nombre + " (" + identificador + "), con capital en " + nombreCapital
|
||||||
|
+ " pertenece a " + continente.getNombre()
|
||||||
|
+ " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user