Actividad1.10-Tratamiento_Excepciones
This commit is contained in:
Binary file not shown.
@@ -4,11 +4,13 @@ import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.gdo.dao.impl.ContinenteDaoFichero;
|
||||
import org.palomafp.aadd.gdo.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.gdo.excepciones.TecnicoException;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
|
||||
public class GestorContinentes {
|
||||
|
||||
private static final IContinenteDAO CONTINENTE_DAO = new ContinenteDaoFichero();
|
||||
private static IContinenteDAO continenteDao;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -24,7 +26,8 @@ public class GestorContinentes {
|
||||
System.out.println("La operación debe ser un número del 1 al 5.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
continenteDao = new ContinenteDaoFichero();
|
||||
switch (operacion) {
|
||||
case 1 -> listarContinentes();
|
||||
case 2 -> {
|
||||
@@ -49,6 +52,11 @@ public class GestorContinentes {
|
||||
}
|
||||
default -> System.out.println("Operación no válida: " + operacion);
|
||||
}
|
||||
} catch (AccesoDatosException e) {
|
||||
System.out.println("Error de acceso a datos: " + e.getMessage());
|
||||
} catch (TecnicoException e) {
|
||||
System.out.println("Error técnico: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean comprobarArgumentos(String[] args, int necesarios) {
|
||||
@@ -59,15 +67,15 @@ public class GestorContinentes {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void listarContinentes() {
|
||||
List<Continente> continentes = CONTINENTE_DAO.obtenerContinentes();
|
||||
private static void listarContinentes() throws AccesoDatosException {
|
||||
List<Continente> continentes = continenteDao.obtenerContinentes();
|
||||
for (Continente c : continentes) {
|
||||
System.out.println(c);
|
||||
}
|
||||
}
|
||||
|
||||
private static void mostrarContinente(String codigo) {
|
||||
Continente c = CONTINENTE_DAO.obtenerContinentePorCodigo(codigo);
|
||||
private static void mostrarContinente(String codigo) throws AccesoDatosException {
|
||||
Continente c = continenteDao.obtenerContinentePorCodigo(codigo);
|
||||
if (c != null) {
|
||||
System.out.println(c);
|
||||
} else {
|
||||
@@ -75,24 +83,24 @@ public class GestorContinentes {
|
||||
}
|
||||
}
|
||||
|
||||
private static void altaContinente(String codigo, String nombre) {
|
||||
if (CONTINENTE_DAO.insertarContinente(new Continente(codigo, nombre))) {
|
||||
private static void altaContinente(String codigo, String nombre) throws AccesoDatosException {
|
||||
if (continenteDao.insertarContinente(new Continente(codigo, nombre))) {
|
||||
System.out.println("Continente insertado correctamente.");
|
||||
} else {
|
||||
System.out.println("No se ha insertado: ya existe el código " + codigo);
|
||||
}
|
||||
}
|
||||
|
||||
private static void bajaContinente(String codigo) {
|
||||
if (CONTINENTE_DAO.eliminarContinente(codigo)) {
|
||||
private static void bajaContinente(String codigo) throws AccesoDatosException {
|
||||
if (continenteDao.eliminarContinente(codigo)) {
|
||||
System.out.println("Continente eliminado correctamente.");
|
||||
} else {
|
||||
System.out.println("No existe ningún continente con código " + codigo);
|
||||
}
|
||||
}
|
||||
|
||||
private static void modificarContinente(String codigo, String nombre) {
|
||||
if (CONTINENTE_DAO.actualizarContinente(new Continente(codigo, nombre))) {
|
||||
private static void modificarContinente(String codigo, String nombre) throws AccesoDatosException {
|
||||
if (continenteDao.actualizarContinente(new Continente(codigo, nombre))) {
|
||||
System.out.println("Continente actualizado correctamente.");
|
||||
} else {
|
||||
System.out.println("No existe ningún continente con código " + codigo);
|
||||
|
||||
@@ -2,17 +2,18 @@ package org.palomafp.aadd.gdo.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
|
||||
public interface IContinenteDAO {
|
||||
|
||||
Continente obtenerContinentePorCodigo(String codContinente);
|
||||
Continente obtenerContinentePorCodigo(String codContinente) throws AccesoDatosException;
|
||||
|
||||
List<Continente> obtenerContinentes();
|
||||
List<Continente> obtenerContinentes() throws AccesoDatosException;
|
||||
|
||||
boolean insertarContinente(Continente continente);
|
||||
boolean insertarContinente(Continente continente) throws AccesoDatosException;
|
||||
|
||||
boolean eliminarContinente(String codContinente);
|
||||
boolean eliminarContinente(String codContinente) throws AccesoDatosException;
|
||||
|
||||
boolean actualizarContinente(Continente continente);
|
||||
boolean actualizarContinente(Continente continente) throws AccesoDatosException;
|
||||
}
|
||||
@@ -2,12 +2,13 @@ package org.palomafp.aadd.gdo.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.gdo.model.Pais;
|
||||
|
||||
public interface IPaisDAO {
|
||||
|
||||
Pais obtenerPaisPorID(int idPais);
|
||||
Pais obtenerPaisPorID(int idPais) throws AccesoDatosException;
|
||||
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente);
|
||||
List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,18 +10,26 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.gdo.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.gdo.excepciones.TecnicoException;
|
||||
import org.palomafp.aadd.gdo.gestores.GestorConfiguracion;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
|
||||
public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
|
||||
private static final Path RUTA_FICHERO = Paths.get(GestorConfiguracion.getRutaFicheroContinente());
|
||||
private static final String SEPARADOR = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
private static final Charset CHARSET = StandardCharsets.UTF_8;
|
||||
private static final String ANYO = GestorConfiguracion.getAnyoContinente();
|
||||
private final Path rutaFichero;
|
||||
private final String separador;
|
||||
private final Charset charset = StandardCharsets.UTF_8;
|
||||
private final String anyo;
|
||||
|
||||
public ContinenteDaoFichero() throws TecnicoException {
|
||||
this.rutaFichero = Paths.get(GestorConfiguracion.getRutaFicheroContinente());
|
||||
this.separador = GestorConfiguracion.getSeparadorCsvFicheroContinente();
|
||||
this.anyo = GestorConfiguracion.getAnyoContinente();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) {
|
||||
public Continente obtenerContinentePorCodigo(String codContinente) throws AccesoDatosException {
|
||||
for (Continente continente : obtenerContinentes()) {
|
||||
if (continente.getCodigo().equals(codContinente)) {
|
||||
return continente;
|
||||
@@ -31,7 +39,7 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Continente> obtenerContinentes() {
|
||||
public List<Continente> obtenerContinentes() throws AccesoDatosException {
|
||||
List<Continente> continentes = new ArrayList<>();
|
||||
List<String> lineas = leerLineas();
|
||||
|
||||
@@ -40,24 +48,24 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
if (lineas.get(i).isBlank()) {
|
||||
continue;
|
||||
}
|
||||
String[] campos = lineas.get(i).split(SEPARADOR);
|
||||
String[] campos = lineas.get(i).split(separador);
|
||||
continentes.add(new Continente(campos[1].trim(), campos[2].trim()));
|
||||
}
|
||||
return continentes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean insertarContinente(Continente continente) {
|
||||
public boolean insertarContinente(Continente continente) throws AccesoDatosException {
|
||||
if (obtenerContinentePorCodigo(continente.getCodigo()) != null) {
|
||||
return false;
|
||||
}
|
||||
List<String> lineas = leerLineas();
|
||||
lineas.add(ANYO + SEPARADOR + continente.getCodigo() + SEPARADOR + continente.getNombre());
|
||||
lineas.add(anyo + separador + continente.getCodigo() + separador + continente.getNombre());
|
||||
return escribirLineas(lineas);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean eliminarContinente(String codContinente) {
|
||||
public boolean eliminarContinente(String codContinente) throws AccesoDatosException {
|
||||
List<String> lineas = leerLineas();
|
||||
boolean eliminado = false;
|
||||
|
||||
@@ -72,15 +80,15 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean actualizarContinente(Continente continente) {
|
||||
public boolean actualizarContinente(Continente continente) throws AccesoDatosException {
|
||||
List<String> lineas = leerLineas();
|
||||
boolean actualizado = false;
|
||||
|
||||
for (int i = 1; i < lineas.size(); i++) {
|
||||
if (coincideCodigo(lineas.get(i), continente.getCodigo())) {
|
||||
String[] campos = lineas.get(i).split(SEPARADOR, -1);
|
||||
String[] campos = lineas.get(i).split(separador, -1);
|
||||
campos[2] = continente.getNombre();
|
||||
lineas.set(i, String.join(SEPARADOR, campos));
|
||||
lineas.set(i, String.join(separador, campos));
|
||||
actualizado = true;
|
||||
break;
|
||||
}
|
||||
@@ -89,26 +97,24 @@ public class ContinenteDaoFichero implements IContinenteDAO {
|
||||
}
|
||||
|
||||
private boolean coincideCodigo(String linea, String codigo) {
|
||||
String[] campos = linea.split(SEPARADOR, -1);
|
||||
String[] campos = linea.split(separador, -1);
|
||||
return campos.length > 2 && campos[1].trim().equals(codigo);
|
||||
}
|
||||
|
||||
private List<String> leerLineas() {
|
||||
private List<String> leerLineas() throws AccesoDatosException {
|
||||
try {
|
||||
return new ArrayList<>(Files.readAllLines(RUTA_FICHERO, CHARSET));
|
||||
return new ArrayList<>(Files.readAllLines(rutaFichero, charset));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||
return new ArrayList<>();
|
||||
throw new AccesoDatosException("Error al leer el fichero de continentes", e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean escribirLineas(List<String> lineas) {
|
||||
private boolean escribirLineas(List<String> lineas) throws AccesoDatosException {
|
||||
try {
|
||||
Files.write(RUTA_FICHERO, lineas, CHARSET);
|
||||
Files.write(rutaFichero, lineas, charset);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al escribir el fichero: " + e.getMessage());
|
||||
return false;
|
||||
throw new AccesoDatosException("Error al leer el fichero de continentes", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,13 +10,13 @@ import java.util.Map;
|
||||
|
||||
import org.palomafp.aadd.gdo.dao.IContinenteDAO;
|
||||
import org.palomafp.aadd.gdo.dao.IPaisDAO;
|
||||
import org.palomafp.aadd.gdo.excepciones.AccesoDatosException;
|
||||
import org.palomafp.aadd.gdo.model.Continente;
|
||||
import org.palomafp.aadd.gdo.model.Pais;
|
||||
|
||||
public class PaisDaoFichero implements IPaisDAO {
|
||||
|
||||
private static final String RUTA_FICHERO =
|
||||
"C:\\Users\\gonza\\aadd\\fic05\\informacion_paises.txt";
|
||||
private static final String RUTA_FICHERO = "C:\\Users\\gonza\\aadd\\fic05\\informacion_paises.txt";
|
||||
|
||||
private final IContinenteDAO continenteDao;
|
||||
|
||||
@@ -25,7 +25,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pais obtenerPaisPorID(int idPais) {
|
||||
public Pais obtenerPaisPorID(int idPais) throws AccesoDatosException {
|
||||
for (Pais pais : leerPaises()) {
|
||||
if (pais.getIdentificador() == idPais) {
|
||||
return pais;
|
||||
@@ -35,7 +35,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) {
|
||||
public List<Pais> obtenerPaisesPorContinente(String codContinente) throws AccesoDatosException {
|
||||
List<Pais> resultado = new ArrayList<>();
|
||||
for (Pais pais : leerPaises()) {
|
||||
if (pais.getContinente().getCodigo().equals(codContinente)) {
|
||||
@@ -45,7 +45,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
return resultado;
|
||||
}
|
||||
|
||||
private List<Pais> leerPaises() {
|
||||
private List<Pais> leerPaises() throws AccesoDatosException {
|
||||
List<Pais> paises = new ArrayList<>();
|
||||
|
||||
Map<String, Continente> continentes = new HashMap<>();
|
||||
@@ -71,7 +71,7 @@ public class PaisDaoFichero implements IPaisDAO {
|
||||
paises.add(new Pais(identificador, nombre, nombreCapital, continente));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error al leer el fichero: " + e.getMessage());
|
||||
throw new AccesoDatosException("Error al leer el fichero de países", e);
|
||||
}
|
||||
|
||||
return paises;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.palomafp.aadd.gdo.excepciones;
|
||||
|
||||
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,18 @@
|
||||
package org.palomafp.aadd.gdo.excepciones;
|
||||
|
||||
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,14 @@
|
||||
package org.palomafp.aadd.gdo.excepciones;
|
||||
|
||||
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,15 @@
|
||||
package org.palomafp.aadd.gdo.excepciones;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +1,45 @@
|
||||
package org.palomafp.aadd.gdo.gestores;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.palomafp.aadd.gdo.excepciones.TecnicoException;
|
||||
|
||||
public class GestorConfiguracion {
|
||||
private static final String FICHERO_CONFIGURACION = "/application.properties";
|
||||
private static final Properties PROPIEDADES_CONFIGURACION = new Properties();
|
||||
private static Properties propiedades;
|
||||
|
||||
private GestorConfiguracion() { }
|
||||
|
||||
static {
|
||||
private static Properties getPropiedades() throws TecnicoException {
|
||||
if (propiedades == null) {
|
||||
Properties cargadas = new Properties();
|
||||
try (InputStream fichero = GestorConfiguracion.class
|
||||
.getResourceAsStream(FICHERO_CONFIGURACION)) {
|
||||
|
||||
if (fichero == null) {
|
||||
throw new IOException("No se encuentra " + FICHERO_CONFIGURACION + " en el classpath");
|
||||
throw new TecnicoException("No se encuentra " + FICHERO_CONFIGURACION + " en el classpath");
|
||||
}
|
||||
PROPIEDADES_CONFIGURACION.load(fichero);
|
||||
cargadas.load(fichero);
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("No se ha podido cargar el fichero de configuración", e);
|
||||
throw new TecnicoException("No se ha podido cargar el fichero de configuración", e);
|
||||
}
|
||||
propiedades = cargadas;
|
||||
}
|
||||
return propiedades;
|
||||
}
|
||||
|
||||
public static String getRutaFicheroContinente() {
|
||||
return PROPIEDADES_CONFIGURACION.getProperty("continente.ruta.fichero");
|
||||
public static String getRutaFicheroContinente() throws TecnicoException {
|
||||
return getPropiedades().getProperty("continente.ruta.fichero");
|
||||
}
|
||||
|
||||
public static String getSeparadorCsvFicheroContinente() {
|
||||
return PROPIEDADES_CONFIGURACION.getProperty("continente.csv.separador");
|
||||
public static String getSeparadorCsvFicheroContinente() throws TecnicoException {
|
||||
return getPropiedades().getProperty("continente.csv.separador");
|
||||
}
|
||||
|
||||
public static String getAnyoContinente() {
|
||||
return PROPIEDADES_CONFIGURACION.getProperty("continente.anyo");
|
||||
public static String getAnyoContinente() throws TecnicoException {
|
||||
return getPropiedades().getProperty("continente.anyo");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user