diff --git a/src/main/java/libsys/DeleteBookDialog.form b/src/main/java/libsys/DeleteDialog.form similarity index 75% rename from src/main/java/libsys/DeleteBookDialog.form rename to src/main/java/libsys/DeleteDialog.form index d31128c..a9f90cd 100644 --- a/src/main/java/libsys/DeleteBookDialog.form +++ b/src/main/java/libsys/DeleteDialog.form @@ -1,9 +1,9 @@ -
+ - + @@ -34,7 +34,7 @@ - + @@ -42,28 +42,41 @@ - + - + - + + + + + - + + + + + + + + + + - - + + diff --git a/src/main/java/libsys/DeleteBookDialog.java b/src/main/java/libsys/DeleteDialog.java similarity index 66% rename from src/main/java/libsys/DeleteBookDialog.java rename to src/main/java/libsys/DeleteDialog.java index 22f18dc..fa7ffa3 100644 --- a/src/main/java/libsys/DeleteBookDialog.java +++ b/src/main/java/libsys/DeleteDialog.java @@ -8,58 +8,81 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; -public class DeleteBookDialog extends JDialog { +public class DeleteDialog extends JDialog +{ private JPanel contentPane; private JButton buttonOK; private JButton buttonCancel; private JLabel warningLbl; + private JLabel bookNameLbl; + private boolean returnStatus; - public DeleteBookDialog() { + public DeleteDialog(String name) + { setContentPane(contentPane); setModal(true); getRootPane().setDefaultButton(buttonOK); - buttonOK.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { + buttonOK.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { onOK(); } }); - buttonCancel.addActionListener(new ActionListener() { - public void actionPerformed(ActionEvent e) { + 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) { + addWindowListener(new WindowAdapter() + { + public void windowClosing(WindowEvent e) + { onCancel(); } }); // call onCancel() on ESCAPE - contentPane.registerKeyboardAction(new ActionListener() { + contentPane.registerKeyboardAction(new ActionListener() + { public void actionPerformed(ActionEvent e) { onCancel(); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + this.pack(); + bookNameLbl.setText(name + "?"); } - private void onOK() { + private void onOK() + { // add your code here + returnStatus = true; dispose(); } - private void onCancel() { + private void onCancel() + { // add your code here if necessary + returnStatus = false; dispose(); } - public static void main(String[] args) { - DeleteBookDialog dialog = new DeleteBookDialog(); - dialog.pack(); + public boolean getReturnStatus() + { + return returnStatus; + } + + public static void main(String[] args) + { + DeleteDialog dialog = new DeleteDialog("SomeBook or SomeUser"); dialog.setVisible(true); System.exit(0); } @@ -80,7 +103,7 @@ public class DeleteBookDialog extends JDialog { */ private void $$$setupUI$$$() { contentPane = new JPanel(); - contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1)); + contentPane.setLayout(new GridLayoutManager(2, 1, new Insets(10, 10, 10, 10), -1, -1, true, false)); 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)); @@ -90,18 +113,22 @@ public class DeleteBookDialog extends JDialog { 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"); + buttonOK.setText("Yes"); 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"); + buttonCancel.setText("No"); 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)); + panel3.setLayout(new GridLayoutManager(2, 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_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, new Dimension(350, 80), new Dimension(350, 80), new Dimension(99999, 99999), 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)); + warningLbl.setFont(new Font(warningLbl.getFont().getName(), warningLbl.getFont().getStyle(), 20)); + warningLbl.setText("Are you sure you want to delete"); + panel3.add(warningLbl, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); + bookNameLbl = new JLabel(); + bookNameLbl.setFont(new Font(bookNameLbl.getFont().getName(), bookNameLbl.getFont().getStyle(), 20)); + bookNameLbl.setText("{Book Name}"); + panel3.add(bookNameLbl, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); } /** diff --git a/src/main/java/libsys/MainGUI.form b/src/main/java/libsys/MainGUI.form index 94b36ad..7bcc1d3 100644 --- a/src/main/java/libsys/MainGUI.form +++ b/src/main/java/libsys/MainGUI.form @@ -32,9 +32,10 @@ - + + @@ -103,27 +104,32 @@ - - - - - - + - - - - + + + + + + + + + + + + + + + + + + + - - - - - - + @@ -150,7 +156,9 @@ - + + + @@ -205,6 +213,17 @@ + + + + + + + + + + + @@ -233,7 +252,6 @@ - @@ -245,6 +263,11 @@ + + + + + @@ -272,11 +295,19 @@ - - - - - + + + + + + + + + + + + + @@ -354,6 +385,14 @@ + + + + + + + + diff --git a/src/main/java/libsys/MainGUI.java b/src/main/java/libsys/MainGUI.java index 99a3d5c..3e6d568 100644 --- a/src/main/java/libsys/MainGUI.java +++ b/src/main/java/libsys/MainGUI.java @@ -45,6 +45,7 @@ public class MainGUI extends javax.swing.JFrame bookID = new javax.swing.JLabel(); bookDueDate = new javax.swing.JLabel(); editBookBtn = new javax.swing.JButton(); + deleteBookBtn = new javax.swing.JButton(); userPanel = new javax.swing.JPanel(); userMgtLabel = new javax.swing.JLabel(); userSearch = new javax.swing.JTextField(); @@ -56,6 +57,7 @@ public class MainGUI extends javax.swing.JFrame rentBtn = new javax.swing.JButton(); returnBtn = new javax.swing.JButton(); editUserBtn = new javax.swing.JButton(); + deleteUserBtn = new javax.swing.JButton(); nameLabel = new javax.swing.JLabel(); createBookBtn = new javax.swing.JButton(); createUserBtn = new javax.swing.JButton(); @@ -92,28 +94,39 @@ public class MainGUI extends javax.swing.JFrame } }); + deleteBookBtn.setVisible(false); + deleteBookBtn.setText("Delete Book"); + deleteBookBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + deleteBookBtnActionPerformed(evt); + } + }); + javax.swing.GroupLayout bookPanelLayout = new javax.swing.GroupLayout(bookPanel); bookPanel.setLayout(bookPanelLayout); bookPanelLayout.setHorizontalGroup( bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(bookPanelLayout.createSequentialGroup() - .addContainerGap() .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(bookPanelLayout.createSequentialGroup() - .addComponent(bookSearch) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(bookSearchBtn)) - .addGroup(bookPanelLayout.createSequentialGroup() + .addContainerGap() .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(bookTitle) - .addComponent(bookStatus) - .addComponent(bookMgtLabel) - .addComponent(bookID)) - .addGap(0, 214, Short.MAX_VALUE)) - .addGroup(bookPanelLayout.createSequentialGroup() - .addComponent(bookDueDate) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(editBookBtn))) + .addGroup(bookPanelLayout.createSequentialGroup() + .addComponent(bookSearch) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(bookSearchBtn)) + .addGroup(bookPanelLayout.createSequentialGroup() + .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(bookTitle) + .addComponent(bookStatus) + .addComponent(bookMgtLabel) + .addComponent(bookID)) + .addGap(0, 214, Short.MAX_VALUE)) + .addGroup(bookPanelLayout.createSequentialGroup() + .addComponent(bookDueDate) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(editBookBtn)))) + .addComponent(deleteBookBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); bookPanelLayout.setVerticalGroup( @@ -135,7 +148,9 @@ public class MainGUI extends javax.swing.JFrame .addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(bookDueDate) .addComponent(editBookBtn)) - .addContainerGap(88, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(deleteBookBtn) + .addContainerGap(44, Short.MAX_VALUE)) ); jTabbedPane.addTab("Book", bookPanel); @@ -183,6 +198,14 @@ public class MainGUI extends javax.swing.JFrame } }); + deleteUserBtn.setVisible(false); + deleteUserBtn.setText("Delete User"); + deleteUserBtn.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + deleteUserBtnActionPerformed(evt); + } + }); + javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel); userPanel.setLayout(userPanelLayout); userPanelLayout.setHorizontalGroup( @@ -199,15 +222,18 @@ public class MainGUI extends javax.swing.JFrame .addGroup(userPanelLayout.createSequentialGroup() .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(userMgtLabel) - .addComponent(userName) - .addComponent(userBooks)) + .addComponent(userName)) .addGap(0, 219, Short.MAX_VALUE)) .addGroup(userPanelLayout.createSequentialGroup() .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(userID) .addComponent(userLimit)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(editUserBtn))) + .addComponent(editUserBtn)) + .addGroup(userPanelLayout.createSequentialGroup() + .addComponent(userBooks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(deleteUserBtn))) .addContainerGap()) ); userPanelLayout.setVerticalGroup( @@ -229,11 +255,16 @@ public class MainGUI extends javax.swing.JFrame .addComponent(userLimit)) .addComponent(editUserBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(userBooks) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) - .addComponent(rentBtn) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(returnBtn) + .addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(userPanelLayout.createSequentialGroup() + .addComponent(userBooks) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE) + .addComponent(rentBtn) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(returnBtn)) + .addGroup(userPanelLayout.createSequentialGroup() + .addComponent(deleteUserBtn) + .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap()) ); @@ -307,9 +338,10 @@ public class MainGUI extends javax.swing.JFrame ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(mainPanel, 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) + .addContainerGap()) ); pack(); @@ -405,6 +437,28 @@ public class MainGUI extends javax.swing.JFrame bookSearchBtnActionPerformed(evt); }//GEN-LAST:event_editBookBtnActionPerformed + private void deleteBookBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBookBtnActionPerformed + DeleteDialog deleteDialog = new DeleteDialog(book.getTitle()); + deleteDialog.setVisible(true); + + if (deleteDialog.getReturnStatus()) + { + handler.books.deleteBook(book); + bookSearchBtnActionPerformed(evt); + } + }//GEN-LAST:event_deleteBookBtnActionPerformed + + private void deleteUserBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteUserBtnActionPerformed + DeleteDialog deleteDialog = new DeleteDialog(user.getName()); + deleteDialog.setVisible(true); + + if (deleteDialog.getReturnStatus()) + { + handler.users.deleteUser(user); + userSearchBtnActionPerformed(evt); + } + }//GEN-LAST:event_deleteUserBtnActionPerformed + private void bookSearchBtnActionPerformed(ActionEvent evt) { try @@ -423,6 +477,7 @@ public class MainGUI extends javax.swing.JFrame } book = thisBook; editBookBtn.setVisible(true); + deleteBookBtn.setVisible(true); } catch (Exception e) { @@ -431,6 +486,7 @@ public class MainGUI extends javax.swing.JFrame bookStatus.setText(""); bookDueDate.setText(""); editBookBtn.setVisible(false); + deleteBookBtn.setVisible(false); } } @@ -446,6 +502,7 @@ public class MainGUI extends javax.swing.JFrame rentBtn.setVisible(thisUser.status()); returnBtn.setVisible(thisUser.bookStatus().length > 0); editUserBtn.setVisible(true); + deleteUserBtn.setVisible(true); user = thisUser; } catch (Exception e) @@ -457,6 +514,7 @@ public class MainGUI extends javax.swing.JFrame rentBtn.setVisible(false); returnBtn.setVisible(false); editUserBtn.setVisible(false); + deleteUserBtn.setVisible(false); } } @@ -471,6 +529,8 @@ public class MainGUI extends javax.swing.JFrame private javax.swing.JLabel bookTitle; private javax.swing.JButton createBookBtn; private javax.swing.JButton createUserBtn; + private javax.swing.JButton deleteBookBtn; + private javax.swing.JButton deleteUserBtn; private javax.swing.JButton editBookBtn; private javax.swing.JButton editUserBtn; private javax.swing.JTabbedPane jTabbedPane; diff --git a/src/main/java/libsys/Settings.java b/src/main/java/libsys/Settings.java deleted file mode 100644 index 9250d09..0000000 --- a/src/main/java/libsys/Settings.java +++ /dev/null @@ -1,141 +0,0 @@ -package libsys; -/* - * Written by : Bin Hong Lee - * Last edited : 6/4/2017 - */ - -import java.util.Scanner; -import java.io.FileInputStream; -import java.io.PrintWriter; -import java.util.HashMap; -import java.util.ArrayList; - -/** - * Handles all settings related operations and background processing of SettingsDialog - */ -@SuppressWarnings({"unchecked", "rawtypes"}) -public class Settings -{ - private HashMap items = new HashMap(); - private ArrayList keys = new ArrayList(); - private String settingsFileName; - - /** - * Creates new Settings importing information from the given file name - * @param settingsFileName File name of existing settings stored - */ - public Settings(String settingsFileName) - { - this.settingsFileName = settingsFileName; - - try - { - Scanner in = new Scanner(new FileInputStream(settingsFileName)); - while (in.hasNextLine()) - { - String newInput = in.nextLine(); - String[] inputSplitted = newInput.split(":"); - keys.add(inputSplitted[0]); - items.put(keys.get(keys.size() - 1), inputSplitted[1]); - } - } - catch (Exception e) - { - System.out.println("Settings file not found!"); - setDefault(); - } - } - - /** - * Save the settings to the given input file - */ - private void toFile() - { - try - { - PrintWriter out = new PrintWriter(settingsFileName); - for (String key : keys) { - out.println(key + ":" + items.get(key)); - } - out.close(); - } - catch (Exception e) - { - System.out.println("Invalid output filename"); - setDefault(); - } - } - - /** - * Amount of fields in the settings - * @return Amount of keys - */ - public int size() - { - return keys.size(); - } - - /** - * Get the field name from that specific location in the array list - * @param location Location of the field in the array list - * @return The field name - */ - String getKey(int location) - { - return keys.get(location); - } - - /** - * Get the value associated to the field name - * @param key Field name - * @return Field value - */ - public String get(String key) - { - return items.get(key); - } - - /** - * Change the title of the application - * @param newTitle New Title - */ - public void setTitle(String newTitle) - { - update("Title", newTitle); - } - - /** - * Change the file name for the Users - * @param newUsersFilename New file name for Users - */ - void setUsersFilename(String newUsersFilename) - { - update("Users Filename", newUsersFilename); - } - - /** - * Change the file name for the Books - * @param newBooksFilename New file name for Books - */ - void setBooksFilename(String newBooksFilename) - { - update("Books Filename", newBooksFilename); - } - - private void update(String key, String newValue) - { - items.remove(key); - items.put(key, newValue); - toFile(); - } - - private void setDefault() - { - keys.add("Title"); - items.put(keys.get(0), "Welcome to the library"); - keys.add("Users Filename"); - items.put(keys.get(1), "users.json"); - keys.add("Books Filename"); - items.put(keys.get(2), "books.json"); - } -} diff --git a/src/main/kotlin/libsys/BookFactory.kt b/src/main/kotlin/libsys/BookFactory.kt index 0b774c2..e31a72b 100644 --- a/src/main/kotlin/libsys/BookFactory.kt +++ b/src/main/kotlin/libsys/BookFactory.kt @@ -2,7 +2,7 @@ package libsys /* * Written by : Bin Hong Lee - * Last edited : 7/4/2017 + * Last edited : 7/6/2017 */ import java.util.ArrayList @@ -14,7 +14,8 @@ import java.io.PrintWriter /** * Handles all the Book(s) */ -class BookFactory { +class BookFactory +{ private val books = ArrayList() private var id: Int = 0 private var bookFilename: String? = null @@ -22,7 +23,8 @@ class BookFactory { /** * @constructor Create a new empty BookFactory */ - constructor() { + constructor() + { id = 0 bookFilename = "books.json" } @@ -32,7 +34,8 @@ class BookFactory { * * @param bookFilename Name of the input JSON file */ - constructor(bookFilename: String) { + constructor(bookFilename: String) + { try { val `in` = FileInputStream(bookFilename) val obj = JSONObject(JSONTokener(`in`)) @@ -116,6 +119,36 @@ class BookFactory { return temp } + /** + * Deletes the Book with the given id from the class (if found) + * @param id id of the Book to be deleted + * + * @return if Book deletion is successful + */ + fun deleteBook(id: Int): Boolean + { + try + { + books.removeAt(id) + return true + } + catch(e: Exception) + { + return false + } + } + + /** + * Deletes the given Book from the class (if found) + * @param book Book to be deleted + * + * @return if Book deletion is successful + */ + fun deleteBook(book: Book): Boolean + { + return books.remove(book) + } + /** * Looks for a Book with the given id * @param index id of the Book to be found diff --git a/src/main/kotlin/libsys/Settings.kt b/src/main/kotlin/libsys/Settings.kt new file mode 100644 index 0000000..b9341a3 --- /dev/null +++ b/src/main/kotlin/libsys/Settings.kt @@ -0,0 +1,142 @@ +package libsys + +/* + * Written by : Bin Hong Lee + * Last edited : 6/4/2017 + */ + +import java.util.Scanner +import java.io.FileInputStream +import java.io.PrintWriter +import java.util.HashMap +import java.util.ArrayList + +/** + * Handles all settings related operations and background processing of SettingsDialog + * + * @constructor Creates new Settings importing information from the given file name + * @param settingsFileName File name of existing settings stored + */ +class Settings(private val settingsFileName: String) +{ + private val items = HashMap() + private val keys = ArrayList() + + init + { + try + { + val `in` = Scanner(FileInputStream(settingsFileName)) + while (`in`.hasNextLine()) + { + val newInput = `in`.nextLine() + val inputSplitted = newInput.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + keys.add(inputSplitted[0]) + items.put(keys[keys.size - 1], inputSplitted[1]) + } + } + catch (e: Exception) + { + println("Settings file not found!") + setDefault() + } + + } + + /** + * Save the settings to the given input file + */ + private fun toFile() + { + try + { + val out = PrintWriter(settingsFileName) + for (key in keys) + { + out.println(key + ":" + items[key]) + } + out.close() + } + catch (e: Exception) + { + println("Invalid output filename") + setDefault() + } + + } + + /** + * Amount of fields in the settings + * @return Amount of keys + */ + fun size(): Int + { + return keys.size + } + + /** + * Get the field name from that specific location in the array list + * @param location Location of the field in the array list + * * + * @return The field name + */ + fun getKey(location: Int): String + { + return keys[location] + } + + /** + * Get the value associated to the field name + * @param key Field name + * * + * @return Field value + */ + fun get(key: String): String + { + return items[key].toString() + } + + /** + * Change the title of the application + * @param newTitle New Title + */ + fun setTitle(newTitle: String) + { + update("Title", newTitle) + } + + /** + * Change the file name for the Users + * @param newUsersFilename New file name for Users + */ + fun setUsersFilename(newUsersFilename: String) + { + update("Users Filename", newUsersFilename) + } + + /** + * Change the file name for the Books + * @param newBooksFilename New file name for Books + */ + fun setBooksFilename(newBooksFilename: String) + { + update("Books Filename", newBooksFilename) + } + + private fun update(key: String, newValue: String) + { + items.remove(key) + items.put(key, newValue) + toFile() + } + + private fun setDefault() + { + keys.add("Title") + items.put(keys[0], "Welcome to the library") + keys.add("Users Filename") + items.put(keys[1], "users.json") + keys.add("Books Filename") + items.put(keys[2], "books.json") + } +} diff --git a/src/main/kotlin/libsys/UserFactory.kt b/src/main/kotlin/libsys/UserFactory.kt index d257732..f491633 100644 --- a/src/main/kotlin/libsys/UserFactory.kt +++ b/src/main/kotlin/libsys/UserFactory.kt @@ -15,7 +15,8 @@ import java.util.ArrayList /** * Handles all the User(s) */ -class UserFactory { +class UserFactory +{ private val users = ArrayList() private var id: Int = 0 private var userFilename: String? = null @@ -23,7 +24,8 @@ class UserFactory { /** * @constructor Create a new empty UserFactory */ - constructor() { + constructor() + { id = 0 userFilename = "users.json" } @@ -32,13 +34,16 @@ class UserFactory { * @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 { + constructor(userFilename: String) + { + try + { val `in` = FileInputStream(userFilename) val obj = JSONObject(JSONTokener(`in`)) val ids = JSONObject.getNames(obj) - for (id1 in ids) { + for (id1 in ids) + { val jsonUser = obj.getJSONObject(id1) val id = Integer.parseInt(id1) val name = jsonUser.getString("Name") @@ -54,7 +59,9 @@ class UserFactory { } `in`.close() id = getUser(users.size - 1).id + 1 - } catch (ex: Exception) { + } + catch (ex: Exception) + { println("Exception importing from json: " + ex.message) id = 0 } @@ -65,11 +72,14 @@ class UserFactory { /** * Output the data into a JSON file replacing the input file (or if filename not given, "users.json") */ - fun toJsonFile() { - try { + fun toJsonFile() + { + try + { val out = PrintWriter(userFilename!!) val usersObj = JSONObject() - for (user in users) { + for (user in users) + { val userObj = JSONObject() userObj.put("Name", user.name) userObj.put("Limit", user.limit) @@ -78,7 +88,9 @@ class UserFactory { } out.println(usersObj.toString(4)) out.close() - } catch (e: Exception) { + } + catch (e: Exception) + { println("Invalid output filename") } @@ -88,7 +100,8 @@ class UserFactory { * Update the output filename for the object * @param userFilename The new filename */ - fun setUserFileName(userFilename: String) { + fun setUserFileName(userFilename: String) + { this.userFilename = userFilename } @@ -99,7 +112,8 @@ class UserFactory { * * @return The new User that is just created */ - fun newUser(name: String, limit: Int): User { + fun newUser(name: String, limit: Int): User + { val temp = User(name, id, limit) users.add(temp) @@ -115,7 +129,8 @@ class UserFactory { * * @return User with the given name */ - fun getUser(name: String): User { + fun getUser(name: String): User + { users .asSequence() .filter { it.name == name } @@ -130,7 +145,8 @@ class UserFactory { * * @return User with the given id */ - fun getUser(index: Int): User { + fun getUser(index: Int): User + { return search(index, 0, users.size - 1) } @@ -142,36 +158,71 @@ class UserFactory { * * @return User with the given id */ - private fun search(index: Int, start: Int, end: Int): User { - if (start == end && users[start].id == index) { + private fun search(index: Int, start: Int, end: Int): User + { + if (start == end && users[start].id == index) + { return users[start] } - if (start >= end) { + if (start >= end) + { throw NullPointerException() } val currentId = (start + end) / 2 - if (users[currentId].id == index) { + if (users[currentId].id == index) + { return users[currentId] - } else if (users[currentId].id > index) { + } + else if (users[currentId].id > index) + { return search(index, start, currentId - 1) - } else { + } + else + { return search(index, currentId + 1, end) } } + fun deleteUser(id: Int): Boolean + { + try + { + return users.remove(getUser(id)) + } + catch (e: Exception) + { + return false + } + } + + fun deleteUser(user: User): Boolean + { + try + { + return users.remove(user) + } + catch (e: Exception) + { + return false + } + } + /** * 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) { + fun update(oldUser: User, newUser: User) + { + for (i in users.indices) + { val temp = users[i] - if (temp.id == oldUser.id) { + if (temp.id == oldUser.id) + { users[i] = newUser } } diff --git a/src/test/kotlin/libsys/BookTest.kt b/src/test/kotlin/libsys/BookTest.kt index 1a9bed6..6019475 100644 --- a/src/test/kotlin/libsys/BookTest.kt +++ b/src/test/kotlin/libsys/BookTest.kt @@ -10,6 +10,11 @@ class BookTest private var book1: Book? = null private var book2: Book? = null + init + { + + } + /** * Set up before testing */