primer commit
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# --- Directorios de compilación (binarios) ---
|
||||||
|
bin/
|
||||||
|
|
||||||
|
# --- Archivos de configuración de Eclipse ---
|
||||||
|
.metadata/
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# --- Archivos de configuración de VS Code ---
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
# --- Archivos temporales y del sistema ---
|
||||||
|
*.log
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
anyo#codigo_continente#nombre_continente
|
||||||
|
2026#01# África
|
||||||
|
2026#02 #América
|
||||||
|
2026#03#Asia
|
||||||
|
2026# 04#Europa
|
||||||
|
2026#05#Oceanía
|
||||||
|
@@ -0,0 +1,23 @@
|
|||||||
|
# Información de países
|
||||||
|
# Se muestra información del código de continente, identificador de país, nombre del país junto con su capital
|
||||||
|
"01",1, "Angola", "Luanda"
|
||||||
|
"02",62, "Brasil", "Brasilia"
|
||||||
|
"02",63, "Canadá", "Ottawa"
|
||||||
|
"02",64, "Chile", "Santiago De Chile"
|
||||||
|
"02",65, "Colombia", "Bogotá"
|
||||||
|
# Otra lista de países
|
||||||
|
"03",92, "Afganistán", "Kabul"
|
||||||
|
"03",93, "Arabia Saudita", "Riad"
|
||||||
|
"03",105, "Filipinas", "Manila"
|
||||||
|
"03",106, "India", "Nueva Delhi"
|
||||||
|
"03",136, "Vietnam", "Hanoi"
|
||||||
|
"04",147, "Bulgaria", "Sofía"
|
||||||
|
"04",152, "España", "Madrid"
|
||||||
|
"04",181, "Suecia", "Estocolmo"
|
||||||
|
# La parte final de países
|
||||||
|
"04",182, "Suiza", "Berna"
|
||||||
|
"05",185, "Australia", "Canberra"
|
||||||
|
"05",186, "Fiyi", "Suva"
|
||||||
|
"05",187, "Islas Marshall", "Majuro"
|
||||||
|
"05",196, "Tonga", "Nuku'alofa"
|
||||||
|
"05",197, "Tuvalu", "Funafuti"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module aadd_fic08 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisDaoText();
|
||||||
|
pais = paisDAO.obtenerPaisPorID(identificador);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> obtenerListaPaisesPorContinente(String codContinente) {
|
||||||
|
List<Pais> listaPaises = null;
|
||||||
|
|
||||||
|
IPaisDAO paisDAO = new PaisDaoText();
|
||||||
|
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.insertarContinentePorCodigo(codContinente, nombreNuevoContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void eliminarContinente(String codContinente) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||||
|
continenteDAO.eliminarContinentePorCodigo(codContinente);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void actualizarContinente(String codContinente, String nuevoNombre) {
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoCsv();
|
||||||
|
continenteDAO.cambiarNombreContinentePorCodigo(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,34 @@
|
|||||||
|
package org.palomafp.aadd.jaec.config;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class Configuracion {
|
||||||
|
|
||||||
|
private static final Properties PROPIEDADES = new Properties();
|
||||||
|
|
||||||
|
// Cargamos el fichero de configuracion
|
||||||
|
static {
|
||||||
|
try (InputStream input = new FileInputStream("config/application.properties")) {
|
||||||
|
|
||||||
|
PROPIEDADES.load(input);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("No se pudo cargar config/application.properties", 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 insertarContinentePorCodigo(String codContinente, String nombre);
|
||||||
|
|
||||||
|
void eliminarContinentePorCodigo(String codContinente);
|
||||||
|
|
||||||
|
void cambiarNombreContinentePorCodigo(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,233 @@
|
|||||||
|
package org.palomafp.aadd.jaec.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import org.palomafp.aadd.jaec.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.jaec.model.Continente;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
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>();
|
||||||
|
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
|
||||||
|
if (!ficheroContinentes.exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||||
|
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.out.println(ioe.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parametro 1: 2
|
||||||
|
// Metodo para obtener un Continente por codigo
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||||
|
Continente continenteEncontrado = null;
|
||||||
|
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
|
||||||
|
if (!ficheroContinentes.exists()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes))) {
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (!linea.trim().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||||
|
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 insertarContinentePorCodigo(String codContinente, String nombre) {
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
|
||||||
|
if (!ficheroContinentes.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inserto el Continente al fichero Csv
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroContinentes, true))) {
|
||||||
|
bw.write("2026" + SEPARADOR + codContinente + SEPARADOR + nombre);
|
||||||
|
bw.newLine();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
System.out.println(ioe.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parametro 1: 4
|
||||||
|
// Metodo para ELIMINAR un Continente
|
||||||
|
@Override
|
||||||
|
public void eliminarContinentePorCodigo(String codContinente) {
|
||||||
|
Continente continenteEncontrado = null;
|
||||||
|
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
File ficheroTemporal = new File(RUTA_FICHERO_TEMPORAL);
|
||||||
|
|
||||||
|
if (!ficheroContinentes.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes));
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroTemporal))) {
|
||||||
|
|
||||||
|
// 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().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||||
|
String codigoContinente = informacion[1].trim();
|
||||||
|
String nombreContinente = informacion[2].trim();
|
||||||
|
|
||||||
|
if (codContinente.trim().equalsIgnoreCase(codigoContinente)) {
|
||||||
|
continenteEncontrado = new Continente();
|
||||||
|
} else {
|
||||||
|
bw.write(linea);
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
System.err.println(ioe);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Por defecto continenteEncontrado es null, pero
|
||||||
|
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||||
|
if (continenteEncontrado instanceof Continente) {
|
||||||
|
if (ficheroContinentes.delete()) {
|
||||||
|
ficheroTemporal.renameTo(ficheroContinentes);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||||
|
ficheroTemporal.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parametro 1: 5
|
||||||
|
// Metodo para cambiar el nombre de un continente
|
||||||
|
@Override
|
||||||
|
public void cambiarNombreContinentePorCodigo(String codContinente, String nuevoNombre) {
|
||||||
|
Continente continenteEncontrado = null;
|
||||||
|
|
||||||
|
File ficheroContinentes = new File(RUTA_FICHERO);
|
||||||
|
File ficheroTemporal = new File(RUTA_FICHERO_TEMPORAL);
|
||||||
|
|
||||||
|
if (!ficheroContinentes.exists()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(ficheroContinentes));
|
||||||
|
BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroTemporal))) {
|
||||||
|
|
||||||
|
// 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().toLowerCase().startsWith("anyo") && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
int anyoContinente = Integer.parseInt(informacion[0].trim());
|
||||||
|
String codigoContinente = informacion[1].trim();
|
||||||
|
String nombreContinente = informacion[2].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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Por defecto continenteEncontrado es null, pero
|
||||||
|
// sera Continente si se encuentra un codContinente = codigoContinente
|
||||||
|
if (continenteEncontrado instanceof Continente) {
|
||||||
|
if (ficheroContinentes.delete()) {
|
||||||
|
ficheroTemporal.renameTo(ficheroContinentes);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Si no se encuentra el continente, eliminamos el fichero temporal no utilizado
|
||||||
|
ficheroTemporal.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package org.palomafp.aadd.jaec.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
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 = new ContinenteDaoCsv();
|
||||||
|
|
||||||
|
// Metodo para obtener un List<Pais> con objetos Pais por codigo de Continente
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codigoContinenteBusqueda) {
|
||||||
|
File fichero = new File(RUTA_FICHERO);
|
||||||
|
List<Pais> paises = new ArrayList<Pais>();
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
// Controlamos que no empiece por # ni por --
|
||||||
|
// Controlamos que la línea no esté vacía
|
||||||
|
if (linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--")) && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
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() {
|
||||||
|
File fichero = new File(RUTA_FICHERO);
|
||||||
|
List<Pais> paises = new ArrayList<Pais>();
|
||||||
|
|
||||||
|
String linea = "";
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fichero))) {
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
// Controlamos que no empiece por # ni por --
|
||||||
|
// Controlamos que la línea no esté vacía
|
||||||
|
if (linea.trim().charAt(0) != '#' && !(linea.trim().startsWith("--")) && !linea.isEmpty()) {
|
||||||
|
String[] informacion = linea.split(Pattern.quote(SEPARADOR));
|
||||||
|
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,57 @@
|
|||||||
|
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) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.capital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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