diff options
author | Josh Walters <josh@joshwalters.com> | 2011-12-14 02:35:57 +0100 |
---|---|---|
committer | Josh Walters <josh@joshwalters.com> | 2011-12-14 02:35:57 +0100 |
commit | b6869036b7839b4540cee1114f44bc5c9b33d9c4 (patch) | |
tree | 8347cd87eb6b156685bf8c6e200c5fc984f07708 /src/com/joshwalters/bookcatalog/AddBook.java | |
parent | 7dd58abb4878c3ff5a5c5cd41a1ee0d3ff2decd3 (diff) | |
download | BookCatalog-b6869036b7839b4540cee1114f44bc5c9b33d9c4.tar.gz BookCatalog-b6869036b7839b4540cee1114f44bc5c9b33d9c4.tar.xz |
Initial commit of code.
Diffstat (limited to 'src/com/joshwalters/bookcatalog/AddBook.java')
-rw-r--r-- | src/com/joshwalters/bookcatalog/AddBook.java | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/com/joshwalters/bookcatalog/AddBook.java b/src/com/joshwalters/bookcatalog/AddBook.java new file mode 100644 index 0000000..05b227c --- /dev/null +++ b/src/com/joshwalters/bookcatalog/AddBook.java @@ -0,0 +1,123 @@ +/* + * Book Catalog - Catalog your book collection. + * + * Copyright (C) 2009 Joshua Walters + * URL: http://joshwalters.com + * + * This file is part of Book Catalog. + * + * Book Catalog is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Book Catalog is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Book Catalog. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.joshwalters.bookcatalog; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.SQLException; + +import javax.swing.JTable; +import javax.swing.JTextField; + +import com.joshwalters.bookcatalog.bookdatabase.BookAlreadyInDatabase; +import com.joshwalters.bookcatalog.bookdatabase.BookDatabase; +import com.joshwalters.bookcatalog.bookdatabase.EmptyDatabase; +import com.joshwalters.bookcatalog.isbnlookup.ISBNLookup; +import com.joshwalters.bookcatalog.isbnlookup.InvalidSearchTerms; +import com.joshwalters.bookcatalog.isbnlookup.NoBookFound; +import com.joshwalters.bookcatalog.sound.PlayWavSoundFile; +import com.joshwalters.bookcatalog.table.BookCatalogTableModel; + +/** + * Adds a book to the book database using an ISBN number. + * + * @author Josh Walters + */ +class AddBook implements ActionListener { + + private ISBNLookup bookLookup = null; + private BookDatabase bookDatabase = null; + private JTable bookCatalogTable = null; + + /** + * Get the different objects needed for the class to function. + * + * @param bookDatabase + * @param bookLookup + * @param bookCatalogTable + */ + public AddBook(BookDatabase bookDatabase, ISBNLookup bookLookup, + JTable bookCatalogTable) { + this.bookDatabase = bookDatabase; + this.bookLookup = bookLookup; + this.bookCatalogTable = bookCatalogTable; + } + + /** + * Add the book to the database using its ISBN number. + */ + public void actionPerformed(ActionEvent arg) { + try { + String barcode = null; + JTextField addBookField = (JTextField) arg.getSource(); + barcode = addBookField.getText(); + addBookField.setText(""); + // Search for the book using its barcode. + bookLookup.searchBooks(barcode); + // Insert the book into the database. + bookDatabase.insertBook(bookLookup.getTitle(), bookLookup + .getAuthor(), bookLookup.getDate(), bookLookup + .getDescription(), bookLookup.getISBN(), bookLookup + .getPrice(), bookLookup.getPublisher(), bookLookup + .getSubject()); + // Play the beep sound to alert the user that the book was added to + // the database. + new PlayWavSoundFile("data/sounds/beep.wav").start(); + try { + // Get the table model of the table. + BookCatalogTableModel bookCatalogTableModel = (BookCatalogTableModel) bookCatalogTable + .getModel(); + // Update the data in the table with the new book. + bookCatalogTableModel.setData(bookDatabase + .getDatabaseForTable()); + // Refresh the table to draw the new info. + bookCatalogTableModel.fireTableDataChanged(); + } catch (EmptyDatabase e) { + // The database was empty, print an error message. + System.err.println(e.getLocalizedMessage()); + } + // The following code selects the newly added row. + int row = bookCatalogTable.getRowCount() - 1; + int col = 4; + // Search for the row, and then select it. + for (int i = 0; i <= row; i++) { + String value = (String) bookCatalogTable.getValueAt(i, col); + if (value.equalsIgnoreCase(bookLookup.getISBN())) { + bookCatalogTable.setRowSelectionInterval(i, i); + } + } + } catch (SQLException e) { + // There was an SQL error of some sort. + System.err.println(e.getLocalizedMessage()); + } catch (NoBookFound e) { + // The book data was not found online, play the error sound. + new PlayWavSoundFile("data/sounds/error.wav").start(); + } catch (BookAlreadyInDatabase e) { + // The book was already in the error database, play the error sound. + new PlayWavSoundFile("data/sounds/beep.wav").start(); + } catch (InvalidSearchTerms e) { + // Invalid search terms, play the error sound. + new PlayWavSoundFile("data/sounds/error.wav").start(); + } + } +} |