Anda di halaman 1dari 61

About.

java
//import the packages for using the classes in them into the program

import javax.swing.*;
import java.awt.*;

/**
*A public class
*/
public class About extends JPanel {
//constructor of about
public About() {
//for creating the image icon
ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("images/java.jpg"));
//for creating the label and setting the image icon
JLabel label1 = new JLabel(icon);
//for adding the label to the panel
this.add(label1);
//for creating the label and setting the text
JLabel label2 = new JLabel("<html><li> Library System"
+ "</li><li><p>Ver# 1.0</li>"
+ "<li><p>Coded by: Govt Polytechnic Project
Students</li><li><p>Neyyatinkara</li><li>"
+ "<p>Copyright<font size=\"2\">&copy;</font> 2007 - 2008</li></html>");
//for setting the font to the label
label2.setFont(new Font("Tahoma", Font.PLAIN, 11));
//for adding the lable to the panel
this.add(label2);
}
}





















Addbook.java

//import the packages for using the classes in them into the program

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
*A public class
*/
public class AddBooks extends JInternalFrame {

/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creaing the North Label
private JLabel northLabel = new JLabel("BOOK INFORMATION");

//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationLabelPanel = new JPanel();

//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[10];
private JLabel lblShelfNo = new JLabel(" Shelf No");
private JTextField txtShelfNo = new JTextField();
//for creating an array of String
private String[] informationString = {
" The book subject: ", " The book title: ",
" The name of the Author(s): ", " The name of the Publisher: ",
" Copyright for the book: ", " The edition number: ", " The number of Pages: ",
" ISBN for the book: ", " The number of copies: ", " The name of the Library: "
};
//for creating an Internal Panel in the center panel
private JPanel informationTextFieldPanel = new JPanel();
//for creating an array of JTextField
private JTextField[] informationTextField = new JTextField[10];

//for creating an Internal Panel in the center panel
private JPanel insertInformationButtonPanel = new JPanel();
//for creating a button
private JButton insertInformationButton = new JButton("Insert the Information");

//for creating South Panel
private JPanel southPanel = new JPanel();
//for creating a button
private JButton OKButton = new JButton("Exit");

//create objects from another classes for using them in the ActionListener
private Books book;
//for creating an array of string to store the data
private String[] data;
//for setting availble option to true
private boolean availble = true;

//for checking the information from the text field
public boolean isCorrect() {
data = new String[10];
for (int i = 0; i < informationLabel.length; i++) {
if (!informationTextField[i].getText().equals("")) {
data[i] = informationTextField[i].getText();
} else {
return false;
}
}
return true;
}

//for setting the array of JTextField to empty
public void clearTextField() {
for (int i = 0; i < informationTextField.length; i++) {
informationTextField[i].setText(null);
}
txtShelfNo.setText(null);
}

//constructor of addBooks
public AddBooks() {
//for setting the title for the internal frame
super("Add Books", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Add16.gif")));
//for getting the graphical user interface components display area
Container cp = getContentPane();

//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font for the North Panel
northLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
//for adding the label in the North Panel
northPanel.add(northLabel);
//for adding the north panel to the container
cp.add("North", northPanel);

//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the border to the panel
centerPanel.setBorder(BorderFactory.createTitledBorder("Add a new book:"));
//for setting the layout
informationLabelPanel.setLayout(new GridLayout(11, 1, 1, 1));
/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for (int i = 0; i < informationLabel.length; i++) {
informationLabelPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Tahoma", Font.BOLD, 11));
}
centerPanel.add("West", informationLabelPanel);

//for setting the layout
informationTextFieldPanel.setLayout(new GridLayout(11, 1, 1, 1));
/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for (int i = 0; i < informationTextField.length; i++) {
informationTextFieldPanel.add(informationTextField[i] = new JTextField(25));
informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
}
lblShelfNo.setFont(new Font("Tahoma", Font.BOLD, 11));
informationLabelPanel.add(lblShelfNo);
txtShelfNo.setFont(new Font("Tahoma", Font.PLAIN, 11));
informationTextFieldPanel.add(txtShelfNo);
centerPanel.add("East", informationTextFieldPanel);

/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* and adding the button to the panel.
*
* finally adding the panel to the container *
***********************************************************************/
insertInformationButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
insertInformationButton.setFont(new Font("Tahoma", Font.BOLD, 11));
insertInformationButtonPanel.add(insertInformationButton);
centerPanel.add("South", insertInformationButtonPanel);
cp.add("Center", centerPanel);

/***********************************************************************
* for setting the layout for the panel,setting the font for the button*
* adding the button to the panel & setting the border. *
* finally adding the panel to the container *
***********************************************************************/
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
OKButton.setFont(new Font("Tahoma", Font.BOLD, 11));
southPanel.add(OKButton);
southPanel.setBorder(BorderFactory.createEtchedBorder());
cp.add("South", southPanel);

/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField[] and make the connection for database, *
* after that update the table in the database with the new value *
***********************************************************************/
insertInformationButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

//for checking if there is a missing information
if (isCorrect()) {
Thread runner = new Thread() {

public void run() {
book = new Books();
//for checking if there is no double information in the database
book.connection("SELECT BookID FROM Books WHERE ISBN = '" + data[7] + "'");
String ISBN = book.getISBN();
if (!data[7].equalsIgnoreCase(ISBN)) {
try{
String sql="INSERT INTO Books (Subject,Title,Author,Publisher,Copyright," +

"Edition,Pages,ISBN,NumberOfBooks,NumberOfAvailbleBooks,Library,Availble,ShelfNo) VALUES "+
" (?,?,?,?,?,?,?,?,?,?,?,?,?)";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:JLibrary");
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, data[0]);
ps.setString(2, data[1]);
ps.setString(3, data[2]);
ps.setString(4, data[3]);
ps.setInt(5, Integer.parseInt(data[4]));
ps.setInt(6,Integer.parseInt(data[5]));
ps.setInt(7, Integer.parseInt(data[6]));
ps.setString(8, data[7]);
ps.setInt(9, Integer.parseInt(data[8]));
ps.setInt(10, Integer.parseInt(data[8]));
ps.setString(11, data[9]);
ps.setBoolean(12, availble);
ps.setInt(13, Integer.parseInt(txtShelfNo.getText()));
ps.executeUpdate();
}catch(Exception ex){
JOptionPane.showMessageDialog(null, ex.toString());
}

/*String sql="INSERT INTO Books (Subject,Title,Author,Publisher,Copyright," +

"Edition,Pages,ISBN,NumberOfBooks,NumberOfAvailbleBooks,Library,Availble,ShelfNo) VALUES ('" +
data[0] + "','" + data[1] + "','" + data[2] + "','" +
data[3] + "', " + data[4] + ", " + data[5] + ", " +
data[6] + ", '" + data[7] + "', " + data[8] + "," + data[8] + ",'" +
data[9] + "', " + availble + ", '" + txtShelfNo.getText() + "')";
book.update(sql);*/
//JOptionPane.showMessageDialog(null, sql);
//for setting the array of JTextField to empty
clearTextField();
} else {
JOptionPane.showMessageDialog(null, "The book is in the library", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
};
runner.start();
} //if there is a missing data, then display Message Dialog
else {
JOptionPane.showMessageDialog(null, "Please, complete the information", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
});
//for adding the action listener for the button to dispose the frame
OKButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {
dispose();
}
});
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}
}











Book.java





//import the packages for using the classes in them into the program

import java.sql.*;

public class Books {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

private Connection connection = null;
private Statement statement = null;
private ResultSet resultSet = null;

private int bookID;
private String subject;
private String title;
private String author;
private String publisher;
private int copyright;
private int edition;
private int pages;
private String ISBN;
private int numberOfBooks;
private int numberOfAvailbleBooks;
private int numberOfBorrowedBooks;
private String library;
private boolean availble;
private String URL = "jdbc:odbc:JLibrary";

public Books() {
}

public int getBookID() {
return bookID;
}

public String getSubject() {
return subject;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public String getPublisher() {
return publisher;
}

public int getCopyright() {
return copyright;
}

public int getEdition() {
return edition;
}

public int getPages() {
return pages;
}

public String getISBN() {
return ISBN;
}

public int getNumberOfBooks() {
return numberOfBooks;
}

public int getNumberOfAvailbleBooks() {
return numberOfAvailbleBooks;
}

public int getNumberOfBorrowedBooks() {
return numberOfBorrowedBooks;
}

public String getLibrary() {
return library;
}

public boolean getAvailble() {
return availble;
}

public void connection(String Query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe) {
System.out.println("Books.java\n" + cnfe.toString());
}
catch (Exception e) {
System.out.println("Books.java\n" + e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection(URL);
statement = connection.createStatement();
resultSet = statement.executeQuery(Query);
while (resultSet.next()) {
bookID = resultSet.getInt(1);
subject = resultSet.getString(2);
title = resultSet.getString(3);
author = resultSet.getString(4);
publisher = resultSet.getString(5);
copyright = resultSet.getInt(6);
edition = resultSet.getInt(7);
pages = resultSet.getInt(8);
ISBN = resultSet.getString(9);
numberOfBooks = resultSet.getInt(10);
numberOfAvailbleBooks = resultSet.getInt(11);
numberOfBorrowedBooks = resultSet.getInt(12);
library = resultSet.getString(13);
availble = resultSet.getBoolean(14);
}
resultSet.close();
statement.close();
connection.close();
}
catch (SQLException SQLe) {
System.out.println("Books.java\n" + SQLe.toString());
}
}

public void update(String Query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe) {
System.out.println("Books.java\n" + cnfe.toString());
}
catch (Exception e) {
System.out.println("Books.java\n" + e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
//connection =
DriverManager.getConnection("jdbc:odbc:JLibrary2");
connection = DriverManager.getConnection(URL);
statement = connection.createStatement();
statement.executeUpdate(Query);
statement.close();
connection.close();
}
catch (SQLException SQLe) {
System.out.println("Books.java\nError:" + SQLe.toString());
}
}
}






















Borrowbook.java


//import the packages for using the classes in them into the program

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
*A public class
*/
public class BorrowBooks extends JInternalFrame {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creating the label
private JLabel title = new JLabel("BOOK INFORMATION");

//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationPanel = new JPanel();
//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[4];
//for creating an array of String
private String[] informationString = {" Write the Book ID:", " Write the Member ID:",
" The Current Data:", " The Return Date:"};
//for creating an array of JTextField
private JTextField[] informationTextField = new JTextField[4];
//for creating the date in the String
private String date = new SimpleDateFormat("dd-MM-yy", Locale.getDefault()).format(new
java.util.Date());
//for creating an array of string to store the data
private String[] data;

//for creating an Internal Panel in the center panel
private JPanel borrowButtonPanel = new JPanel();
//for creating the button
private JButton borrowButton = new JButton("Borrow");

//for creating South Panel
private JPanel southPanel = new JPanel();
//for creating the button
private JButton cancelButton = new JButton("Cancel");

//for creating an object
private Books book;
private Members member;
private Borrow borrow;

//for checking the information from the text field
public boolean isCorrect() {
data = new String[4];
for (int i = 0; i < informationLabel.length; i++) {
if (!informationTextField[i].getText().equals(""))
data[i] = informationTextField[i].getText();
else
return false;
}
return true;
}

//for setting the array of JTextField to null
public void clearTextField() {
for (int i = 0; i < informationTextField.length; i++)
if (i != 2)
informationTextField[i].setText(null);
}

//constructor of borrowBooks
public BorrowBooks() {
//for setting the title for the internal frame
super("Borrow Books", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Export16.gif")));
//for getting the graphical user interface components display area
Container cp = getContentPane();

//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font
title.setFont(new Font("Tahoma", Font.BOLD, 14));
//for adding the label to the panel
northPanel.add(title);
//for adding the panel to the container
cp.add("North", northPanel);

//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the layout for the internal panel
informationPanel.setLayout(new GridLayout(4, 2, 1, 1));

/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel.
*
* finally adding the panel to the container
*
***********************************************************************/
for (int i = 0; i < informationLabel.length; i++) {
informationPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Tahoma", Font.BOLD, 11));
if (i == 2) {
informationPanel.add(informationTextField[i] = new JTextField(date));
informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
informationTextField[i].setEnabled(false);
}
else {
informationPanel.add(informationTextField[i] = new JTextField());
informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
}
}
centerPanel.add("Center", informationPanel);

//for setting the layout
borrowButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for setting the font to the button
borrowButton.setFont(new Font("Tahoma", Font.BOLD, 11));
//for adding the button to the panel
borrowButtonPanel.add(borrowButton);
//for adding the panel to the center panel
centerPanel.add("South", borrowButtonPanel);
//for setting the border to the panel
centerPanel.setBorder(BorderFactory.createTitledBorder("Borrow a book:"));
//for adding the panel to the container
cp.add("Center", centerPanel);

//for adding the layout
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for setting the font to the button
cancelButton.setFont(new Font("Tahoma", Font.BOLD, 11));
//for adding the button to the panel
southPanel.add(cancelButton);
//for setting the border to the panel
southPanel.setBorder(BorderFactory.createEtchedBorder());
//for adding the panel to the container
cp.add("South", southPanel);

/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField[] and make the connection for database, *
* after that update the table in the database with the new value *
***********************************************************************/
borrowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//for checking if there is a missing information
if (isCorrect()) {
Thread runner = new Thread() {
public void run() {
book = new Books();
member = new Members();
borrow = new Borrow();
book.connection("SELECT * FROM Books
WHERE BookID = " + data[0]);
member.connection("SELECT * FROM Members
WHERE MemberID = " + data[1]);
int numberOfAvailbleBooks =
book.getNumberOfAvailbleBooks();
int numberOfBorrowedBooks = 1 +
book.getNumberOfBorrowedBooks();
int numberOfBooks = 1 +
member.getNumberOfBooks();
//for checking if there is no same information in
the database
if (numberOfAvailbleBooks == 1) {
numberOfAvailbleBooks -= 1;
book.update("UPDATE Books SET
NumberOfAvailbleBooks =" + numberOfAvailbleBooks +
",NumberOfBorrowedBooks =" +
numberOfBorrowedBooks + ",Availble = false WHERE BookID =" + data[0]);
member.update("UPDATE Members
SET NumberOfBooks = " + numberOfBooks + " WHERE MemberID = " + data[1]);
borrow.update("INSERT INTO Borrow
(BookID, MemberID, DayOfBorrowed, DayOfReturn) VALUES (" +
data[0] + "," + data[1] + ",'" +
data[2] + "','" + data[3] + "')");
//for setting the array of JTextField to
null
clearTextField();
}
else if (numberOfAvailbleBooks > 1) {
numberOfAvailbleBooks -= 1;
book.update("UPDATE Books SET
NumberOfAvailbleBooks =" + numberOfAvailbleBooks +
",NumberOfBorrowedBooks =" +
numberOfBorrowedBooks + " WHERE BookID =" + data[0]);
member.update("UPDATE Members
SET NumberOfBooks =" + numberOfBooks + " WHERE MemberID =" + data[1]);
borrow.update("INSERT INTO Borrow
(BookID, MemberID, DayOfBorrowed, DayOfReturn) VALUES (" +
data[0] + "," + data[1] + ",'" +
data[2] + "','" + data[3] + "')");
//for setting the array of JTextField to
null
clearTextField();
}
else
JOptionPane.showMessageDialog(null,
"The book is borrowed", "Warning", JOptionPane.WARNING_MESSAGE);
}
};
runner.start();
}
//if there is a missing data, then display Message Dialog
else
JOptionPane.showMessageDialog(null, "Please, complete the
information", "Warning", JOptionPane.WARNING_MESSAGE);
}
});
//for adding the action listener for the button to dispose the frame
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
dispose();
}
});
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}
}








Jlibrary.java
//import the packages for using the classes in them into the program
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
*A public class
*/
public class JLibrary extends JFrame implements ActionListener {

/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

//for creating the JPanel
private JPanel searchPanel = new JPanel();
//for creating the JToolBar for the program
private JToolBar searchToolBar = new JToolBar();
//for creating the label
private JLabel searchLabel = new JLabel("Book title: ");
//for creating the JTextField to use it on the searchToolBar
private JTextField searchTextField = new JTextField(15);
//for creating the JButton to use it on the searchToolBar
private JButton goButton = new JButton("Go");
//for creating JTabbedPane
//private JTabbedPane tabbedPane = new JTabbedPane();
//for creating JDeskTopPane for using JInternalFrame on the desktop
private JDesktopPane desktop = new JDesktopPane();
//private JDesktopPane desktop;
//for creating JSplitPane
private JSplitPane splitPane;
//for creating JScrollPane for JDesktopPane
private JScrollPane desktopScrollPane;
private JScrollPane treeScrollPane;
//for creating the background
//private ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("images/Logo.JPG"));
//for creating JLabel
//private JLabel background = new JLabel(icon);
/***************************************************************************
*create objects from another classes for using them in the ActionListener *
***************************************************************************/
private Menubar menu;
private Toolbar toolbar;
private StatusBar statusbar = new StatusBar();
private ListBooks listBooks;
private AddBooks addBooks;
private ListAvailbleBooks listAvailble;
private ListBorrowedBooks listBorrowed;
private EditBooks editBooks;
private RemoveBooks removeBooks;
private BorrowBooks borrowBooks;
private ReturnBooks returnBooks;
//private BooksInformation booksInformation;
private AddMembers addMembers;
private ListMembers listMembers;
private EditMembers editMembers;
private RemoveMembers removeMembers;
private MembersInformation membersInformation;
private SearchBooksAndMembers search;


//constructor of JLibrary
public JLibrary() {
//for setting the title for the frame
super("JAVA Library System - JLibrary");
//for setting the size
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//setSize(screenSize.width, screenSize.height - 30);
setExtendedState(JFrame.MAXIMIZED_BOTH);
//for setting resizable to false
//setResizable(false);

/**
*for settting an icon for the program
*/
Toolkit kit = Toolkit.getDefaultToolkit();
Image image = kit.getImage(ClassLoader.getSystemResource("images/Host16.gif"));
setIconImage(image);

menu = new Menubar();
toolbar = new Toolbar();
//for setting the menu bar
setJMenuBar(menu);
//for adding the actionListener
menu.printBook.addActionListener(this);
menu.exit.addActionListener(this);
menu.addBook.addActionListener(this);
menu.listBook.addActionListener(this);
menu.listAvailbleBook.addActionListener(this);
menu.listBorrowedBook.addActionListener(this);
menu.editBook.addActionListener(this);
menu.removeBook.addActionListener(this);
//menu.bookInformation.addActionListener(this);
menu.addMember.addActionListener(this);
menu.listMember.addActionListener(this);
menu.editMember.addActionListener(this);
menu.removeMember.addActionListener(this);
menu.memberInformation.addActionListener(this);
menu.searchBooksAndMembers.addActionListener(this);
menu.borrowBook.addActionListener(this);
menu.returnBook.addActionListener(this);
menu.listissuedbooks.addActionListener(this);
menu.reserveBook.addActionListener(this);
menu.help.addActionListener(this);
menu.about.addActionListener(this);

//get the graphical user interface components display the desktop
Container cp = getContentPane();
desktop.setBackground(Color.GRAY);
cp.add("Center", desktop);
//for setting the font
searchLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
//for setting the font
searchTextField.setFont(new Font("Tahoma", Font.PLAIN, 12));
goButton.setFont(new Font("Tahoma", Font.BOLD, 9));
//for adding the searchLable to the searchToolBar
searchToolBar.add(searchLabel);
//for adding the searchTextField to searchToolBar
searchToolBar.add(searchTextField);
//for adding the goButton to searchToolBar
searchToolBar.add(goButton);
//for adding listenerAction for the button
goButton.addActionListener(this);
//for setting the layout
searchPanel.setLayout(new BorderLayout());
//for adding the toolBar to the searchPanel
searchPanel.add("Center", toolbar);
//for adding the searchToolBar to the searchPanel
//searchPanel.add("South", searchToolBar);
//for adding the searchPanel to the Container
cp.add("North", searchPanel);
//for adding the statusbar to the Container
cp.add("South", statusbar);

for (int i = 0; i < toolbar.imageName24.length; i++) {
//for adding the action to the button
toolbar.button[i].addActionListener(this);
}

//for adding WindowListener to the program
addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//show the program
show();
}

/**
*this method is invoked when an action occurs.
*@param ae the action event.
*/
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == menu.addBook || ae.getSource() == toolbar.button[0]) {
Thread runner = new Thread() {

public void run() {
addBooks = new AddBooks();
desktop.add(addBooks);
try {
addBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listBook || ae.getSource() == toolbar.button[1]) {
Thread runner = new Thread() {

public void run() {
listBooks = new ListBooks();
desktop.add(listBooks);
try {
listBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listAvailbleBook || ae.getSource() == toolbar.button[2]) {
Thread runner = new Thread() {

public void run() {
listAvailble = new ListAvailbleBooks();
desktop.add(listAvailble);
try {
listAvailble.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listBorrowedBook || ae.getSource() == toolbar.button[3]) {
Thread runner = new Thread() {

public void run() {
listBorrowed = new ListBorrowedBooks();
desktop.add(listBorrowed);
try {
listBorrowed.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.editBook || ae.getSource() == toolbar.button[4]) {
Thread runner = new Thread() {

public void run() {
editBooks = new EditBooks();
desktop.add(editBooks);
try {
editBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.removeBook || ae.getSource() == toolbar.button[5]) {
Thread runner = new Thread() {

public void run() {
removeBooks = new RemoveBooks();
desktop.add(removeBooks);
try {
removeBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
/* if (ae.getSource() == menu.bookInformation || ae.getSource() == toolbar.button[6]) {
Thread runner = new Thread() {
public void run() {
booksInformation = new BooksInformation();
desktop.add(booksInformation);
try {
booksInformation.setSelected(true);
}
catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}*/
if (ae.getSource() == menu.addMember || ae.getSource() == toolbar.button[7]) {
Thread runner = new Thread() {

public void run() {
addMembers = new AddMembers();
desktop.add(addMembers);
try {
addMembers.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.listMember || ae.getSource() == toolbar.button[8]) {
Thread runner = new Thread() {

public void run() {
listMembers = new ListMembers();
desktop.add(listMembers);
try {
listMembers.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.editMember || ae.getSource() == toolbar.button[9]) {
Thread runner = new Thread() {

public void run() {
editMembers = new EditMembers();
desktop.add(editMembers);
try {
editMembers.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.removeMember || ae.getSource() == toolbar.button[10]) {
Thread runner = new Thread() {

public void run() {
removeMembers = new RemoveMembers();
desktop.add(removeMembers);
try {
removeMembers.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.memberInformation || ae.getSource() == toolbar.button[11]) {
Thread runner = new Thread() {

public void run() {
membersInformation = new MembersInformation();
desktop.add(membersInformation);
try {
membersInformation.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.searchBooksAndMembers || ae.getSource() == toolbar.button[12]) {
Thread runner = new Thread() {

public void run() {
search = new SearchBooksAndMembers();
desktop.add(search);
try {
search.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.borrowBook || ae.getSource() == toolbar.button[13]) {
Thread runner = new Thread() {

public void run() {
borrowBooks = new BorrowBooks();
desktop.add(borrowBooks);
try {
borrowBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
if (ae.getSource() == menu.returnBook || ae.getSource() == toolbar.button[14]) {
Thread runner = new Thread() {

public void run() {
returnBooks = new ReturnBooks();
desktop.add(returnBooks);
try {
returnBooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}

if (ae.getSource() == menu.reserveBook) {
Thread runner = new Thread() {

public void run() {
ReserveBook reserveBook = new ReserveBook();
desktop.add(reserveBook);
try {
reserveBook.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}

if (ae.getSource() == menu.help || ae.getSource() == toolbar.button[15]) {
//
}
if (ae.getSource() == menu.about || ae.getSource() == toolbar.button[16]) {
Thread runner = new Thread() {

public void run() {
JOptionPane.showMessageDialog(null, new About(), "About JLibrary",
JOptionPane.PLAIN_MESSAGE);
}
};
runner.start();
}
if (ae.getSource() == menu.printBook || ae.getSource() == toolbar.button[17]) {
Thread runner = new Thread() {

public void run() {
//Print.printComponent(new JLibrary());
}
};
runner.start();
}
if (ae.getSource() == menu.exit || ae.getSource() == toolbar.button[18]) {
dispose();
System.exit(0);
}
if(ae.getSource()==menu.listissuedbooks){
Thread runner = new Thread() {

public void run() {
ListIssuedBooks listissuedbooks=new ListIssuedBooks();
desktop.add(listissuedbooks);
try {
listissuedbooks.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
};
runner.start();
}
}
Main.java

/*******************************************************************************
*/

//import the packages for using the classes in them into the program

import java.awt.EventQueue;
import java.awt.Frame;
import javax.swing.JDialog;



public class Main implements Runnable {
final Frame frame;

public Main(Frame frame) {
this.frame = frame;
}

public void run() {
frame.show();
}

public static void main(String[] args) {
JDialog.setDefaultLookAndFeelDecorated(true);
// Throw a nice little title page up on the screen first
new Splash().showSplash(3000);
EventQueue.invokeLater(new Main(new JLibrary()));
}
}



















Menubar.java



//import the packages for using the classes in them into the program
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.management.JMException;

public class Menubar extends JMenuBar {

/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

//for creating the JMenu for the program
public JMenu fileMenu, bookMenu, memberMenu, searchMenu, loanMenu, reserveMenu,
helpMenu;
//for creating the JMenuItem for JMenu
public JMenuItem printBook, exit, addBook, listBook, listAvailbleBook, listBorrowedBook;
public JMenuItem editBook, removeBook, bookInformation, addMember, listMember,
editMember;
public JMenuItem removeMember, memberInformation, searchBooksAndMembers, borrowBook,
returnBook;
public JMenuItem listissuedbooks,reserveBook, help, about;
//for creating an imageIcon
public ImageIcon[] icons;
//for creating the name of the image file 16*16
public String[] imageName16 = {"images/Print16.gif", "images/Exit16.gif",
"images/Add16.gif", "images/List16.gif",
"images/Edit16.gif", "images/Delete16.gif",
"images/Information16.gif", "images/Find16.gif",
"images/Export16.gif", "images/Import16.gif",
"images/Help16.gif", "images/About16.gif"
};

public Menubar() {
//for adding book, member, search, loan & help Menus to the menu bar
this.add(fileMenu = new JMenu("File"));
this.add(bookMenu = new JMenu("Books"));
this.add(memberMenu = new JMenu("Members"));
this.add(searchMenu = new JMenu("Search"));
this.add(loanMenu = new JMenu("Loan"));
this.add(reserveMenu = new JMenu("Reserve"));
this.add(helpMenu = new JMenu("Help"));

/**
*for setting the Mnemonic
*/
fileMenu.setMnemonic('f');
bookMenu.setMnemonic('b');
memberMenu.setMnemonic('m');
searchMenu.setMnemonic('s');
loanMenu.setMnemonic('l');
reserveMenu.setMnemonic('r');
helpMenu.setMnemonic('h');

//for setting the image icons
icons = new ImageIcon[12];
for (int i = 0; i < imageName16.length; i++) {
icons[i] = new ImageIcon(ClassLoader.getSystemResource(imageName16[i]));
}

//for adding print books & exit
fileMenu.add(printBook = new JMenuItem("Print Books", icons[0]));
fileMenu.add(exit = new JMenuItem("Exit", icons[1]));

//for adding add, list, listAvailble, listBorrowed, edit & remove Books and book information to the
bookMenu
bookMenu.add(addBook = new JMenuItem("Add Book", icons[2]));
bookMenu.add(listBook = new JMenuItem("List All Books", icons[3]));
bookMenu.add(listAvailbleBook = new JMenuItem("List Availble Books", icons[3]));
bookMenu.add(listBorrowedBook = new JMenuItem("List Borrowed Books", icons[3]));
bookMenu.add(editBook = new JMenuItem("Edit Books", icons[4]));
bookMenu.add(removeBook = new JMenuItem("Remove Book", icons[5]));
//bookMenu.add(bookInformation = new JMenuItem("Book Information", icons[6]));

//for adding add, list, edit & remove Members and member information to the memberMenu
memberMenu.add(addMember = new JMenuItem("Add Member", icons[2]));
memberMenu.add(listMember = new JMenuItem("List All Members", icons[3]));
memberMenu.add(editMember = new JMenuItem("Edit Members", icons[4]));
memberMenu.add(removeMember = new JMenuItem("Remove Member", icons[5]));
memberMenu.add(memberInformation = new JMenuItem("Member Information", icons[6]));

//for adding add, list & remove Members to the memberMenu
searchMenu.add(searchBooksAndMembers = new JMenuItem("Search", icons[7]));

//for adding borrow & return books to the loanMenu
loanMenu.add(borrowBook = new JMenuItem("Borrow a Book", icons[8]));
loanMenu.add(returnBook = new JMenuItem("Return a Book", icons[9]));
loanMenu.add(listissuedbooks=new JMenuItem("Issued book details",icons[3]));
//Reserve a book
reserveMenu.add(reserveBook = new JMenuItem("Reserve a Book", icons[10]));

//for adding help & about to the helpMenu
helpMenu.add(help = new JMenuItem("Help", icons[10]));
helpMenu.add(about = new JMenuItem("About", icons[11]));

//for setting the shortcut
printBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
searchBooksAndMembers.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
ActionEvent.CTRL_MASK));

addBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
listBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, ActionEvent.CTRL_MASK));
editBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
removeBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));

addMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
listMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
editMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
removeMember.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));

borrowBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
returnBook.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
listissuedbooks.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
help.setAccelerator(KeyStroke.getKeyStroke("F1"));
about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
}
}






Returnbook.java


//import the packages for using the classes in them into the program

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.GregorianCalendar;

/**
*A public class
*/
public class ReturnBooks extends JInternalFrame implements ActionListener {

/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/

//for creating the North Panel
private JPanel northPanel = new JPanel();
//for creating the label
private JLabel title = new JLabel("BOOK INFORMATION");

//for creating the Center Panel
private JPanel centerPanel = new JPanel();
//for creating an Internal Panel in the center panel
private JPanel informationPanel = new JPanel();
//for creating an array of JLabel
private JLabel[] informationLabel = new JLabel[2];
//for creating an array of String
private String[] informationString = {" Write the Book ID:", " Write the Member ID:"};
//for creating an array of JTextField
private JTextField[] informationTextField = new JTextField[2];
//for creating an array of string to store the data
private String[] data;
private JLabel lblFinePerDay = new JLabel("Fine/Day");
private JTextField txtFinePerDay = new JTextField();
private JLabel lblTotalFineAmt = new JLabel("Total fine amount");
private JTextField txtTotalFineAmt = new JTextField();
//for creating an Internal Panel in the center panel
private JPanel returnButtonPanel = new JPanel();
//for creating the buton
private JButton returnButton = new JButton("Return");

//for creating the panel
private JPanel southPanel = new JPanel();
//for creating the button
private JButton cancelButton = new JButton("Cancel");

//for creating an object
private Books book;
private Members member;
private Borrow borrow;


//for checking the information from the text field
public boolean isCorrect() {
data = new String[2];
for (int i = 0; i < informationLabel.length; i++) {
if (!informationTextField[i].getText().equals("")) {
data[i] = informationTextField[i].getText();
} else {
return false;
}
}
return true;
}

//for setting the array of JTextField to null
public void clearTextField() {
for (int i = 0; i < informationTextField.length; i++) {
if (i != 2) {
informationTextField[i].setText(null);
}
txtFinePerDay.setText(null);
txtTotalFineAmt.setText(null);
}
}

//constructor of returnBooks
public ReturnBooks() {
//for setting the title for the internal frame
super("Return books", false, true, false, true);
//for setting the icon
setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/Import16.gif")));
//for getting the graphical user interface components display area
Container cp = getContentPane();

//for setting the layout
northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//for setting the font
title.setFont(new Font("Tahoma", Font.BOLD, 14));
//for adding the label
northPanel.add(title);
//for adding the north panel to the container
cp.add("North", northPanel);

//for setting the layout
centerPanel.setLayout(new BorderLayout());
//for setting the layout for the internal panel
informationPanel.setLayout(new GridLayout(4, 2, 1, 1));

/***********************************************************************
* for adding the strings to the labels, for setting the font *
* and adding these labels to the panel. *
* finally adding the panel to the container *
***********************************************************************/
for (int i = 0; i < informationLabel.length; i++) {
informationPanel.add(informationLabel[i] = new JLabel(informationString[i]));
informationLabel[i].setFont(new Font("Tahoma", Font.BOLD, 11));
informationPanel.add(informationTextField[i] = new JTextField());
informationTextField[i].setFont(new Font("Tahoma", Font.PLAIN, 11));
}
informationPanel.add(lblFinePerDay);
informationPanel.add(txtFinePerDay);
informationPanel.add(lblTotalFineAmt);
informationPanel.add(txtTotalFineAmt);
txtTotalFineAmt.setEditable(false);
txtFinePerDay.addKeyListener(new keyListener());
centerPanel.add("Center", informationPanel);
//for setting the layout
returnButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button
returnButtonPanel.add(returnButton);
//for setting the font to the button
returnButton.setFont(new Font("Tahoma", Font.BOLD, 11));
//for adding the internal panel to the panel
centerPanel.add("South", returnButtonPanel);
//for setting the border
centerPanel.setBorder(BorderFactory.createTitledBorder("Return a book:"));
//for adding the center panel to the container
cp.add("Center", centerPanel);

//for setting the layout
southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
//for adding the button

southPanel.add(cancelButton);
//for setting the font to the button
cancelButton.setFont(new Font("Tahoma", Font.BOLD, 11));
//for setting the border
southPanel.setBorder(BorderFactory.createEtchedBorder());
//for adding the south panel to the container
cp.add("South", southPanel);

/***********************************************************************
* for adding the action listener to the button,first the text will be *
* taken from the JTextField and make the connection for database, *
* after that update the table in the database with the new value *
***********************************************************************/
returnButton.addActionListener(this);
//for adding the action listener for the button to dispose the frame
cancelButton.addActionListener(this);
//for setting the visible to true
setVisible(true);
//show the internal frame
pack();
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == returnButton) {
//for checking if there is a missing information
if (isCorrect()) {
Thread runner = new Thread() {

public void run() {
book = new Books();
member = new Members();
borrow = new Borrow();
book.connection("SELECT * FROM Books WHERE BookID = " + data[0]);
member.connection("SELECT * FROM Members WHERE MemberID = " + data[1]);
int numberOfAvailbleBooks = book.getNumberOfAvailbleBooks();
int numberOfBorrowedBooks = book.getNumberOfBorrowedBooks() - 1;
int numberOfBooks = member.getNumberOfBooks();
//for checking if there is no same information in the database
if (numberOfAvailbleBooks == 0 && numberOfBooks > 0) {
numberOfAvailbleBooks += 1;
numberOfBooks -= 1;
book.update("UPDATE Books SET NumberOfAvailbleBooks =" + numberOfAvailbleBooks
+
",NumberOfBorrowedBooks =" + numberOfBorrowedBooks + ",Availble = true
WHERE BookID =" + data[0]);
member.update("UPDATE Members SET NumberOfBooks =" + numberOfBooks + "
WHERE MemberID =" + data[1]);
borrow.update("DELETE FROM Borrow WHERE BookID =" + data[0] + " AND MemberID
=" + data[1]);
//for setting the array of JTextField to null
clearTextField();
} else if (numberOfAvailbleBooks > 0 && numberOfBooks > 0) {
numberOfAvailbleBooks += 1;
numberOfBooks -= 1;
book.update("UPDATE Books SET NumberOfAvailbleBooks =" + numberOfAvailbleBooks
+
",NumberOfBorrowedBooks =" + numberOfBorrowedBooks + " WHERE BookID =" +
data[0]);
member.update("UPDATE Members SET NumberOfBooks =" + numberOfBooks + "
WHERE MemberID =" + data[1]);
borrow.update("DELETE FROM Borrow WHERE BookID =" + data[0] + " AND MemberID
=" + data[1]);
//for setting the array of JTextField to null
clearTextField();
} else {
JOptionPane.showMessageDialog(null, "The book is not borrowed", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
};
runner.start();
} //if there is a missing data, then display Message Dialog
else {
JOptionPane.showMessageDialog(null, "Please, complete the information", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
if (ae.getSource() == cancelButton) {
dispose();
}
}

class keyListener extends KeyAdapter {

public void keyPressed(KeyEvent k) {
java.sql.Date da = null;
if (k.getKeyCode() == KeyEvent.VK_ENTER) {
try {
int fineamt = Integer.parseInt(txtFinePerDay.getText());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:JLibrary");
Statement st = con.createStatement();
int bookid = Integer.parseInt(informationTextField[0].getText());
int memid = Integer.parseInt(informationTextField[1].getText());
try {
String sql = "SELECT DayOfReturn from Borrow where MemberID=" + memid + " and
BookID=" + bookid;
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {

da = rs.getDate(1);
java.util.Date today = new java.util.Date();
/*java.util.Date retdate=new java.util.Date(da.getYear(),da.getMonth(),da.getDate());
JOptionPane.showMessageDialog(null, "today=" + today + "\nRet date=" + retdate);*/

System.out.println(today.after(da));

if (today.after(da)) {
long finedays = today.getTime() - da.getTime();
int days = (int) (finedays / (1000 * 60 * 60 * 24));
System.out.println(days);
txtTotalFineAmt.setText(String.valueOf(fineamt * days));
} else {
txtTotalFineAmt.setText("0");
}
} else {
JOptionPane.showMessageDialog(null, "Member ID entered not found on databse");
}

} catch (Exception ex1) {
JOptionPane.showMessageDialog(null, "Error, Cannot retrieve date value from table" +
ex1.toString());
}

} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error, cannot connect to database" + ex.toString());
}
}
}
}//inner class closed
}//class closed

Anda mungkin juga menyukai