Actividad 1.9: Probar el Acceso a datos de Mapamundi desde un cliente Swing
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
# Ignorar la carpeta de compilados
|
||||||
|
bin/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Ignorar archivos de configuración de Eclipse
|
||||||
|
.settings/
|
||||||
|
.project
|
||||||
|
.classpath
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
# config/application.properties
|
||||||
|
ruta.fichero.continente=C:\\Users\\lauuj\\aadd\\fic09\\informacion_continentes.csv
|
||||||
|
separador.csv.continente=#
|
||||||
|
ruta.fichero.pais=C:\\Users\\lauuj\\aadd\\fic09\\informacion_paises.txt
|
||||||
|
separador.txt.pais=,
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
module Ficheros_03 {
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
package org.palomafp.aadd.ljl;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.ljl.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapamundi {
|
||||||
|
|
||||||
|
private IContinenteDAO continenteDao = new ContinenteDaoFichero();
|
||||||
|
private IPaisDAO paisDao = new PaisDaoFichero(continenteDao);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// lo que nos devuelve el array de los argumentos son Strings, asi que lo parseamos
|
||||||
|
int opcion = Integer.parseInt(args[0]);
|
||||||
|
|
||||||
|
GestorMapamundi gestor = new GestorMapamundi();
|
||||||
|
|
||||||
|
Continente continente = null;
|
||||||
|
|
||||||
|
switch(opcion) {
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
// Obtenemos una Lista de continentes
|
||||||
|
List<Continente> continentes = gestor.obtenerListaDeContinentes();
|
||||||
|
|
||||||
|
for (Continente cont : continentes) {
|
||||||
|
System.out.println(cont);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// Obtenemos un Continente por su código
|
||||||
|
continente = gestor.obtenerContinenteDeCodigo(args[1]);
|
||||||
|
System.out.println("El continente que buscas es: " + continente);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
// Insertaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
if(gestor.insertarContinente(continente)){
|
||||||
|
System.out.println("Continente insertado correctamente");
|
||||||
|
}else{
|
||||||
|
System.out.println("El continente no se ha podido insertar. Comprueba que el código no exista");
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
// Eliminaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
if (gestor.borrarContinente(continente)) {
|
||||||
|
System.out.println("Continente eliminado correctamente");
|
||||||
|
}else{
|
||||||
|
System.out.println("El continente no se ha podido eliminar. Comprueba que el código exista");
|
||||||
|
};
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
// Actualizaremos un continente
|
||||||
|
continente = new Continente(args[1], args[2]);
|
||||||
|
if (gestor.actualizarContinente(continente)) {
|
||||||
|
System.out.println("Continente actualizado correctamente");
|
||||||
|
}else{
|
||||||
|
System.out.println("El continente no se ha podido actualizar. Comprueba que el código exista");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
// Obtenemos la lista de países de un continente
|
||||||
|
List<Pais> paises = gestor.obtenerPaisesDeContinente(args[1]);
|
||||||
|
|
||||||
|
for (Pais pais : paises) {
|
||||||
|
System.out.println(pais);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Opción no esperada");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Pais> obtenerPaisesDeContinente(String codigoContinente) {
|
||||||
|
List<Pais> listaPaises = paisDao.obtenerPaisesPorContinente(codigoContinente);
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean actualizarContinente(Continente continente) {
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
return continenteDao.actualizarContinente(continente);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean borrarContinente(Continente continente) {
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero != null) {
|
||||||
|
return continenteDao.eliminarContinente(continente);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean insertarContinente(Continente continente) {
|
||||||
|
|
||||||
|
Continente continenteFichero = continenteDao.obtenerContinentePorCod(continente.getCodigo());
|
||||||
|
if (continenteFichero == null) {
|
||||||
|
return continenteDao.insertarContinenteNuevo(continente);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente obtenerContinenteDeCodigo(String codigoContinente) {
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCod(codigoContinente);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Continente> obtenerListaDeContinentes() {
|
||||||
|
List<Continente> listaContinentes = continenteDao.obtenerListaContinentes();
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package org.palomafp.aadd.ljl.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class ConfigManager {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO_CONFIGURACION = "application.properties";
|
||||||
|
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||||
|
|
||||||
|
private ConfigManager() {}
|
||||||
|
|
||||||
|
static {
|
||||||
|
// Leemos el archivo desde el classpath (carpeta src)
|
||||||
|
try (InputStream is = ConfigManager.class.getClassLoader().getResourceAsStream(RUTA_FICHERO_CONFIGURACION)) {
|
||||||
|
|
||||||
|
if (is != null) {
|
||||||
|
PROPIEDADES_CONFIGURACION.load(is);
|
||||||
|
} else {
|
||||||
|
System.err.println("No se ha encontrado application.properties en el classpath.");
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error al cargar application.properties: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// estos métodos -> .getProperty() -> sirve para extraer la información del fichero
|
||||||
|
|
||||||
|
public static String getRutaFicheroContinente() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("ruta.fichero.continente", "C:\\Users\\lauuj\\aadd\\fic07\\informacion_continentes.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorCsv() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("separador.csv.continente", "#");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRutaFicheroPais() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("ruta.fichero.pais", "C:\\Users\\lauuj\\aadd\\fic09\\informacion_paises.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getSeparadorTxt() {
|
||||||
|
return PROPIEDADES_CONFIGURACION.getProperty("separador.txt.pais", ",");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
public List<Continente> obtenerListaContinentes();
|
||||||
|
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente);
|
||||||
|
|
||||||
|
public boolean insertarContinenteNuevo(Continente continente);
|
||||||
|
|
||||||
|
public boolean eliminarContinente(Continente continente);
|
||||||
|
|
||||||
|
public boolean actualizarContinente(Continente continente);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
public Pais obtenerPaisPorId(int idPais);
|
||||||
|
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||||
|
}
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
package org.palomafp.aadd.ljl.dao.impl;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.ljl.config.ConfigManager;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO = ConfigManager.getRutaFicheroContinente();
|
||||||
|
private static final String SEPARADOR = ConfigManager.getSeparadorCsv();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Continente> obtenerListaContinentes() {
|
||||||
|
|
||||||
|
List<Continente> continentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
continentes.add(crearContinente(partes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return continentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Continente obtenerContinentePorCod(String codContinente) {
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[1].equals(codContinente)) {
|
||||||
|
return crearContinente(partes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean insertarContinenteNuevo(Continente continente) {
|
||||||
|
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO, true))) {
|
||||||
|
// bajar el cursor (que no se apelotonen las lineas)
|
||||||
|
bw.newLine();
|
||||||
|
bw.write(formatearLinea(continente));
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean eliminarContinente(Continente continente) {
|
||||||
|
|
||||||
|
List<String> lineasAConservar = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos el fichero y guardamos todo EXCEPTO el continente a borrar
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
// Conservamos las lineas que no empiecen por un numero y líneas vacías (las lineas no validas)
|
||||||
|
if (!validarLinea(linea)) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
// para que no se intente dividir en partes una linea no valida
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Conservamos todo lo que no coincida con el cod del continente que se quiere eliminar
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if(!partes[1].equals(continente.getCodigo())) {
|
||||||
|
lineasAConservar.add(linea);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero con las líneas conservadas
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO, false))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < lineasAConservar.size(); i++) {
|
||||||
|
bw.write(lineasAConservar.get(i));
|
||||||
|
|
||||||
|
// Salto de línea entre líneas para que no se apelotonen, y en la ultima linea ya no se salta (-1)
|
||||||
|
if (i < lineasAConservar.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean actualizarContinente(Continente continente) {
|
||||||
|
|
||||||
|
List<String> lineasActualizadas = new ArrayList<>();
|
||||||
|
|
||||||
|
// 1. Leemos y sustituimos solo la línea que coincida
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[1].equals(continente.getCodigo())) {
|
||||||
|
lineasActualizadas.add(formatearLinea(continente));
|
||||||
|
} else {
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Cabecera o línea vacía: se queda como estaba
|
||||||
|
lineasActualizadas.add(linea);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Sobrescribimos el fichero
|
||||||
|
try (BufferedWriter bw = new BufferedWriter(new FileWriter(RUTA_FICHERO, false))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < lineasActualizadas.size(); i++) {
|
||||||
|
bw.write(lineasActualizadas.get(i));
|
||||||
|
|
||||||
|
if (i < lineasActualizadas.size() - 1) {
|
||||||
|
bw.newLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// MÉTODOS AUXILIARES
|
||||||
|
|
||||||
|
// Solo son datos las líneas que empiezan por el año (un dígito); descarta vacías y cabecera
|
||||||
|
private boolean validarLinea(String linea) {
|
||||||
|
String limpia = linea.trim();
|
||||||
|
return !limpia.isEmpty() && Character.isDigit(limpia.charAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String[] dividirLinea(String linea) {
|
||||||
|
String[] partes = linea.split(SEPARADOR);
|
||||||
|
|
||||||
|
if (partes.length < 3) {
|
||||||
|
throw new IllegalArgumentException("Línea de continente no válida: " + linea);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < partes.length; i++) {
|
||||||
|
partes[i] = partes[i].trim();
|
||||||
|
}
|
||||||
|
return partes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Continente crearContinente(String[] partes) {
|
||||||
|
Continente continente = new Continente();
|
||||||
|
|
||||||
|
continente.setCodigo(partes[1]);
|
||||||
|
continente.setNombre(partes[2]);
|
||||||
|
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatearLinea(Continente continente) {
|
||||||
|
String anioActual = String.valueOf(LocalDate.now().getYear());
|
||||||
|
return anioActual + SEPARADOR + continente.getCodigo() + SEPARADOR + continente.getNombre();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package org.palomafp.aadd.ljl.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.ljl.config.ConfigManager;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.ljl.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.ljl.model.Continente;
|
||||||
|
import org.palomafp.aadd.ljl.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private static final String RUTA_FICHERO = ConfigManager.getRutaFicheroPais();
|
||||||
|
private static final String SEPARADOR = ConfigManager.getSeparadorTxt();
|
||||||
|
|
||||||
|
// No puede ser static porque no es un atributo de clase (será de la instancia del continenteDao que nos pasen)
|
||||||
|
// nota: continenteDao no es un Continente en concreto, es una manera de acceder a los datos de los continentes (métodos)
|
||||||
|
// y necesitamos poder acceder, ya que un Pais tiene un Continente como atributo (no solo el codContinente que hay en el fichero)
|
||||||
|
// continenteDao es "final" -> significa esta variable se asigna una sola vez (en el constructor)
|
||||||
|
private final IContinenteDAO continenteDao;
|
||||||
|
|
||||||
|
// Constructor: aquí "entra" la dependencia
|
||||||
|
public PaisDaoFichero(IContinenteDAO continenteDao) {
|
||||||
|
this.continenteDao = continenteDao;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pais obtenerPaisPorId(int idPais) {
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (Integer.parseInt(partes[1]) == idPais) {
|
||||||
|
|
||||||
|
Continente cont = obtenerContinente(partes[0]);
|
||||||
|
return crearPais(partes, cont);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||||
|
|
||||||
|
List<Pais> paises = new ArrayList<>();
|
||||||
|
|
||||||
|
// creamos el continente una sola vez fuera del bucle
|
||||||
|
Continente continente = obtenerContinente(codContinente);
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(RUTA_FICHERO))) {
|
||||||
|
String linea;
|
||||||
|
|
||||||
|
while ((linea = br.readLine()) != null) {
|
||||||
|
if (validarLinea(linea)) {
|
||||||
|
|
||||||
|
String[] partes = dividirLinea(linea);
|
||||||
|
|
||||||
|
if (partes[0].equals(codContinente)) {
|
||||||
|
paises.add(crearPais(partes, continente));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return paises;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// MÉTODOS AUXILIARES (para no repetir código)
|
||||||
|
|
||||||
|
private boolean validarLinea(String linea) {
|
||||||
|
String limpia = linea.trim();
|
||||||
|
return !limpia.isEmpty() && !limpia.startsWith("#") && !limpia.startsWith("--");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String[] dividirLinea(String linea) {
|
||||||
|
|
||||||
|
String[] partes = linea.replace("\"", "").split(SEPARADOR);
|
||||||
|
|
||||||
|
if (partes.length < 4) {
|
||||||
|
throw new IllegalArgumentException("Línea de país no válida: " + linea);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < partes.length; i++) {
|
||||||
|
partes[i] = partes[i].trim();
|
||||||
|
}
|
||||||
|
return partes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Continente obtenerContinente(String codContinente) {
|
||||||
|
|
||||||
|
Continente continente = continenteDao.obtenerContinentePorCod(codContinente);
|
||||||
|
|
||||||
|
if (continente == null) {
|
||||||
|
continente = new Continente();
|
||||||
|
continente.setCodigo(codContinente);
|
||||||
|
continente.setNombre("Desconocido");
|
||||||
|
}
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Pais crearPais(String[] partes, Continente continente) {
|
||||||
|
Pais pais = new Pais();
|
||||||
|
|
||||||
|
pais.setContinente(continente);
|
||||||
|
pais.setIdentificador(Integer.parseInt(partes[1]));
|
||||||
|
pais.setNombre(partes[2]);
|
||||||
|
pais.setCapital(partes[3]);
|
||||||
|
|
||||||
|
return pais;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Continente() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Continente(String codigo, String nombre) {
|
||||||
|
super();
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Getters y setters
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generamos el método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package org.palomafp.aadd.ljl.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private Continente continente;
|
||||||
|
|
||||||
|
// Constructores
|
||||||
|
public Pais() {
|
||||||
|
|
||||||
|
}
|
||||||
|
public Pais(int identificador, String nombre, String capital, Continente continente) {
|
||||||
|
super();
|
||||||
|
this.identificador = identificador;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters y Setters
|
||||||
|
public int getIdentificador() {
|
||||||
|
return identificador;
|
||||||
|
}
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
public String getCapital() {
|
||||||
|
return nombreCapital;
|
||||||
|
}
|
||||||
|
public Continente getContinente() {
|
||||||
|
return continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdentificador(int identificador) {
|
||||||
|
this.identificador = identificador;
|
||||||
|
}
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
}
|
||||||
|
public void setContinente(Continente continente) {
|
||||||
|
this.continente = continente;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Método toString
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.nombre + " (" + this.identificador + ") con capital en " + this.nombreCapital +
|
||||||
|
" pertenece al continente " + this.continente.getNombre() + " (" + this.continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user