summaryrefslogtreecommitdiffstats
path: root/src/com/joshwalters/bookcatalog/AddBook.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/joshwalters/bookcatalog/AddBook.java')
-rw-r--r--src/com/joshwalters/bookcatalog/AddBook.java123
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();
+ }
+ }
+}