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 @@
-
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"
+ }
}