diff --git a/books.json b/books.json index 8c006bb..97bce6f 100644 --- a/books.json +++ b/books.json @@ -1,19 +1,20 @@ -{"0": { - "Status": "RENTED", - "Title": "Cracking the Coding Interview", - "Due Date": [ - 10, - 10, - 2010 - ] -}, -"1": { - "Status": "RENTED", - "Title": "Who was that?", - "Due Date": [ - 1, - 1, - 1 - ] -} +{ + "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/Book.java b/src/main/java/libsys/Book.java index 81f4424..d538dc7 100644 --- a/src/main/java/libsys/Book.java +++ b/src/main/java/libsys/Book.java @@ -74,7 +74,7 @@ class Book public boolean rent(int[] dueDate) { //Check if the book is available to be rented - if(status != "AVAILABLE") + if(!"AVAILABLE".equals(status)) { return false; } diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index e037ce1..1ae9e92 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -30,13 +30,10 @@ class BookFactory { try { - System.out.println("Reading data from file..."); FileInputStream in = new FileInputStream(bookFilename); JSONObject obj = new JSONObject(new JSONTokener(in)); String [] ids = JSONObject.getNames(obj); - System.out.println(); - System.out.println("Parsing data into ArrayList..."); for (int i = 0; i < ids.length; i++) { JSONObject jsonBook = obj.getJSONObject(ids[i]); @@ -52,8 +49,6 @@ class BookFactory } books.add(new Book(id, title, status, dueDate)); } - System.out.println(); - in.close(); } catch (Exception ex) @@ -81,7 +76,6 @@ class BookFactory } out.println(booksObj.toString(4)); out.close(); - System.out.println("Books exported to JSON file."); } catch (Exception e) { @@ -127,13 +121,13 @@ class BookFactory throw new NullPointerException(); } - public void update(Book newBook) + public void update(Book oldBook, Book newBook) { for (int i = 0; i < books.size(); i++) { Book temp = books.get(i); - if(temp.getId() == newBook.getId()) + if(temp.getId() == oldBook.getId()) { books.set(i, newBook); } diff --git a/src/main/java/libsys/Handler.java b/src/main/java/libsys/Handler.java index 2d42d70..6876e21 100644 --- a/src/main/java/libsys/Handler.java +++ b/src/main/java/libsys/Handler.java @@ -12,7 +12,7 @@ class Handler Exception BookNotFound = new Exception("Error 404 : Book not found"); BookFactory books = new BookFactory(); UserFactory users = new UserFactory(); - Calendar cal; + Calendar cal = Calendar.getInstance(); public Handler() { @@ -22,15 +22,16 @@ class Handler public boolean borrowBook(User user, Book book) { User newUser = user; + Book newBook = book; - if(book.getStatus() == "AVAILABLE" && newUser.status()) + if(("AVAILABLE".equals(book.getStatus())) && newUser.status() && newBook.rent(calDueDate(10)) && newUser.borrowNewBook(book.getId())) { - book.rent(calDueDate(10)); - newUser.borrowNewBook(book.getId()); - - books.update(book); + books.update(book, newBook); users.update(user, newUser); + books.toJsonFile("books.json"); + users.toJsonFile("users.json"); + return true; } @@ -40,14 +41,18 @@ class Handler public boolean returnBook(User user, Book book) { User newUser = user; + Book newBook = book; if(newUser.returnBook(book.getId())) { book.returned(); - books.update(book); + books.update(book, newBook); users.update(user, newUser); + books.toJsonFile("books.json"); + users.toJsonFile("users.json"); + return true; } @@ -127,9 +132,9 @@ class Handler { int[] currentDay = new int[3]; - currentDay[0] = cal.get(Calendar.YEAR) + 1900; + currentDay[0] = cal.get(Calendar.YEAR); currentDay[1] = cal.get(Calendar.MONTH) + 1; - currentDay[2] = cal.get(Calendar.DAY_OF_MONTH); + currentDay[2] = cal.get(Calendar.DATE); return currentDay; } @@ -141,11 +146,9 @@ class Handler for (int i = 0; i < bookIDs.length; i++) { titlesString += books.getBook(bookIDs[i]).getTitle(); - titlesString += "; \n"; + titlesString += "; "; } - System.out.println(titlesString); - return titlesString; } } diff --git a/src/main/java/libsys/MainGUI.form b/src/main/java/libsys/MainGUI.form index 80f0464..0780ed6 100644 --- a/src/main/java/libsys/MainGUI.form +++ b/src/main/java/libsys/MainGUI.form @@ -1,6 +1,6 @@ -
+ @@ -24,31 +24,35 @@ - - + + + - - - + + + - + + + + - - - + + + @@ -56,14 +60,14 @@ - - + + - + @@ -82,9 +86,9 @@ - - - + + + @@ -93,8 +97,9 @@ + - + @@ -117,7 +122,9 @@ - + + + @@ -137,6 +144,9 @@ + + + @@ -153,6 +163,11 @@ + + + + + @@ -170,9 +185,10 @@ - - - + + + + @@ -183,8 +199,9 @@ - + + @@ -208,7 +225,11 @@ - + + + + + @@ -228,6 +249,9 @@ + + + @@ -249,6 +273,28 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index e4c4f03..5198c4f 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -7,227 +7,324 @@ import java.awt.event.ActionListener; public class MainGUI extends javax.swing.JFrame { - Handler handler; - - public MainGUI(Handler handler) - { - this.handler = handler; - initComponents(); - } - - //GEN-BEGIN:initComponents - private void initComponents() - { - setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); - mainPanel = new javax.swing.JPanel(); - jTabbedPane = new javax.swing.JTabbedPane(); - bookPanel = new javax.swing.JPanel(); - bookMgtLabel = new javax.swing.JLabel(); - bookSearch = new javax.swing.JTextField(); - bookSearchBtn = new javax.swing.JButton(); - bookTitle = new javax.swing.JLabel(); - bookStatus = new javax.swing.JLabel(); - bookID = new javax.swing.JLabel(); - userPanel = new javax.swing.JPanel(); - userMgtLabel = new javax.swing.JLabel(); - userSearch = new javax.swing.JTextField(); - userSearchBtn = new javax.swing.JButton(); - userName = new javax.swing.JLabel(); - userLimit = new javax.swing.JLabel(); - userID = new javax.swing.JLabel(); - userBooks = new javax.swing.JLabel(); - - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); - - bookMgtLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N - bookMgtLabel.setText("Book Management"); - - bookSearchBtn.setText("Search"); - bookSearchBtn.addActionListener(new ActionListener() + Handler handler; + + public MainGUI(Handler handler) { - public void actionPerformed(ActionEvent evt) - { - bookSearchBtnActionPerformed(evt); - } - }); + this.handler = handler; + initComponents(); + } + + // //GEN-BEGIN:initComponents + private void initComponents() { + + mainPanel = new javax.swing.JPanel(); + jTabbedPane = new javax.swing.JTabbedPane(); + bookPanel = new javax.swing.JPanel(); + bookMgtLabel = new javax.swing.JLabel(); + bookSearch = new javax.swing.JTextField(); + bookSearchBtn = new javax.swing.JButton(); + bookTitle = new javax.swing.JLabel(); + bookStatus = new javax.swing.JLabel(); + bookID = new javax.swing.JLabel(); + bookDueDate = new javax.swing.JLabel(); + userPanel = new javax.swing.JPanel(); + userMgtLabel = new javax.swing.JLabel(); + userSearch = new javax.swing.JTextField(); + userSearchBtn = new javax.swing.JButton(); + userName = new javax.swing.JLabel(); + userLimit = new javax.swing.JLabel(); + userID = new javax.swing.JLabel(); + userBooks = new javax.swing.JLabel(); + rentBtn = new javax.swing.JButton(); + returnBtn = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + //setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH); - bookTitle.setText("Title : "); + bookMgtLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N + bookMgtLabel.setText("Book Management"); - bookStatus.setText("Status :"); + bookSearchBtn.setText("Search"); + bookSearchBtn.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) + { + bookSearchBtnActionPerformed(evt); + } + }); - bookID.setText("ID :"); + bookTitle.setText("Title : "); - javax.swing.GroupLayout bookPanelLayout = new javax.swing.GroupLayout(bookPanel); - bookPanel.setLayout(bookPanelLayout); - bookPanelLayout.setHorizontalGroup( - bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(bookPanelLayout.createSequentialGroup() - .addContainerGap() - .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + bookStatus.setText("Status :"); + + bookID.setText("ID :"); + + bookDueDate.setText("Due Date :"); + + javax.swing.GroupLayout bookPanelLayout = new javax.swing.GroupLayout(bookPanel); + bookPanel.setLayout(bookPanelLayout); + bookPanelLayout.setHorizontalGroup( + bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(bookPanelLayout.createSequentialGroup() + .addContainerGap() + .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(bookPanelLayout.createSequentialGroup() + .addComponent(bookSearch) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(bookSearchBtn)) + .addGroup(bookPanelLayout.createSequentialGroup() + .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(bookTitle) + .addComponent(bookStatus) + .addComponent(bookMgtLabel) + .addComponent(bookID) + .addComponent(bookDueDate)) + .addGap(0, 115, Short.MAX_VALUE))) + .addContainerGap()) + ); + bookPanelLayout.setVerticalGroup( + bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(bookPanelLayout.createSequentialGroup() + .addContainerGap() .addComponent(bookMgtLabel) - .addGroup(bookPanelLayout.createSequentialGroup() - .addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(bookSearchBtn)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(bookTitle) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(bookID) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(bookStatus) - .addComponent(bookID)) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - bookPanelLayout.setVerticalGroup( - bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(bookPanelLayout.createSequentialGroup() - .addContainerGap() - .addComponent(bookMgtLabel) - .addGap(18, 18, 18) - .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(bookSearchBtn)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(bookID) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(bookTitle) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(bookStatus) - .addContainerGap(38, Short.MAX_VALUE)) - ); - - jTabbedPane.addTab("Book", bookPanel); - - userMgtLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N - userMgtLabel.setText("User Management"); - - userSearchBtn.setText("Search"); - userSearchBtn.addActionListener(new ActionListener() - { - public void actionPerformed(ActionEvent evt) - { - userSearchBtnActionPerformed(evt); - } - }); - - userName.setText("Name : "); - - userLimit.setText("Limit :"); - - userID.setText("ID :"); - - userBooks.setText("Books :"); - - javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel); - userPanel.setLayout(userPanelLayout); - userPanelLayout.setHorizontalGroup( - userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(userPanelLayout.createSequentialGroup() - .addContainerGap() - .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(userPanelLayout.createSequentialGroup() - .addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(bookDueDate) + .addContainerGap(88, Short.MAX_VALUE)) + ); + + jTabbedPane.addTab("Book", bookPanel); + + userMgtLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N + userMgtLabel.setText("User Management"); + + userSearchBtn.setText("Search"); + userSearchBtn.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) + { + userSearchBtnActionPerformed(evt); + } + }); + + userName.setText("Name : "); + + userLimit.setText("Limit :"); + + userID.setText("ID :"); + + userBooks.setText("Books :"); + + rentBtn.setText("Rent Another Book"); + rentBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + rentBtnActionPerformed(evt); + } + }); + rentBtn.setVisible(false); + + returnBtn.setText("Return Book"); + returnBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + returnBtnActionPerformed(evt); + } + }); + rentBtn.setVisible(false); + + javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel); + userPanel.setLayout(userPanelLayout); + userPanelLayout.setHorizontalGroup( + userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(userPanelLayout.createSequentialGroup() + .addContainerGap() + .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(rentBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(userPanelLayout.createSequentialGroup() + .addComponent(userSearch) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(userSearchBtn)) + .addGroup(userPanelLayout.createSequentialGroup() + .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(userID) + .addComponent(userMgtLabel) + .addComponent(userName) + .addComponent(userBooks) + .addComponent(userLimit)) + .addGap(0, 120, Short.MAX_VALUE)) + .addComponent(returnBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap()) + ); + userPanelLayout.setVerticalGroup( + userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(userPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(userMgtLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(userSearchBtn)) - .addGroup(userPanelLayout.createSequentialGroup() - .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(userMgtLabel) - .addComponent(userName) - .addComponent(userID) - .addComponent(userBooks) - .addComponent(userLimit)) - .addGap(0, 0, Short.MAX_VALUE))) - .addContainerGap()) - ); - userPanelLayout.setVerticalGroup( - userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(userPanelLayout.createSequentialGroup() - .addContainerGap() - .addComponent(userMgtLabel) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(userSearchBtn)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(userName) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(userID) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(userLimit) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(userBooks) - .addContainerGap(28, Short.MAX_VALUE)) - ); - - jTabbedPane.addTab("User", userPanel); - - javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); - mainPanel.setLayout(mainPanelLayout); - mainPanelLayout.setHorizontalGroup( - mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(mainPanelLayout.createSequentialGroup() - .addContainerGap() - .addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(32, Short.MAX_VALUE)) - ); - mainPanelLayout.setVerticalGroup( - mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(mainPanelLayout.createSequentialGroup() - .addContainerGap() - .addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); - getContentPane().setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(0, 7, Short.MAX_VALUE) - .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGap(0, 0, Short.MAX_VALUE) - .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(0, 0, Short.MAX_VALUE)) - ); - - pack(); - } - //GEN-END:initComponents - - private void bookSearchBtnActionPerformed(ActionEvent evt) - { - Book thisBook = handler.books.getBook(0); - bookID.setText("ID : " + thisBook.getId()); - bookTitle.setText("Title : " + thisBook.getTitle()); - bookStatus.setText("Status : " + thisBook.getStatus()); - } - - private void userSearchBtnActionPerformed(ActionEvent evt) - { - User thisUser = handler.users.getUser(0); - userID.setText("ID : " + thisUser.getId()); - userName.setText("Name : " + thisUser.getName()); - userLimit.setText("Limit : " + thisUser.getLimit()); - userBooks.setText("Books : " + handler.bookIdsToTitlesString(thisUser.bookStatus())); - } - - //GEN-BEGIN:variables - private javax.swing.JLabel bookID; - private javax.swing.JLabel bookMgtLabel; - private javax.swing.JPanel bookPanel; - private javax.swing.JTextField bookSearch; - private javax.swing.JButton bookSearchBtn; - private javax.swing.JLabel bookStatus; - private javax.swing.JLabel bookTitle; - private javax.swing.JPanel mainPanel; - private javax.swing.JTabbedPane jTabbedPane; - private javax.swing.JLabel userBooks; - private javax.swing.JLabel userID; - private javax.swing.JLabel userLimit; - private javax.swing.JLabel userMgtLabel; - private javax.swing.JLabel userName; - private javax.swing.JPanel userPanel; - private javax.swing.JTextField userSearch; - private javax.swing.JButton userSearchBtn; - //GEN-END:variables + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(userName) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(userID) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(userLimit) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(userBooks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(rentBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(returnBtn) + .addContainerGap()) + ); + + jTabbedPane.addTab("User", userPanel); + + javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel); + mainPanel.setLayout(mainPanelLayout); + mainPanelLayout.setHorizontalGroup( + mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(mainPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(jTabbedPane) + .addContainerGap()) + ); + mainPanelLayout.setVerticalGroup( + mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(mainPanelLayout.createSequentialGroup() + .addContainerGap() + .addComponent(jTabbedPane) + .addContainerGap()) + ); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(7, 7, 7) + .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + ); + + pack(); + }// //GEN-END:initComponents + + private void returnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnBtnActionPerformed + + User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); + ReturnDialog toReturnUI = new ReturnDialog(this, true, handler, thisUser.getId()); + toReturnUI.setVisible(true); + int toReturnID = toReturnUI.getReturnStatus(); + + if (toReturnID != -1) + { + handler.returnBook(thisUser, handler.books.getBook(toReturnID)); + userSearchBtnActionPerformed(evt); + bookSearchBtnActionPerformed(evt); + } + }//GEN-LAST:event_returnBtnActionPerformed + + private void rentBtnActionPerformed(java.awt.event.ActionEvent evt) + { + User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); + RentDialog toRentUI = new RentDialog(this, true, handler); + toRentUI.setVisible(true); + int toRentID = toRentUI.getReturnStatus(); + + if (toRentID != -1) + { + handler.borrowBook(thisUser, handler.books.getBook(toRentID)); + userSearchBtnActionPerformed(evt); + bookSearchBtnActionPerformed(evt); + } + } + + private void bookSearchBtnActionPerformed(ActionEvent evt) + { + try + { + Book thisBook = handler.books.getBook(Integer.parseInt(bookSearch.getText())); + bookID.setText("ID : " + thisBook.getId()); + bookTitle.setText("Title : " + thisBook.getTitle()); + bookStatus.setText("Status : " + thisBook.getStatus()); + if ("RENTED".equals(thisBook.getStatus())) + { + bookDueDate.setText("Due date : " + thisBook.getDueDate()[0] + "/" + thisBook.getDueDate()[1] + "/" + thisBook.getDueDate()[2]); + } + else + { + bookDueDate.setText("Due date : "); + } + } + catch (Exception e) + { + bookID.setText("ERROR : BOOK NOT FOUND"); + bookTitle.setText(""); + bookStatus.setText(""); + bookDueDate.setText(""); + } + } + + private void userSearchBtnActionPerformed(ActionEvent evt) + { + try + { + User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText())); + userID.setText("ID : " + thisUser.getId()); + userName.setText("Name : " + thisUser.getName()); + userLimit.setText("Limit : " + thisUser.getLimit()); + userBooks.setText("Books : " + handler.bookIdsToTitlesString(thisUser.bookStatus())); + rentBtn.setVisible(thisUser.status()); + returnBtn.setVisible(thisUser.bookStatus().length > 0); + } + catch (Exception e) + { + userID.setText("ERROR : USER NOT FOUND"); + userName.setText(""); + userLimit.setText(""); + userBooks.setText(""); + rentBtn.setVisible(false); + returnBtn.setVisible(false); + } + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel bookDueDate; + private javax.swing.JLabel bookID; + private javax.swing.JLabel bookMgtLabel; + private javax.swing.JPanel bookPanel; + private javax.swing.JTextField bookSearch; + private javax.swing.JButton bookSearchBtn; + private javax.swing.JLabel bookStatus; + private javax.swing.JLabel bookTitle; + private javax.swing.JTabbedPane jTabbedPane; + private javax.swing.JPanel mainPanel; + private javax.swing.JButton rentBtn; + private javax.swing.JButton returnBtn; + private javax.swing.JLabel userBooks; + private javax.swing.JLabel userID; + private javax.swing.JLabel userLimit; + private javax.swing.JLabel userMgtLabel; + private javax.swing.JLabel userName; + private javax.swing.JPanel userPanel; + private javax.swing.JTextField userSearch; + private javax.swing.JButton userSearchBtn; + // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/libsys/RentDialog.form b/src/main/java/libsys/RentDialog.form new file mode 100644 index 0000000..1a34fbb --- /dev/null +++ b/src/main/java/libsys/RentDialog.form @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/libsys/RentDialog.java b/src/main/java/libsys/RentDialog.java new file mode 100644 index 0000000..d0081ea --- /dev/null +++ b/src/main/java/libsys/RentDialog.java @@ -0,0 +1,214 @@ +package libsys; + +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import javax.swing.AbstractAction; +import javax.swing.ActionMap; +import javax.swing.InputMap; +import javax.swing.JComponent; +import javax.swing.KeyStroke; + +public class RentDialog extends javax.swing.JDialog { + + static Handler handler = new Handler(); + /** + * A return status code - returned if Cancel button has been pressed + */ + public static final int RET_CANCEL = -1; + + /** + * Creates new form RentDialog + */ + public RentDialog(java.awt.Frame parent, boolean modal, Handler handler) { + super(parent, modal); + this.handler = handler; + initComponents(); + + // Close the dialog when Esc is pressed + String cancelName = "cancel"; + InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName); + ActionMap actionMap = getRootPane().getActionMap(); + actionMap.put(cancelName, new AbstractAction() { + public void actionPerformed(ActionEvent e) { + doClose(RET_CANCEL); + } + }); + } + + /** + * @return the return status of this dialog - one of RET_OK or RET_CANCEL + */ + public int getReturnStatus() { + return returnStatus; + } + + /** + * 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() { + + okButton = new javax.swing.JButton(); + cancelButton = new javax.swing.JButton(); + label = new javax.swing.JLabel(); + givenBookId = new javax.swing.JTextField(); + invalidLabel = new javax.swing.JLabel(); + + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent evt) { + closeDialog(evt); + } + }); + + okButton.setText("OK"); + okButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + okButtonActionPerformed(evt); + } + }); + + cancelButton.setText("Cancel"); + cancelButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + cancelButtonActionPerformed(evt); + } + }); + + label.setText("Please input the book ID:"); + + invalidLabel.setText("INVALID ID!"); + invalidLabel.setVisible(false); + + 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) + .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(cancelButton) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(givenBookId) + .addGroup(layout.createSequentialGroup() + .addComponent(invalidLabel) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + + layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton}); + + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(label) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE) + .addComponent(invalidLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(givenBookId, 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(cancelButton) + .addComponent(okButton)) + .addContainerGap()) + ); + + getRootPane().setDefaultButton(okButton); + + pack(); + }// //GEN-END:initComponents + + private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed + try + { + if ("AVAILABLE".equals(handler.books.getBook(Integer.parseInt(givenBookId.getText())).getStatus())) + { + doClose(Integer.parseInt(givenBookId.getText())); + } + else + { + invalidLabel.setVisible(true); + } + } + catch (Exception e) + { + invalidLabel.setVisible(true); + } + }//GEN-LAST:event_okButtonActionPerformed + + private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed + doClose(RET_CANCEL); + }//GEN-LAST:event_cancelButtonActionPerformed + + /** + * Closes the dialog + */ + private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog + doClose(RET_CANCEL); + }//GEN-LAST:event_closeDialog + + 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(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the dialog */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + RentDialog dialog = new RentDialog(new javax.swing.JFrame(), true, handler); + 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 cancelButton; + private javax.swing.JTextField givenBookId; + private javax.swing.JLabel invalidLabel; + private javax.swing.JLabel label; + private javax.swing.JButton okButton; + // End of variables declaration//GEN-END:variables + + private int returnStatus = RET_CANCEL; +} diff --git a/src/main/java/libsys/ReturnDialog.form b/src/main/java/libsys/ReturnDialog.form new file mode 100644 index 0000000..29b7c42 --- /dev/null +++ b/src/main/java/libsys/ReturnDialog.form @@ -0,0 +1,100 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/libsys/ReturnDialog.java b/src/main/java/libsys/ReturnDialog.java new file mode 100644 index 0000000..99d7220 --- /dev/null +++ b/src/main/java/libsys/ReturnDialog.java @@ -0,0 +1,227 @@ +package libsys; + +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import javax.swing.AbstractAction; +import javax.swing.ActionMap; +import javax.swing.InputMap; +import javax.swing.JComponent; +import javax.swing.KeyStroke; + +public class ReturnDialog extends javax.swing.JDialog { + + static Handler handler = new Handler(); + static int id; + /** + * A return status code - returned if Cancel button has been pressed + */ + public static final int RET_CANCEL = -1; + + /** + * Creates new form ReturnDialog + */ + public ReturnDialog(java.awt.Frame parent, boolean modal, Handler handler, int id) { + super(parent, modal); + this.id = id; + this.handler = handler; + initComponents(); + + // Close the dialog when Esc is pressed + String cancelName = "cancel"; + InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName); + ActionMap actionMap = getRootPane().getActionMap(); + actionMap.put(cancelName, new AbstractAction() { + public void actionPerformed(ActionEvent e) { + doClose(RET_CANCEL); + } + }); + } + + /** + * @return the return status of this dialog - one of RET_OK or RET_CANCEL + */ + public int getReturnStatus() { + return returnStatus; + } + + /** + * 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() { + + okButton = new javax.swing.JButton(); + cancelButton = new javax.swing.JButton(); + label = new javax.swing.JLabel(); + givenBookId = new javax.swing.JTextField(); + invalidLabel = new javax.swing.JLabel(); + + addWindowListener(new java.awt.event.WindowAdapter() { + public void windowClosing(java.awt.event.WindowEvent evt) { + closeDialog(evt); + } + }); + + okButton.setText("OK"); + okButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + okButtonActionPerformed(evt); + } + }); + + cancelButton.setText("Cancel"); + cancelButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + cancelButtonActionPerformed(evt); + } + }); + + label.setText("Please input the book ID:"); + + invalidLabel.setText("INVALID ID!"); + invalidLabel.setVisible(false); + + 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) + .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(cancelButton) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(givenBookId) + .addGroup(layout.createSequentialGroup() + .addComponent(invalidLabel) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) + ); + + layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton}); + + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(label) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE) + .addComponent(invalidLabel) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(givenBookId, 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(cancelButton) + .addComponent(okButton)) + .addContainerGap()) + ); + + getRootPane().setDefaultButton(okButton); + + pack(); + }// //GEN-END:initComponents + + private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed + try + { + int[] borrowedList = handler.users.getUser(id).bookStatus(); + boolean exist = false; + + for (int i = 0; i < borrowedList.length; i++) + { + if (borrowedList[i] == Integer.parseInt(givenBookId.getText())) + { + exist = true; + } + } + + if (exist) + { + doClose(Integer.parseInt(givenBookId.getText())); + } + else + { + invalidLabel.setVisible(true); + } + } + catch (Exception e) + { + invalidLabel.setVisible(true); + } + }//GEN-LAST:event_okButtonActionPerformed + + private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed + doClose(RET_CANCEL); + }//GEN-LAST:event_cancelButtonActionPerformed + + /** + * Closes the dialog + */ + private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog + doClose(RET_CANCEL); + }//GEN-LAST:event_closeDialog + + 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(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the dialog */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + ReturnDialog dialog = new ReturnDialog(new javax.swing.JFrame(), true, handler, id); + 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 cancelButton; + private javax.swing.JTextField givenBookId; + private javax.swing.JLabel invalidLabel; + private javax.swing.JLabel label; + private javax.swing.JButton okButton; + // End of variables declaration//GEN-END:variables + + private int returnStatus = RET_CANCEL; +} diff --git a/src/main/java/libsys/UserFactory.java b/src/main/java/libsys/UserFactory.java index 1a4cf7e..e7a0d05 100644 --- a/src/main/java/libsys/UserFactory.java +++ b/src/main/java/libsys/UserFactory.java @@ -33,9 +33,7 @@ class UserFactory FileInputStream in = new FileInputStream(userFilename); JSONObject obj = new JSONObject(new JSONTokener(in)); String [] ids = JSONObject.getNames(obj); - System.out.println(); - System.out.println("Parsing data into ArrayList..."); for (int i = 0; i < ids.length; i++) { JSONObject jsonUser = obj.getJSONObject(ids[i]); @@ -52,13 +50,39 @@ class UserFactory users.add(new User(name, id, limit, books)); } - System.out.println(); in.close(); } catch (Exception ex) { System.out.println("Exception importing from json: " + ex.getMessage()); } + + id = getUser(users.size()-1).getId() + 1; + } + + public void toJsonFile(String userFilename) + { + try + { + PrintWriter out = new PrintWriter(userFilename); + JSONObject usersObj = new JSONObject(); + for (int i = 0; i < users.size(); i++) + { + User user = users.get(i); + JSONObject userObj = new JSONObject(); + userObj.put("Name", user.getName()); + userObj.put("Limit", user.getLimit()); + userObj.put("Books", user.bookStatus()); + usersObj.put(Integer.toString(user.getId()), userObj); + } + out.println(usersObj.toString(4)); + out.close(); + } + catch (Exception e) + { + System.out.println("exception: " + e.getMessage()); + e.printStackTrace(); + } } public User newUser(String name, int limit) diff --git a/users.json b/users.json index 9ba627d..b4dc94e 100644 --- a/users.json +++ b/users.json @@ -1,9 +1,12 @@ -{"0": { - "Name": "Whoever", - "Limit": "4", - "Books": [ - 0, - 1 - ] -} +{ + "0": { + "Limit": 2, + "Books": [], + "Name": "FirstName LastName" + }, + "1": { + "Limit": 100, + "Books": [0], + "Name": "John Doe" + } }