diff --git a/src/main/java/libsys/Book.java b/src/main/java/libsys/Book.java index d538dc7..db124c4 100644 --- a/src/main/java/libsys/Book.java +++ b/src/main/java/libsys/Book.java @@ -1,7 +1,7 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 5/28/2017 + * Last edited : 5/30/2017 */ class Book @@ -23,12 +23,11 @@ class Book this.status = "NOT AVAILABLE"; } - //Constructor with book title - public Book(String title, int id) + public Book(String title, int id, String status) { this.title = title; this.id = id; - this.status = "AVAILABLE"; + this.status = status; } public Book(int id, String title, String status, int[] dueDate) diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index f94679a..4cbaa08 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -58,7 +58,7 @@ class BookFactory } this.bookFilename = bookFilename; - id = getBook(books.size()-1).getId() + 1; + id = books.get(books.size() - 1).getId() + 1; } public void toJsonFile() @@ -91,9 +91,9 @@ class BookFactory this.bookFilename = bookFilename; } - public Book newBook(String title) + public Book newBook(String title, String status) { - Book temp = new Book(title, id); + Book temp = new Book(title, id, status); books.add(temp); id++; @@ -104,14 +104,35 @@ class BookFactory public Book getBook(int index) { - try + return search(index, 0, books.size() - 1); + } + + public Book search(int index, int start, int end) + { + if (start == end && books.get(start).getId() == index) { - return books.get(index); + return books.get(start); } - catch (Exception e) + + if (start >= end) { throw new NullPointerException(); } + + int currentId = ((start + end) / 2); + + if (books.get(currentId).getId() == index) + { + return books.get(currentId); + } + else if (books.get(currentId).getId() > index) + { + return search(index, start, currentId - 1); + } + else + { + return search(index, currentId + 1, end); + } } public Book getBook(String title) diff --git a/src/main/java/libsys/MainGUI.form b/src/main/java/libsys/MainGUI.form index d9114f1..a972b75 100644 --- a/src/main/java/libsys/MainGUI.form +++ b/src/main/java/libsys/MainGUI.form @@ -331,6 +331,9 @@ + + + diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 56bfc8a..3ea0d89 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -196,6 +196,11 @@ public class MainGUI extends javax.swing.JFrame nameLabel.setText("Welcome to abc Library"); createBookBtn.setText("Add new book"); + createBookBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createBookBtnActionPerformed(evt); + } + }); createUserBtn.setText("Add new user"); createUserBtn.addActionListener(new java.awt.event.ActionListener() { @@ -282,6 +287,17 @@ public class MainGUI extends javax.swing.JFrame } }//GEN-LAST:event_createUserBtnActionPerformed + private void createBookBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBookBtnActionPerformed + NewBookDialog newBookUI = new NewBookDialog(this, true); + newBookUI.setVisible(true); + + if (newBookUI.getReturnStatus() != -1) + { + bookSearch.setText(String.valueOf(handler.books.newBook(newBookUI.getName(), newBookUI.getStatus()).getId())); + bookSearchBtnActionPerformed(evt); + } + }//GEN-LAST:event_createBookBtnActionPerformed + private void rentBtnActionPerformed(java.awt.event.ActionEvent evt) { User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); diff --git a/src/main/java/libsys/NewBookDialog.form b/src/main/java/libsys/NewBookDialog.form new file mode 100644 index 0000000..c425045 --- /dev/null +++ b/src/main/java/libsys/NewBookDialog.form @@ -0,0 +1,128 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/libsys/NewBookDialog.java b/src/main/java/libsys/NewBookDialog.java new file mode 100644 index 0000000..5677c2b --- /dev/null +++ b/src/main/java/libsys/NewBookDialog.java @@ -0,0 +1,195 @@ +package libsys; + +public class NewBookDialog extends javax.swing.JDialog { + + /** + * Creates new form NewBookDialog + * @param parent + * @param modal + */ + public NewBookDialog(java.awt.Frame parent, boolean modal) { + super(parent, modal); + initComponents(); + } + + /** + * @return the return status of this dialog - one of RET_OK or RET_CANCEL + */ + public int getReturnStatus() { + return returnStatus; + } + + @Override + public String getName() { + return name; + } + + public String getStatus() + { + return status; + } + + /** + * 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 + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + title = new javax.swing.JLabel(); + titleLbl = new javax.swing.JLabel(); + statusLbl = new javax.swing.JLabel(); + createBtn = new javax.swing.JButton(); + cancelBtn = new javax.swing.JButton(); + titleTxtField = new javax.swing.JTextField(); + statusComboBox = new javax.swing.JComboBox(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + + title.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N + title.setText("New Book"); + + titleLbl.setText("Title :"); + + statusLbl.setText("Status :"); + + createBtn.setText("Add book"); + createBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createBtnActionPerformed(evt); + } + }); + + cancelBtn.setText("Cancel"); + cancelBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + cancelBtnActionPerformed(evt); + } + }); + + statusComboBox.setModel(new javax.swing.DefaultComboBoxModel(statusOptions)); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(createBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE) + .addComponent(cancelBtn)) + .addGroup(layout.createSequentialGroup() + .addComponent(title) + .addGap(0, 100, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(statusLbl) + .addComponent(titleLbl)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(titleTxtField) + .addComponent(statusComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(title) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(titleLbl) + .addComponent(titleTxtField, 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(statusLbl) + .addComponent(statusComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(createBtn) + .addComponent(cancelBtn)) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void createBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBtnActionPerformed + status = statusOptions[statusComboBox.getSelectedIndex()]; + name = titleTxtField.getText(); + doClose(1); + }//GEN-LAST:event_createBtnActionPerformed + + private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed + doClose(-1); + }//GEN-LAST:event_cancelBtnActionPerformed + + private void doClose(int retStatus) { + returnStatus = retStatus; + setVisible(false); + dispose(); + } + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the dialog */ + java.awt.EventQueue.invokeLater(new Runnable() { + @Override + public void run() { + NewBookDialog dialog = new NewBookDialog(new javax.swing.JFrame(), true); + dialog.addWindowListener(new java.awt.event.WindowAdapter() { + @Override + public void windowClosing(java.awt.event.WindowEvent e) { + System.exit(0); + } + }); + dialog.setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton cancelBtn; + private javax.swing.JButton createBtn; + private javax.swing.JComboBox statusComboBox; + private javax.swing.JLabel statusLbl; + private javax.swing.JLabel title; + private javax.swing.JLabel titleLbl; + private javax.swing.JTextField titleTxtField; + // End of variables declaration//GEN-END:variables + + private int returnStatus = -1; + private String name; + private String status; + private String[] statusOptions = new String[] { "AVAILABLE", "NOT AVAILABLE", "RESERVED" }; + +} diff --git a/src/main/java/libsys/NewUserDialog.form b/src/main/java/libsys/NewUserDialog.form index 1c7b491..ec46345 100644 --- a/src/main/java/libsys/NewUserDialog.form +++ b/src/main/java/libsys/NewUserDialog.form @@ -28,7 +28,7 @@ - + @@ -36,7 +36,7 @@ - + @@ -102,7 +102,7 @@
- + diff --git a/src/main/java/libsys/NewUserDialog.java b/src/main/java/libsys/NewUserDialog.java index 2e2d445..aa657a1 100644 --- a/src/main/java/libsys/NewUserDialog.java +++ b/src/main/java/libsys/NewUserDialog.java @@ -1,9 +1,5 @@ package libsys; -/** - * - * @author binhonglee - */ public class NewUserDialog extends javax.swing.JDialog { /** @@ -13,18 +9,18 @@ public class NewUserDialog extends javax.swing.JDialog { super(parent, modal); initComponents(); } - + /** * @return the return status of this dialog - one of RET_OK or RET_CANCEL */ public int getReturnStatus() { return returnStatus; } - + public String getName() { return name; } - + public int getLimit() { return limit; @@ -57,7 +53,7 @@ public class NewUserDialog extends javax.swing.JDialog { limitLbl.setText("Limit :"); - createBtn.setText("Create user"); + createBtn.setText("Add user"); createBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { createBtnActionPerformed(evt); @@ -83,13 +79,13 @@ public class NewUserDialog extends javax.swing.JDialog { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(createBtn) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(cancelBtn)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(title) .addComponent(errorLbl)) - .addGap(0, 0, Short.MAX_VALUE)) + .addGap(0, 13, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(limitLbl) @@ -141,7 +137,7 @@ public class NewUserDialog extends javax.swing.JDialog { private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed doClose(-1); }//GEN-LAST:event_cancelBtnActionPerformed - + private void doClose(int retStatus) { returnStatus = retStatus; setVisible(false); @@ -155,7 +151,7 @@ public class NewUserDialog extends javax.swing.JDialog { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. - * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { diff --git a/src/main/java/libsys/UserFactory.java b/src/main/java/libsys/UserFactory.java index cbf83d8..d10e747 100644 --- a/src/main/java/libsys/UserFactory.java +++ b/src/main/java/libsys/UserFactory.java @@ -4,17 +4,17 @@ package libsys; * Last edited : 5/30/2017 */ - import java.util.List; - import java.util.ArrayList; - import org.json.JSONString; - import org.json.JSONObject; - import org.json.JSONTokener; - import org.json.JSONArray; - import java.io.FileInputStream; - import java.io.PrintWriter; - import java.io.File; - import java.util.Enumeration; - import java.io.Serializable; +import java.util.List; +import java.util.ArrayList; +import org.json.JSONString; +import org.json.JSONObject; +import org.json.JSONTokener; +import org.json.JSONArray; +import java.io.FileInputStream; +import java.io.PrintWriter; +import java.io.File; +import java.util.Enumeration; +import java.io.Serializable; class UserFactory {