diff --git a/pom.xml b/pom.xml index 569d1dc..7e90305 100644 --- a/pom.xml +++ b/pom.xml @@ -36,9 +36,32 @@ ${kotlin.version} test + + com.intellij + forms_rt + 7.0.3 + + + org.codehaus.mojo + ideauidesigner-maven-plugin + 1.0-beta-1 + + + + javac2 + + + + + + true + true + true + + org.codehaus.mojo exec-maven-plugin diff --git a/src/main/java/libsys/BookFactory.java b/src/main/java/libsys/BookFactory.java deleted file mode 100644 index 8129859..0000000 --- a/src/main/java/libsys/BookFactory.java +++ /dev/null @@ -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 books = new ArrayList(); - 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(); - } -} diff --git a/src/main/java/libsys/DeleteBook.form b/src/main/java/libsys/DeleteBook.form new file mode 100644 index 0000000..027a5b8 --- /dev/null +++ b/src/main/java/libsys/DeleteBook.form @@ -0,0 +1,73 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/libsys/DeleteBook.java b/src/main/java/libsys/DeleteBook.java new file mode 100644 index 0000000..878c75e --- /dev/null +++ b/src/main/java/libsys/DeleteBook.java @@ -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; + } +} diff --git a/src/main/java/libsys/Main.java b/src/main/java/libsys/Main.java index fbfb462..ec0283e 100644 --- a/src/main/java/libsys/Main.java +++ b/src/main/java/libsys/Main.java @@ -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) diff --git a/src/main/java/libsys/Settings.java b/src/main/java/libsys/Settings.java index 7d48883..9250d09 100644 --- a/src/main/java/libsys/Settings.java +++ b/src/main/java/libsys/Settings.java @@ -61,7 +61,7 @@ public class Settings } catch (Exception e) { - System.out.println(e.getMessage()); + System.out.println("Invalid output filename"); setDefault(); } } diff --git a/src/main/java/libsys/UserFactory.java b/src/main/java/libsys/UserFactory.java deleted file mode 100644 index 95800e7..0000000 --- a/src/main/java/libsys/UserFactory.java +++ /dev/null @@ -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 users = new ArrayList(); - 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 books = new ArrayList(); - 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(); - } -} diff --git a/src/main/kotlin/libsys/BookFactory.kt b/src/main/kotlin/libsys/BookFactory.kt new file mode 100644 index 0000000..0b774c2 --- /dev/null +++ b/src/main/kotlin/libsys/BookFactory.kt @@ -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() + 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() + } +} diff --git a/src/main/kotlin/libsys/UserFactory.kt b/src/main/kotlin/libsys/UserFactory.kt new file mode 100644 index 0000000..d257732 --- /dev/null +++ b/src/main/kotlin/libsys/UserFactory.kt @@ -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() + 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()) + { + 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() + } +}