Browse Source

Rent and return are now both working features

master
BinHong Lee 7 years ago
parent
commit
154a6380df
12 changed files with 1095 additions and 286 deletions
  1. +19
    -18
      books.json
  2. +1
    -1
      src/main/java/libsys/Book.java
  3. +2
    -8
      src/main/java/libsys/BookFactory.java
  4. +15
    -12
      src/main/java/libsys/Handler.java
  5. +69
    -23
      src/main/java/libsys/MainGUI.form
  6. +310
    -213
      src/main/java/libsys/MainGUI.java
  7. +100
    -0
      src/main/java/libsys/RentDialog.form
  8. +214
    -0
      src/main/java/libsys/RentDialog.java
  9. +100
    -0
      src/main/java/libsys/ReturnDialog.form
  10. +227
    -0
      src/main/java/libsys/ReturnDialog.java
  11. +27
    -3
      src/main/java/libsys/UserFactory.java
  12. +11
    -8
      users.json

+ 19
- 18
books.json View File

@@ -1,19 +1,20 @@
{"0": {
"Status": "RENTED",
"Title": "Cracking the Coding Interview",
"Due Date": [
10,
10,
2010
]
},
"1": {
"Status": "RENTED",
"Title": "Who was that?",
"Due Date": [
1,
1,
1
]
}
{
"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
]
}
}

+ 1
- 1
src/main/java/libsys/Book.java View File

@@ -74,7 +74,7 @@ class Book
public boolean rent(int[] dueDate)
{
//Check if the book is available to be rented
if(status != "AVAILABLE")
if(!"AVAILABLE".equals(status))
{
return false;
}


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

@@ -30,13 +30,10 @@ class BookFactory
{
try
{
System.out.println("Reading data from file...");
FileInputStream in = new FileInputStream(bookFilename);
JSONObject obj = new JSONObject(new JSONTokener(in));
String [] ids = JSONObject.getNames(obj);
System.out.println();

System.out.println("Parsing data into ArrayList...");
for (int i = 0; i < ids.length; i++)
{
JSONObject jsonBook = obj.getJSONObject(ids[i]);
@@ -52,8 +49,6 @@ class BookFactory
}
books.add(new Book(id, title, status, dueDate));
}
System.out.println();

in.close();
}
catch (Exception ex)
@@ -81,7 +76,6 @@ class BookFactory
}
out.println(booksObj.toString(4));
out.close();
System.out.println("Books exported to JSON file.");
}
catch (Exception e)
{
@@ -127,13 +121,13 @@ class BookFactory
throw new NullPointerException();
}

public void update(Book newBook)
public void update(Book oldBook, Book newBook)
{
for (int i = 0; i < books.size(); i++)
{
Book temp = books.get(i);

if(temp.getId() == newBook.getId())
if(temp.getId() == oldBook.getId())
{
books.set(i, newBook);
}


+ 15
- 12
src/main/java/libsys/Handler.java View File

@@ -12,7 +12,7 @@ class Handler
Exception BookNotFound = new Exception("Error 404 : Book not found");
BookFactory books = new BookFactory();
UserFactory users = new UserFactory();
Calendar cal;
Calendar cal = Calendar.getInstance();

public Handler()
{
@@ -22,15 +22,16 @@ class Handler
public boolean borrowBook(User user, Book book)
{
User newUser = user;
Book newBook = book;

if(book.getStatus() == "AVAILABLE" && newUser.status())
if(("AVAILABLE".equals(book.getStatus())) && newUser.status() && newBook.rent(calDueDate(10)) && newUser.borrowNewBook(book.getId()))
{
book.rent(calDueDate(10));
newUser.borrowNewBook(book.getId());

books.update(book);
books.update(book, newBook);
users.update(user, newUser);

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

return true;
}

@@ -40,14 +41,18 @@ class Handler
public boolean returnBook(User user, Book book)
{
User newUser = user;
Book newBook = book;

if(newUser.returnBook(book.getId()))
{
book.returned();

books.update(book);
books.update(book, newBook);
users.update(user, newUser);

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

return true;
}

@@ -127,9 +132,9 @@ class Handler
{
int[] currentDay = new int[3];

currentDay[0] = cal.get(Calendar.YEAR) + 1900;
currentDay[0] = cal.get(Calendar.YEAR);
currentDay[1] = cal.get(Calendar.MONTH) + 1;
currentDay[2] = cal.get(Calendar.DAY_OF_MONTH);
currentDay[2] = cal.get(Calendar.DATE);

return currentDay;
}
@@ -141,11 +146,9 @@ class Handler
for (int i = 0; i < bookIDs.length; i++)
{
titlesString += books.getBook(bookIDs[i]).getTitle();
titlesString += "; \n";
titlesString += "; ";
}

System.out.println(titlesString);

return titlesString;
}
}

+ 69
- 23
src/main/java/libsys/MainGUI.form View File

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

<Form version="1.3" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
<Properties>
<Property name="defaultCloseOperation" type="int" value="3"/>
</Properties>
@@ -24,31 +24,35 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="0" pref="7" max="32767" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="7" max="-2" attributes="0"/>
<Component id="mainPanel" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="mainPanel" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="jPanel1">
<Container class="javax.swing.JPanel" name="mainPanel">
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePre" type="java.lang.String" value="//setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);"/>
</AuxValues>

<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jTabbedPane1" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="32" max="32767" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="jTabbedPane" max="32767" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -56,14 +60,14 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jTabbedPane1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="jTabbedPane" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JTabbedPane" name="jTabbedPane1">
<Container class="javax.swing.JTabbedPane" name="jTabbedPane">

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JTabbedPaneSupportLayout"/>
<SubComponents>
@@ -82,9 +86,9 @@
<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="bookSearch" min="-2" pref="108" max="-2" attributes="0"/>
<EmptySpace max="32767" 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">
@@ -93,8 +97,9 @@
<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"/>
<Component id="bookDueDate" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<EmptySpace min="0" pref="115" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
@@ -117,7 +122,9 @@
<Component id="bookID" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="bookStatus" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="74" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="bookDueDate" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="88" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -137,6 +144,9 @@
<Properties>
<Property name="text" type="java.lang.String" value="Search"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePost" type="java.lang.String" value="bookSearchBtn.addActionListener(new ActionListener() {&#xa; public void actionPerformed(ActionEvent evt)&#xa; {&#xa; bookSearchBtnActionPerformed(evt);&#xa; }&#xa;});"/>
</AuxValues>
</Component>
<Component class="javax.swing.JLabel" name="bookTitle">
<Properties>
@@ -153,6 +163,11 @@
<Property name="text" type="java.lang.String" value="ID :"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="bookDueDate">
<Properties>
<Property name="text" type="java.lang.String" value="Due Date :"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="userPanel">
@@ -170,9 +185,10 @@
<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="userSearch" min="-2" pref="108" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="rentBtn" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Component id="userSearch" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="userSearchBtn" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" attributes="0">
@@ -183,8 +199,9 @@
<Component id="userBooks" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="userLimit" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<EmptySpace min="0" pref="120" max="32767" attributes="0"/>
</Group>
<Component id="returnBtn" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
@@ -208,7 +225,11 @@
<Component id="userLimit" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="userBooks" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="46" max="32767" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="rentBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="returnBtn" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@@ -228,6 +249,9 @@
<Properties>
<Property name="text" type="java.lang.String" value="Search"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePost" type="java.lang.String" value="userSearchBtn.addActionListener(new ActionListener() {&#xa; public void actionPerformed(ActionEvent evt)&#xa; {&#xa; userSearchBtnActionPerformed(evt);&#xa; }&#xa;});"/>
</AuxValues>
</Component>
<Component class="javax.swing.JLabel" name="userName">
<Properties>
@@ -249,6 +273,28 @@
<Property name="text" type="java.lang.String" value="Books :"/>
</Properties>
</Component>
<Component class="javax.swing.JButton" name="rentBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Rent Another Book"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="rentBtnActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_ListenersCodePost" type="java.lang.String" value="rentBtn.setVisible(false);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="returnBtn">
<Properties>
<Property name="text" type="java.lang.String" value="Return Book"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="returnBtnActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_ListenersCodePost" type="java.lang.String" value="rentBtn.setVisible(false);"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>


+ 310
- 213
src/main/java/libsys/MainGUI.java View File

@@ -7,227 +7,324 @@ import java.awt.event.ActionListener;

public class MainGUI extends javax.swing.JFrame
{
Handler handler;

public MainGUI(Handler handler)
{
this.handler = handler;
initComponents();
}

//GEN-BEGIN:initComponents
private void initComponents()
{
setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
mainPanel = new javax.swing.JPanel();
jTabbedPane = new javax.swing.JTabbedPane();
bookPanel = new javax.swing.JPanel();
bookMgtLabel = new javax.swing.JLabel();
bookSearch = new javax.swing.JTextField();
bookSearchBtn = new javax.swing.JButton();
bookTitle = new javax.swing.JLabel();
bookStatus = new javax.swing.JLabel();
bookID = new javax.swing.JLabel();
userPanel = new javax.swing.JPanel();
userMgtLabel = new javax.swing.JLabel();
userSearch = new javax.swing.JTextField();
userSearchBtn = new javax.swing.JButton();
userName = new javax.swing.JLabel();
userLimit = new javax.swing.JLabel();
userID = new javax.swing.JLabel();
userBooks = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

bookSearchBtn.setText("Search");
bookSearchBtn.addActionListener(new ActionListener()
Handler handler;

public MainGUI(Handler handler)
{
public void actionPerformed(ActionEvent evt)
{
bookSearchBtnActionPerformed(evt);
}
});
this.handler = handler;
initComponents();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

mainPanel = new javax.swing.JPanel();
jTabbedPane = new javax.swing.JTabbedPane();
bookPanel = new javax.swing.JPanel();
bookMgtLabel = new javax.swing.JLabel();
bookSearch = new javax.swing.JTextField();
bookSearchBtn = new javax.swing.JButton();
bookTitle = new javax.swing.JLabel();
bookStatus = new javax.swing.JLabel();
bookID = new javax.swing.JLabel();
bookDueDate = new javax.swing.JLabel();
userPanel = new javax.swing.JPanel();
userMgtLabel = new javax.swing.JLabel();
userSearch = new javax.swing.JTextField();
userSearchBtn = new javax.swing.JButton();
userName = new javax.swing.JLabel();
userLimit = new javax.swing.JLabel();
userID = new javax.swing.JLabel();
userBooks = new javax.swing.JLabel();
rentBtn = new javax.swing.JButton();
returnBtn = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

//setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);

bookTitle.setText("Title : ");
bookMgtLabel.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
bookMgtLabel.setText("Book Management");

bookStatus.setText("Status :");
bookSearchBtn.setText("Search");
bookSearchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
bookSearchBtnActionPerformed(evt);
}
});

bookID.setText("ID :");
bookTitle.setText("Title : ");

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)
bookStatus.setText("Status :");

bookID.setText("ID :");

bookDueDate.setText("Due Date :");

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()
.addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bookTitle)
.addComponent(bookStatus)
.addComponent(bookMgtLabel)
.addComponent(bookID)
.addComponent(bookDueDate))
.addGap(0, 115, Short.MAX_VALUE)))
.addContainerGap())
);
bookPanelLayout.setVerticalGroup(
bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bookPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(bookMgtLabel)
.addGroup(bookPanelLayout.createSequentialGroup()
.addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bookSearchBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bookTitle)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookID)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookStatus)
.addComponent(bookID))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bookPanelLayout.setVerticalGroup(
bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bookPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(bookMgtLabel)
.addGap(18, 18, 18)
.addGroup(bookPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bookSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bookSearchBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookID)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookTitle)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookStatus)
.addContainerGap(38, Short.MAX_VALUE))
);

jTabbedPane.addTab("Book", bookPanel);

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

userSearchBtn.setText("Search");
userSearchBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
userSearchBtnActionPerformed(evt);
}
});

userName.setText("Name : ");

userLimit.setText("Limit :");

userID.setText("ID :");

userBooks.setText("Books :");

javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel);
userPanel.setLayout(userPanelLayout);
userPanelLayout.setHorizontalGroup(
userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(userPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(userPanelLayout.createSequentialGroup()
.addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bookDueDate)
.addContainerGap(88, Short.MAX_VALUE))
);

jTabbedPane.addTab("Book", bookPanel);

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

userSearchBtn.setText("Search");
userSearchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
userSearchBtnActionPerformed(evt);
}
});

userName.setText("Name : ");

userLimit.setText("Limit :");

userID.setText("ID :");

userBooks.setText("Books :");

rentBtn.setText("Rent Another Book");
rentBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
rentBtnActionPerformed(evt);
}
});
rentBtn.setVisible(false);

returnBtn.setText("Return Book");
returnBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
returnBtnActionPerformed(evt);
}
});
rentBtn.setVisible(false);

javax.swing.GroupLayout userPanelLayout = new javax.swing.GroupLayout(userPanel);
userPanel.setLayout(userPanelLayout);
userPanelLayout.setHorizontalGroup(
userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(userPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(rentBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(userPanelLayout.createSequentialGroup()
.addComponent(userSearch)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userSearchBtn))
.addGroup(userPanelLayout.createSequentialGroup()
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(userID)
.addComponent(userMgtLabel)
.addComponent(userName)
.addComponent(userBooks)
.addComponent(userLimit))
.addGap(0, 120, Short.MAX_VALUE))
.addComponent(returnBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
userPanelLayout.setVerticalGroup(
userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(userPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(userMgtLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(userSearchBtn))
.addGroup(userPanelLayout.createSequentialGroup()
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(userMgtLabel)
.addComponent(userName)
.addComponent(userID)
.addComponent(userBooks)
.addComponent(userLimit))
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
userPanelLayout.setVerticalGroup(
userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(userPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(userMgtLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(userPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(userSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(userSearchBtn))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(userName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userID)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userLimit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userBooks)
.addContainerGap(28, Short.MAX_VALUE))
);

jTabbedPane.addTab("User", userPanel);

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(32, Short.MAX_VALUE))
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 7, Short.MAX_VALUE)
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);

pack();
}
//GEN-END:initComponents

private void bookSearchBtnActionPerformed(ActionEvent evt)
{
Book thisBook = handler.books.getBook(0);
bookID.setText("ID : " + thisBook.getId());
bookTitle.setText("Title : " + thisBook.getTitle());
bookStatus.setText("Status : " + thisBook.getStatus());
}

private void userSearchBtnActionPerformed(ActionEvent evt)
{
User thisUser = handler.users.getUser(0);
userID.setText("ID : " + thisUser.getId());
userName.setText("Name : " + thisUser.getName());
userLimit.setText("Limit : " + thisUser.getLimit());
userBooks.setText("Books : " + handler.bookIdsToTitlesString(thisUser.bookStatus()));
}

//GEN-BEGIN:variables
private javax.swing.JLabel bookID;
private javax.swing.JLabel bookMgtLabel;
private javax.swing.JPanel bookPanel;
private javax.swing.JTextField bookSearch;
private javax.swing.JButton bookSearchBtn;
private javax.swing.JLabel bookStatus;
private javax.swing.JLabel bookTitle;
private javax.swing.JPanel mainPanel;
private javax.swing.JTabbedPane jTabbedPane;
private javax.swing.JLabel userBooks;
private javax.swing.JLabel userID;
private javax.swing.JLabel userLimit;
private javax.swing.JLabel userMgtLabel;
private javax.swing.JLabel userName;
private javax.swing.JPanel userPanel;
private javax.swing.JTextField userSearch;
private javax.swing.JButton userSearchBtn;
//GEN-END:variables
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(userName)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userID)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userLimit)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(userBooks)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(rentBtn)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(returnBtn)
.addContainerGap())
);

jTabbedPane.addTab("User", userPanel);

javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane)
.addContainerGap())
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane)
.addContainerGap())
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(7, 7, 7)
.addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

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

private void returnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnBtnActionPerformed

User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText()));
ReturnDialog toReturnUI = new ReturnDialog(this, true, handler, thisUser.getId());
toReturnUI.setVisible(true);
int toReturnID = toReturnUI.getReturnStatus();

if (toReturnID != -1)
{
handler.returnBook(thisUser, handler.books.getBook(toReturnID));
userSearchBtnActionPerformed(evt);
bookSearchBtnActionPerformed(evt);
}
}//GEN-LAST:event_returnBtnActionPerformed

private void rentBtnActionPerformed(java.awt.event.ActionEvent evt)
{
User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText()));
RentDialog toRentUI = new RentDialog(this, true, handler);
toRentUI.setVisible(true);
int toRentID = toRentUI.getReturnStatus();

if (toRentID != -1)
{
handler.borrowBook(thisUser, handler.books.getBook(toRentID));
userSearchBtnActionPerformed(evt);
bookSearchBtnActionPerformed(evt);
}
}
private void bookSearchBtnActionPerformed(ActionEvent evt)
{
try
{
Book thisBook = handler.books.getBook(Integer.parseInt(bookSearch.getText()));
bookID.setText("ID : " + thisBook.getId());
bookTitle.setText("Title : " + thisBook.getTitle());
bookStatus.setText("Status : " + thisBook.getStatus());
if ("RENTED".equals(thisBook.getStatus()))
{
bookDueDate.setText("Due date : " + thisBook.getDueDate()[0] + "/" + thisBook.getDueDate()[1] + "/" + thisBook.getDueDate()[2]);
}
else
{
bookDueDate.setText("Due date : ");
}
}
catch (Exception e)
{
bookID.setText("ERROR : BOOK NOT FOUND");
bookTitle.setText("");
bookStatus.setText("");
bookDueDate.setText("");
}
}

private void userSearchBtnActionPerformed(ActionEvent evt)
{
try
{
User thisUser = handler.users.getUser(Integer.parseInt(userSearch.getText()));
userID.setText("ID : " + thisUser.getId());
userName.setText("Name : " + thisUser.getName());
userLimit.setText("Limit : " + thisUser.getLimit());
userBooks.setText("Books : " + handler.bookIdsToTitlesString(thisUser.bookStatus()));
rentBtn.setVisible(thisUser.status());
returnBtn.setVisible(thisUser.bookStatus().length > 0);
}
catch (Exception e)
{
userID.setText("ERROR : USER NOT FOUND");
userName.setText("");
userLimit.setText("");
userBooks.setText("");
rentBtn.setVisible(false);
returnBtn.setVisible(false);
}
}

// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel bookDueDate;
private javax.swing.JLabel bookID;
private javax.swing.JLabel bookMgtLabel;
private javax.swing.JPanel bookPanel;
private javax.swing.JTextField bookSearch;
private javax.swing.JButton bookSearchBtn;
private javax.swing.JLabel bookStatus;
private javax.swing.JLabel bookTitle;
private javax.swing.JTabbedPane jTabbedPane;
private javax.swing.JPanel mainPanel;
private javax.swing.JButton rentBtn;
private javax.swing.JButton returnBtn;
private javax.swing.JLabel userBooks;
private javax.swing.JLabel userID;
private javax.swing.JLabel userLimit;
private javax.swing.JLabel userMgtLabel;
private javax.swing.JLabel userName;
private javax.swing.JPanel userPanel;
private javax.swing.JTextField userSearch;
private javax.swing.JButton userSearchBtn;
// End of variables declaration//GEN-END:variables
}

+ 100
- 0
src/main/java/libsys/RentDialog.form View File

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

<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<Events>
<EventHandler event="windowClosing" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="closeDialog"/>
</Events>
<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">
<Component id="label" pref="234" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Component id="cancelButton" linkSize="1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="okButton" linkSize="1" min="-2" pref="67" max="-2" attributes="0"/>
</Group>
<Component id="givenBookId" alignment="0" max="32767" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="invalidLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="label" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="16" max="32767" attributes="0"/>
<Component id="invalidLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="givenBookId" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="okButton" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JButton" name="okButton">
<Properties>
<Property name="text" type="java.lang.String" value="OK"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="okButtonActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_AddingCodePost" type="java.lang.String" value="getRootPane().setDefaultButton(okButton);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="cancelButton">
<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="cancelButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="label">
<Properties>
<Property name="text" type="java.lang.String" value="Please input the book ID:"/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="givenBookId">
</Component>
<Component class="javax.swing.JLabel" name="invalidLabel">
<Properties>
<Property name="text" type="java.lang.String" value="INVALID ID!"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePost" type="java.lang.String" value="invalidLabel.setVisible(false);"/>
</AuxValues>
</Component>
</SubComponents>
</Form>

+ 214
- 0
src/main/java/libsys/RentDialog.java View File

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

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class RentDialog extends javax.swing.JDialog {

static Handler handler = new Handler();
/**
* A return status code - returned if Cancel button has been pressed
*/
public static final int RET_CANCEL = -1;

/**
* Creates new form RentDialog
*/
public RentDialog(java.awt.Frame parent, boolean modal, Handler handler) {
super(parent, modal);
this.handler = handler;
initComponents();

// Close the dialog when Esc is pressed
String cancelName = "cancel";
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put(cancelName, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
doClose(RET_CANCEL);
}
});
}

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

/**
* 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() {

okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
label = new javax.swing.JLabel();
givenBookId = new javax.swing.JTextField();
invalidLabel = new javax.swing.JLabel();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});

okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});

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

label.setText("Please input the book ID:");

invalidLabel.setText("INVALID ID!");
invalidLabel.setVisible(false);

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)
.addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(cancelButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(givenBookId)
.addGroup(layout.createSequentialGroup()
.addComponent(invalidLabel)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
.addComponent(invalidLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(givenBookId, 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(cancelButton)
.addComponent(okButton))
.addContainerGap())
);

getRootPane().setDefaultButton(okButton);

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

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
try
{
if ("AVAILABLE".equals(handler.books.getBook(Integer.parseInt(givenBookId.getText())).getStatus()))
{
doClose(Integer.parseInt(givenBookId.getText()));
}
else
{
invalidLabel.setVisible(true);
}
}
catch (Exception e)
{
invalidLabel.setVisible(true);
}
}//GEN-LAST:event_okButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
doClose(RET_CANCEL);
}//GEN-LAST:event_cancelButtonActionPerformed

/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
doClose(RET_CANCEL);
}//GEN-LAST:event_closeDialog
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(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(RentDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(RentDialog.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() {
RentDialog dialog = new RentDialog(new javax.swing.JFrame(), true, handler);
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 cancelButton;
private javax.swing.JTextField givenBookId;
private javax.swing.JLabel invalidLabel;
private javax.swing.JLabel label;
private javax.swing.JButton okButton;
// End of variables declaration//GEN-END:variables

private int returnStatus = RET_CANCEL;
}

+ 100
- 0
src/main/java/libsys/ReturnDialog.form View File

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

<Form version="1.5" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
<SyntheticProperty name="generateCenter" type="boolean" value="false"/>
</SyntheticProperties>
<Events>
<EventHandler event="windowClosing" listener="java.awt.event.WindowListener" parameters="java.awt.event.WindowEvent" handler="closeDialog"/>
</Events>
<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">
<Component id="label" pref="234" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Component id="cancelButton" linkSize="1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="okButton" linkSize="1" min="-2" pref="67" max="-2" attributes="0"/>
</Group>
<Component id="givenBookId" alignment="0" max="32767" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="invalidLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="label" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="15" max="32767" attributes="0"/>
<Component id="invalidLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="givenBookId" min="-2" max="-2" attributes="0"/>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="okButton" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JButton" name="okButton">
<Properties>
<Property name="text" type="java.lang.String" value="OK"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="okButtonActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_AddingCodePost" type="java.lang.String" value="getRootPane().setDefaultButton(okButton);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="cancelButton">
<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="cancelButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="label">
<Properties>
<Property name="text" type="java.lang.String" value="Please input the book ID:"/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="givenBookId">
</Component>
<Component class="javax.swing.JLabel" name="invalidLabel">
<Properties>
<Property name="text" type="java.lang.String" value="INVALID ID!"/>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_InitCodePost" type="java.lang.String" value="invalidLabel.setVisible(false);"/>
</AuxValues>
</Component>
</SubComponents>
</Form>

+ 227
- 0
src/main/java/libsys/ReturnDialog.java View File

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

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

public class ReturnDialog extends javax.swing.JDialog {

static Handler handler = new Handler();
static int id;
/**
* A return status code - returned if Cancel button has been pressed
*/
public static final int RET_CANCEL = -1;

/**
* Creates new form ReturnDialog
*/
public ReturnDialog(java.awt.Frame parent, boolean modal, Handler handler, int id) {
super(parent, modal);
this.id = id;
this.handler = handler;
initComponents();

// Close the dialog when Esc is pressed
String cancelName = "cancel";
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put(cancelName, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
doClose(RET_CANCEL);
}
});
}

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

/**
* 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() {

okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
label = new javax.swing.JLabel();
givenBookId = new javax.swing.JTextField();
invalidLabel = new javax.swing.JLabel();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});

okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});

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

label.setText("Please input the book ID:");

invalidLabel.setText("INVALID ID!");
invalidLabel.setVisible(false);

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)
.addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(cancelButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(givenBookId)
.addGroup(layout.createSequentialGroup()
.addComponent(invalidLabel)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);

layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
.addComponent(invalidLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(givenBookId, 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(cancelButton)
.addComponent(okButton))
.addContainerGap())
);

getRootPane().setDefaultButton(okButton);

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

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
try
{
int[] borrowedList = handler.users.getUser(id).bookStatus();
boolean exist = false;
for (int i = 0; i < borrowedList.length; i++)
{
if (borrowedList[i] == Integer.parseInt(givenBookId.getText()))
{
exist = true;
}
}
if (exist)
{
doClose(Integer.parseInt(givenBookId.getText()));
}
else
{
invalidLabel.setVisible(true);
}
}
catch (Exception e)
{
invalidLabel.setVisible(true);
}
}//GEN-LAST:event_okButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
doClose(RET_CANCEL);
}//GEN-LAST:event_cancelButtonActionPerformed

/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
doClose(RET_CANCEL);
}//GEN-LAST:event_closeDialog

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(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ReturnDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ReturnDialog.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() {
ReturnDialog dialog = new ReturnDialog(new javax.swing.JFrame(), true, handler, id);
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 cancelButton;
private javax.swing.JTextField givenBookId;
private javax.swing.JLabel invalidLabel;
private javax.swing.JLabel label;
private javax.swing.JButton okButton;
// End of variables declaration//GEN-END:variables

private int returnStatus = RET_CANCEL;
}

+ 27
- 3
src/main/java/libsys/UserFactory.java View File

@@ -33,9 +33,7 @@ class UserFactory
FileInputStream in = new FileInputStream(userFilename);
JSONObject obj = new JSONObject(new JSONTokener(in));
String [] ids = JSONObject.getNames(obj);
System.out.println();

System.out.println("Parsing data into ArrayList...");
for (int i = 0; i < ids.length; i++)
{
JSONObject jsonUser = obj.getJSONObject(ids[i]);
@@ -52,13 +50,39 @@ class UserFactory

users.add(new User(name, id, limit, books));
}
System.out.println();
in.close();
}
catch (Exception ex)
{
System.out.println("Exception importing from json: " + ex.getMessage());
}

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

public void toJsonFile(String userFilename)
{
try
{
PrintWriter out = new PrintWriter(userFilename);
JSONObject usersObj = new JSONObject();
for (int i = 0; i < users.size(); i++)
{
User user = users.get(i);
JSONObject userObj = new JSONObject();
userObj.put("Name", user.getName());
userObj.put("Limit", user.getLimit());
userObj.put("Books", user.bookStatus());
usersObj.put(Integer.toString(user.getId()), userObj);
}
out.println(usersObj.toString(4));
out.close();
}
catch (Exception e)
{
System.out.println("exception: " + e.getMessage());
e.printStackTrace();
}
}

public User newUser(String name, int limit)


+ 11
- 8
users.json View File

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

Loading…
Cancel
Save