From fa5c2eec2472838464a1017d068329f48ab5d6c3 Mon Sep 17 00:00:00 2001 From: Agnieszka Janicka Date: Thu, 9 Jun 2016 22:19:01 +0200 Subject: [PATCH] Input Verifier wszystko okej --- biblioteka.db | Bin 5120 -> 5120 bytes src/library/AlbumFieldPattern.form | 21 +------ src/library/AlbumFieldPattern.java | 35 +++++------- src/library/Book.java | 10 +++- src/library/BookFieldPattern.form | 39 +++++-------- src/library/BookFieldPattern.java | 71 +++++++++++++---------- src/library/Database.java | 89 +++++++++++++++-------------- src/library/ListResult.form | 11 +++- src/library/ListResult.java | 2 + src/library/MainMenu.java | 5 +- src/library/MovieFieldPattern.java | 11 +++- src/library/Validation.java | 7 ++- 12 files changed, 149 insertions(+), 152 deletions(-) diff --git a/biblioteka.db b/biblioteka.db index 67eb5aeabeb367081b57a353da7c608b5faee735..5ea8ff9068462e9beeabc266ca0c2f9691e7b17b 100644 GIT binary patch delta 111 zcmZqBXwaA-&B!}Z#+j9uLH8N=#*_ujT+GZ449o|Z>zN%k3$hq7+uJg8GRRBIi!!Oo zhi9gy9Nm?hndg|7QdE_i6`WX*onKy@o#|ebn$CXJ$k0?52#t&lbxi~E%QI7t&OXw< Oxt_g&g^_s?4+j8`5Fn%g delta 45 zcmZqBXwaA-&B!xR#+j9eLHB3J#*_uj98Aph49o|Z>o*H>6f - - - - - + @@ -86,11 +82,8 @@ - - - - - + + @@ -193,13 +186,5 @@ - - - - - - - - diff --git a/src/library/AlbumFieldPattern.java b/src/library/AlbumFieldPattern.java index c828364..c8401f6 100644 --- a/src/library/AlbumFieldPattern.java +++ b/src/library/AlbumFieldPattern.java @@ -19,13 +19,22 @@ public class AlbumFieldPattern extends javax.swing.JFrame { * Creates new form bookFieldPattern */ public AlbumFieldPattern() { - this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); initComponents(); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public AlbumFieldPattern(boolean required) { initComponents(); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + Validation verify; + if (required) { + verify = new Validation(true); + } else { + verify = new Validation(); + } + albumTitle.setInputVerifier(verify); + musican.setInputVerifier(verify); + year.setInputVerifier(verify); } public boolean add = false, delete = false, edit = false, search = false; @@ -48,7 +57,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame { Category = new javax.swing.JComboBox<>(); mainlabel = new javax.swing.JLabel(); SaveChanges = new javax.swing.JButton(); - cancel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -94,13 +102,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame { } }); - cancel.setText("Anuluj"); - cancel.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - cancelActionPerformed(evt); - } - }); - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -125,10 +126,7 @@ public class AlbumFieldPattern extends javax.swing.JFrame { .addComponent(lcategory, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addComponent(SaveChanges, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(37, 37, 37) - .addComponent(cancel)) + .addComponent(SaveChanges, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Category, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addContainerGap(26, Short.MAX_VALUE)) ); @@ -154,10 +152,8 @@ public class AlbumFieldPattern extends javax.swing.JFrame { .addComponent(lcategory) .addComponent(Category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(SaveChanges) - .addComponent(cancel)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addComponent(SaveChanges) + .addContainerGap(14, Short.MAX_VALUE)) ); pack(); @@ -192,10 +188,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame { // TODO add your handling code here: }//GEN-LAST:event_yearActionPerformed - private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed - this.dispose(); - }//GEN-LAST:event_cancelActionPerformed - /** * @param args the command line arguments */ @@ -240,7 +232,6 @@ public class AlbumFieldPattern extends javax.swing.JFrame { private javax.swing.JButton SaveChanges; private javax.swing.JLabel Title; private javax.swing.JTextField albumTitle; - private javax.swing.JButton cancel; private javax.swing.JLabel lcategory; private javax.swing.JLabel lmusican; private javax.swing.JLabel lyear; diff --git a/src/library/Book.java b/src/library/Book.java index 3d745ae..a58cc17 100644 --- a/src/library/Book.java +++ b/src/library/Book.java @@ -22,12 +22,18 @@ public class Book extends Item { //nothing to do } - Book(int id, String n, String an, String asn, int y, String isbn, String p, String c) { + Book(int id, String n, String an, String asn, String y, String isbn, String p, String c) { this.setId(id); this.setName(n); this.authorName = an; this.authorSurname = asn; - this.setYear(y); + try{ + this.setYear(Integer.parseInt(y)); + } + catch(NumberFormatException e){ + this.setYear(0); + } + this.setCategory(c); this.ISBN = isbn; this.publishing = p; diff --git a/src/library/BookFieldPattern.form b/src/library/BookFieldPattern.form index cdaa58b..ac45a4c 100644 --- a/src/library/BookFieldPattern.form +++ b/src/library/BookFieldPattern.form @@ -56,10 +56,7 @@ - - - - + @@ -113,11 +110,8 @@ - - - - - + + @@ -242,15 +236,16 @@ - - - - - - - - - + + + + + + + + + + @@ -295,13 +290,5 @@ - - - - - - - - diff --git a/src/library/BookFieldPattern.java b/src/library/BookFieldPattern.java index 32a3e08..67a8293 100644 --- a/src/library/BookFieldPattern.java +++ b/src/library/BookFieldPattern.java @@ -7,6 +7,8 @@ package library; import java.sql.SQLException; import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; @@ -18,20 +20,27 @@ import javax.swing.JOptionPane; */ public class BookFieldPattern extends javax.swing.JFrame { - /** - * Creates new form bookFieldPattern - */ + public boolean add = false, delete = false, edit = false, search = false; + private Database d = new Database(); + public BookFieldPattern() { initComponents(); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + Validation verify = new Validation(); year.setInputVerifier(verify); ISBN.setInputVerifier(verify); - + } public BookFieldPattern(boolean required) { initComponents(); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + Validation verify; + if (required) { + verify = new Validation(true); + } else { + verify = new Validation(); + } bookTitle.setInputVerifier(verify); authorName.setInputVerifier(verify); authorSurname.setInputVerifier(verify); @@ -40,9 +49,6 @@ public class BookFieldPattern extends javax.swing.JFrame { publishing.setInputVerifier(verify); } - public boolean add = false, delete = false, edit = false, search = false; - private Validation verify = new Validation(); - /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always @@ -69,7 +75,6 @@ public class BookFieldPattern extends javax.swing.JFrame { mainlabel = new javax.swing.JLabel(); SaveChanges = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); - cancel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); @@ -132,7 +137,7 @@ public class BookFieldPattern extends javax.swing.JFrame { lcategory.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lcategory.setText("Kategoria:"); - Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Przygodowe", "Naukowe", "Powieść", "Poezja", "Kryminał", "Dla dzieci", "Psychologiczne", "Podręczniki" })); + Category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { " ", "Przygodowe", "Naukowe", "Powieść", "Poezja", "Kryminał", "Dla dzieci", "Psychologiczne", "Podręczniki" })); Category.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CategoryActionPerformed(evt); @@ -157,13 +162,6 @@ public class BookFieldPattern extends javax.swing.JFrame { jLabel1.setDoubleBuffered(true); jLabel1.setName(""); // NOI18N - cancel.setText("Anuluj"); - cancel.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - cancelActionPerformed(evt); - } - }); - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( @@ -196,9 +194,7 @@ public class BookFieldPattern extends javax.swing.JFrame { .addComponent(Category, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(SaveChanges, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel1) - .addComponent(cancel)) + .addComponent(jLabel1) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) @@ -240,10 +236,8 @@ public class BookFieldPattern extends javax.swing.JFrame { .addComponent(lcategory) .addComponent(Category, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(SaveChanges) - .addComponent(cancel)) - .addContainerGap(32, Short.MAX_VALUE)) + .addComponent(SaveChanges) + .addContainerGap(33, Short.MAX_VALUE)) ); jLabel1.setVisible(false); @@ -263,18 +257,38 @@ public class BookFieldPattern extends javax.swing.JFrame { private void SaveChangesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveChangesActionPerformed if (this.add) { try { - Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), Integer.parseInt(year.getText()), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString()); + Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), year.getText(), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString()); b.insertToDB(); } catch (SQLException e) { e.getMessage(); } } else if (this.edit) { - JOptionPane.showMessageDialog(null, "EDYTUJ"); + try { + Book b = new Book(0, bookTitle.getText(), authorName.getText(), authorSurname.getText(), year.getText(), ISBN.getText(), publishing.getText(), Category.getSelectedItem().toString()); + List books = new LinkedList<>(); + //books = d.selectBooks(b); + d.selectBooks(b); + /*Object[][] data = d.convertToTable(books); + String[] columnNames = {"ID", + "Tytuł", + "Imię Autora", + "Nazwisko Autora", + "Rok", + "ISBN", + "Wydawnictwo", + "Kategoria"}; + ListResult table = new ListResult(data, columnNames); + table.setVisible(true);*/ + } catch (IndexOutOfBoundsException e) { + //nothing to do here + } + // ListResult l = new ListResult(); + } else if (this.delete) { JOptionPane.showMessageDialog(null, "USUŃ"); } else if (this.search) { - + } dispose(); }//GEN-LAST:event_SaveChangesActionPerformed @@ -299,10 +313,6 @@ public class BookFieldPattern extends javax.swing.JFrame { jLabel1.setVisible(false); }//GEN-LAST:event_ISBNFocusLost - private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed - this.dispose(); - }//GEN-LAST:event_cancelActionPerformed - /** * @param args the command line arguments */ @@ -350,7 +360,6 @@ public class BookFieldPattern extends javax.swing.JFrame { private javax.swing.JLabel authorname; private javax.swing.JLabel authorsurname; javax.swing.JTextField bookTitle; - private javax.swing.JButton cancel; private javax.swing.JLabel jLabel1; private javax.swing.JLabel lISBN; private javax.swing.JLabel lcategory; diff --git a/src/library/Database.java b/src/library/Database.java index 97d97b2..4ccc700 100644 --- a/src/library/Database.java +++ b/src/library/Database.java @@ -135,11 +135,11 @@ public class Database { List books = new ArrayList(); try { ResultSet result = stat.executeQuery("SELECT * FROM books"); - int id, year; - String name, authorName, authorSurname, isbn, category, publishing; + int id; + String name, authorName, authorSurname, isbn, category, publishing, year; while (result.next()) { id = result.getInt("id"); - year = result.getInt("year"); + year = result.getString("year"); name = result.getString("name"); authorName = result.getString("author_name"); authorSurname = result.getString("author_surname"); @@ -154,48 +154,51 @@ public class Database { return books; } - public List selectBooks(Book searchData) { + //public List selectBooks(Book searchData) { + public void selectBooks(Book searchData) { List books = new ArrayList<>(); - try { - List wheres = new ArrayList<>(); - if (searchData.getName().length() > 0) { - wheres.add("WHERE name LIKE " + searchData.getName()); - System.out.println("WHERE name LIKE " + searchData.getName()); - } - if (searchData.getAuthorName().length() > 0) { - wheres.add("WHERE author_name LIKE " + searchData.getAuthorName()); - System.out.println("WHERE author_name LIKE " + searchData.getAuthorName()); - } - if (searchData.getPublishing().length() > 0) { - wheres.add("WHERE publishing LIKE " + searchData.getPublishing()); - System.out.println("WHERE publishing LIKE " + searchData.getPublishing()); - } - if (searchData.getYear() > 0) { - wheres.add("WHERE year = " + searchData.getYear()); - System.out.println("WHERE year = " + searchData.getYear()); - } - if (searchData.getName().length() > 0) { - wheres.add("WHERE name LIKE " + searchData.getName()); - System.out.println("WHERE name LIKE " + searchData.getName()); - } - if (searchData.getName().length() > 0) { - wheres.add("WHERE name LIKE " + searchData.getName()); - System.out.println("WHERE name LIKE " + searchData.getName()); - } - if (searchData.getISBN().length() > 0) { - wheres.add("WHERE isbn LIKE " + searchData.getISBN()); - System.out.println("WHERE isbn LIKE " + searchData.getISBN()); - } - if (searchData.getCategory().length() > 0) { - wheres.add("WHERE category LIKE " + searchData.getCategory()); - System.out.println("WHERE category LIKE " + searchData.getCategory()); - } + boolean EmptyAll = true; + //try { + List wheres = new ArrayList<>(); + if (searchData.getName().length() > 0) { + wheres.add("WHERE name LIKE '" + searchData.getName() + "'"); + System.out.println("WHERE name LIKE '" + searchData.getName() + "'"); + EmptyAll = false; + } + if (searchData.getAuthorName().length() > 0) { + wheres.add("WHERE author_name LIKE '" + searchData.getAuthorName() + "'"); + System.out.println("WHERE author_name LIKE '" + searchData.getAuthorName() + "'"); + EmptyAll = false; + } + if (searchData.getPublishing().length() > 0) { + wheres.add("WHERE publishing LIKE '" + searchData.getPublishing() + "'"); + System.out.println("WHERE publishing LIKE '" + searchData.getPublishing() + "'"); + EmptyAll = false; + } + if (searchData.getYear() > 0) { + wheres.add("WHERE year = " + searchData.getYear()); + System.out.println("WHERE year = " + searchData.getYear()); + EmptyAll = false; + } + if (searchData.getISBN().length() > 0) { + wheres.add("WHERE isbn LIKE " + searchData.getISBN()); + System.out.println("WHERE isbn LIKE '" + searchData.getISBN() + "'"); + EmptyAll = false; + } + if (searchData.getCategory().length() > 1) { + wheres.add("WHERE category LIKE '" + searchData.getCategory() + "'"); + System.out.println("WHERE category LIKE '" + searchData.getCategory() + "'"); + EmptyAll = false; + } + if (EmptyAll) { + System.out.println("PUSTO"); + } - String query = "SELECT * FROM books"; - if (query.length() == 19) { + String query = "SELECT * FROM books"; + if (query.length() == 19) { - } - ResultSet result = stat.executeQuery("SELECT * FROM books"); + } + /*ResultSet result = stat.executeQuery("SELECT * FROM books"); int id, year; String name, authorName, authorSurname, isbn, category, publishing; while (result.next()) { @@ -212,7 +215,7 @@ public class Database { } catch (SQLException e) { JOptionPane.showMessageDialog(null, "Błąd przy odczycie z bazy.", "Error:", JOptionPane.INFORMATION_MESSAGE); } - return books; + return books;*/ } public List selectAlbums() { diff --git a/src/library/ListResult.form b/src/library/ListResult.form index 70d22a7..0bd187f 100644 --- a/src/library/ListResult.form +++ b/src/library/ListResult.form @@ -1,6 +1,6 @@ -
+ @@ -72,10 +72,15 @@ - + - + + + + + + diff --git a/src/library/ListResult.java b/src/library/ListResult.java index 3a744c2..eda967a 100644 --- a/src/library/ListResult.java +++ b/src/library/ListResult.java @@ -57,7 +57,9 @@ public class ListResult extends javax.swing.JFrame { rows, columnsNames )); + table.setRowSelectionInterval(0, 0); table.setRowHeight(25); + table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane1.setViewportView(table); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); diff --git a/src/library/MainMenu.java b/src/library/MainMenu.java index 953e8bc..8cc8d7e 100644 --- a/src/library/MainMenu.java +++ b/src/library/MainMenu.java @@ -387,7 +387,7 @@ public class MainMenu extends javax.swing.JFrame { private void bAlbumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAlbumActionPerformed if (radioBAdd.getModel().isSelected()) { - AlbumFieldPattern b = new AlbumFieldPattern(); + AlbumFieldPattern b = new AlbumFieldPattern(true); b.setVisible(true); b.add = true; } @@ -440,7 +440,6 @@ public class MainMenu extends javax.swing.JFrame { private void allBooksShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allBooksShowActionPerformed try { - List books = new LinkedList<>(); books = d.selectBooks(); Object[][] data = d.convertToTable(books); @@ -504,7 +503,7 @@ public class MainMenu extends javax.swing.JFrame { n = ""; n = Integer.toString(d.countAll("albums")); numberOfAlbums.setText(n); - n = ""; + n = ""; n = Integer.toString(d.countAll("movies")); numberOfMovies.setText(n); }//GEN-LAST:event_refreshNumbersOfItemsActionPerformed diff --git a/src/library/MovieFieldPattern.java b/src/library/MovieFieldPattern.java index 5c0fa7d..21b528d 100644 --- a/src/library/MovieFieldPattern.java +++ b/src/library/MovieFieldPattern.java @@ -19,13 +19,22 @@ public class MovieFieldPattern extends javax.swing.JFrame { * Creates new form bookFieldPattern */ public MovieFieldPattern() { - this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); initComponents(); + this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public MovieFieldPattern(boolean required) { initComponents(); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + Validation verify; + if (required) { + verify = new Validation(true); + } else { + verify = new Validation(); + } + movieTitle.setInputVerifier(verify); + director.setInputVerifier(verify); + year.setInputVerifier(verify); } public boolean add = false, delete = false, edit = false, search = false; diff --git a/src/library/Validation.java b/src/library/Validation.java index 3427884..139b665 100644 --- a/src/library/Validation.java +++ b/src/library/Validation.java @@ -18,6 +18,7 @@ import javax.swing.JTextField; */ public class Validation extends InputVerifier { + public final int nowadays = 2016; String expression; boolean required = false; int year; @@ -37,7 +38,7 @@ public class Validation extends InputVerifier { public boolean checkField(JComponent input) { JTextField tf = (JTextField) input; if (required) { - if ((tf.getText().equals(""))) { + if ("".equals(tf.getText())) { JOptionPane.showMessageDialog(null, "Pole wymagane!", "Błąd wprowadzanych danych", JOptionPane.INFORMATION_MESSAGE); return false; } @@ -47,7 +48,7 @@ public class Validation extends InputVerifier { if (!"".equals(tf.getText())) { try { year = Integer.parseInt(tf.getText()); - if (year > 2016 || year < 1900) { + if (year > nowadays || year < 1900) { JOptionPane.showMessageDialog(null, "Data spoza zakresu.", "Błąd wprowadzanych danych", JOptionPane.INFORMATION_MESSAGE); return false; } @@ -64,7 +65,7 @@ public class Validation extends InputVerifier { if (m.matches()) { return true; } else { - JOptionPane.showMessageDialog(null, "Niepoprawne dane!", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(null, "Wpisz ISBN we właściwym formacie", "Sprawdzenie pola:", JOptionPane.INFORMATION_MESSAGE); return false; } }