diff --git a/books.json b/books.json new file mode 100644 index 0000000..fd1c340 --- /dev/null +++ b/books.json @@ -0,0 +1,8 @@ +{ + "0": + { + "Title": "Cracking the Coding Interview", + "Status": "AVAILABLE", + "Due Date": [10, 10, 2010] + } +} diff --git a/pom.xml b/pom.xml index 5ec68dd..5ea4591 100644 --- a/pom.xml +++ b/pom.xml @@ -15,11 +15,9 @@ test - org - json - 1.0 - system - ${basedir}/src/main/resources/json.jar + org.json + org.json + chargebee-1.0 @@ -41,16 +39,6 @@ 1.6 - - org.apache.maven.plugins - maven-surefire-plugin - 2.20 - - - /home/binhong/Git/LibrarySystem/lib/json.jar - - - diff --git a/src/main/java/libsys/Book.java b/src/main/java/libsys/Book.java index e5ba362..38ed3b3 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/31/2016 + * Last edited : 5/28/2017 */ class Book @@ -31,6 +31,14 @@ class Book this.status = "AVAILABLE"; } + public Book(int id, String title, String status, int[] dueDate) + { + this.title = title; + this.id = id; + this.status = status; + this.dueDate = dueDate; + } + //Getters and setters public String getTitle() { diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java index 8249cf1..668935d 100644 --- a/src/main/java/libsys/BookFactory.java +++ b/src/main/java/libsys/BookFactory.java @@ -4,10 +4,17 @@ package libsys; * Last edited : 5/28/2017 */ -import java.util.*; +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 BookFactory { @@ -19,6 +26,40 @@ class BookFactory id = 0; } + public BookFactory(String jsonFileName) + { + try + { + System.out.println("Reading data from file..."); + FileInputStream in = new FileInputStream(jsonFileName); + 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]); + int id = Integer.parseInt(ids[i]); + String title = jsonBook.getString("Title"); + String status = jsonBook.getString("Status"); + JSONArray jsonDueDate = jsonBook.getJSONArray("Due Date"); + int [] dueDate = new int[jsonDueDate.length()]; + + for (int j = 0; j < jsonDueDate.length(); j++) + { + dueDate[j] = jsonDueDate.optInt(j); + } + books.add(new Book(id, title, status, dueDate)); + } + System.out.println(); + } + catch (Exception ex) + { + System.out.println(ex.getMessage()); + } + } + public Book newBook(String title) { Book temp = new Book(title, id); diff --git a/src/main/java/libsys/Handler.java b/src/main/java/libsys/Handler.java new file mode 100644 index 0000000..3182014 --- /dev/null +++ b/src/main/java/libsys/Handler.java @@ -0,0 +1,127 @@ +package libsys; + +import java.util.Calendar; +import org.json.JSONObject; + +class Handler +{ + Exception BookNotFound = new Exception("Error 404 : Book not found"); + BookFactory books = new BookFactory(); + UserFactory users = new UserFactory(); + Calendar cal; + + public boolean borrowBook(User user, Book book) + { + User newUser = user; + + if(book.getStatus() == "AVAILABLE" && newUser.status()) + { + book.rent(calDueDate(10)); + newUser.borrowNewBook(book.getId()); + + books.update(book); + users.update(user, newUser); + + return true; + } + + return false; + } + + public boolean returnBook(User user, Book book) + { + User newUser = user; + + if(newUser.returnBook(book.getId())) + { + book.returned(); + + books.update(book); + users.update(user, newUser); + + return true; + } + + return false; + } + + public int[] calDueDate(int days) + { + int[] dueDate = currentDay(); + + for (int i = 0; i < days; i++) + { + if(dueDate[2] < 28) + { + dueDate[2]++; + } + else if(dueDate[1] == 12) + { + if(dueDate[2] == 31) + { + dueDate[0]++; + dueDate[1] = 1; + dueDate[2] = 1; + } + } + else if(dueDate[1] == 2) + { + if((dueDate[0] % 4) == 0) + { + if(dueDate[2] == 29) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[2] == 28) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[1] == 4 || dueDate[1] == 6 || dueDate[1] == 9 || dueDate[1] == 11) + { + if(dueDate[2] == 30) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + else if(dueDate[2] == 31) + { + dueDate[1]++; + dueDate[2] = 1; + } + else + { + dueDate[2]++; + } + } + + return dueDate; + } + + public int[] currentDay() + { + int[] currentDay = new int[3]; + + currentDay[0] = cal.get(Calendar.YEAR) + 1900; + currentDay[1] = cal.get(Calendar.MONTH) + 1; + currentDay[2] = cal.get(Calendar.DAY_OF_MONTH); + + return currentDay; + } +} diff --git a/src/main/java/libsys/Main.java b/src/main/java/libsys/Main.java index 7529a8e..eb5da75 100644 --- a/src/main/java/libsys/Main.java +++ b/src/main/java/libsys/Main.java @@ -1,135 +1,15 @@ package libsys; /* * Written by : Bin Hong Lee - * Last edited : 5/27/2017 + * Last edited : 5/28/2017 */ -import java.util.Date; - class Main { - Exception BookNotFound = new Exception("Error 404 : Book not found"); - static BookFactory books = new BookFactory(); - static UserFactory users = new UserFactory(); - static Date date = new Date(); - public static void main(String[] args) { - new MainGUI().setVisible(true); - } - - public static boolean borrowBook(User user, Book book) - { - User newUser = user; - - if(book.getStatus() == "AVAILABLE" && newUser.status()) - { - book.rent(calDueDate(10)); - newUser.borrowNewBook(book.getId()); - - books.update(book); - users.update(user, newUser); - - return true; - } - - return false; - } - - public static boolean returnBook(User user, Book book) - { - User newUser = user; - - if(newUser.returnBook(book.getId())) - { - book.returned(); - - books.update(book); - users.update(user, newUser); - - return true; - } - - return false; - } - - public static int[] calDueDate(int days) - { - int[] dueDate = currentDay(); - - for (int i = 0; i < days; i++) - { - if(dueDate[2] < 28) - { - dueDate[2]++; - } - else if(dueDate[1] == 12) - { - if(dueDate[2] == 31) - { - dueDate[0]++; - dueDate[1] = 1; - dueDate[2] = 1; - } - } - else if(dueDate[1] == 2) - { - if((dueDate[0] % 4) == 0) - { - if(dueDate[2] == 29) - { - dueDate[1]++; - dueDate[2] = 1; - } - else - { - dueDate[2]++; - } - } - else if(dueDate[2] == 28) - { - dueDate[1]++; - dueDate[2] = 1; - } - else - { - dueDate[2]++; - } - } - else if(dueDate[1] == 4 || dueDate[1] == 6 || dueDate[1] == 9 || dueDate[1] == 11) - { - if(dueDate[2] == 30) - { - dueDate[1]++; - dueDate[2] = 1; - } - else - { - dueDate[2]++; - } - } - else if(dueDate[2] == 31) - { - dueDate[1]++; - dueDate[2] = 1; - } - else - { - dueDate[2]++; - } - } - - return dueDate; - } - - public static int[] currentDay() - { - int[] currentDay = new int[3]; - - currentDay[0] = date.getYear() + 1900; - currentDay[1] = date.getMonth() + 1; - currentDay[2] = date.getDate(); - - return currentDay; + Handler handler = new Handler(); + handler.books = new BookFactory("books.json"); + new MainGUI(handler).setVisible(true); } } diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 7a922b3..0e6987b 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -1,14 +1,20 @@ package libsys; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +@SuppressWarnings({"unchecked", "serial"}) + public class MainGUI extends javax.swing.JFrame { - public MainGUI() + Handler handler; + + public MainGUI(Handler handler) { + this.handler = handler; initComponents(); } - @SuppressWarnings("unchecked") - //GEN-BEGIN:initComponents private void initComponents() { @@ -36,6 +42,13 @@ public class MainGUI extends javax.swing.JFrame bookMgtLabel.setText("Book Management"); bookSearchBtn.setText("Search"); + bookSearchBtn.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + bookSearchBtnActionPerformed(evt); + } + }); bookTitle.setText("Title : "); @@ -173,6 +186,14 @@ public class MainGUI extends javax.swing.JFrame } //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()); + } + //GEN-BEGIN:variables private javax.swing.JLabel bookID; private javax.swing.JLabel bookMgtLabel; @@ -192,4 +213,6 @@ public class MainGUI extends javax.swing.JFrame private javax.swing.JTextField userSearch; private javax.swing.JButton userSearchBtn; //GEN-END:variables + + } diff --git a/src/main/resources/json.jar b/src/main/resources/json.jar deleted file mode 100644 index 82f08ba..0000000 Binary files a/src/main/resources/json.jar and /dev/null differ