Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. public static HashMap<String, Libros> CargarColeccion(File fichero) {
  2.  
  3. HashMap <String, Libros> biblio = null;
  4. FileInputStream fis = null;
  5. ObjectInputStream ois = null;
  6. try {
  7.  
  8. fis = new FileInputStream(fichero);
  9. ois = new ObjectInputStream(fis);
  10. biblio = (HashMap<String, Libros>) ois.readObject();
  11.  
  12. }catch (ClassNotFoundException | FileNotFoundException e) { // Contemplamos excepción si no se encuentran la clase o el fichero.
  13. } catch (IOException e) { // Contemplamos excepción si hay error en la entrada o salida de datos.
  14. } catch (Exception e) { // Contemplamos excepción genérica.
  15. }finally {
  16. if (ois != null) {
  17. try {
  18. ois.close();
  19. fis.close();
  20. } catch (IOException ex) {
  21. System.out.println("Ha ocurrido una IOException");
  22. }
  23. }
  24.  
  25. }
  26.  
  27. return biblio;
  28. }
  29.  
  30. static File fichero = new File("libros.dat");
  31. //Creamos el HashMap
  32. HashMap<String, Libros> biblio = Metodos.CargarColeccion(fichero);
  33.  
  34. //Botón aceptar de dar de alta libro, que cuando se pulsa se añade la información al hashmap
  35. private void btnAceptarAltaActionPerformed(java.awt.event.ActionEvent evt) {
  36.  
  37. //guardamos los datos
  38. if (this.textCodigo == null
  39. || this.textTitulo == null
  40. || this.textAutor == null
  41. || this.fechaDia == null
  42. || this.fechaMes == null
  43. || this.FechaAno == null
  44. || (String) this.Lista.getSelectedItem() == "..."
  45. || (this.radioSi.isSelected() == false && this.radioNo.isSelected() == false)) {
  46. JOptionPane.showMessageDialog(this.ventanaAlta, "Debe rellenar todos los campos.", "Alta de libro fallida", JOptionPane.ERROR_MESSAGE);
  47.  
  48. }
  49. else{
  50. //tranformación de text a integer
  51. int fechaDia = Integer.parseInt(this.fechaDia.getText());
  52. int fechaMes = Integer.parseInt(this.fechaMes.getText());
  53. int fechaAno = Integer.parseInt(this.FechaAno.getText());
  54.  
  55.  
  56. String codigo = this.textCodigo.getText();
  57. String titulo = this.textTitulo.getText();
  58. String autor = this.textAutor.getText();
  59. Calendar fecha = new GregorianCalendar(fechaAno, fechaMes, fechaDia);
  60. String paginas = (String) this.Lista.getSelectedItem();
  61. boolean libroPremiado = this.radioSi.isSelected();
  62. Libros libro = new Libros(codigo,titulo, autor, fecha, paginas, libroPremiado);
  63. System.out.println(fecha);
  64. System.out.println(libro.toString());
  65. biblio.put(codigo, libro);
  66.  
  67. this.textCodigo.setText(null);
  68. this.textTitulo.setText(null);
  69. this.textAutor.setText(null);
  70. this.fechaDia.setText(null);
  71. this.fechaMes.setText(null);
  72. this.FechaAno.setText(null);
  73. this.Lista.setSelectedItem(0);
  74. this.grupo_premiado.clearSelection();
  75.  
  76. this.ventanaAlta.setVisible(false);
  77. this.Principal.setVisible(true);
  78.  
  79. }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement