Browse Source

Can create new book through UI, BookFactory now search for id instead of position in list

master
BinHong Lee 7 years ago
parent
commit
3bfcf9cd02
9 changed files with 394 additions and 36 deletions
  1. +3
    -4
      src/main/java/libsys/Book.java
  2. +27
    -6
      src/main/java/libsys/BookFactory.java
  3. +3
    -0
      src/main/java/libsys/MainGUI.form
  4. +16
    -0
      src/main/java/libsys/MainGUI.java
  5. +128
    -0
      src/main/java/libsys/NewBookDialog.form
  6. +195
    -0
      src/main/java/libsys/NewBookDialog.java
  7. +3
    -3
      src/main/java/libsys/NewUserDialog.form
  8. +8
    -12
      src/main/java/libsys/NewUserDialog.java
  9. +11
    -11
      src/main/java/libsys/UserFactory.java

+ 3
- 4
src/main/java/libsys/Book.java View File

@@ -1,7 +1,7 @@
package libsys;
/*
* Written by : Bin Hong Lee
* Last edited : 5/28/2017
* Last edited : 5/30/2017
*/

class Book
@@ -23,12 +23,11 @@ class Book
this.status = "NOT AVAILABLE";
}

//Constructor with book title
public Book(String title, int id)
public Book(String title, int id, String status)
{
this.title = title;
this.id = id;
this.status = "AVAILABLE";
this.status = status;
}

public Book(int id, String title, String status, int[] dueDate)


+ 27
- 6
src/main/java/libsys/BookFactory.java View File

@@ -58,7 +58,7 @@ class BookFactory
}

this.bookFilename = bookFilename;
id = getBook(books.size()-1).getId() + 1;
id = books.get(books.size() - 1).getId() + 1;
}

public void toJsonFile()
@@ -91,9 +91,9 @@ class BookFactory
this.bookFilename = bookFilename;
}

public Book newBook(String title)
public Book newBook(String title, String status)
{
Book temp = new Book(title, id);
Book temp = new Book(title, id, status);
books.add(temp);

id++;
@@ -104,14 +104,35 @@ class BookFactory

public Book getBook(int index)
{
try
return search(index, 0, books.size() - 1);
}

public Book search(int index, int start, int end)
{
if (start == end && books.get(start).getId() == index)
{
return books.get(index);
return books.get(start);
}
catch (Exception e)

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

public Book getBook(String title)


+ 3
- 0
src/main/java/libsys/MainGUI.form View File

@@ -331,6 +331,9 @@
<Properties>
<Property name="text" type="java.lang.String" value="Add new book"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="createBookBtnActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="createUserBtn">
<Properties>


+ 16
- 0
src/main/java/libsys/MainGUI.java View File

@@ -196,6 +196,11 @@ public class MainGUI extends javax.swing.JFrame
nameLabel.setText("Welcome to abc Library");

createBookBtn.setText("Add new book");
createBookBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createBookBtnActionPerformed(evt);
}
});

createUserBtn.setText("Add new user");
createUserBtn.addActionListener(new java.awt.event.ActionListener() {
@@ -282,6 +287,17 @@ public class MainGUI extends javax.swing.JFrame
}
}//GEN-LAST:event_createUserBtnActionPerformed

private void createBookBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBookBtnActionPerformed
NewBookDialog newBookUI = new NewBookDialog(this, true);
newBookUI.setVisible(true);
if (newBookUI.getReturnStatus() != -1)
{
bookSearch.setText(String.valueOf(handler.books.newBook(newBookUI.getName(), newBookUI.getStatus()).getId()));
bookSearchBtnActionPerformed(evt);
}
}//GEN-LAST:event_createBookBtnActionPerformed

private void rentBtnActionPerformed(java.awt.event.ActionEvent evt)
{
User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText()));


+ 128
- 0
src/main/java/libsys/NewBookDialog.form View File

@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8" ?>

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>

<Layout>
<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" alignment="0" attributes="0">
<Component id="createBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="23" max="32767" attributes="0"/>
<Component id="cancelBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Component id="title" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="100" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="statusLbl" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="titleLbl" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="titleTxtField" max="32767" attributes="0"/>
<Component id="statusComboBox" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="title" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="titleLbl" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="titleTxtField" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="statusLbl" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="statusComboBox" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="createBtn" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cancelBtn" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="title">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor">
<Font name="Lucida Grande" size="18" style="0"/>
</Property>
<Property name="text" type="java.lang.String" value="New Book"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="titleLbl">
<Properties>
<Property name="text" type="java.lang.String" value="Title :"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="statusLbl">
<Properties>
<Property name="text" type="java.lang.String" value="Status :"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="createBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Add book"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="createBtnActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="cancelBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Cancel"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cancelBtnActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JTextField" name="titleTxtField">
</Component>
<Component class="javax.swing.JComboBox" name="statusComboBox">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code="new javax.swing.DefaultComboBoxModel(statusOptions)" type="code"/>
</Property>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_CreateCodeCustom" type="java.lang.String" value="new javax.swing.JComboBox()"/>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
</SubComponents>
</Form>

+ 195
- 0
src/main/java/libsys/NewBookDialog.java View File

@@ -0,0 +1,195 @@
package libsys;

public class NewBookDialog extends javax.swing.JDialog {

/**
* Creates new form NewBookDialog
* @param parent
* @param modal
*/
public NewBookDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}

/**
* @return the return status of this dialog - one of RET_OK or RET_CANCEL
*/
public int getReturnStatus() {
return returnStatus;
}

@Override
public String getName() {
return name;
}

public String getStatus()
{
return status;
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

title = new javax.swing.JLabel();
titleLbl = new javax.swing.JLabel();
statusLbl = new javax.swing.JLabel();
createBtn = new javax.swing.JButton();
cancelBtn = new javax.swing.JButton();
titleTxtField = new javax.swing.JTextField();
statusComboBox = new javax.swing.JComboBox();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

title.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
title.setText("New Book");

titleLbl.setText("Title :");

statusLbl.setText("Status :");

createBtn.setText("Add book");
createBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createBtnActionPerformed(evt);
}
});

cancelBtn.setText("Cancel");
cancelBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelBtnActionPerformed(evt);
}
});

statusComboBox.setModel(new javax.swing.DefaultComboBoxModel(statusOptions));

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(createBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
.addComponent(cancelBtn))
.addGroup(layout.createSequentialGroup()
.addComponent(title)
.addGap(0, 100, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusLbl)
.addComponent(titleLbl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(titleTxtField)
.addComponent(statusComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(title)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(titleLbl)
.addComponent(titleTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusLbl)
.addComponent(statusComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(createBtn)
.addComponent(cancelBtn))
.addContainerGap())
);

pack();
}// </editor-fold>//GEN-END:initComponents

private void createBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createBtnActionPerformed
status = statusOptions[statusComboBox.getSelectedIndex()];
name = titleTxtField.getText();
doClose(1);
}//GEN-LAST:event_createBtnActionPerformed

private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
doClose(-1);
}//GEN-LAST:event_cancelBtnActionPerformed

private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
dispose();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewBookDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
NewBookDialog dialog = new NewBookDialog(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelBtn;
private javax.swing.JButton createBtn;
private javax.swing.JComboBox<String> statusComboBox;
private javax.swing.JLabel statusLbl;
private javax.swing.JLabel title;
private javax.swing.JLabel titleLbl;
private javax.swing.JTextField titleTxtField;
// End of variables declaration//GEN-END:variables

private int returnStatus = -1;
private String name;
private String status;
private String[] statusOptions = new String[] { "AVAILABLE", "NOT AVAILABLE", "RESERVED" };

}

+ 3
- 3
src/main/java/libsys/NewUserDialog.form View File

@@ -28,7 +28,7 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="createBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="13" max="32767" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="cancelBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
@@ -36,7 +36,7 @@
<Component id="title" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="errorLbl" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<EmptySpace min="0" pref="13" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
@@ -102,7 +102,7 @@
</Component>
<Component class="javax.swing.JButton" name="createBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Create user"/>
<Property name="text" type="java.lang.String" value="Add user"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="createBtnActionPerformed"/>


+ 8
- 12
src/main/java/libsys/NewUserDialog.java View File

@@ -1,9 +1,5 @@
package libsys;

/**
*
* @author binhonglee
*/
public class NewUserDialog extends javax.swing.JDialog {

/**
@@ -13,18 +9,18 @@ public class NewUserDialog extends javax.swing.JDialog {
super(parent, modal);
initComponents();
}
/**
* @return the return status of this dialog - one of RET_OK or RET_CANCEL
*/
public int getReturnStatus() {
return returnStatus;
}
public String getName() {
return name;
}
public int getLimit()
{
return limit;
@@ -57,7 +53,7 @@ public class NewUserDialog extends javax.swing.JDialog {

limitLbl.setText("Limit :");

createBtn.setText("Create user");
createBtn.setText("Add user");
createBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createBtnActionPerformed(evt);
@@ -83,13 +79,13 @@ public class NewUserDialog extends javax.swing.JDialog {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(createBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelBtn))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(title)
.addComponent(errorLbl))
.addGap(0, 0, Short.MAX_VALUE))
.addGap(0, 13, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(limitLbl)
@@ -141,7 +137,7 @@ public class NewUserDialog extends javax.swing.JDialog {
private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
doClose(-1);
}//GEN-LAST:event_cancelBtnActionPerformed
private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
@@ -155,7 +151,7 @@ public class NewUserDialog extends javax.swing.JDialog {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {


+ 11
- 11
src/main/java/libsys/UserFactory.java View File

@@ -4,17 +4,17 @@ package libsys;
* Last edited : 5/30/2017
*/

import java.util.List;
import java.util.ArrayList;
import org.json.JSONString;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONArray;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.File;
import java.util.Enumeration;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import org.json.JSONString;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONArray;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.io.File;
import java.util.Enumeration;
import java.io.Serializable;

class UserFactory
{


Loading…
Cancel
Save