|
| 1 | +packageunit15.jpa.Actividad15_Aplicacion; |
| 2 | + |
| 3 | +importjava.text.ParseException; |
| 4 | +importjava.text.SimpleDateFormat; |
| 5 | +importjava.util.Date; |
| 6 | +importjava.util.List; |
| 7 | +importjava.util.Locale; |
| 8 | +importjava.util.Scanner; |
| 9 | +importjavax.persistence.EntityManager; |
| 10 | +importjavax.persistence.EntityManagerFactory; |
| 11 | +importjavax.persistence.Persistence; |
| 12 | +importjavax.persistence.Query; |
| 13 | + |
| 14 | + |
| 15 | +publicclassMain { |
| 16 | + |
| 17 | +publicstaticvoidmain(String[]args) { |
| 18 | +EntityManagerFactoryemf |
| 19 | + =Persistence.createEntityManagerFactory("taxistasPU"); |
| 20 | +intopc; |
| 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 =newScanner(System.in).nextInt(); |
| 31 | + |
| 32 | +switch (opc) { |
| 33 | +case1 ->nuevoTaxista(emf); |
| 34 | +case2 ->nuevoTaxi(emf); |
| 35 | +case3 ->comienzoJornadaTaxista(emf); |
| 36 | +case4 ->finJornada(emf); |
| 37 | +case5 ->informacionTaxistaTaxi(emf); |
| 38 | +case6 ->taxistasTrabajando(emf); |
| 39 | +case7 ->taxistasFueraServicio(emf); |
| 40 | + } |
| 41 | + }while (opc !=9); |
| 42 | + |
| 43 | +emf.close(); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +staticvoidnuevoTaxista(EntityManagerFactoryemf) { |
| 48 | +TaxistaDAOdao =newTaxistaDAO(emf); |
| 49 | + |
| 50 | +System.out.println("DNI: "); |
| 51 | +Stringdni =newScanner(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 | +Stringnombre =newScanner(System.in).nextLine(); |
| 60 | + |
| 61 | +System.out.println("Fecha (aaaa/mm/dd): "); |
| 62 | +StringfechaTexto =newScanner(System.in).nextLine(); |
| 63 | + |
| 64 | +SimpleDateFormatformat =newSimpleDateFormat("yyyy/MM/dd"); |
| 65 | +DatefechaNacimiento=null; |
| 66 | +try { |
| 67 | +fechaNacimiento =format.parse(fechaTexto); |
| 68 | + }catch (ParseExceptionex) { |
| 69 | +System.out.println("Formato de fecha incorrecto"); |
| 70 | + } |
| 71 | +Taxistataxista =newTaxista(dni,nombre,fechaNacimiento); |
| 72 | + |
| 73 | +try { |
| 74 | +dao.create(taxista); |
| 75 | + }catch (Exceptionex) { |
| 76 | +System.out.println("Error al insertar taxista"); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | +staticvoidnuevoTaxi(EntityManagerFactoryemf) { |
| 83 | +TaxiDAOdao =newTaxiDAO(emf); |
| 84 | + |
| 85 | +System.out.println("Matricula: "); |
| 86 | +Stringmatricula =newScanner(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 | +Doubleprecio =newScanner(System.in).useLocale(Locale.US).nextDouble(); |
| 94 | + |
| 95 | +System.out.println("Número de plazas: "); |
| 96 | +intplazas =newScanner(System.in).nextInt(); |
| 97 | + |
| 98 | + |
| 99 | +Taxitaxi =newTaxi(matricula,precio,plazas); |
| 100 | +try { |
| 101 | +dao.create(taxi); |
| 102 | + }catch (Exceptionex) { |
| 103 | +System.out.println("Error al insertar taxi."); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +staticvoidinformacionTaxistaTaxi(EntityManagerFactoryemf) { |
| 110 | +TaxistaDAOdao =newTaxistaDAO(emf); |
| 111 | + |
| 112 | +System.out.println("Dni:"); |
| 113 | +Stringdni =newScanner(System.in).nextLine(); |
| 114 | + |
| 115 | +Taxistataxista =dao.findTaxista(dni); |
| 116 | + |
| 117 | +if (taxista ==null) { |
| 118 | +System.out.println("El taxista no existe"); |
| 119 | + }else { |
| 120 | + |
| 121 | +Taxitaxi =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 | +staticvoidtaxistasTrabajando(EntityManagerFactoryemf) { |
| 137 | +EntityManagerem =emf.createEntityManager(); |
| 138 | + |
| 139 | +Queryquery =em.createNamedQuery("Taxista.trabajando"); |
| 140 | +List<Taxista>trabajando =query.getResultList(); |
| 141 | + |
| 142 | +for (Taxistat :trabajando) { |
| 143 | +System.out.println(t +" --> " +t.getTaxi()); |
| 144 | + } |
| 145 | + |
| 146 | +em.close(); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | +staticvoidtaxistasFueraServicio(EntityManagerFactoryemf) { |
| 151 | +EntityManagerem =emf.createEntityManager(); |
| 152 | + |
| 153 | +Queryquery =em.createNamedQuery("Taxista.fueraServicio"); |
| 154 | +List<Taxista>trabajando =query.getResultList(); |
| 155 | + |
| 156 | +for (Taxistat :trabajando) { |
| 157 | +System.out.println(t); |
| 158 | + } |
| 159 | + |
| 160 | +em.close(); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | +staticvoidcomienzoJornadaTaxista(EntityManagerFactoryemf) { |
| 165 | +EntityManagerem =emf.createEntityManager(); |
| 166 | + |
| 167 | +TaxistaDAOtaxistaDAO =newTaxistaDAO(emf); |
| 168 | +TaxiDAOtaxiDAO =newTaxiDAO(emf); |
| 169 | + |
| 170 | +System.out.println("Taxistas fuera de servicio"); |
| 171 | +taxistasFueraServicio(emf); |
| 172 | + |
| 173 | +System.out.println("DNI:"); |
| 174 | +Stringdni =newScanner(System.in).nextLine(); |
| 175 | + |
| 176 | +Taxistataxista =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 | +Stringjpql ="SELECT taxi FROM Taxi taxi WHERE taxi NOT IN (" |
| 187 | + +"SELECT taxista.taxi FROM Taxista taxista WHERE taxista.taxi IS NOT NULL)"; |
| 188 | + |
| 189 | +Queryquery =em.createQuery(jpql); |
| 190 | + |
| 191 | +List<Taxi>aparcados =query.getResultList(); |
| 192 | + |
| 193 | +System.out.println("Taxis disponibles:"); |
| 194 | +for (Taxit :aparcados) { |
| 195 | +System.out.println(t); |
| 196 | + } |
| 197 | + |
| 198 | +System.out.println("Matrícula:"); |
| 199 | +Stringmatricula =newScanner(System.in).nextLine(); |
| 200 | + |
| 201 | +Taxitaxi =taxiDAO.findTaxi(matricula); |
| 202 | +taxista.setTaxi(taxi); |
| 203 | +try { |
| 204 | +taxistaDAO.edit(taxista); |
| 205 | + }catch (Exceptionex) { |
| 206 | +System.out.println("Error al actualizar la BD."); |
| 207 | +System.out.println("Coche asignado"); |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | +staticvoidfinJornada(EntityManagerFactoryemf) { |
| 215 | +EntityManagerem =emf.createEntityManager(); |
| 216 | + |
| 217 | +TaxistaDAOtaxistaDAO =newTaxistaDAO(emf); |
| 218 | +TaxiDAOtaxiDAO =newTaxiDAO(emf); |
| 219 | + |
| 220 | +System.out.println("Taxistas trabajando."); |
| 221 | +taxistasTrabajando(emf); |
| 222 | +System.out.println("DNI:"); |
| 223 | +Stringdni =newScanner(System.in).nextLine(); |
| 224 | + |
| 225 | +Taxistataxista =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 (Exceptionex) { |
| 239 | +System.out.println("Error al actualizar la BD."); |
| 240 | + } |
| 241 | +System.out.println("Taxista fuera de servicio"); |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | +} |