@@ -1,8 +1,19 @@ | |||
{ | |||
"0": | |||
{ | |||
{"0": { | |||
"Status": "RENTED", | |||
"Title": "Cracking the Coding Interview", | |||
"Status": "AVAILABLE", | |||
"Due Date": [10, 10, 2010] | |||
} | |||
"Due Date": [ | |||
10, | |||
10, | |||
2010 | |||
] | |||
}, | |||
"1": { | |||
"Status": "RENTED", | |||
"Title": "Who was that?", | |||
"Due Date": [ | |||
1, | |||
1, | |||
1 | |||
] | |||
} | |||
} |
@@ -60,6 +60,11 @@ class Book | |||
return id; | |||
} | |||
public int[] getDueDate() | |||
{ | |||
return dueDate; | |||
} | |||
public void setTitle(String title) | |||
{ | |||
this.title = title; | |||
@@ -26,12 +26,12 @@ class BookFactory | |||
id = 0; | |||
} | |||
public BookFactory(String jsonFileName) | |||
public BookFactory(String bookFilename) | |||
{ | |||
try | |||
{ | |||
System.out.println("Reading data from file..."); | |||
FileInputStream in = new FileInputStream(jsonFileName); | |||
FileInputStream in = new FileInputStream(bookFilename); | |||
JSONObject obj = new JSONObject(new JSONTokener(in)); | |||
String [] ids = JSONObject.getNames(obj); | |||
System.out.println(); | |||
@@ -53,11 +53,41 @@ class BookFactory | |||
books.add(new Book(id, title, status, dueDate)); | |||
} | |||
System.out.println(); | |||
in.close(); | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.out.println(ex.getMessage()); | |||
} | |||
id = getBook(books.size()-1).getId() + 1; | |||
} | |||
public void toJsonFile(String bookFilename) | |||
{ | |||
try | |||
{ | |||
PrintWriter out = new PrintWriter(bookFilename); | |||
JSONObject booksObj = new JSONObject(); | |||
for (int i = 0; i < books.size(); i++) | |||
{ | |||
Book book = books.get(i); | |||
JSONObject bookObj = new JSONObject(); | |||
bookObj.put("Title", book.getTitle()); | |||
bookObj.put("Status", book.getStatus()); | |||
bookObj.put("Due Date", book.getDueDate()); | |||
booksObj.put(Integer.toString(book.getId()), bookObj); | |||
} | |||
out.println(booksObj.toString(4)); | |||
out.close(); | |||
System.out.println("Books exported to JSON file."); | |||
} | |||
catch (Exception e) | |||
{ | |||
System.out.println("exception: " + e.getMessage()); | |||
e.printStackTrace(); | |||
} | |||
} | |||
public Book newBook(String title) | |||
@@ -1,4 +1,8 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 5/28/2017 | |||
*/ | |||
import java.util.Calendar; | |||
import org.json.JSONObject; | |||
@@ -10,6 +14,11 @@ class Handler | |||
UserFactory users = new UserFactory(); | |||
Calendar cal; | |||
public Handler() | |||
{ | |||
} | |||
public boolean borrowBook(User user, Book book) | |||
{ | |||
User newUser = user; | |||
@@ -124,4 +133,19 @@ class Handler | |||
return currentDay; | |||
} | |||
public String bookIdsToTitlesString(int[] bookIDs) | |||
{ | |||
String titlesString = ""; | |||
for (int i = 0; i < bookIDs.length; i++) | |||
{ | |||
titlesString += books.getBook(bookIDs[i]).getTitle(); | |||
titlesString += "; \n"; | |||
} | |||
System.out.println(titlesString); | |||
return titlesString; | |||
} | |||
} |
@@ -6,10 +6,14 @@ package libsys; | |||
class Main | |||
{ | |||
static String bookFilename = "books.json"; | |||
static String userFilename = "users.json"; | |||
public static void main(String[] args) | |||
{ | |||
Handler handler = new Handler(); | |||
handler.books = new BookFactory("books.json"); | |||
handler.books = new BookFactory(bookFilename); | |||
handler.users = new UserFactory(userFilename); | |||
new MainGUI(handler).setVisible(true); | |||
} | |||
} |
@@ -18,8 +18,9 @@ public class MainGUI extends javax.swing.JFrame | |||
//GEN-BEGIN:initComponents | |||
private void initComponents() | |||
{ | |||
jPanel1 = new javax.swing.JPanel(); | |||
jTabbedPane1 = new javax.swing.JTabbedPane(); | |||
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(); | |||
@@ -91,12 +92,19 @@ public class MainGUI extends javax.swing.JFrame | |||
.addContainerGap(38, Short.MAX_VALUE)) | |||
); | |||
jTabbedPane1.addTab("Book", bookPanel); | |||
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 : "); | |||
@@ -147,22 +155,22 @@ public class MainGUI extends javax.swing.JFrame | |||
.addContainerGap(28, Short.MAX_VALUE)) | |||
); | |||
jTabbedPane1.addTab("User", userPanel); | |||
jTabbedPane.addTab("User", userPanel); | |||
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); | |||
jPanel1.setLayout(jPanel1Layout); | |||
jPanel1Layout.setHorizontalGroup( | |||
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |||
.addGroup(jPanel1Layout.createSequentialGroup() | |||
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(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |||
.addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |||
.addContainerGap(32, Short.MAX_VALUE)) | |||
); | |||
jPanel1Layout.setVerticalGroup( | |||
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |||
.addGroup(jPanel1Layout.createSequentialGroup() | |||
mainPanelLayout.setVerticalGroup( | |||
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |||
.addGroup(mainPanelLayout.createSequentialGroup() | |||
.addContainerGap() | |||
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |||
.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)) | |||
); | |||
@@ -172,13 +180,13 @@ public class MainGUI extends javax.swing.JFrame | |||
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |||
.addGroup(layout.createSequentialGroup() | |||
.addGap(0, 7, Short.MAX_VALUE) | |||
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) | |||
.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(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |||
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |||
.addGap(0, 0, Short.MAX_VALUE)) | |||
); | |||
@@ -194,6 +202,15 @@ public class MainGUI extends javax.swing.JFrame | |||
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; | |||
@@ -202,8 +219,8 @@ public class MainGUI extends javax.swing.JFrame | |||
private javax.swing.JButton bookSearchBtn; | |||
private javax.swing.JLabel bookStatus; | |||
private javax.swing.JLabel bookTitle; | |||
private javax.swing.JPanel jPanel1; | |||
private javax.swing.JTabbedPane jTabbedPane1; | |||
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; | |||
@@ -213,6 +230,4 @@ public class MainGUI extends javax.swing.JFrame | |||
private javax.swing.JTextField userSearch; | |||
private javax.swing.JButton userSearchBtn; | |||
//GEN-END:variables | |||
} |
@@ -1,7 +1,7 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 6/6/2016 | |||
* Last edited : 5/28/2017 | |||
*/ | |||
import java.util.*; | |||
@@ -20,6 +20,14 @@ class User | |||
this.limit = limit; | |||
} | |||
public User(String name, int id, int limit, ArrayList<Integer> books) | |||
{ | |||
this.name = name; | |||
this.id = id; | |||
this.limit = limit; | |||
this.books = books; | |||
} | |||
public String getName() | |||
{ | |||
return name; | |||
@@ -30,6 +38,11 @@ class User | |||
return id; | |||
} | |||
public int getLimit() | |||
{ | |||
return limit; | |||
} | |||
public void setName() | |||
{ | |||
this.name = name; | |||
@@ -45,9 +58,16 @@ class User | |||
return false; | |||
} | |||
public List<Integer> bookStatus() | |||
public int[] bookStatus() | |||
{ | |||
return books; | |||
int[] currentBooks = new int[books.size()]; | |||
for (int i = 0; i < books.size(); i++) | |||
{ | |||
currentBooks[i] = books.get(i).intValue(); | |||
} | |||
return currentBooks; | |||
} | |||
public boolean borrowNewBook(int id) | |||
@@ -1,10 +1,20 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 6/7/2016 | |||
* 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 UserFactory | |||
{ | |||
@@ -16,6 +26,41 @@ class UserFactory | |||
id = 0; | |||
} | |||
public UserFactory(String userFilename) | |||
{ | |||
try | |||
{ | |||
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]); | |||
int id = Integer.parseInt(ids[i]); | |||
String name = jsonUser.getString("Name"); | |||
int limit = jsonUser.getInt("Limit"); | |||
JSONArray jsonBooks = jsonUser.getJSONArray("Books"); | |||
ArrayList<Integer> books = new ArrayList<Integer>(); | |||
for (int j = 0; j < jsonBooks.length(); j++) | |||
{ | |||
books.add(Integer.parseInt(jsonBooks.get(j).toString())); | |||
} | |||
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()); | |||
} | |||
} | |||
public User newUser(String name, int limit) | |||
{ | |||
User temp = new User(name, id, limit); | |||
@@ -0,0 +1,9 @@ | |||
{"0": { | |||
"Name": "Whoever", | |||
"Limit": "4", | |||
"Books": [ | |||
0, | |||
1 | |||
] | |||
} | |||
} |