Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static HashMap<String, Libros> CargarColeccion(File fichero) {
- HashMap <String, Libros> biblio = null;
- FileInputStream fis = null;
- ObjectInputStream ois = null;
- try {
- fis = new FileInputStream(fichero);
- ois = new ObjectInputStream(fis);
- biblio = (HashMap<String, Libros>) ois.readObject();
- }catch (ClassNotFoundException | FileNotFoundException e) { // Contemplamos excepción si no se encuentran la clase o el fichero.
- } catch (IOException e) { // Contemplamos excepción si hay error en la entrada o salida de datos.
- } catch (Exception e) { // Contemplamos excepción genérica.
- }finally {
- if (ois != null) {
- try {
- ois.close();
- fis.close();
- } catch (IOException ex) {
- System.out.println("Ha ocurrido una IOException");
- }
- }
- }
- return biblio;
- }
- static File fichero = new File("libros.dat");
- //Creamos el HashMap
- HashMap<String, Libros> biblio = Metodos.CargarColeccion(fichero);
- //Botón aceptar de dar de alta libro, que cuando se pulsa se añade la información al hashmap
- private void btnAceptarAltaActionPerformed(java.awt.event.ActionEvent evt) {
- //guardamos los datos
- if (this.textCodigo == null
- || this.textTitulo == null
- || this.textAutor == null
- || this.fechaDia == null
- || this.fechaMes == null
- || this.FechaAno == null
- || (String) this.Lista.getSelectedItem() == "..."
- || (this.radioSi.isSelected() == false && this.radioNo.isSelected() == false)) {
- JOptionPane.showMessageDialog(this.ventanaAlta, "Debe rellenar todos los campos.", "Alta de libro fallida", JOptionPane.ERROR_MESSAGE);
- }
- else{
- //tranformación de text a integer
- int fechaDia = Integer.parseInt(this.fechaDia.getText());
- int fechaMes = Integer.parseInt(this.fechaMes.getText());
- int fechaAno = Integer.parseInt(this.FechaAno.getText());
- String codigo = this.textCodigo.getText();
- String titulo = this.textTitulo.getText();
- String autor = this.textAutor.getText();
- Calendar fecha = new GregorianCalendar(fechaAno, fechaMes, fechaDia);
- String paginas = (String) this.Lista.getSelectedItem();
- boolean libroPremiado = this.radioSi.isSelected();
- Libros libro = new Libros(codigo,titulo, autor, fecha, paginas, libroPremiado);
- System.out.println(fecha);
- System.out.println(libro.toString());
- biblio.put(codigo, libro);
- this.textCodigo.setText(null);
- this.textTitulo.setText(null);
- this.textAutor.setText(null);
- this.fechaDia.setText(null);
- this.fechaMes.setText(null);
- this.FechaAno.setText(null);
- this.Lista.setSelectedItem(0);
- this.grupo_premiado.clearSelection();
- this.ventanaAlta.setVisible(false);
- this.Principal.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement