From 4a945aec5abb0158b6ffb3e30cbaf2a2ee46f2a9 Mon Sep 17 00:00:00 2001 From: BinHong Lee Date: Tue, 30 May 2017 11:45:26 -0700 Subject: [PATCH] Now creates new json if it doesn't exist and allow to create new user through UI --- .gitignore | 3 + books.json | 20 --- src/main/java/libsys/BookFactory.java | 14 +- src/main/java/libsys/Handler.java | 10 +- src/main/java/libsys/Main.java | 27 ++- src/main/java/libsys/MainGUI.form | 9 +- src/main/java/libsys/MainGUI.java | 22 ++- src/main/java/libsys/NewUserDialog.form | 132 +++++++++++++++ src/main/java/libsys/NewUserDialog.java | 208 ++++++++++++++++++++++++ src/main/java/libsys/UserFactory.java | 14 +- users.json | 12 -- 11 files changed, 418 insertions(+), 53 deletions(-) delete mode 100644 books.json create mode 100644 src/main/java/libsys/NewUserDialog.form create mode 100644 src/main/java/libsys/NewUserDialog.java delete mode 100644 users.json diff --git a/.gitignore b/.gitignore index 5f1fd3b..10fd9a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.json + .classpath effective.pom *.class @@ -6,6 +8,7 @@ target .mtj.tmp/ # Package Files # +*.jar *.war *.ear diff --git a/books.json b/books.json deleted file mode 100644 index 97bce6f..0000000 --- a/books.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "0": { - "Status": "RENTED", - "Title": "Cracking the Coding Interview", - "Due Date": [ - 2017, - 6, - 8 - ] - }, - "1": { - "Status": "AVAILABLE", - "Title": "Who was that?", - "Due Date": [ - 2017, - 6, - 8 - ] - } -} diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index 1ae9e92..f94679a 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -1,7 +1,7 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 5/28/2017 + * Last edited : 5/30/2017 */ import java.util.List; @@ -20,6 +20,7 @@ class BookFactory { private List books = new ArrayList(); private int id; + private String bookFilename; public BookFactory() { @@ -56,10 +57,11 @@ class BookFactory System.out.println(ex.getMessage()); } + this.bookFilename = bookFilename; id = getBook(books.size()-1).getId() + 1; } - public void toJsonFile(String bookFilename) + public void toJsonFile() { try { @@ -84,12 +86,18 @@ class BookFactory } } + public void setBookFileName(String bookFilename) + { + this.bookFilename = bookFilename; + } + public Book newBook(String title) { Book temp = new Book(title, id); books.add(temp); id++; + toJsonFile(); return temp; } @@ -132,5 +140,7 @@ class BookFactory books.set(i, newBook); } } + + toJsonFile(); } } diff --git a/src/main/java/libsys/Handler.java b/src/main/java/libsys/Handler.java index 6876e21..cbb1d18 100644 --- a/src/main/java/libsys/Handler.java +++ b/src/main/java/libsys/Handler.java @@ -10,8 +10,8 @@ import org.json.JSONObject; class Handler { Exception BookNotFound = new Exception("Error 404 : Book not found"); - BookFactory books = new BookFactory(); - UserFactory users = new UserFactory(); + BookFactory books; + UserFactory users; Calendar cal = Calendar.getInstance(); public Handler() @@ -29,9 +29,6 @@ class Handler books.update(book, newBook); users.update(user, newUser); - books.toJsonFile("books.json"); - users.toJsonFile("users.json"); - return true; } @@ -50,9 +47,6 @@ class Handler books.update(book, newBook); users.update(user, newUser); - books.toJsonFile("books.json"); - users.toJsonFile("users.json"); - return true; } diff --git a/src/main/java/libsys/Main.java b/src/main/java/libsys/Main.java index f2f9cf7..b17598f 100644 --- a/src/main/java/libsys/Main.java +++ b/src/main/java/libsys/Main.java @@ -1,7 +1,7 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 5/28/2017 + * Last edited : 5/30/2017 */ class Main @@ -12,8 +12,29 @@ class Main public static void main(String[] args) { Handler handler = new Handler(); - handler.books = new BookFactory(bookFilename); - handler.users = new UserFactory(userFilename); + + try + { + handler.books = new BookFactory(bookFilename); + } + catch (Exception e) + { + System.out.println("Book Exception"); + handler.books = new BookFactory(); + handler.books.setBookFileName(bookFilename); + } + + try + { + handler.users = new UserFactory(userFilename); + } + catch (Exception e) + { + System.out.println("User Exception"); + handler.users = new UserFactory(); + handler.users.setUserFileName(userFilename); + } + new MainGUI(handler).setVisible(true); } } diff --git a/src/main/java/libsys/MainGUI.form b/src/main/java/libsys/MainGUI.form index d89aa47..d9114f1 100644 --- a/src/main/java/libsys/MainGUI.form +++ b/src/main/java/libsys/MainGUI.form @@ -74,8 +74,8 @@ - - + + @@ -144,7 +144,7 @@ - + @@ -336,6 +336,9 @@ + + + diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 836cc77..56bfc8a 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -103,7 +103,7 @@ public class MainGUI extends javax.swing.JFrame .addComponent(bookStatus) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(bookDueDate) - .addContainerGap(94, Short.MAX_VALUE)) + .addContainerGap(88, Short.MAX_VALUE)) ); jTabbedPane.addTab("Book", bookPanel); @@ -198,6 +198,11 @@ public class MainGUI extends javax.swing.JFrame createBookBtn.setText("Add new book"); createUserBtn.setText("Add new user"); + createUserBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + createUserBtnActionPerformed(evt); + } + }); javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); mainPanel.setLayout(mainPanelLayout); @@ -222,8 +227,8 @@ public class MainGUI extends javax.swing.JFrame .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup() .addContainerGap() .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10) .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(createBookBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) @@ -266,6 +271,17 @@ public class MainGUI extends javax.swing.JFrame } }//GEN-LAST:event_returnBtnActionPerformed + private void createUserBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createUserBtnActionPerformed + NewUserDialog newUserUI = new NewUserDialog(this, true); + newUserUI.setVisible(true); + + if (newUserUI.getReturnStatus() != -1) + { + userSearch.setText(String.valueOf(handler.users.newUser(newUserUI.getName(), newUserUI.getLimit()).getId())); + userSearchBtnActionPerformed(evt); + } + }//GEN-LAST:event_createUserBtnActionPerformed + private void rentBtnActionPerformed(java.awt.event.ActionEvent evt) { User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); diff --git a/src/main/java/libsys/NewUserDialog.form b/src/main/java/libsys/NewUserDialog.form new file mode 100644 index 0000000..1c7b491 --- /dev/null +++ b/src/main/java/libsys/NewUserDialog.form @@ -0,0 +1,132 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/libsys/NewUserDialog.java b/src/main/java/libsys/NewUserDialog.java new file mode 100644 index 0000000..2e2d445 --- /dev/null +++ b/src/main/java/libsys/NewUserDialog.java @@ -0,0 +1,208 @@ +package libsys; + +/** + * + * @author binhonglee + */ +public class NewUserDialog extends javax.swing.JDialog { + + /** + * Creates new form NewUserDialog + */ + public NewUserDialog(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; + } + + public String getName() { + return name; + } + + public int getLimit() + { + return limit; + } + + /** + * 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(); + nameLbl = new javax.swing.JLabel(); + limitLbl = new javax.swing.JLabel(); + createBtn = new javax.swing.JButton(); + cancelBtn = new javax.swing.JButton(); + nameTxtField = new javax.swing.JTextField(); + limitTxtField = new javax.swing.JTextField(); + errorLbl = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + + title.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N + title.setText("New User"); + + nameLbl.setText("Name :"); + + limitLbl.setText("Limit :"); + + createBtn.setText("Create user"); + 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); + } + }); + + errorLbl.setVisible(false); + errorLbl.setText("LIMIT MUST BE NUMERICAL!"); + + 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, 13, 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)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(limitLbl) + .addComponent(nameLbl)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(nameTxtField) + .addComponent(limitTxtField)))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(title) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(errorLbl) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(nameLbl) + .addComponent(nameTxtField, 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(limitLbl) + .addComponent(limitTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, 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 + try + { + limit = Integer.parseInt(limitTxtField.getText()); + name = nameTxtField.getText(); + doClose(1); + } + catch (Exception e) + { + errorLbl.setVisible(true); + } + }//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(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the dialog */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + NewUserDialog dialog = new NewUserDialog(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.JLabel errorLbl; + private javax.swing.JLabel limitLbl; + private javax.swing.JTextField limitTxtField; + private javax.swing.JLabel nameLbl; + private javax.swing.JTextField nameTxtField; + private javax.swing.JLabel title; + // End of variables declaration//GEN-END:variables + + private int returnStatus = -1; + private String name; + private int limit; + +} diff --git a/src/main/java/libsys/UserFactory.java b/src/main/java/libsys/UserFactory.java index e7a0d05..cbf83d8 100644 --- a/src/main/java/libsys/UserFactory.java +++ b/src/main/java/libsys/UserFactory.java @@ -1,7 +1,7 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 5/28/2017 + * Last edited : 5/30/2017 */ import java.util.List; @@ -20,6 +20,7 @@ class UserFactory { private List users = new ArrayList(); private int id; + private String userFilename; public UserFactory() { @@ -57,10 +58,11 @@ class UserFactory System.out.println("Exception importing from json: " + ex.getMessage()); } + this.userFilename = userFilename; id = getUser(users.size()-1).getId() + 1; } - public void toJsonFile(String userFilename) + public void toJsonFile() { try { @@ -85,12 +87,18 @@ class UserFactory } } + public void setUserFileName(String userFilename) + { + this.userFilename = userFilename; + } + public User newUser(String name, int limit) { User temp = new User(name, id, limit); users.add(temp); id++; + toJsonFile(); return temp; } @@ -136,5 +144,7 @@ class UserFactory users.set(i, newUser); } } + + toJsonFile(); } } diff --git a/users.json b/users.json deleted file mode 100644 index b4dc94e..0000000 --- a/users.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "0": { - "Limit": 2, - "Books": [], - "Name": "FirstName LastName" - }, - "1": { - "Limit": 100, - "Books": [0], - "Name": "John Doe" - } -}