@@ -36,9 +36,32 @@ | |||
<version>${kotlin.version}</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.intellij</groupId> | |||
<artifactId>forms_rt</artifactId> | |||
<version>7.0.3</version> | |||
</dependency> | |||
</dependencies> | |||
<build> | |||
<plugins> | |||
<plugin> | |||
<groupId>org.codehaus.mojo</groupId> | |||
<artifactId>ideauidesigner-maven-plugin</artifactId> | |||
<version>1.0-beta-1</version> | |||
<executions> | |||
<execution> | |||
<goals> | |||
<goal>javac2</goal> | |||
</goals> | |||
</execution> | |||
</executions> | |||
<configuration> | |||
<fork>true</fork> | |||
<debug>true</debug> | |||
<failOnError>true</failOnError> | |||
</configuration> | |||
</plugin> | |||
<plugin> | |||
<groupId>org.codehaus.mojo</groupId> | |||
<artifactId>exec-maven-plugin</artifactId> | |||
@@ -1,210 +0,0 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 7/4/2017 | |||
*/ | |||
import java.util.List; | |||
import java.util.ArrayList; | |||
import org.json.JSONObject; | |||
import org.json.JSONTokener; | |||
import org.json.JSONArray; | |||
import java.io.FileInputStream; | |||
import java.io.PrintWriter; | |||
/** | |||
* Handles all the Book(s) | |||
*/ | |||
public class BookFactory | |||
{ | |||
private List<Book> books = new ArrayList<Book>(); | |||
private int id; | |||
private String bookFilename; | |||
/** | |||
* Create a new empty BookFactory | |||
*/ | |||
public BookFactory() | |||
{ | |||
id = 0; | |||
bookFilename = "books.json"; | |||
} | |||
/** | |||
* Create a new BookFactory and fill it with information from a JSON file | |||
* @param bookFilename Name of the input JSON file | |||
*/ | |||
public BookFactory(String bookFilename) | |||
{ | |||
try | |||
{ | |||
FileInputStream in = new FileInputStream(bookFilename); | |||
JSONObject obj = new JSONObject(new JSONTokener(in)); | |||
String [] ids = JSONObject.getNames(obj); | |||
for (String id1 : ids) { | |||
JSONObject jsonBook = obj.getJSONObject(id1); | |||
int id = Integer.parseInt(id1); | |||
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)); | |||
} | |||
in.close(); | |||
id = books.get(books.size() - 1).getId() + 1; | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.out.println(ex.getMessage()); | |||
id = 0; | |||
} | |||
this.bookFilename = bookFilename; | |||
} | |||
/** | |||
* Output the data into a JSON file replacing the input file (or if filename not given, "books.json") | |||
*/ | |||
void toJsonFile() | |||
{ | |||
try | |||
{ | |||
PrintWriter out = new PrintWriter(bookFilename); | |||
JSONObject booksObj = new JSONObject(); | |||
for (Book book : books) { | |||
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(); | |||
} | |||
catch (Exception e) | |||
{ | |||
System.out.println("exception: " + e.getMessage()); | |||
e.printStackTrace(); | |||
} | |||
} | |||
/** | |||
* Update the output filename for the object | |||
* @param bookFilename The new filename | |||
*/ | |||
void setBookFileName(String bookFilename) | |||
{ | |||
this.bookFilename = bookFilename; | |||
} | |||
/** | |||
* Adds a new Book into this class | |||
* @param title Title of the Book | |||
* @param status Status of the Book | |||
* @return The new Book that is just created | |||
*/ | |||
Book newBook(String title, String status) | |||
{ | |||
Book temp = new Book(title, id, status); | |||
books.add(temp); | |||
id++; | |||
toJsonFile(); | |||
return temp; | |||
} | |||
/** | |||
* Looks for a Book with the given id | |||
* @param index id of the Book to be found | |||
* @return Book with the given id | |||
*/ | |||
Book getBook(int index) | |||
{ | |||
return search(index, 0, books.size() - 1); | |||
} | |||
/** | |||
* Recursive binary search through the array list for the Book with the given id | |||
* @param index id of the Book to be found | |||
* @param start Starting point to search | |||
* @param end Ending point to search | |||
* @return Book with the given id | |||
*/ | |||
private Book search(int index, int start, int end) | |||
{ | |||
if (start == end && books.get(start).getId() == index) | |||
{ | |||
return books.get(start); | |||
} | |||
if (start >= end) | |||
{ | |||
throw new NullPointerException(); | |||
} | |||
int currentId = ((start + end) / 2); | |||
if (books.get(currentId).getId() == index) | |||
{ | |||
return books.get(currentId); | |||
} | |||
else if (books.get(currentId).getId() > index) | |||
{ | |||
return search(index, start, currentId - 1); | |||
} | |||
else | |||
{ | |||
return search(index, currentId + 1, end); | |||
} | |||
} | |||
/** | |||
* Linear search through the array list for Book with the given Title | |||
* @param title Title of the Book to be found | |||
* @return Book with the given title | |||
*/ | |||
Book getBook(String title) | |||
{ | |||
for (Book temp : books) { | |||
if (temp.getTitle().equals(title)) | |||
{ | |||
return temp; | |||
} | |||
} | |||
throw new NullPointerException(); | |||
} | |||
/** | |||
* Replacing a Book in the array list with a new Book | |||
* @param oldBook Book to be replaced | |||
* @param newBook Book replacing it | |||
*/ | |||
public void update(Book oldBook, Book newBook) | |||
{ | |||
boolean found = false; | |||
for (int i = 0; i < books.size(); i++) | |||
{ | |||
Book temp = books.get(i); | |||
if(temp.getId() == oldBook.getId()) | |||
{ | |||
books.set(i, newBook); | |||
found = true; | |||
} | |||
} | |||
if (!found) | |||
{ | |||
throw new NullPointerException(); | |||
} | |||
toJsonFile(); | |||
} | |||
} |
@@ -0,0 +1,73 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="libsys.DeleteBook"> | |||
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1"> | |||
<margin top="10" left="10" bottom="10" right="10"/> | |||
<constraints> | |||
<xy x="48" y="54" width="550" height="173"/> | |||
</constraints> | |||
<properties/> | |||
<border type="none"/> | |||
<children> | |||
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | |||
<margin top="0" left="0" bottom="0" right="0"/> | |||
<constraints> | |||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties/> | |||
<border type="none"/> | |||
<children> | |||
<hspacer id="98af6"> | |||
<constraints> | |||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
</hspacer> | |||
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1"> | |||
<margin top="0" left="0" bottom="0" right="0"/> | |||
<constraints> | |||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties/> | |||
<border type="none"/> | |||
<children> | |||
<component id="e7465" class="javax.swing.JButton" binding="buttonOK"> | |||
<constraints> | |||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties> | |||
<text value="OK"/> | |||
</properties> | |||
</component> | |||
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel"> | |||
<constraints> | |||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties> | |||
<text value="Cancel"/> | |||
</properties> | |||
</component> | |||
</children> | |||
</grid> | |||
</children> | |||
</grid> | |||
<grid id="e3588" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | |||
<margin top="0" left="0" bottom="0" right="0"/> | |||
<constraints> | |||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties/> | |||
<border type="none"/> | |||
<children> | |||
<component id="f9920" class="javax.swing.JLabel" binding="warningLbl"> | |||
<constraints> | |||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/> | |||
</constraints> | |||
<properties> | |||
<font size="26"/> | |||
<text value="Are you sure you want to delete this book?"/> | |||
</properties> | |||
</component> | |||
</children> | |||
</grid> | |||
</children> | |||
</grid> | |||
</form> |
@@ -0,0 +1,113 @@ | |||
package libsys; | |||
import com.intellij.uiDesigner.core.GridConstraints; | |||
import com.intellij.uiDesigner.core.GridLayoutManager; | |||
import com.intellij.uiDesigner.core.Spacer; | |||
import javax.swing.*; | |||
import java.awt.*; | |||
import java.awt.event.*; | |||
public class DeleteBook extends JDialog { | |||
private JPanel contentPane; | |||
private JButton buttonOK; | |||
private JButton buttonCancel; | |||
private JLabel warningLbl; | |||
public DeleteBook() { | |||
setContentPane(contentPane); | |||
setModal(true); | |||
getRootPane().setDefaultButton(buttonOK); | |||
buttonOK.addActionListener(new ActionListener() { | |||
public void actionPerformed(ActionEvent e) { | |||
onOK(); | |||
} | |||
}); | |||
buttonCancel.addActionListener(new ActionListener() { | |||
public void actionPerformed(ActionEvent e) { | |||
onCancel(); | |||
} | |||
}); | |||
// call onCancel() when cross is clicked | |||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); | |||
addWindowListener(new WindowAdapter() { | |||
public void windowClosing(WindowEvent e) { | |||
onCancel(); | |||
} | |||
}); | |||
// call onCancel() on ESCAPE | |||
contentPane.registerKeyboardAction(new ActionListener() { | |||
public void actionPerformed(ActionEvent e) { | |||
onCancel(); | |||
} | |||
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); | |||
} | |||
private void onOK() { | |||
// add your code here | |||
dispose(); | |||
} | |||
private void onCancel() { | |||
// add your code here if necessary | |||
dispose(); | |||
} | |||
public static void main(String[] args) { | |||
DeleteBook dialog = new DeleteBook(); | |||
dialog.pack(); | |||
dialog.setVisible(true); | |||
System.exit(0); | |||
} | |||
{ | |||
// GUI initializer generated by IntelliJ IDEA GUI Designer | |||
// >>> IMPORTANT!! <<< | |||
// DO NOT EDIT OR ADD ANY CODE HERE! | |||
$$$setupUI$$$(); | |||
} | |||
/** | |||
* Method generated by IntelliJ IDEA GUI Designer | |||
* >>> IMPORTANT!! <<< | |||
* DO NOT edit this method OR call it in your code! | |||
* | |||
* @noinspection ALL | |||
*/ | |||
private void $$$setupUI$$$() { | |||
contentPane = new JPanel(); | |||
contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1)); | |||
final JPanel panel1 = new JPanel(); | |||
panel1.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1)); | |||
contentPane.add(panel1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false)); | |||
final Spacer spacer1 = new Spacer(); | |||
panel1.add(spacer1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); | |||
final JPanel panel2 = new JPanel(); | |||
panel2.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1, true, false)); | |||
panel1.add(panel2, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); | |||
buttonOK = new JButton(); | |||
buttonOK.setText("OK"); | |||
panel2.add(buttonOK, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); | |||
buttonCancel = new JButton(); | |||
buttonCancel.setText("Cancel"); | |||
panel2.add(buttonCancel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); | |||
final JPanel panel3 = new JPanel(); | |||
panel3.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1)); | |||
contentPane.add(panel3, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); | |||
warningLbl = new JLabel(); | |||
warningLbl.setFont(new Font(warningLbl.getFont().getName(), warningLbl.getFont().getStyle(), 26)); | |||
warningLbl.setText("Are you sure you want to delete this book?"); | |||
panel3.add(warningLbl, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); | |||
} | |||
/** | |||
* @noinspection ALL | |||
*/ | |||
public JComponent $$$getRootComponent$$$() { | |||
return contentPane; | |||
} | |||
} |
@@ -1,7 +1,7 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 6/4/2017 | |||
* Last edited : 7/4/2017 | |||
*/ | |||
/** | |||
@@ -9,12 +9,12 @@ package libsys; | |||
*/ | |||
public class Main | |||
{ | |||
static Settings settings = new Settings("settings.txt"); | |||
static String bookFilename = settings.get("Books Filename"); | |||
static String userFilename = settings.get("Users Filename"); | |||
private static Settings settings = new Settings("settings.txt"); | |||
private static String bookFilename = settings.get("Books Filename"); | |||
private static String userFilename = settings.get("Users Filename"); | |||
/** | |||
* Start the applicationn | |||
* Start the application | |||
* @param args | |||
*/ | |||
public static void main(String[] args) | |||
@@ -61,7 +61,7 @@ public class Settings | |||
} | |||
catch (Exception e) | |||
{ | |||
System.out.println(e.getMessage()); | |||
System.out.println("Invalid output filename"); | |||
setDefault(); | |||
} | |||
} | |||
@@ -1,204 +0,0 @@ | |||
package libsys; | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 6/4/2017 | |||
*/ | |||
import org.json.JSONArray; | |||
import org.json.JSONObject; | |||
import org.json.JSONTokener; | |||
import java.io.FileInputStream; | |||
import java.io.PrintWriter; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
/** | |||
* Handles all the User(s) | |||
*/ | |||
public class UserFactory | |||
{ | |||
private List<User> users = new ArrayList<User>(); | |||
private int id; | |||
private String userFilename; | |||
/** | |||
* Create a new empty UserFactory | |||
*/ | |||
public UserFactory() | |||
{ | |||
id = 0; | |||
userFilename = "users.json"; | |||
} | |||
/** | |||
* Create a new UserFactory and fill it with information from a JSON file | |||
* @param userFilename Name of the input JSON file | |||
*/ | |||
public UserFactory(String userFilename) | |||
{ | |||
try | |||
{ | |||
FileInputStream in = new FileInputStream(userFilename); | |||
JSONObject obj = new JSONObject(new JSONTokener(in)); | |||
String [] ids = JSONObject.getNames(obj); | |||
for (String id1 : ids) { | |||
JSONObject jsonUser = obj.getJSONObject(id1); | |||
int id = Integer.parseInt(id1); | |||
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)); | |||
} | |||
in.close(); | |||
id = getUser(users.size()-1).getId() + 1; | |||
} | |||
catch (Exception ex) | |||
{ | |||
System.out.println("Exception importing from json: " + ex.getMessage()); | |||
id = 0; | |||
} | |||
this.userFilename = userFilename; | |||
} | |||
/** | |||
* Output the data into a JSON file replacing the input file (or if filename not given, "users.json") | |||
*/ | |||
void toJsonFile() | |||
{ | |||
try | |||
{ | |||
PrintWriter out = new PrintWriter(userFilename); | |||
JSONObject usersObj = new JSONObject(); | |||
for (User user : users) { | |||
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(); | |||
} | |||
} | |||
/** | |||
* Update the output filename for the object | |||
* @param userFilename The new filename | |||
*/ | |||
void setUserFileName(String userFilename) | |||
{ | |||
this.userFilename = userFilename; | |||
} | |||
/** | |||
* Adds a new User into this class | |||
* @param name Name of the User | |||
* @param limit Limit of Book the User can borrow | |||
* @return The new User that is just created | |||
*/ | |||
User newUser(String name, int limit) | |||
{ | |||
User temp = new User(name, id, limit); | |||
users.add(temp); | |||
id++; | |||
toJsonFile(); | |||
return temp; | |||
} | |||
/** | |||
* Looks for the User with the given name | |||
* @param name Name of the User to be found | |||
* @return User with the given name | |||
*/ | |||
User getUser(String name) | |||
{ | |||
for (User temp : users) { | |||
if (temp.getName().equals(name)) { | |||
return temp; | |||
} | |||
} | |||
throw new NullPointerException(); | |||
} | |||
/** | |||
* Looks for a User with the given id | |||
* @param index id of the User to be found | |||
* @return User with the given id | |||
*/ | |||
User getUser(int index) | |||
{ | |||
return search(index, 0, users.size() - 1); | |||
} | |||
/** | |||
* Recursive binary search through the array list for the User with the given id | |||
* @param index id of the User to be found | |||
* @param start Starting point to search | |||
* @param end Ending point to search | |||
* @return User with the given id | |||
*/ | |||
private User search(int index, int start, int end) | |||
{ | |||
if (start == end && users.get(start).getId() == index) | |||
{ | |||
return users.get(start); | |||
} | |||
if (start >= end) | |||
{ | |||
throw new NullPointerException(); | |||
} | |||
int currentId = ((start + end) / 2); | |||
if (users.get(currentId).getId() == index) | |||
{ | |||
return users.get(currentId); | |||
} | |||
else if (users.get(currentId).getId() > index) | |||
{ | |||
return search(index, start, currentId - 1); | |||
} | |||
else | |||
{ | |||
return search(index, currentId + 1, end); | |||
} | |||
} | |||
/** | |||
* Replacing a User in the array list with a new User | |||
* @param oldUser User to be replaced | |||
* @param newUser User replacing it | |||
*/ | |||
public void update(User oldUser, User newUser) | |||
{ | |||
for (int i = 0; i < users.size(); i++) | |||
{ | |||
User temp = users.get(i); | |||
if(temp.getId() == oldUser.getId()) | |||
{ | |||
users.set(i, newUser); | |||
} | |||
} | |||
toJsonFile(); | |||
} | |||
} |
@@ -0,0 +1,208 @@ | |||
package libsys | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 7/4/2017 | |||
*/ | |||
import java.util.ArrayList | |||
import org.json.JSONObject | |||
import org.json.JSONTokener | |||
import java.io.FileInputStream | |||
import java.io.PrintWriter | |||
/** | |||
* Handles all the Book(s) | |||
*/ | |||
class BookFactory { | |||
private val books = ArrayList<Book>() | |||
private var id: Int = 0 | |||
private var bookFilename: String? = null | |||
/** | |||
* @constructor Create a new empty BookFactory | |||
*/ | |||
constructor() { | |||
id = 0 | |||
bookFilename = "books.json" | |||
} | |||
/** | |||
* @constructor Create a new BookFactory and fill it with information from a JSON file | |||
* | |||
* @param bookFilename Name of the input JSON file | |||
*/ | |||
constructor(bookFilename: String) { | |||
try { | |||
val `in` = FileInputStream(bookFilename) | |||
val obj = JSONObject(JSONTokener(`in`)) | |||
val ids = JSONObject.getNames(obj) | |||
for (id1 in ids) { | |||
val jsonBook = obj.getJSONObject(id1) | |||
val id = Integer.parseInt(id1) | |||
val title = jsonBook.getString("Title") | |||
val status = jsonBook.getString("Status") | |||
val jsonDueDate = jsonBook.getJSONArray("Due Date") | |||
val dueDate = IntArray(jsonDueDate.length()) | |||
for (j in 0..jsonDueDate.length() - 1) { | |||
dueDate[j] = jsonDueDate.optInt(j) | |||
} | |||
books.add(Book(id, title, status, dueDate)) | |||
} | |||
`in`.close() | |||
id = books[books.size - 1].id + 1 | |||
} catch (ex: Exception) { | |||
println(ex.message) | |||
id = 0 | |||
} | |||
this.bookFilename = bookFilename | |||
} | |||
/** | |||
* Output the data into a JSON file replacing the input file (or if filename not given, "books.json") | |||
*/ | |||
fun toJsonFile() | |||
{ | |||
try | |||
{ | |||
val out = PrintWriter(bookFilename!!) | |||
val booksObj = JSONObject() | |||
for (book in books) | |||
{ | |||
val bookObj = JSONObject() | |||
bookObj.put("Title", book.title) | |||
bookObj.put("Status", book.status) | |||
bookObj.put("Due Date", book.dueDate) | |||
booksObj.put(Integer.toString(book.id), bookObj) | |||
} | |||
out.println(booksObj.toString(4)) | |||
out.close() | |||
} | |||
catch (e: Exception) | |||
{ | |||
println("Invalid output filename.") | |||
} | |||
} | |||
/** | |||
* Update the output filename for the object | |||
* @param bookFilename The new filename | |||
*/ | |||
fun setBookFileName(bookFilename: String) | |||
{ | |||
this.bookFilename = bookFilename | |||
} | |||
/** | |||
* Adds a new Book into this class | |||
* @param title Title of the Book | |||
* @param status Status of the Book | |||
* | |||
* @return The new Book that is just created | |||
*/ | |||
fun newBook(title: String, status: String): Book | |||
{ | |||
val temp = Book(title, id, status) | |||
books.add(temp) | |||
id++ | |||
toJsonFile() | |||
return temp | |||
} | |||
/** | |||
* Looks for a Book with the given id | |||
* @param index id of the Book to be found | |||
* | |||
* @return Book with the given id | |||
*/ | |||
fun getBook(index: Int): Book | |||
{ | |||
return search(index, 0, books.size - 1) | |||
} | |||
/** | |||
* Recursive binary search through the array list for the Book with the given id | |||
* @param index id of the Book to be found | |||
* @param start Starting point to search | |||
* @param end Ending point to search | |||
* | |||
* @return Book with the given id | |||
*/ | |||
private fun search(index: Int, start: Int, end: Int): Book | |||
{ | |||
if (start == end && books[start].id == index) | |||
{ | |||
return books[start] | |||
} | |||
if (start >= end) | |||
{ | |||
throw NullPointerException() | |||
} | |||
val currentId = (start + end) / 2 | |||
if (books[currentId].id == index) | |||
{ | |||
return books[currentId] | |||
} | |||
else if (books[currentId].id > index) | |||
{ | |||
return search(index, start, currentId - 1) | |||
} | |||
else | |||
{ | |||
return search(index, currentId + 1, end) | |||
} | |||
} | |||
/** | |||
* Linear search through the array list for Book with the given Title | |||
* @param title Title of the Book to be found | |||
* | |||
* @return Book with the given title | |||
*/ | |||
fun getBook(title: String): Book | |||
{ | |||
books | |||
.asSequence() | |||
.filter { it.title == title } | |||
.forEach { return it } | |||
throw NullPointerException() | |||
} | |||
/** | |||
* Replacing a Book in the array list with a new Book | |||
* @param oldBook Book to be replaced | |||
* @param newBook Book replacing it | |||
*/ | |||
fun update(oldBook: Book, newBook: Book) | |||
{ | |||
var found = false | |||
for (i in books.indices) | |||
{ | |||
val temp = books[i] | |||
if (temp.id == oldBook.id) | |||
{ | |||
books[i] = newBook | |||
found = true | |||
} | |||
} | |||
if (!found) | |||
{ | |||
throw NullPointerException() | |||
} | |||
toJsonFile() | |||
} | |||
} |
@@ -0,0 +1,181 @@ | |||
package libsys | |||
/* | |||
* Written by : Bin Hong Lee | |||
* Last edited : 7/4/2017 | |||
*/ | |||
import org.json.JSONObject | |||
import org.json.JSONTokener | |||
import java.io.FileInputStream | |||
import java.io.PrintWriter | |||
import java.util.ArrayList | |||
/** | |||
* Handles all the User(s) | |||
*/ | |||
class UserFactory { | |||
private val users = ArrayList<User>() | |||
private var id: Int = 0 | |||
private var userFilename: String? = null | |||
/** | |||
* @constructor Create a new empty UserFactory | |||
*/ | |||
constructor() { | |||
id = 0 | |||
userFilename = "users.json" | |||
} | |||
/** | |||
* @constructor Create a new UserFactory and fill it with information from a JSON file | |||
* @param userFilename Name of the input JSON file | |||
*/ | |||
constructor(userFilename: String) { | |||
try { | |||
val `in` = FileInputStream(userFilename) | |||
val obj = JSONObject(JSONTokener(`in`)) | |||
val ids = JSONObject.getNames(obj) | |||
for (id1 in ids) { | |||
val jsonUser = obj.getJSONObject(id1) | |||
val id = Integer.parseInt(id1) | |||
val name = jsonUser.getString("Name") | |||
val limit = jsonUser.getInt("Limit") | |||
val jsonBooks = jsonUser.getJSONArray("Books") | |||
val books = (0..jsonBooks.length() - 1).mapTo(ArrayList<Int>()) | |||
{ | |||
Integer.parseInt(jsonBooks.get(it).toString()) | |||
} | |||
users.add(User(name, id, limit, books)) | |||
} | |||
`in`.close() | |||
id = getUser(users.size - 1).id + 1 | |||
} catch (ex: Exception) { | |||
println("Exception importing from json: " + ex.message) | |||
id = 0 | |||
} | |||
this.userFilename = userFilename | |||
} | |||
/** | |||
* Output the data into a JSON file replacing the input file (or if filename not given, "users.json") | |||
*/ | |||
fun toJsonFile() { | |||
try { | |||
val out = PrintWriter(userFilename!!) | |||
val usersObj = JSONObject() | |||
for (user in users) { | |||
val userObj = JSONObject() | |||
userObj.put("Name", user.name) | |||
userObj.put("Limit", user.limit) | |||
userObj.put("Books", user.bookStatus()) | |||
usersObj.put(Integer.toString(user.id), userObj) | |||
} | |||
out.println(usersObj.toString(4)) | |||
out.close() | |||
} catch (e: Exception) { | |||
println("Invalid output filename") | |||
} | |||
} | |||
/** | |||
* Update the output filename for the object | |||
* @param userFilename The new filename | |||
*/ | |||
fun setUserFileName(userFilename: String) { | |||
this.userFilename = userFilename | |||
} | |||
/** | |||
* Adds a new User into this class | |||
* @param name Name of the User | |||
* @param limit Limit of Book the User can borrow | |||
* | |||
* @return The new User that is just created | |||
*/ | |||
fun newUser(name: String, limit: Int): User { | |||
val temp = User(name, id, limit) | |||
users.add(temp) | |||
id++ | |||
toJsonFile() | |||
return temp | |||
} | |||
/** | |||
* Looks for the User with the given name | |||
* @param name Name of the User to be found | |||
* | |||
* @return User with the given name | |||
*/ | |||
fun getUser(name: String): User { | |||
users | |||
.asSequence() | |||
.filter { it.name == name } | |||
.forEach { return it } | |||
throw NullPointerException() | |||
} | |||
/** | |||
* Looks for a User with the given id | |||
* @param index id of the User to be found | |||
* | |||
* @return User with the given id | |||
*/ | |||
fun getUser(index: Int): User { | |||
return search(index, 0, users.size - 1) | |||
} | |||
/** | |||
* Recursive binary search through the array list for the User with the given id | |||
* @param index id of the User to be found | |||
* @param start Starting point to search | |||
* @param end Ending point to search | |||
* | |||
* @return User with the given id | |||
*/ | |||
private fun search(index: Int, start: Int, end: Int): User { | |||
if (start == end && users[start].id == index) { | |||
return users[start] | |||
} | |||
if (start >= end) { | |||
throw NullPointerException() | |||
} | |||
val currentId = (start + end) / 2 | |||
if (users[currentId].id == index) { | |||
return users[currentId] | |||
} else if (users[currentId].id > index) { | |||
return search(index, start, currentId - 1) | |||
} else { | |||
return search(index, currentId + 1, end) | |||
} | |||
} | |||
/** | |||
* Replacing a User in the array list with a new User | |||
* @param oldUser User to be replaced | |||
* @param newUser User replacing it | |||
*/ | |||
fun update(oldUser: User, newUser: User) { | |||
for (i in users.indices) { | |||
val temp = users[i] | |||
if (temp.id == oldUser.id) { | |||
users[i] = newUser | |||
} | |||
} | |||
toJsonFile() | |||
} | |||
} |