First commit
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
continente.ruta.fichero=C:/Users/sajed/Desktop/2DAM/AADD/aadd/fic04/informacion_continentes.csv
|
continente.ruta.fichero=C:/Users/sajed/Desktop/2DAM/AADD/aadd/fic04/informacion_continentes.csv
|
||||||
continente.csv.separador=#
|
continente.csv.separador=#
|
||||||
|
pais.ruta.fichero=C:/Users/sajed/Desktop/2DAM/AADD/aadd/fic01/informacion_paises.txt
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.ss.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
Pais obtenerPaisPorIdentificador(int idPais);
|
||||||
|
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
package org.palomafp.aadd.ss.dao.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ss.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ss.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ss.gestores.GestorConfiguracion;
|
||||||
|
import org.palomafp.aadd.ss.model.Continente;
|
||||||
|
import org.palomafp.aadd.ss.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private String ruta = GestorConfiguracion.getRutaFicheroPais();
|
||||||
|
private Path rutaNio = Path.of(ruta);
|
||||||
|
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDao) {
|
||||||
|
this.continenteDAO = (ContinenteDaoCSV) continenteDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IContinenteDAO continenteDAO = new ContinenteDaoCSV();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorIdentificador(int idPais) {
|
||||||
|
Pais pais = null;
|
||||||
|
try {
|
||||||
|
List<String> todasLineas = Files.readAllLines(rutaNio, StandardCharsets.UTF_8);
|
||||||
|
for(String linea : todasLineas) {
|
||||||
|
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
linea = linea.replace("\"", "");
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
|
||||||
|
String codigoContinente = datos[0].trim();
|
||||||
|
int idpais = Integer.parseInt(datos[1].trim());
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
if (idpais == idPais) {
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setIdentificador(idpais);
|
||||||
|
pais.setNombre(nombre);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
|
||||||
|
Continente continente = continenteDAO.obtenerPorCodigo(codigoContinente);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el fichero de países: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
|
||||||
|
List<Pais> listaPaises =new ArrayList<>();
|
||||||
|
Pais pais = null;
|
||||||
|
Continente continente = null;
|
||||||
|
try {
|
||||||
|
List<String> todasLineas = Files.readAllLines(rutaNio, StandardCharsets.UTF_8);
|
||||||
|
for (String linea : todasLineas) {
|
||||||
|
if (linea.isEmpty() || linea.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
linea = linea.replace("\"", "");
|
||||||
|
String[] datos = linea.split(",");
|
||||||
|
|
||||||
|
String codigo = datos[0].trim();
|
||||||
|
int identificador = Integer.parseInt(datos[1].trim());
|
||||||
|
String nombre = datos[2].trim();
|
||||||
|
String capital = datos[3].trim();
|
||||||
|
|
||||||
|
if(codigo.equals(codContinente)) {
|
||||||
|
pais = new Pais();
|
||||||
|
pais.setIdentificador(identificador);
|
||||||
|
pais.setNombre(nombre);
|
||||||
|
pais.setCapital(capital);
|
||||||
|
|
||||||
|
continente = continenteDAO.obtenerPorCodigo(codigo);
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al leer el fichero de países: " + e.getMessage()); }
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,34 +3,47 @@ package org.palomafp.aadd.ss.gestores;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
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 {
|
||||||
// 1. Le decimos a Java dónde está el archivo que creamos en el PASO 1
|
// LÍNEA 1: Nombre del archivo dentro del Classpath.
|
||||||
private static final String RUTA_FICHERO_CONFIGURACION = "config/application.properties";
|
// Como marcamos la carpeta 'config' como Source Folder, el archivo se encuentra en la raíz del Classpath,
|
||||||
// 2. Preparamos la herramienta mágica de Java que sabe leer estos archivos ("Properties")
|
// así que no ponemos "config/application.properties", solo "application.properties".
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
|
||||||
|
// LÍNEA 2: Creamos el objeto Properties (la "estantería" vacía en memoria donde guardaremos las claves=valores).
|
||||||
private static final Properties PROPIEDAD_CONFIGURACION = new Properties();
|
private static final Properties PROPIEDAD_CONFIGURACION = new Properties();
|
||||||
|
|
||||||
// 3. Constructor escondido (private). Significa: "Nadie puede hacer copias de esta clase, es única".
|
// 3. Constructor escondido (private). Significa: "Nadie puede hacer copias de esta clase, es única".
|
||||||
private GestorConfiguracion() {}
|
private GestorConfiguracion() {}
|
||||||
// Su único trabajo es ejecutarse ÉL SOLO al arrancar el programa, sin que nadie se lo pida.
|
|
||||||
|
// LÍNEA 4: Bloque estático (static {}).
|
||||||
|
// Se ejecuta automáticamente UNA SOLA VEZ cuando la aplicación arranca.
|
||||||
static {
|
static {
|
||||||
// Intenta abrir el archivo de texto...
|
// LÍNEA 5: Usamos getResourceAsStream() en lugar de FileInputStream.
|
||||||
try(FileInputStream fichero = new FileInputStream(RUTA_FICHERO_CONFIGURACION)){
|
// GestorConfiguracion.class.getClassLoader().getResourceAsStream(...) busca el archivo dentro de la memoria compartida (Classpath).
|
||||||
// ... y le dice a la herramienta que lo lea y se lo aprenda de memoria (.load)
|
// Retorna un InputStream (flujo de datos).
|
||||||
|
try(InputStream fichero = GestorConfiguracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)){
|
||||||
|
if (fichero == null) {
|
||||||
|
throw new RuntimeException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||||
|
}
|
||||||
|
// LÍNEA 7: La estantería PROPIEDADES_CONFIGURACION lee y organiza las claves y valores del inputStream (.load).
|
||||||
PROPIEDAD_CONFIGURACION.load(fichero);
|
PROPIEDAD_CONFIGURACION.load(fichero);
|
||||||
// Si el archivo no existe o te has equivocado de nombre, explota y avisa.
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 5. Cuando otra parte del programa necesite el nombre del archivo, llamará a esto y le devolverá "informacion_continentes.csv"
|
|
||||||
public static String getRutaFicheroContinente() {
|
public static String getRutaFicheroContinente() {
|
||||||
return PROPIEDAD_CONFIGURACION.getProperty("continente.ruta.fichero");
|
return PROPIEDAD_CONFIGURACION.getProperty("continente.ruta.fichero");
|
||||||
}
|
}
|
||||||
// 6. Cuando otra parte del programa necesite saber el separador, llamará a esto y le devolverá la almohadilla "#"
|
|
||||||
public static String getSeparadorCsvFicheroContinente() {
|
public static String getSeparadorCsvFicheroContinente() {
|
||||||
return PROPIEDAD_CONFIGURACION.getProperty("continente.csv.separador");
|
return PROPIEDAD_CONFIGURACION.getProperty("continente.csv.separador");
|
||||||
}
|
}
|
||||||
|
public static String getRutaFicheroPais() {
|
||||||
|
return PROPIEDAD_CONFIGURACION.getProperty("pais.ruta.fichero");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package org.palomafp.aadd.ss.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String capital;
|
||||||
|
private Continente continente; // Relación con 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 Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return nombre + "(" + identificador + ") con capital en "
|
||||||
|
+ capital + " partenece sl continente " + continente.getNombre()
|
||||||
|
+ "(" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user