Browse Source

Added JSON implementation for books

master
BinHong Lee 7 years ago
parent
commit
76017c0d0d
8 changed files with 219 additions and 144 deletions
  1. +8
    -0
      books.json
  2. +3
    -15
      pom.xml
  3. +9
    -1
      src/main/java/libsys/Book.java
  4. +42
    -1
      src/main/java/libsys/BookFactory.java
  5. +127
    -0
      src/main/java/libsys/Handler.java
  6. +4
    -124
      src/main/java/libsys/Main.java
  7. +26
    -3
      src/main/java/libsys/MainGUI.java
  8. BIN
      src/main/resources/json.jar

+ 8
- 0
books.json View File

@@ -0,0 +1,8 @@
{
"0":
{
"Title": "Cracking the Coding Interview",
"Status": "AVAILABLE",
"Due Date": [10, 10, 2010]
}
}

+ 3
- 15
pom.xml View File

@@ -15,11 +15,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org</groupId>
<artifactId>json</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/json.jar</systemPath>
<groupId>org.json</groupId>
<artifactId>org.json</artifactId>
<version>chargebee-1.0</version>
</dependency>
</dependencies>
<build>
@@ -41,16 +39,6 @@
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>/home/binhong/Git/LibrarySystem/lib/json.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
</build>
</project>

+ 9
- 1
src/main/java/libsys/Book.java View File

@@ -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()
{


+ 42
- 1
src/main/java/libsys/BookFactory.java View File

@@ -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);


+ 127
- 0
src/main/java/libsys/Handler.java View File

@@ -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;
}
}

+ 4
- 124
src/main/java/libsys/Main.java View File

@@ -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);
}
}

+ 26
- 3
src/main/java/libsys/MainGUI.java View File

@@ -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


}

BIN
src/main/resources/json.jar View File


Loading…
Cancel
Save