Browse Source

DeleteDialog is now implemented and for both User and Book

master
BinHong Lee 7 years ago
parent
commit
2eff50d2ab
9 changed files with 477 additions and 248 deletions
  1. +22
    -9
      src/main/java/libsys/DeleteDialog.form
  2. +49
    -22
      src/main/java/libsys/DeleteDialog.java
  3. +63
    -24
      src/main/java/libsys/MainGUI.form
  4. +85
    -25
      src/main/java/libsys/MainGUI.java
  5. +0
    -141
      src/main/java/libsys/Settings.java
  6. +37
    -4
      src/main/kotlin/libsys/BookFactory.kt
  7. +142
    -0
      src/main/kotlin/libsys/Settings.kt
  8. +74
    -23
      src/main/kotlin/libsys/UserFactory.kt
  9. +5
    -0
      src/test/kotlin/libsys/BookTest.kt

src/main/java/libsys/DeleteBookDialog.form → src/main/java/libsys/DeleteDialog.form View File

@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="libsys.DeleteBookDialog">
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="libsys.DeleteDialog">
<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"/>
<xy x="48" y="54" width="414" height="173"/>
</constraints>
<properties/>
<border type="none"/>
@@ -34,7 +34,7 @@
<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"/>
<text value="Yes"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
@@ -42,28 +42,41 @@
<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"/>
<text value="No"/>
</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">
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" 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"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="4" hsize-policy="4" anchor="0" fill="3" indent="0" use-parent-layout="false">
<minimum-size width="350" height="80"/>
<preferred-size width="350" height="80"/>
<maximum-size width="99999" height="99999"/>
</grid>
</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"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="2" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="20"/>
<text value="Are you sure you want to delete"/>
</properties>
</component>
<component id="a61a2" class="javax.swing.JLabel" binding="bookNameLbl">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="1" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="26"/>
<text value="Are you sure you want to delete this book?"/>
<font size="20"/>
<text value="{Name} ?"/>
</properties>
</component>
</children>

src/main/java/libsys/DeleteBookDialog.java → src/main/java/libsys/DeleteDialog.java View File

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

/**

+ 63
- 24
src/main/java/libsys/MainGUI.form View File

@@ -32,9 +32,10 @@
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Component id="mainPanel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -103,27 +104,32 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="bookSearch" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="bookSearchBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="bookTitle" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookStatus" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookMgtLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookID" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<Component id="bookSearch" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="bookSearchBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="bookTitle" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookStatus" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookMgtLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="bookID" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="214" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="bookDueDate" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="editBookBtn" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace min="0" pref="214" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="bookDueDate" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="editBookBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="deleteBookBtn" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
@@ -150,7 +156,9 @@
<Component id="bookDueDate" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="editBookBtn" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace pref="88" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="deleteBookBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="44" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -205,6 +213,17 @@
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="editBookBtn.setVisible(false);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="deleteBookBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Delete Book"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="deleteBookBtnActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="deleteBookBtn.setVisible(false);"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="userPanel">
@@ -233,7 +252,6 @@
<Group type="103" groupAlignment="0" attributes="0">
<Component id="userMgtLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="userName" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="userBooks" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="219" max="32767" attributes="0"/>
</Group>
@@ -245,6 +263,11 @@
<EmptySpace max="32767" attributes="0"/>
<Component id="editUserBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="userBooks" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="deleteUserBtn" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
@@ -272,11 +295,19 @@
<Component id="editUserBtn" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="userBooks" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="28" max="32767" attributes="0"/>
<Component id="rentBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="returnBtn" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="userBooks" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="28" max="32767" attributes="0"/>
<Component id="rentBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="returnBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Component id="deleteUserBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
@@ -354,6 +385,14 @@
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="editUserBtn.setVisible(false);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="deleteUserBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Delete User"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="deleteUserBtnActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>
</SubComponents>


+ 85
- 25
src/main/java/libsys/MainGUI.java View File

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


+ 0
- 141
src/main/java/libsys/Settings.java View File

@@ -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<String, String> items = new HashMap();
private ArrayList<String> 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");
}
}

+ 37
- 4
src/main/kotlin/libsys/BookFactory.kt View File

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


+ 142
- 0
src/main/kotlin/libsys/Settings.kt View File

@@ -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<String, String>()
private val keys = ArrayList<String>()

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")
}
}

+ 74
- 23
src/main/kotlin/libsys/UserFactory.kt View File

@@ -15,7 +15,8 @@ import java.util.ArrayList
/**
* Handles all the User(s)
*/
class UserFactory {
class UserFactory
{
private val users = ArrayList<User>()
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
}
}


+ 5
- 0
src/test/kotlin/libsys/BookTest.kt View File

@@ -10,6 +10,11 @@ class BookTest
private var book1: Book? = null
private var book2: Book? = null

init
{

}

/**
* Set up before testing
*/


Loading…
Cancel
Save