Browse Source

Now creates new json if it doesn't exist and allow to create new user through UI

master
BinHong Lee 7 years ago
parent
commit
4a945aec5a
11 changed files with 418 additions and 53 deletions
  1. +3
    -0
      .gitignore
  2. +0
    -20
      books.json
  3. +12
    -2
      src/main/java/libsys/BookFactory.java
  4. +2
    -8
      src/main/java/libsys/Handler.java
  5. +24
    -3
      src/main/java/libsys/Main.java
  6. +6
    -3
      src/main/java/libsys/MainGUI.form
  7. +19
    -3
      src/main/java/libsys/MainGUI.java
  8. +132
    -0
      src/main/java/libsys/NewUserDialog.form
  9. +208
    -0
      src/main/java/libsys/NewUserDialog.java
  10. +12
    -2
      src/main/java/libsys/UserFactory.java
  11. +0
    -12
      users.json

+ 3
- 0
.gitignore View File

@@ -1,3 +1,5 @@
*.json

.classpath
effective.pom
*.class
@@ -6,6 +8,7 @@ target
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear



+ 0
- 20
books.json View File

@@ -1,20 +0,0 @@
{
"0": {
"Status": "RENTED",
"Title": "Cracking the Coding Interview",
"Due Date": [
2017,
6,
8
]
},
"1": {
"Status": "AVAILABLE",
"Title": "Who was that?",
"Due Date": [
2017,
6,
8
]
}
}

+ 12
- 2
src/main/java/libsys/BookFactory.java View File

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

import java.util.List;
@@ -20,6 +20,7 @@ class BookFactory
{
private List<Book> books = new ArrayList<Book>();
private int id;
private String bookFilename;

public BookFactory()
{
@@ -56,10 +57,11 @@ class BookFactory
System.out.println(ex.getMessage());
}

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

public void toJsonFile(String bookFilename)
public void toJsonFile()
{
try
{
@@ -84,12 +86,18 @@ class BookFactory
}
}

public void setBookFileName(String bookFilename)
{
this.bookFilename = bookFilename;
}

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

id++;
toJsonFile();

return temp;
}
@@ -132,5 +140,7 @@ class BookFactory
books.set(i, newBook);
}
}

toJsonFile();
}
}

+ 2
- 8
src/main/java/libsys/Handler.java View File

@@ -10,8 +10,8 @@ import org.json.JSONObject;
class Handler
{
Exception BookNotFound = new Exception("Error 404 : Book not found");
BookFactory books = new BookFactory();
UserFactory users = new UserFactory();
BookFactory books;
UserFactory users;
Calendar cal = Calendar.getInstance();

public Handler()
@@ -29,9 +29,6 @@ class Handler
books.update(book, newBook);
users.update(user, newUser);

books.toJsonFile("books.json");
users.toJsonFile("users.json");

return true;
}

@@ -50,9 +47,6 @@ class Handler
books.update(book, newBook);
users.update(user, newUser);

books.toJsonFile("books.json");
users.toJsonFile("users.json");

return true;
}



+ 24
- 3
src/main/java/libsys/Main.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 Main
@@ -12,8 +12,29 @@ class Main
public static void main(String[] args)
{
Handler handler = new Handler();
handler.books = new BookFactory(bookFilename);
handler.users = new UserFactory(userFilename);

try
{
handler.books = new BookFactory(bookFilename);
}
catch (Exception e)
{
System.out.println("Book Exception");
handler.books = new BookFactory();
handler.books.setBookFileName(bookFilename);
}

try
{
handler.users = new UserFactory(userFilename);
}
catch (Exception e)
{
System.out.println("User Exception");
handler.users = new UserFactory();
handler.users.setUserFileName(userFilename);
}

new MainGUI(handler).setVisible(true);
}
}

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

@@ -74,8 +74,8 @@
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="nameLabel" min="-2" pref="59" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jTabbedPane" min="-2" pref="290" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="jTabbedPane" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="createBookBtn" alignment="3" max="32767" attributes="0"/>
@@ -144,7 +144,7 @@
<Component id="bookStatus" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="bookDueDate" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="94" max="32767" attributes="0"/>
<EmptySpace pref="88" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -336,6 +336,9 @@
<Properties>
<Property name="text" type="java.lang.String" value="Add new user"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="createUserBtnActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Container>


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

@@ -103,7 +103,7 @@ public class MainGUI extends javax.swing.JFrame
.addComponent(bookStatus)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookDueDate)
.addContainerGap(94, Short.MAX_VALUE))
.addContainerGap(88, Short.MAX_VALUE))
);

jTabbedPane.addTab("Book", bookPanel);
@@ -198,6 +198,11 @@ public class MainGUI extends javax.swing.JFrame
createBookBtn.setText("Add new book");

createUserBtn.setText("Add new user");
createUserBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createUserBtnActionPerformed(evt);
}
});

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
@@ -222,8 +227,8 @@ public class MainGUI extends javax.swing.JFrame
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(createBookBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
@@ -266,6 +271,17 @@ public class MainGUI extends javax.swing.JFrame
}
}//GEN-LAST:event_returnBtnActionPerformed

private void createUserBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createUserBtnActionPerformed
NewUserDialog newUserUI = new NewUserDialog(this, true);
newUserUI.setVisible(true);
if (newUserUI.getReturnStatus() != -1)
{
userSearch.setText(String.valueOf(handler.users.newUser(newUserUI.getName(), newUserUI.getLimit()).getId()));
userSearchBtnActionPerformed(evt);
}
}//GEN-LAST:event_createUserBtnActionPerformed

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


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

@@ -0,0 +1,132 @@
<?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="13" max="32767" attributes="0"/>
<Component id="cancelBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<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"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="limitLbl" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="nameLbl" 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="nameTxtField" max="32767" attributes="0"/>
<Component id="limitTxtField" 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"/>
<Component id="errorLbl" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="nameLbl" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="nameTxtField" 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="limitLbl" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="limitTxtField" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace pref="12" 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 User"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="nameLbl">
<Properties>
<Property name="text" type="java.lang.String" value="Name :"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="limitLbl">
<Properties>
<Property name="text" type="java.lang.String" value="Limit :"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="createBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Create user"/>
</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="nameTxtField">
</Component>
<Component class="javax.swing.JTextField" name="limitTxtField">
</Component>
<Component class="javax.swing.JLabel" name="errorLbl">
<Properties>
<Property name="text" type="java.lang.String" value="LIMIT MUST BE NUMERICAL!"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="errorLbl.setVisible(false);"/>
</AuxValues>
</Component>
</SubComponents>
</Form>

+ 208
- 0
src/main/java/libsys/NewUserDialog.java View File

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

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

/**
* Creates new form NewUserDialog
*/
public NewUserDialog(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;
}
public String getName() {
return name;
}
public int getLimit()
{
return limit;
}

/**
* 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();
nameLbl = new javax.swing.JLabel();
limitLbl = new javax.swing.JLabel();
createBtn = new javax.swing.JButton();
cancelBtn = new javax.swing.JButton();
nameTxtField = new javax.swing.JTextField();
limitTxtField = new javax.swing.JTextField();
errorLbl = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

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

nameLbl.setText("Name :");

limitLbl.setText("Limit :");

createBtn.setText("Create user");
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);
}
});

errorLbl.setVisible(false);
errorLbl.setText("LIMIT MUST BE NUMERICAL!");

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, 13, 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))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(limitLbl)
.addComponent(nameLbl))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(nameTxtField)
.addComponent(limitTxtField))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(title)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(errorLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(nameLbl)
.addComponent(nameTxtField, 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(limitLbl)
.addComponent(limitTxtField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, 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
try
{
limit = Integer.parseInt(limitTxtField.getText());
name = nameTxtField.getText();
doClose(1);
}
catch (Exception e)
{
errorLbl.setVisible(true);
}
}//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(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewUserDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
NewUserDialog dialog = new NewUserDialog(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.JLabel errorLbl;
private javax.swing.JLabel limitLbl;
private javax.swing.JTextField limitTxtField;
private javax.swing.JLabel nameLbl;
private javax.swing.JTextField nameTxtField;
private javax.swing.JLabel title;
// End of variables declaration//GEN-END:variables

private int returnStatus = -1;
private String name;
private int limit;

}

+ 12
- 2
src/main/java/libsys/UserFactory.java View File

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

import java.util.List;
@@ -20,6 +20,7 @@ class UserFactory
{
private List<User> users = new ArrayList<User>();
private int id;
private String userFilename;

public UserFactory()
{
@@ -57,10 +58,11 @@ class UserFactory
System.out.println("Exception importing from json: " + ex.getMessage());
}

this.userFilename = userFilename;
id = getUser(users.size()-1).getId() + 1;
}

public void toJsonFile(String userFilename)
public void toJsonFile()
{
try
{
@@ -85,12 +87,18 @@ class UserFactory
}
}

public void setUserFileName(String userFilename)
{
this.userFilename = userFilename;
}

public User newUser(String name, int limit)
{
User temp = new User(name, id, limit);
users.add(temp);

id++;
toJsonFile();

return temp;
}
@@ -136,5 +144,7 @@ class UserFactory
users.set(i, newUser);
}
}

toJsonFile();
}
}

+ 0
- 12
users.json View File

@@ -1,12 +0,0 @@
{
"0": {
"Limit": 2,
"Books": [],
"Name": "FirstName LastName"
},
"1": {
"Limit": 100,
"Books": [0],
"Name": "John Doe"
}
}

Loading…
Cancel
Save