primer commit
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
package org.palomafp.aadd.jaec;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.dao.impl.ContinenteDaoCsv;
|
||||
import org.palomafp.aadd.jaec.dao.impl.PaisDaoText;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class GestorMapaMundi {
|
||||
|
||||
public static void main(String[] args) {
|
||||
GestorMapaMundi app = new GestorMapaMundi();
|
||||
app.menu(args);
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
int identificadorPais = 1;
|
||||
Pais pais = app.obtenerPaisPorIdentificador(identificadorPais);
|
||||
System.out.println(pais);
|
||||
System.out.println(textoAImprimir(pais));
|
||||
|
||||
System.out.println("----------------------------------------------------");
|
||||
|
||||
String codigoContinente = "04";
|
||||
List<Pais> listaPaises = app.obtenerListaPaisesPorContinente("04");
|
||||
for (Pais p2 : listaPaises) {
|
||||
System.out.println(p2);
|
||||
System.out.println(textoAImprimir(p2));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// METODOS PARA LA ENCAPSULACION DEL DAO
|
||||
private Pais obtenerPaisPorIdentificador(int identificador) {
|
||||
Pais pais = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
IPaisDAO paisDAO = new PaisDaoText(continenteDAO);
|
||||
pais = paisDAO.obtenerPaisPorID(identificador);
|
||||
|
||||
return pais;
|
||||
}
|
||||
|
||||
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||
List<Pais> listaPaises = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
IPaisDAO paisDAO = new PaisDaoText(continenteDAO);
|
||||
listaPaises = paisDAO.obtenerPaisesPorContinente(codContinente);
|
||||
|
||||
return listaPaises;
|
||||
}
|
||||
|
||||
private List<Continente> obtenerTodosContinentes() {
|
||||
List<Continente> todosContinentes = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
todosContinentes = continenteDAO.obtenerListaContinentes();
|
||||
|
||||
return todosContinentes;
|
||||
}
|
||||
|
||||
private Continente obtenerContinentePorIdentificador(String codContinente) {
|
||||
Continente continente = null;
|
||||
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
return continente;
|
||||
}
|
||||
|
||||
private void insertarContinente(String codContinente, String nombreNuevoContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.insertarContinente(codContinente, nombreNuevoContinente);
|
||||
}
|
||||
|
||||
private void eliminarContinente(String codContinente) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.borrarContinente(codContinente);
|
||||
}
|
||||
|
||||
private void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||
continenteDAO.actualizarContinente(codContinente, nuevoNombre);
|
||||
}
|
||||
|
||||
// METODO PARA IMPRIMIR TEXTO
|
||||
|
||||
private static String textoAImprimir(Pais pais) {
|
||||
String texto = String.format("%s (%s) con capital en %s pertenece al continente %s (%s)", pais.getNombre(),
|
||||
pais.getIdentificador(), pais.getCapital(), pais.getContinente().getNombre(),
|
||||
pais.getContinente().getCodigo());
|
||||
return texto;
|
||||
}
|
||||
|
||||
// MENU
|
||||
private void menu(String[] args) {
|
||||
|
||||
// Me aseguro que existan argumentos antes de iniciar el programa menu
|
||||
if (args == null || args.length == 0) {
|
||||
System.out.println("Debe indicar la opción a ejecutar en los Program Arguments (1 a 5).");
|
||||
return;
|
||||
}
|
||||
|
||||
// El primer argumento es args[0] y indicará que opción del menu se ejecuta
|
||||
String opcion = args[0];
|
||||
|
||||
switch (opcion) {
|
||||
case "1":
|
||||
List<Continente> lista = obtenerTodosContinentes();
|
||||
if (lista != null && !lista.isEmpty()) {
|
||||
for (Continente c : lista) {
|
||||
System.out.println(c);
|
||||
}
|
||||
} else {
|
||||
System.out.println("La lista de Continentes esta vacia o no existe");
|
||||
}
|
||||
break;
|
||||
|
||||
case "2":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
System.out.println("No se ha encontrado al continente con código: " + cod);
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "3":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c == null) {
|
||||
insertarContinente(cod, nombre);
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo insertar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "4":
|
||||
if (args.length >= 2) {
|
||||
String cod = args[1];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
eliminarContinente(cod);
|
||||
System.out.println("Continente eliminado.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo eliminar.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
case "5":
|
||||
if (args.length >= 3) {
|
||||
String cod = args[1];
|
||||
String nombre = args[2];
|
||||
Continente c = obtenerContinentePorIdentificador(cod);
|
||||
if (c != null) {
|
||||
actualizarContinente(cod, nombre);
|
||||
System.out.println("Se actualizó el nombre del continente.");
|
||||
} else {
|
||||
System.out.println("Error: No se pudo modificar el nombre.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("Faltan parámetros.");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Opción no válida.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.palomafp.aadd.jaec.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Configuracion {
|
||||
|
||||
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||
private static final Properties PROPIEDADES = new Properties();
|
||||
|
||||
// Constructor privado: de esta manera conseguimos que esta clase no se pueda
|
||||
// instanciar
|
||||
private Configuracion() {
|
||||
}
|
||||
|
||||
// Cargamos el fichero de configuracion
|
||||
static {
|
||||
try (InputStream input = Configuracion.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||
|
||||
if (input == null) {
|
||||
throw new IOException("No se encuentra " + RUTA_FICHERO_CONFIGURACION + " en el classpath");
|
||||
}
|
||||
|
||||
PROPIEDADES.load(input);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se pudo cargar el fichero de configuración", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Metodo para obtener el valor de una propiedad
|
||||
public static String get(String clave) {
|
||||
|
||||
String valor = PROPIEDADES.getProperty(clave);
|
||||
|
||||
if (valor == null || valor.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("No existe la propiedad: " + clave);
|
||||
}
|
||||
|
||||
return valor.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
List<Continente> obtenerListaContinentes();
|
||||
|
||||
Continente obtenerContinentePorCodigo(String codContinente);
|
||||
|
||||
void insertarContinente(String codContinente, String nombre);
|
||||
|
||||
void borrarContinente(String codContinente);
|
||||
|
||||
void actualizarContinente(String codContinente, String nuevoNombre);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.jaec.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
Pais obtenerPaisPorID(int identificador);
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package org.palomafp.aadd.jaec.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.config.Configuracion;
|
||||
|
||||
public class ContinenteDaoCsv implements IContinenteDAO {
|
||||
private static final String RUTA_FICHERO = Configuracion.get("continente.ruta");
|
||||
private static final String RUTA_FICHERO_TEMPORAL = Configuracion.get("continente.ruta.temporal");
|
||||
private static final String SEPARADOR = Configuracion.get("continente.separador");
|
||||
|
||||
// Parametro 1: 1
|
||||
// Metodo para obtener una lista List<Continente>
|
||||
@Override
|
||||
public List<Continente> obtenerListaContinentes() {
|
||||
List<Continente> listaContinentes = new ArrayList<Continente>();
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return listaContinentes;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(ficheroContinentes, StandardCharsets.UTF_8)) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().isEmpty() && !linea.trim().toLowerCase().startsWith("anyo")) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 3) {
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
// Instancio un Continente y le asigno sus atributos
|
||||
Continente continenteImprimir = new Continente();
|
||||
continenteImprimir.setCodigo(codigoContinente);
|
||||
continenteImprimir.setNombre(nombreContinente);
|
||||
|
||||
// Lo agrego a la lista
|
||||
listaContinentes.add(continenteImprimir);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
return listaContinentes;
|
||||
}
|
||||
|
||||
// Parametro 1: 2
|
||||
// Metodo para obtener un Continente por codigo
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return null;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(ficheroContinentes, StandardCharsets.UTF_8)) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().isEmpty() && !linea.trim().toLowerCase().startsWith("anyo")) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 3) {
|
||||
String codigoContinente = informacion[1].trim();
|
||||
String nombreContinente = informacion[2].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(nombreContinente);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
|
||||
return continenteEncontrado;
|
||||
}
|
||||
|
||||
// Parametro 1: 3
|
||||
// Metodo para INSERTAR un Continente
|
||||
@Override
|
||||
public void insertarContinente(String codContinente, String nombre) {
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
// Inserto el Continente al fichero Csv
|
||||
try (BufferedWriter bw = Files.newBufferedWriter(ficheroContinentes, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.WRITE, StandardOpenOption.APPEND)) {
|
||||
bw.write("2026" + SEPARADOR + codContinente + SEPARADOR + nombre);
|
||||
bw.newLine();
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Parametro 1: 4
|
||||
// Metodo para ELIMINAR un Continente
|
||||
@Override
|
||||
public void borrarContinente(String codContinente) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
Path ficheroTemporal = Path.of(RUTA_FICHERO_TEMPORAL);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(ficheroContinentes, StandardCharsets.UTF_8);
|
||||
BufferedWriter bw = Files.newBufferedWriter(ficheroTemporal, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
|
||||
|
||||
// Escribimos la cabecera en el fichero temporal
|
||||
bw.write("anyo" + SEPARADOR + "codigo_continente" + SEPARADOR + "nombre_continente");
|
||||
bw.newLine();
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().isEmpty() && !linea.trim().toLowerCase().startsWith("anyo")) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 3) {
|
||||
String codigoContinente = informacion[1].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
} else {
|
||||
bw.write(linea);
|
||||
bw.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||
if (continenteEncontrado instanceof Continente) {
|
||||
try {
|
||||
Files.move(ficheroTemporal, ficheroContinentes, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
} else {
|
||||
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||
try {
|
||||
Files.deleteIfExists(ficheroTemporal);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parametro 1: 5
|
||||
// Metodo para cambiar el nombre de un continente
|
||||
@Override
|
||||
public void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||
Continente continenteEncontrado = null;
|
||||
|
||||
Path ficheroContinentes = Path.of(RUTA_FICHERO);
|
||||
Path ficheroTemporal = Path.of(RUTA_FICHERO_TEMPORAL);
|
||||
|
||||
if (!Files.exists(ficheroContinentes)) {
|
||||
System.out.println("Directorio de trabajo: " + System.getProperty("user.dir"));
|
||||
System.out.println("Ruta del CSV buscado: " + ficheroContinentes.toAbsolutePath());
|
||||
return;
|
||||
}
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(ficheroContinentes, StandardCharsets.UTF_8);
|
||||
BufferedWriter bw = Files.newBufferedWriter(ficheroTemporal, StandardCharsets.UTF_8,
|
||||
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
|
||||
|
||||
// Escribimos la cabecera en el fichero temporal
|
||||
bw.write("anyo" + SEPARADOR + "codigo_continente" + SEPARADOR + "nombre_continente");
|
||||
bw.newLine();
|
||||
|
||||
while ((linea = br.readLine()) != null) {
|
||||
if (!linea.trim().isEmpty() && !linea.trim().toLowerCase().startsWith("anyo")) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 3) {
|
||||
String codigoContinente = informacion[1].trim();
|
||||
|
||||
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||
continenteEncontrado = new Continente();
|
||||
continenteEncontrado.setCodigo(codigoContinente);
|
||||
continenteEncontrado.setNombre(nuevoNombre);
|
||||
|
||||
// Escribimos la línea modificada directamente en el fichero temporal
|
||||
bw.write(informacion[0].trim() + SEPARADOR + continenteEncontrado.getCodigo() + SEPARADOR
|
||||
+ continenteEncontrado.getNombre());
|
||||
bw.newLine();
|
||||
} else {
|
||||
bw.write(linea);
|
||||
bw.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
// Por defecto continenteEncontrado es null, pero
|
||||
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||
if (continenteEncontrado instanceof Continente) {
|
||||
try {
|
||||
Files.move(ficheroTemporal, ficheroContinentes, StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
} else {
|
||||
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||
try {
|
||||
Files.deleteIfExists(ficheroTemporal);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
|
||||
package org.palomafp.aadd.jaec.dao.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
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.jaec.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.jaec.model.Continente;
|
||||
import org.palomafp.aadd.jaec.model.Pais;
|
||||
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import org.palomafp.aadd.jaec.config.Configuracion;
|
||||
|
||||
public class PaisDaoText implements IPaisDAO {
|
||||
private static final String RUTA_FICHERO = Configuracion.get("pais.ruta");
|
||||
private static final String SEPARADOR = Configuracion.get("pais.separador");
|
||||
|
||||
private IContinenteDAO continenteDAO;
|
||||
|
||||
public PaisDaoText(IContinenteDAO continenteDAO) {
|
||||
this.continenteDAO = continenteDAO;
|
||||
}
|
||||
|
||||
// Metodo para obtener un List<Pais> con objetos Pais por codigo de Continente
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda) {
|
||||
Path fichero = Path.of(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(fichero, StandardCharsets.UTF_8)) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
// Controlamos que no empiece por # ni por --
|
||||
// Controlamos que la línea no esté vacía
|
||||
if (!linea.trim().isEmpty() && linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--"))) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 4) {
|
||||
String codContinente = informacion[0].replace("\"", "").trim();
|
||||
int idPais = Integer.parseInt(informacion[1].trim());
|
||||
String nombrePais = informacion[2].replace("\"", "").trim();
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
if (codigoContinenteBusqueda.compareTo(codContinente) == 0) {
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
paises.add(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
return paises;
|
||||
}
|
||||
|
||||
// Metodo para obtener un Pais por ID
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPaisBusqueda) {
|
||||
// Leo la lista de TODOS los paises
|
||||
List<Pais> listaPaises = leerListaPaises();
|
||||
|
||||
// Busco solamente a un unico pais
|
||||
for (Pais p : listaPaises) {
|
||||
if (p.getIdentificador() == idPaisBusqueda) {
|
||||
return p;
|
||||
}
|
||||
}
|
||||
// Si no encuentro nada devuelvo nulo
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// Metodo para leer una lista de paises a partir de un fichero
|
||||
// Devuelve un List<Pais> con varios objetos <Pais>
|
||||
public List<Pais> leerListaPaises() {
|
||||
Path fichero = Path.of(RUTA_FICHERO);
|
||||
List<Pais> paises = new ArrayList<Pais>();
|
||||
|
||||
String linea = "";
|
||||
|
||||
try (BufferedReader br = Files.newBufferedReader(fichero, StandardCharsets.UTF_8)) {
|
||||
while ((linea = br.readLine()) != null) {
|
||||
// Controlamos que no empiece por # ni por --
|
||||
// Controlamos que la línea no esté vacía
|
||||
if (!linea.trim().isEmpty() && linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--"))) {
|
||||
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||
|
||||
if (informacion.length >= 4) {
|
||||
String codContinente = informacion[0].replace("\"", "").trim();
|
||||
int idPais = Integer.parseInt(informacion[1].trim());
|
||||
String nombrePais = informacion[2].replace("\"", "").trim();
|
||||
String capitalPais = informacion[3].replace("\"", "").trim();
|
||||
|
||||
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||
|
||||
Pais pais = new Pais();
|
||||
pais.setIdentificador(idPais);
|
||||
pais.setNombre(nombrePais);
|
||||
pais.setCapital(capitalPais);
|
||||
pais.setContinente(continente);
|
||||
|
||||
paises.add(pais);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println(ioe.getMessage());
|
||||
}
|
||||
|
||||
// En caso no se encuentre nada se devuelve la lista paises vacio
|
||||
return paises;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.palomafp.aadd.jaec.model;
|
||||
|
||||
public class Continente {
|
||||
private String codigo, nombre;
|
||||
|
||||
// Constructores
|
||||
public Continente() {
|
||||
|
||||
}
|
||||
|
||||
public Continente(String codigo, String nombre) {
|
||||
this.codigo = codigo;
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
// Getters y Setters
|
||||
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;
|
||||
}
|
||||
|
||||
// toString
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.palomafp.aadd.jaec.model;
|
||||
|
||||
public class Pais {
|
||||
private int identificador;
|
||||
private String nombre, capital;
|
||||
private Continente continente;
|
||||
|
||||
// Constructores
|
||||
public Pais() {
|
||||
|
||||
}
|
||||
|
||||
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||
this.identificador = identificador;
|
||||
this.nombre = nombre;
|
||||
this.capital = capital;
|
||||
this.continente = continente;
|
||||
}
|
||||
|
||||
// Getters y Setters
|
||||
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 "Pais [identificador=" + identificador + ", nombre=" + nombre + ", capital=" + capital + "]";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user