|
| 1 | +package unit15.jpa.Actividad15_Aplicacion; |
| 2 | + |
| 3 | +import java.text.ParseException; |
| 4 | +import java.text.SimpleDateFormat; |
| 5 | +import java.util.Date; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Locale; |
| 8 | +import java.util.Scanner; |
| 9 | +import javax.persistence.EntityManager; |
| 10 | +import javax.persistence.EntityManagerFactory; |
| 11 | +import javax.persistence.Persistence; |
| 12 | +import javax.persistence.Query; |
| 13 | + |
| 14 | + |
| 15 | +public class Main { |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + EntityManagerFactory emf |
| 19 | + = Persistence.createEntityManagerFactory("taxistasPU"); |
| 20 | + int opc; |
| 21 | + do { |
| 22 | + System.out.println("1. Alta nuevo taxista."); |
| 23 | + System.out.println("2. Alta nuevo taxi."); |
| 24 | + System.out.println("3. Comienzo jornada taxista."); |
| 25 | + System.out.println("4. Fin jornada taxista."); |
| 26 | + System.out.println("5. Información de un taxista y su taxi."); |
| 27 | + System.out.println("6. Mostrar taxistas trabajando."); |
| 28 | + System.out.println("7. Mostrar taxistas fuera de servicio."); |
| 29 | + System.out.println("9. Salir"); |
| 30 | + opc = new Scanner(System.in).nextInt(); |
| 31 | + |
| 32 | + switch (opc) { |
| 33 | + case 1 -> nuevoTaxista(emf); |
| 34 | + case 2 -> nuevoTaxi(emf); |
| 35 | + case 3 -> comienzoJornadaTaxista(emf); |
| 36 | + case 4 -> finJornada(emf); |
| 37 | + case 5 -> informacionTaxistaTaxi(emf); |
| 38 | + case 6 -> taxistasTrabajando(emf); |
| 39 | + case 7 -> taxistasFueraServicio(emf); |
| 40 | + } |
| 41 | + } while (opc != 9); |
| 42 | + |
| 43 | + emf.close(); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + static void nuevoTaxista(EntityManagerFactory emf) { |
| 48 | + TaxistaDAO dao = new TaxistaDAO(emf); |
| 49 | + |
| 50 | + System.out.println("DNI: "); |
| 51 | + String dni = new Scanner(System.in).nextLine(); |
| 52 | + |
| 53 | + |
| 54 | + if (dao.findTaxista(dni) != null) { |
| 55 | + System.out.println("Ese taxista ya existe."); |
| 56 | + } else { |
| 57 | + |
| 58 | + System.out.println("Nombre: "); |
| 59 | + String nombre = new Scanner(System.in).nextLine(); |
| 60 | + |
| 61 | + System.out.println("Fecha (aaaa/mm/dd): "); |
| 62 | + String fechaTexto = new Scanner(System.in).nextLine(); |
| 63 | + |
| 64 | + SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd"); |
| 65 | + Date fechaNacimiento=null; |
| 66 | + try { |
| 67 | + fechaNacimiento = format.parse(fechaTexto); |
| 68 | + } catch (ParseException ex) { |
| 69 | + System.out.println("Formato de fecha incorrecto"); |
| 70 | + } |
| 71 | + Taxista taxista = new Taxista(dni, nombre, fechaNacimiento); |
| 72 | + |
| 73 | + try { |
| 74 | + dao.create(taxista); |
| 75 | + } catch (Exception ex) { |
| 76 | + System.out.println("Error al insertar taxista"); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + static void nuevoTaxi(EntityManagerFactory emf) { |
| 83 | + TaxiDAO dao = new TaxiDAO(emf); |
| 84 | + |
| 85 | + System.out.println("Matricula: "); |
| 86 | + String matricula = new Scanner(System.in).nextLine(); |
| 87 | + |
| 88 | + if (dao.findTaxi(matricula) != null) { //buscamos el taxi |
| 89 | + System.out.println("Ese taxi ya existe."); |
| 90 | + } else { |
| 91 | + |
| 92 | + System.out.println("Precio: "); |
| 93 | + Double precio = new Scanner(System.in).useLocale(Locale.US).nextDouble(); |
| 94 | + |
| 95 | + System.out.println("Número de plazas: "); |
| 96 | + int plazas = new Scanner(System.in).nextInt(); |
| 97 | + |
| 98 | + |
| 99 | + Taxi taxi = new Taxi(matricula, precio, plazas); |
| 100 | + try { |
| 101 | + dao.create(taxi); |
| 102 | + } catch (Exception ex) { |
| 103 | + System.out.println("Error al insertar taxi."); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + static void informacionTaxistaTaxi(EntityManagerFactory emf) { |
| 110 | + TaxistaDAO dao = new TaxistaDAO(emf); |
| 111 | + |
| 112 | + System.out.println("Dni:"); |
| 113 | + String dni = new Scanner(System.in).nextLine(); |
| 114 | + |
| 115 | + Taxista taxista = dao.findTaxista(dni); |
| 116 | + |
| 117 | + if (taxista == null) { |
| 118 | + System.out.println("El taxista no existe"); |
| 119 | + } else { |
| 120 | + |
| 121 | + Taxi taxi = taxista.getTaxi(); |
| 122 | + |
| 123 | + //mostramos |
| 124 | + System.out.println(taxista); |
| 125 | + if (taxi != null) { |
| 126 | + |
| 127 | + System.out.println(taxi); |
| 128 | + } else { |
| 129 | + |
| 130 | + System.out.println("Fuera de servicio: sin taxi asignado."); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + static void taxistasTrabajando(EntityManagerFactory emf) { |
| 137 | + EntityManager em = emf.createEntityManager(); |
| 138 | + |
| 139 | + Query query = em.createNamedQuery("Taxista.trabajando"); |
| 140 | + List<Taxista> trabajando = query.getResultList(); |
| 141 | + |
| 142 | + for (Taxista t : trabajando) { |
| 143 | + System.out.println(t + " --> " + t.getTaxi()); |
| 144 | + } |
| 145 | + |
| 146 | + em.close(); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | + static void taxistasFueraServicio(EntityManagerFactory emf) { |
| 151 | + EntityManager em = emf.createEntityManager(); |
| 152 | + |
| 153 | + Query query = em.createNamedQuery("Taxista.fueraServicio"); |
| 154 | + List<Taxista> trabajando = query.getResultList(); |
| 155 | + |
| 156 | + for (Taxista t : trabajando) { |
| 157 | + System.out.println(t); |
| 158 | + } |
| 159 | + |
| 160 | + em.close(); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + static void comienzoJornadaTaxista(EntityManagerFactory emf) { |
| 165 | + EntityManager em = emf.createEntityManager(); |
| 166 | + |
| 167 | + TaxistaDAO taxistaDAO = new TaxistaDAO(emf); |
| 168 | + TaxiDAO taxiDAO = new TaxiDAO(emf); |
| 169 | + |
| 170 | + System.out.println("Taxistas fuera de servicio"); |
| 171 | + taxistasFueraServicio(emf); |
| 172 | + |
| 173 | + System.out.println("DNI:"); |
| 174 | + String dni = new Scanner(System.in).nextLine(); |
| 175 | + |
| 176 | + Taxista taxista = taxistaDAO.findTaxista(dni); |
| 177 | + |
| 178 | + if (taxista == null) { |
| 179 | + System.out.println("Ese taxista no existe"); |
| 180 | + } else { |
| 181 | + |
| 182 | + if (taxista.getTaxi() != null) { |
| 183 | + System.out.println("El taxista ya está trabajando."); |
| 184 | + } else { |
| 185 | + |
| 186 | + String jpql = "SELECT taxi FROM Taxi taxi WHERE taxi NOT IN (" |
| 187 | + + "SELECT taxista.taxi FROM Taxista taxista WHERE taxista.taxi IS NOT NULL)"; |
| 188 | + |
| 189 | + Query query = em.createQuery(jpql); |
| 190 | + |
| 191 | + List<Taxi> aparcados = query.getResultList(); |
| 192 | + |
| 193 | + System.out.println("Taxis disponibles:"); |
| 194 | + for (Taxi t : aparcados) { |
| 195 | + System.out.println(t); |
| 196 | + } |
| 197 | + |
| 198 | + System.out.println("Matrícula:"); |
| 199 | + String matricula = new Scanner(System.in).nextLine(); |
| 200 | + |
| 201 | + Taxi taxi = taxiDAO.findTaxi(matricula); |
| 202 | + taxista.setTaxi(taxi); |
| 203 | + try { |
| 204 | + taxistaDAO.edit(taxista); |
| 205 | + } catch (Exception ex) { |
| 206 | + System.out.println("Error al actualizar la BD."); |
| 207 | + System.out.println("Coche asignado"); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + static void finJornada(EntityManagerFactory emf) { |
| 215 | + EntityManager em = emf.createEntityManager(); |
| 216 | + |
| 217 | + TaxistaDAO taxistaDAO = new TaxistaDAO(emf); |
| 218 | + TaxiDAO taxiDAO = new TaxiDAO(emf); |
| 219 | + |
| 220 | + System.out.println("Taxistas trabajando."); |
| 221 | + taxistasTrabajando(emf); |
| 222 | + System.out.println("DNI:"); |
| 223 | + String dni = new Scanner(System.in).nextLine(); |
| 224 | + |
| 225 | + Taxista taxista = taxistaDAO.findTaxista(dni); |
| 226 | + |
| 227 | + if (taxista == null) { |
| 228 | + System.out.println("Ese taxista no existe"); |
| 229 | + } else { |
| 230 | + //vamos a comprobar si ya está trabajando |
| 231 | + if (taxista.getTaxi() == null) { |
| 232 | + System.out.println("El taxista NO está trabajando."); |
| 233 | + } else { |
| 234 | + //Vamos a poner a nulo la relación que tiene con el taxi |
| 235 | + taxista.setTaxi(null); |
| 236 | + try { |
| 237 | + taxistaDAO.edit(taxista); //actualizamos la BD |
| 238 | + } catch (Exception ex) { |
| 239 | + System.out.println("Error al actualizar la BD."); |
| 240 | + } |
| 241 | + System.out.println("Taxista fuera de servicio"); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | +} |
0 commit comments