MODIFICACIÓN - Decisión en el lector CSV

This commit is contained in:
rocio.montero1
2026-11-09 20:52:13 +01:00
parent 9865dbd9c5
commit cf01736915
13 changed files with 108 additions and 47 deletions
+2 -2
View File
@@ -2,8 +2,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>SimulacionExamen</groupId> <groupId>RedMetroRMR</groupId>
<artifactId>SimulacionExamen</artifactId> <artifactId>RedMetroRMR</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<dependencies> <dependencies>
<!-- Source: <!-- Source:
+2
View File
@@ -42,6 +42,8 @@ public class Main {
} }
private static void mostrarColorLinea(Scanner sc) { private static void mostrarColorLinea(Scanner sc) {
System.out.println("Introduce el identificador de la línea: "); System.out.println("Introduce el identificador de la línea: ");
int codigoLinea = Integer.parseInt(sc.nextLine().trim()); int codigoLinea = Integer.parseInt(sc.nextLine().trim());
@@ -9,5 +9,7 @@ public interface ICochera {
void insertarCochera (Cochera cochera) throws RedMetroException; void insertarCochera (Cochera cochera) throws RedMetroException;
void actualizarCochera (Cochera cochera) throws RedMetroException; void actualizarCochera (Cochera cochera) throws RedMetroException;
void borrarCochera (int codigoCochera) throws RedMetroException; void borrarCochera (int codigoCochera) throws RedMetroException;
// en caso de que haya relación de fk: borrar la cochera con la conexión del service
// void borrarCochera(Connection conexion, int codigoCochera) throws RedMetroException;
} }
@@ -10,4 +10,6 @@ public interface ITren {
void insertarTren(Tren tren) throws RedMetroException; void insertarTren(Tren tren) throws RedMetroException;
List<Tren> obtenerTodosLosTrenes() throws RedMetroException; List<Tren> obtenerTodosLosTrenes() throws RedMetroException;
// en caso de que haya relación de fk (la cochera tenga trenes)
// void borrarTrenesDeCochera (Connection conexion, int codigoCochera) throws RedMetroException;
} }
@@ -52,7 +52,8 @@ public class CocheraJDBC implements ICochera {
sentencia.setString(2, cochera.getNombre()); sentencia.setString(2, cochera.getNombre());
sentencia.setString(3, cochera.getDireccion()); sentencia.setString(3, cochera.getDireccion());
sentencia.setInt(4, cochera.getDeposito());; sentencia.setInt(4, cochera.getDeposito());;
sentencia.executeUpdate();
sentencia.executeUpdate(); // SOLO PARA INSERTAR UPDATEAR Y BORRAR
} catch (SQLException e) { } catch (SQLException e) {
throw new RedMetroException ("Error al insertar la cochera: " + cochera.getCodigoCochera(), e); throw new RedMetroException ("Error al insertar la cochera: " + cochera.getCodigoCochera(), e);
@@ -95,4 +96,18 @@ public class CocheraJDBC implements ICochera {
} }
} }
// EN ESTE CASO BORRARÍA EL DE ARRIBA
// en caso de que haya relación de fk: borra la cochera con la MISMA conexión que los trenes
// @Override
// public void borrarCochera(Connection conexion, int codigoCochera) throws RedMetroException {
// String consulta = "DELETE FROM T_COCHERA WHERE cod_cochera = ?";
// try (PreparedStatement sentencia = conexion.prepareStatement(consulta)) {
// sentencia.setInt(1, codigoCochera);
// sentencia.executeUpdate();
// } catch (SQLException e) {
// throw new RedMetroException("Error al borrar la cochera " + codigoCochera, e);
// }
// }
} }
@@ -43,7 +43,7 @@ public class LineaJDBC implements ILinea {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RedMetroException("Error al obtener la línea: " + codigoLinea + " por: " + e); throw new RedMetroException("Error al obtener la línea: " + codigoLinea, e);
} }
return lineaEncontrada; return lineaEncontrada;
@@ -91,4 +91,15 @@ public class TrenJDBC implements ITren {
} }
// public void borrarTrenesDeCochera(Connection conexion, int codigoCochera) throws RedMetroException {
// String consulta = "DELETE FROM T_TREN WHERE cod_cochera = ?";
// try (PreparedStatement sentencia = conexion.prepareStatement(consulta)) {
// sentencia.setInt(1, codigoCochera);
// sentencia.executeUpdate();
// } catch (SQLException e) {
// throw new RedMetroException ("Error al borrar los trenes de la cochera", e);
// }
// }
} }
@@ -30,7 +30,7 @@ public class LectorCocherasCSV {
String[] trozos = lineaLeida.split(";"); String[] trozos = lineaLeida.split(";");
int codigo = Integer.parseInt(trozos[0].trim()); int codigoOperacion = Integer.parseInt(trozos[0].trim());
Cochera cocheraLeida = new Cochera(); Cochera cocheraLeida = new Cochera();
cocheraLeida.setCodigoCochera(Integer.parseInt(trozos[1].trim())); cocheraLeida.setCodigoCochera(Integer.parseInt(trozos[1].trim()));
@@ -39,7 +39,20 @@ public class LectorCocherasCSV {
cocheraLeida.setDireccion(trozos[4].trim()); cocheraLeida.setDireccion(trozos[4].trim());
try { try {
cocheraService.procesarOperacion(codigo, cocheraLeida); switch (codigoOperacion) {
case 1:
cocheraService.insertarCochera(cocheraLeida);
break;
case 2:
cocheraService.actualizarCochera(cocheraLeida);
break;
case 3:
cocheraService.borrarCochera(cocheraLeida.getCodigoCochera());
// si hay FK (trenes): cocheraService.borrarCocheraYSusTrenes(cocheraLeida.getCodigoCochera());
break;
default:
break;
}
} catch (RedMetroException e) { } catch (RedMetroException e) {
System.out.println(e.getMessage()); System.out.println(e.getMessage());
} }
@@ -20,5 +20,4 @@ public class GestorConfiguracion {
throw new RedMetroException ("No se puede leer el fichero de configuración: " + e); throw new RedMetroException ("No se puede leer el fichero de configuración: " + e);
} }
} }
} }
@@ -8,34 +8,52 @@ import es.madrid.redmetro.rmr.model.Cochera;
public class CocheraService { public class CocheraService {
private ICochera cocheraDAO = new CocheraJDBC(); private ICochera cocheraDAO = new CocheraJDBC();
// en caso de que haya relación de fk: el service necesita también el dao de tren
// private ITren trenDAO = new TrenJDBC();
public void procesarOperacion(int codigoOperacion, Cochera cochera) throws RedMetroException { public void insertarCochera(Cochera cochera) throws RedMetroException {
Cochera cocheraEnBD = cocheraDAO.obtenerCocheraPorCodigo(cochera.getCodigoCochera());
switch (codigoOperacion) {
case 1:
Cochera cocheraEnBD = cocheraDAO.obtenerCocheraPorCodigo(codigoOperacion);
if (cocheraEnBD == null) { if (cocheraEnBD == null) {
cocheraDAO.insertarCochera(cochera); cocheraDAO.insertarCochera(cochera);
System.out.println("Cochera " + cochera.getCodigoCochera() + " insertada c orrectamente."); System.out.println("Cochera " + cochera.getCodigoCochera() + " insertada c orrectamente.");
} else { } else {
System.out.println("Cochera " + cochera.getCodigoCochera() + " ya existe."); System.out.println("Cochera " + cochera.getCodigoCochera() + " ya existe.");
} }
break; }
case 2: public void actualizarCochera(Cochera cochera) throws RedMetroException {
cocheraDAO.actualizarCochera(cochera); cocheraDAO.actualizarCochera(cochera);
System.out.println("Cochera " + cochera.getCodigoCochera() + " actualizada."); System.out.println("Cocchera " + cochera.getCodigoCochera() + " actualizada");
break;
case 3:
cocheraDAO.borrarCochera(cochera.getCodigoCochera());
System.out.println("Cochera " + cochera.getCodigoCochera() + " borrada.");
break;
default: // default es 0
break;
}
} }
public void borrarCochera (int codigoCochera) throws RedMetroException {
cocheraDAO.borrarCochera(codigoCochera);
System.out.println("Cochera " + codigoCochera + " borrada.");
}
// en caso de haber relación de fk: borra los trenesy la cochera en una transacción
// public void borrarCocheraYSusTrenes(int codigoCochera) throws RedMetroException {
// Connection conexion = null;
// try {
// conexion = GestorConexionBD.getConexion();
// conexion.setAutoCommit(false);
// trenDAO.borrarTrenesDeCochera(conexion, codigoCochera);
// cocheraDAO.borrarCochera(conexion, codigoCochera);
// conexion.commit();
// System.out.println("Cochera " + codigoCochera + " y sus trenes borrados.");
// } catch (SQLException | RedMetroException e) {
// try {
// if (conexion != null) conexion.rollback();
// } catch (SQLException errorRollback) {
// System.out.println("Error al deshacer: " + errorRollback.getMessage());
// }
// throw new RedMetroException("Error al borrar la cochera " + codigoCochera, e);
// } finally {
// try {
// if (conexion != null) conexion.close();
// } catch (SQLException errorCierre) {
// System.out.println("Error al cerrar: " + errorCierre.getMessage());
// }
// }
// }
} }
@@ -12,5 +12,4 @@ public class LineaService {
public Linea obtenerLineaConColor (int codigoLinea) throws RedMetroException { public Linea obtenerLineaConColor (int codigoLinea) throws RedMetroException {
return lineaDAO.obtenerLineaPorCodigo(codigoLinea); return lineaDAO.obtenerLineaPorCodigo(codigoLinea);
} }
} }