Actividad 1.10: Excepciones
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# Eclipse
|
||||||
|
.classpath
|
||||||
|
.project
|
||||||
|
.settings/
|
||||||
|
|
||||||
|
# Compilación Java
|
||||||
|
bin/
|
||||||
|
target/
|
||||||
|
*.class
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sistema operativo
|
||||||
|
.DS_Store
|
||||||
|
Thumbs.db
|
||||||
Binary file not shown.
@@ -0,0 +1,6 @@
|
|||||||
|
fichero.ruta.continentes=C:/Users/Rocío/aadd/fic05/informacion_continentes.csv
|
||||||
|
fichero.separador.continentes=#
|
||||||
|
fichero.ruta.paises=C:/Users/Rocío/aadd/fic05/informacion_paises.txt
|
||||||
|
|
||||||
|
continente.implementacion.clase=org.palomafp.aadd.rmr.dao.impl.ContinenteDaoFichero
|
||||||
|
pais.implementacion.clase=org.palomafp.aadd.rmr.dao.impl.PaisDaoFichero
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package org.palomafp.aadd.rmr;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.rmr.dao.impl.ContinenteDaoFichero;
|
||||||
|
import org.palomafp.aadd.rmr.dao.impl.PaisDaoFichero;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
import org.palomafp.aadd.rmr.model.Pais;
|
||||||
|
|
||||||
|
public class GestorMapaMundi {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// todo dentro de un try para recoger las excepciones al final del main
|
||||||
|
try {
|
||||||
|
String operacion = args[0]; // el primer argumento elige la operacion
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoFichero(); // uno solo para todos los case
|
||||||
|
|
||||||
|
switch (operacion) {
|
||||||
|
case "1": // listar continentes
|
||||||
|
List<Continente> mostrarContinentes = continenteDAO.mostrarContinentes();
|
||||||
|
for (Continente c : mostrarContinentes) {
|
||||||
|
System.out.println(c);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "2": // buscar continente por codigo
|
||||||
|
String codiguito = args[1];
|
||||||
|
Continente continenteEncontrado = continenteDAO.obtenerContinentePorCodigo(codiguito);
|
||||||
|
|
||||||
|
if (continenteEncontrado != null) {
|
||||||
|
System.out.println("Continente: " + continenteEncontrado);
|
||||||
|
} else {
|
||||||
|
System.out.println("No se encuentra el continente, lo siento.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "3": // añadir continente (args: codigo y nombre)
|
||||||
|
String codigoNuevo = args[1];
|
||||||
|
String nombreNuevo = args[2];
|
||||||
|
|
||||||
|
// antes de añadir compruebo que el codigo no exista ya
|
||||||
|
Continente contComprobacion = continenteDAO.obtenerContinentePorCodigo(codigoNuevo);
|
||||||
|
|
||||||
|
if (contComprobacion == null) {
|
||||||
|
Continente contNuevo = new Continente();
|
||||||
|
contNuevo.setCodigo(codigoNuevo);
|
||||||
|
contNuevo.setNombre(nombreNuevo);
|
||||||
|
|
||||||
|
continenteDAO.añadirContinente(contNuevo);
|
||||||
|
System.out.println("El continente " + nombreNuevo + " se ha añadido correctamente");
|
||||||
|
} else {
|
||||||
|
System.out.println("El continente con el código " + codigoNuevo + " ya existe.");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "4": // borrar continente (arg: codigo)
|
||||||
|
String codigoABorrar = args[1];
|
||||||
|
Continente contBorrar = continenteDAO.obtenerContinentePorCodigo(codigoABorrar);
|
||||||
|
|
||||||
|
// solo borro si existe
|
||||||
|
if (contBorrar != null) {
|
||||||
|
continenteDAO.eliminarContinente(codigoABorrar);
|
||||||
|
System.out.println("Continente eliminado");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se puede borrar porque no hay ningún continente con ese código");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "5": // modificar continente (args: codigo y nombre nuevo)
|
||||||
|
String codigoModificar = args[1];
|
||||||
|
String nombreModificar = args[2];
|
||||||
|
|
||||||
|
Continente contiModificar = continenteDAO.obtenerContinentePorCodigo(codigoModificar);
|
||||||
|
|
||||||
|
if (contiModificar != null) {
|
||||||
|
Continente contiModificado = new Continente();
|
||||||
|
contiModificado.setCodigo(codigoModificar);
|
||||||
|
contiModificado.setNombre(nombreModificar);
|
||||||
|
|
||||||
|
continenteDAO.modificarContinente(contiModificado);
|
||||||
|
System.out.println("Continente modificado correctamente");
|
||||||
|
} else {
|
||||||
|
System.out.println("No se puede modificar porque no hay ningún continente con ese código");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "6": // paises de un continente (arg: codigo del continente)
|
||||||
|
IPaisDAO paisDAO = new PaisDaoFichero();
|
||||||
|
List<Pais> paises = paisDAO.obtenerListaPaisesPorContinente(args[1]);
|
||||||
|
for (Pais p : paises) {
|
||||||
|
System.out.println(p);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "7": // buscar pais por id (arg: id)
|
||||||
|
IPaisDAO paisDAO2 = new PaisDaoFichero();
|
||||||
|
Pais pais = paisDAO2.obtenerPaisesPorIdentificador(Integer.parseInt(args[1]));
|
||||||
|
System.out.println(pais != null ? pais : "No existe ese país");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
System.out.println("Operación no válida");
|
||||||
|
}
|
||||||
|
|
||||||
|
// el orden de los catch importa: AccesoDatosException hereda de TecnicoException,
|
||||||
|
// asi que va antes. si fuera al reves, el de acceso a datos nunca se alcanzaria
|
||||||
|
} catch (AccesoDatosException e) {
|
||||||
|
System.err.println("Error de acceso a datos: " + e.getMessage());
|
||||||
|
// getCause es el error original (por ejemplo la IOException)
|
||||||
|
if (e.getCause() != null) {
|
||||||
|
System.err.println("Causa: " + e.getCause());
|
||||||
|
}
|
||||||
|
} catch (TecnicoException e) {
|
||||||
|
System.err.println("Error técnico: " + e.getMessage());
|
||||||
|
} catch (NegocioException e) {
|
||||||
|
// la de la pregunta 5: pais con continente que no existe
|
||||||
|
System.err.println("Error de negocio: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package org.palomafp.aadd.rmr;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.TecnicoException;
|
||||||
|
|
||||||
|
public class GestorPropiedades {
|
||||||
|
|
||||||
|
// ruta del properties dentro del classpath (carpeta src/config)
|
||||||
|
private static final String RUTA = "/config/application.properties";
|
||||||
|
|
||||||
|
// empieza en null, se rellena la primera vez que se pide una clave
|
||||||
|
private static Properties propiedades = null;
|
||||||
|
|
||||||
|
// lee el fichero y lo guarda en propiedades
|
||||||
|
// si falla lanza TecnicoException (ya no se imprime y se sigue)
|
||||||
|
private static void cargar() throws TecnicoException {
|
||||||
|
Properties props = new Properties();
|
||||||
|
|
||||||
|
// try con recursos: el fichero se cierra solo
|
||||||
|
try (InputStream entrada = GestorPropiedades.class.getResourceAsStream(RUTA)) {
|
||||||
|
|
||||||
|
// si no encuentra el fichero devuelve null
|
||||||
|
if (entrada == null) {
|
||||||
|
throw new TecnicoException("No se encuentra el fichero de propiedades: " + RUTA);
|
||||||
|
}
|
||||||
|
|
||||||
|
// el properties esta en iso-8859-1, por eso se indica el charset
|
||||||
|
props.load(new InputStreamReader(entrada, StandardCharsets.ISO_8859_1));
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
// paso la excepcion original (e) como causa para no perder el error real
|
||||||
|
throw new TecnicoException("Error al leer el fichero de propiedades: " + RUTA, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
propiedades = props;
|
||||||
|
}
|
||||||
|
|
||||||
|
// devuelve el valor de una clave. si aun no se ha cargado, lo carga antes
|
||||||
|
public static String get(String clave) throws TecnicoException {
|
||||||
|
if (propiedades == null) {
|
||||||
|
cargar();
|
||||||
|
}
|
||||||
|
return propiedades.getProperty(clave);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
|
||||||
|
public interface IContinenteDAO {
|
||||||
|
|
||||||
|
// busca un continente por su codigo. devuelve null si no existe
|
||||||
|
Continente obtenerContinentePorCodigo(String codigo) throws AccesoDatosException;
|
||||||
|
|
||||||
|
// devuelve todos los continentes del fichero
|
||||||
|
List<Continente> mostrarContinentes() throws AccesoDatosException;
|
||||||
|
|
||||||
|
// añade un continente nuevo al final del fichero
|
||||||
|
void añadirContinente(Continente nuevoContinente) throws AccesoDatosException;
|
||||||
|
|
||||||
|
// borra el continente con ese codigo
|
||||||
|
void eliminarContinente(String codigo) throws AccesoDatosException;
|
||||||
|
|
||||||
|
// cambia el nombre del continente (se identifica por el codigo)
|
||||||
|
void modificarContinente(Continente continenteModificado) throws AccesoDatosException;
|
||||||
|
|
||||||
|
// todos lanzan AccesoDatosException si falla la lectura o escritura del fichero
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.rmr.model.Pais;
|
||||||
|
|
||||||
|
public interface IPaisDAO {
|
||||||
|
|
||||||
|
// busca un pais por su id. devuelve null si no existe
|
||||||
|
// AccesoDatosException = fallo con el fichero
|
||||||
|
// NegocioException = el pais tiene un continente que no existe
|
||||||
|
Pais obtenerPaisesPorIdentificador(int identificador) throws AccesoDatosException, NegocioException;
|
||||||
|
|
||||||
|
// devuelve los paises de un continente (por su codigo)
|
||||||
|
List<Pais> obtenerListaPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao.impl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.GestorPropiedades;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
|
||||||
|
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||||
|
|
||||||
|
// constantes para no repetir los textos en cada metodo
|
||||||
|
private static final String PREFIJO_CABECERA = "anyo"; // la cabecera del csv empieza asi
|
||||||
|
private static final String ANYO_ACTUAL = "2026";
|
||||||
|
private static final String COL_CODIGO = "codigo_continente";
|
||||||
|
private static final String COL_NOMBRE = "nombre_continente";
|
||||||
|
|
||||||
|
private final Path FICHERO; // ruta del csv (viene del properties)
|
||||||
|
private final String SEPARADOR; // separador del csv (#)
|
||||||
|
private final Charset CHARSET = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
// los atributos se rellenan en el constructor porque GestorPropiedades.get
|
||||||
|
// lanza TecnicoException y en la declaracion del atributo no se puede capturar
|
||||||
|
public ContinenteDaoFichero() throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
FICHERO = Paths.get(GestorPropiedades.get("fichero.ruta.continentes"));
|
||||||
|
SEPARADOR = GestorPropiedades.get("fichero.separador.continentes");
|
||||||
|
} catch (TecnicoException e) {
|
||||||
|
// convierto la tecnica en acceso a datos, con la original como causa
|
||||||
|
throw new AccesoDatosException("No se pudo leer la configuración de continentes", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// busca un continente por su codigo. devuelve null si no esta
|
||||||
|
public Continente obtenerContinentePorCodigo(String codigo) throws AccesoDatosException {
|
||||||
|
Continente continenteEncontrado = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// lee todas las lineas del fichero de golpe
|
||||||
|
List<String> lineas = Files.readAllLines(FICHERO, CHARSET);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
// salto lineas vacias y la cabecera
|
||||||
|
if (linea.isBlank() || linea.startsWith(PREFIJO_CABECERA)) continue;
|
||||||
|
|
||||||
|
// trozos[0]=anyo, trozos[1]=codigo, trozos[2]=nombre
|
||||||
|
String[] trozos = linea.split(SEPARADOR);
|
||||||
|
String codContinente = trozos[1].trim();
|
||||||
|
|
||||||
|
if (codContinente.equals(codigo)) {
|
||||||
|
continenteEncontrado = new Continente();
|
||||||
|
continenteEncontrado.setCodigo(codContinente);
|
||||||
|
continenteEncontrado.setNombre(trozos[2].trim());
|
||||||
|
break; // ya lo he encontrado, no sigo buscando
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error leyendo el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return continenteEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
// devuelve todos los continentes del fichero
|
||||||
|
public List<Continente> mostrarContinentes() throws AccesoDatosException {
|
||||||
|
List<Continente> listaContinentes = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(FICHERO, CHARSET);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
if (linea.isBlank() || linea.startsWith(PREFIJO_CABECERA)) continue;
|
||||||
|
|
||||||
|
String[] trozos = linea.split(SEPARADOR);
|
||||||
|
Continente c = new Continente();
|
||||||
|
c.setCodigo(trozos[1].trim()); // trim quita los espacios sueltos del csv
|
||||||
|
c.setNombre(trozos[2].trim());
|
||||||
|
listaContinentes.add(c);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error leyendo el fichero de continentes", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaContinentes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// añade una linea al final del fichero
|
||||||
|
public void añadirContinente(Continente nuevoContinente) throws AccesoDatosException {
|
||||||
|
// el salto de linea va delante de la linea nueva
|
||||||
|
String lineaNueva = System.lineSeparator() + ANYO_ACTUAL + SEPARADOR
|
||||||
|
+ nuevoContinente.getCodigo() + SEPARADOR + nuevoContinente.getNombre();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// append = añadir al final sin borrar lo que hay, create = lo crea si no existe
|
||||||
|
Files.writeString(FICHERO, lineaNueva, CHARSET,
|
||||||
|
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al guardar el continente", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cambia el nombre de un continente y reescribe el fichero entero
|
||||||
|
public void modificarContinente(Continente continenteModificado) throws AccesoDatosException {
|
||||||
|
// 1. leo todos los continentes
|
||||||
|
List<Continente> listaContinentes = mostrarContinentes();
|
||||||
|
|
||||||
|
// 2. busco el que quiero cambiar y le pongo el nombre nuevo
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
if (c.getCodigo().equals(continenteModificado.getCodigo())) {
|
||||||
|
c.setNombre(continenteModificado.getNombre());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. preparo las lineas: primero la cabecera y luego una por continente
|
||||||
|
List<String> lineas = new ArrayList<>();
|
||||||
|
lineas.add(PREFIJO_CABECERA + SEPARADOR + COL_CODIGO + SEPARADOR + COL_NOMBRE);
|
||||||
|
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
lineas.add(ANYO_ACTUAL + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// sin opciones, files.write sobrescribe el fichero entero
|
||||||
|
Files.write(FICHERO, lineas, CHARSET);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al modificar el continente", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// quita un continente de la lista y reescribe el fichero (igual que modificar)
|
||||||
|
public void eliminarContinente(String codigo) throws AccesoDatosException {
|
||||||
|
List<Continente> listaContinentes = mostrarContinentes();
|
||||||
|
|
||||||
|
for (int i = 0; i < listaContinentes.size(); i++) {
|
||||||
|
if (listaContinentes.get(i).getCodigo().equals(codigo)) {
|
||||||
|
listaContinentes.remove(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> lineas = new ArrayList<>();
|
||||||
|
lineas.add(PREFIJO_CABECERA + SEPARADOR + COL_CODIGO + SEPARADOR + COL_NOMBRE);
|
||||||
|
|
||||||
|
for (Continente c : listaContinentes) {
|
||||||
|
lineas.add(ANYO_ACTUAL + SEPARADOR + c.getCodigo() + SEPARADOR + c.getNombre());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Files.write(FICHERO, lineas, CHARSET);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error al borrar el continente", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package org.palomafp.aadd.rmr.dao.impl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.palomafp.aadd.rmr.GestorPropiedades;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IContinenteDAO;
|
||||||
|
import org.palomafp.aadd.rmr.dao.IPaisDAO;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.AccesoDatosException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.NegocioException;
|
||||||
|
import org.palomafp.aadd.rmr.excepciones.TecnicoException;
|
||||||
|
import org.palomafp.aadd.rmr.model.Continente;
|
||||||
|
import org.palomafp.aadd.rmr.model.Pais;
|
||||||
|
|
||||||
|
public class PaisDaoFichero implements IPaisDAO {
|
||||||
|
|
||||||
|
private final Path FICHERO; // ruta del fichero de paises (del properties)
|
||||||
|
private final Charset CHARSET = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
// igual que en continentes: la ruta se carga en el constructor
|
||||||
|
public PaisDaoFichero() throws AccesoDatosException {
|
||||||
|
try {
|
||||||
|
FICHERO = Paths.get(GestorPropiedades.get("fichero.ruta.paises"));
|
||||||
|
} catch (TecnicoException e) {
|
||||||
|
throw new AccesoDatosException("No se pudo leer la configuración de países", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// busca un pais por su id. devuelve null si no existe ese id
|
||||||
|
// lanza NegocioException si el pais tiene un continente que no existe
|
||||||
|
public Pais obtenerPaisesPorIdentificador(int identificador) throws AccesoDatosException, NegocioException {
|
||||||
|
Pais paisEncontrado = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(FICHERO, CHARSET);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
// salto vacias y comentarios (empiezan por #)
|
||||||
|
if (linea.isBlank() || linea.startsWith("#")) continue;
|
||||||
|
|
||||||
|
// formato: continente,id,nombre,capital
|
||||||
|
String[] trozos = linea.split(",");
|
||||||
|
int idPais = Integer.parseInt(trozos[1].trim());
|
||||||
|
|
||||||
|
if (idPais == identificador) {
|
||||||
|
paisEncontrado = new Pais();
|
||||||
|
paisEncontrado.setIdentificador(idPais);
|
||||||
|
// quito comillas y espacios de los textos
|
||||||
|
paisEncontrado.setNombre(trozos[2].replace("\"", "").trim().replace("'", ""));
|
||||||
|
paisEncontrado.setCapital(trozos[3].replace("\"", "").trim().replace("'", ""));
|
||||||
|
|
||||||
|
// con el codigo del primer campo busco el continente en su dao
|
||||||
|
String codContinente = trozos[0].replace("\"", "").trim();
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||||
|
Continente continente = continenteDAO.obtenerContinentePorCodigo(codContinente);
|
||||||
|
|
||||||
|
// pregunta 5: si devuelve null el continente no existe.
|
||||||
|
// no es un fallo tecnico (el fichero se lee bien), es una regla
|
||||||
|
// de negocio rota, por eso NegocioException
|
||||||
|
if (continente == null) {
|
||||||
|
throw new NegocioException("El país " + paisEncontrado.getNombre()
|
||||||
|
+ " referencia un continente que no existe: " + codContinente);
|
||||||
|
}
|
||||||
|
paisEncontrado.setContinente(continente);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error leyendo el fichero de países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return paisEncontrado;
|
||||||
|
}
|
||||||
|
|
||||||
|
// devuelve los paises de un continente
|
||||||
|
public List<Pais> obtenerListaPaisesPorContinente(String codContinente) throws AccesoDatosException, NegocioException {
|
||||||
|
List<Pais> listaPaises = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
List<String> lineas = Files.readAllLines(FICHERO, CHARSET);
|
||||||
|
|
||||||
|
for (String linea : lineas) {
|
||||||
|
if (linea.isBlank() || linea.startsWith("#")) continue;
|
||||||
|
|
||||||
|
String[] partes = linea.split(",");
|
||||||
|
String codigo = partes[0].replace("\"", "").trim();
|
||||||
|
|
||||||
|
// solo me quedo con las lineas del continente que busco
|
||||||
|
if (codigo.equals(codContinente)) {
|
||||||
|
Pais pais = new Pais();
|
||||||
|
pais.setIdentificador(Integer.parseInt(partes[1].trim()));
|
||||||
|
pais.setNombre(partes[2].replace("\"", "").trim().replace("'", ""));
|
||||||
|
pais.setCapital(partes[3].replace("\"", "").trim().replace("'", ""));
|
||||||
|
|
||||||
|
IContinenteDAO continenteDAO = new ContinenteDaoFichero();
|
||||||
|
Continente continente = continenteDAO.obtenerContinentePorCodigo(codigo);
|
||||||
|
|
||||||
|
// mismo control que arriba: continente que no existe = negocio
|
||||||
|
if (continente == null) {
|
||||||
|
throw new NegocioException("El país " + pais.getNombre()
|
||||||
|
+ " referencia un continente que no existe: " + codigo);
|
||||||
|
}
|
||||||
|
pais.setContinente(continente);
|
||||||
|
|
||||||
|
listaPaises.add(pais);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new AccesoDatosException("Error leyendo el fichero de países", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return listaPaises;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package org.palomafp.aadd.rmr.excepciones;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* NegocioException: Clase que representa la excepción técnica ocurrida durante el acceso a datos del proyecto MapaMundi
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 15 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class AccesoDatosException extends TecnicoException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public AccesoDatosException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
public AccesoDatosException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package org.palomafp.aadd.rmr.excepciones;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* MapaMundoException: Clase abstracta que representa la excepción base del proyecto MapaMundi
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 15 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class MapaMundiException extends Exception {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Constructor protegido en clase abstracta
|
||||||
|
// Conseguimos que esta clase no se puede "instanciar directamente", solo a través de sus subclases.
|
||||||
|
protected MapaMundiException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor protegido en clase abstracta
|
||||||
|
protected MapaMundiException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.palomafp.aadd.rmr.excepciones;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* NegocioException: Clase que representa la excepción de negocio del proyecto MapaMundi
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 15 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class NegocioException extends MapaMundiException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Constructor con mensaje como parámetro.
|
||||||
|
public NegocioException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No se incluye el constructor con Throwable; se asume que una excepción de negocio no tiene asociada una excepción.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package org.palomafp.aadd.rmr.excepciones;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* NegocioException: Clase que representa la excepción técnica del proyecto MapaMundi
|
||||||
|
* @author Isidoro Nevares Martín - IES Virgen de la Paloma
|
||||||
|
* @date 15 oct 2026
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TecnicoException extends MapaMundiException {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
// Error de tipo "técnico" que no está asociado a una excepción concreta.
|
||||||
|
public TecnicoException(String mensaje) {
|
||||||
|
super(mensaje);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TecnicoException(String mensaje, Throwable causa) {
|
||||||
|
super(mensaje, causa);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.palomafp.aadd.rmr.model;
|
||||||
|
|
||||||
|
public class Continente {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Continente [codigo=" + codigo + ", nombre=" + nombre + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package org.palomafp.aadd.rmr.model;
|
||||||
|
|
||||||
|
public class Pais {
|
||||||
|
|
||||||
|
private int identificador;
|
||||||
|
private String nombre;
|
||||||
|
private String nombreCapital;
|
||||||
|
private 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 getCapital() {
|
||||||
|
return nombreCapital;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCapital(String capital) {
|
||||||
|
this.nombreCapital = capital;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 al continente "
|
||||||
|
+ continente.getNombre() + " (" + continente.getCodigo() + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user