MODIFICACIÓN - Decisión en el lector CSV
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
1;1;1;PUEEEEEEEEEEERTA DE ARGANDA;CARRETERA M-203 KM 2,300 (CAMINO DE VALLECAS A MEJORADA)
|
1;1;1;PUEEEEEEEEEEERTA DE ARGANDA;CARRETERA M-203 KM 2,300 (CAMINO DE VALLECAS A MEJORADA)
|
||||||
1;2;1;UNIVERSIDAD REY JUAN CARLOS;AVENIDA ALCALDE DE MÓSTOLES Nº 35
|
1;2;1;UNIVERSIDAD REY JUAN CARLOS;AVENIDA ALCALDE DE MÓSTOLES Nº 35
|
||||||
1;3;1;EL BERCIAL;PRÓXIMO A CARRETERA DE TURQUÍA (BARRIO DE EL BERCIAL)
|
1;3;1;EL BERCIAL;PRÓXIMO A CARRETERA DE TURQUÍA (BARRIO DE EL BERCIAL)
|
||||||
1;4;1;ARGANZUELA - PLANETARIO;C/ BRONCE Nº 1
|
1;4;1; ARGANZUELA - PLANETARIO;C/ BRONCE Nº 1
|
||||||
1;5;1;CIUDAD UNIVERSITARIA;PLAZA RAMÓN Y CAJAL
|
1;5;1;CIUDAD UNIVERSITARIA;PLAZA RAMÓN Y CAJAL
|
||||||
1;6;1;MIGUEL HERNANDEZ;AVENIDA DE LA ALBUFERA ESQ. CALLE RAFAEL ALBERTI
|
1;6;1;MIGUEL HERNANDEZ;AVENIDA DE LA ALBUFERA ESQ. CALLE RAFAEL ALBERTI
|
||||||
0;7;1;ARGÜELLES;C/ PRINCESA Nº 56
|
0;7;1;ARGÜELLES;C/ PRINCESA Nº 56
|
||||||
@@ -12,14 +12,14 @@
|
|||||||
0;11;0;CUATRO CAMINOS;VIRGEN DE NIEVA S/N
|
0;11;0;CUATRO CAMINOS;VIRGEN DE NIEVA S/N
|
||||||
1;12;0;VENTAS;ALMERÍA 6
|
1;12;0;VENTAS;ALMERÍA 6
|
||||||
1;13;0;PLAZA DE CASTILLA;SAN BENITO S/N
|
1;13;0;PLAZA DE CASTILLA;SAN BENITO S/N
|
||||||
1;14;0;CANILLEJAS;NECTAR S/N
|
1;14;0 ;CANILLEJAS;NECTAR S/N
|
||||||
1;15;0;ALUCHE;AVDA. DE LOS POBLADOS Nº 11
|
1;15;0;ALUCHE;AVDA. DE LOS POBLADOS Nº 11
|
||||||
1;16;0;FUENCARRAL;AVENIDA CAMPO DE CALATRAVA Nº 86
|
1;16;0;FUENCARRAL;AVENIDA CAMPO DE CALATRAVA Nº 86
|
||||||
0;17;0;SACEDAL;VENTISQUERO DE LA CONDESA S/N
|
0;17;0;SACEDAL;VENTISQUERO DE LA CONDESA S/N
|
||||||
1;18;0;LAGUNA;GOTAREDURA Nº 3
|
1;18;0;LAGUNA;GOTAREDURA Nº 3
|
||||||
1;19;0;HORTALEZA;C/ MANUEL AZAÑA
|
1;19;0;HORTALEZA;C/ MANUEL AZAÑA
|
||||||
1;20;0;CUATRO VIENTOS CUATRO;CRTA. DE CANALEJAS y DE QUIEN LO MATO S/N
|
1;20;0;CUATRO VIENTOS CUATRO;CRTA. DE CANALEJAS y DE QUIEN LO MATO S/N
|
||||||
1;21;0;LORANCA;AVDA. DE LA ALEGRÍA (FUENLABRADA)
|
1;21;0;LORANCA; AVDA. DE LA ALEGRÍA (FUENLABRADA)
|
||||||
1;22;0;VALDECARROS;AVENIDA CERRO MILANO 0103 ENSANCHE DE VALLECAS, 73
|
1;22;0;VALDECARROS;AVENIDA CERRO MILANO 0103 ENSANCHE DE VALLECAS, 73
|
||||||
1;23;0;VILLAVERDE;AVENIDA REAL DE PINTO
|
1;23;0;VILLAVERDE;AVENIDA REAL DE PINTO
|
||||||
2;1;1;PUERTA DE ARGANDA;CARRETERA M-203 KM 2,300 (CAMINO DE VALLECAS A MEJORADA)
|
2;1;1;PUERTA DE ARGANDA;CARRETERA M-203 KM 2,300 (CAMINO DE VALLECAS A MEJORADA)
|
||||||
|
|||||||
|
@@ -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:
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,4 +7,4 @@ public interface ILinea {
|
|||||||
|
|
||||||
Linea obtenerLineaPorCodigo (int codigoLinea) throws RedMetroException;
|
Linea obtenerLineaPorCodigo (int codigoLinea) 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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -18,11 +18,11 @@ public class LineaJDBC implements ILinea {
|
|||||||
|
|
||||||
Linea lineaEncontrada = null;
|
Linea lineaEncontrada = null;
|
||||||
String consulta = "SELECT l.cod_linea, l.nombre_corto, l.nombre_largo, l.kilometros, "
|
String consulta = "SELECT l.cod_linea, l.nombre_corto, l.nombre_largo, l.kilometros, "
|
||||||
+ "c.cod_color, c.nombre, c.cod_hexadecimal "
|
+ "c.cod_color, c.nombre, c.cod_hexadecimal "
|
||||||
+ "FROM T_LINEA l JOIN T_COLOR c ON l.cod_color = c.cod_color " + "WHERE l.cod_linea = ?";
|
+ "FROM T_LINEA l JOIN T_COLOR c ON l.cod_color = c.cod_color " + "WHERE l.cod_linea = ?";
|
||||||
|
|
||||||
try (Connection conexion = GestorConexionBD.getConexion();
|
try (Connection conexion = GestorConexionBD.getConexion();
|
||||||
PreparedStatement sentencia = conexion.prepareStatement(consulta)) {
|
PreparedStatement sentencia = conexion.prepareStatement(consulta)) {
|
||||||
|
|
||||||
sentencia.setInt(1, codigoLinea);
|
sentencia.setInt(1, codigoLinea);
|
||||||
ResultSet resultado = sentencia.executeQuery();
|
ResultSet resultado = sentencia.executeQuery();
|
||||||
@@ -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);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ import es.madrid.redmetro.rmr.model.Cochera;
|
|||||||
import es.madrid.redmetro.rmr.service.CocheraService;
|
import es.madrid.redmetro.rmr.service.CocheraService;
|
||||||
|
|
||||||
public class LectorCocherasCSV {
|
public class LectorCocherasCSV {
|
||||||
|
|
||||||
private CocheraService cocheraService = new CocheraService();
|
private CocheraService cocheraService = new CocheraService();
|
||||||
|
|
||||||
public void tratarFichero() throws RedMetroException {
|
public void tratarFichero() throws RedMetroException {
|
||||||
String rutaFichero = GestorConfiguracion.obtenerValor("fichero.cocheras");
|
String rutaFichero = GestorConfiguracion.obtenerValor("fichero.cocheras");
|
||||||
|
|
||||||
@@ -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());
|
||||||
}
|
}
|
||||||
@@ -50,4 +63,4 @@ public class LectorCocherasCSV {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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) {
|
if (cocheraEnBD == null) {
|
||||||
|
cocheraDAO.insertarCochera(cochera);
|
||||||
case 1:
|
System.out.println("Cochera " + cochera.getCodigoCochera() + " insertada c orrectamente.");
|
||||||
Cochera cocheraEnBD = cocheraDAO.obtenerCocheraPorCodigo(codigoOperacion);
|
} else {
|
||||||
if (cocheraEnBD == null) {
|
System.out.println("Cochera " + cochera.getCodigoCochera() + " ya existe.");
|
||||||
cocheraDAO.insertarCochera(cochera);
|
|
||||||
System.out.println("Cochera " + cochera.getCodigoCochera()+ " insertada correctamente.");
|
|
||||||
} else {
|
|
||||||
System.out.println("Cochera " + cochera.getCodigoCochera() + " ya existe.");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
cocheraDAO.actualizarCochera(cochera);
|
|
||||||
System.out.println("Cochera " + 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 actualizarCochera(Cochera cochera) throws RedMetroException {
|
||||||
|
cocheraDAO.actualizarCochera(cochera);
|
||||||
|
System.out.println("Cocchera " + cochera.getCodigoCochera() + " actualizada");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user