//
JdbcTemplate.java
package
edu.jlu.fuliang.library.jdbc;
import
java.util.ArrayList;
import
java.util.List;
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
public
class
JdbcTemplate {
public
List query(String sql,RowCallBackHandler handler){
Connection connection
=
JdbcUtils.createConnection();
PreparedStatement ps
=
JdbcUtils.createPreparedStatement(sql,connection);
ResultSet rs
=
null
;
List list
=
new
ArrayList();
try
{
rs
=
ps.executeQuery();
while
(rs.next()){
list.add(handler.processRow(rs));
}
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
JdbcUtils.closeStatement(ps);
JdbcUtils.relaseConnection(connection);
}
return
list;
}
public
Object queryForObject(String sql,RowCallBackHandler handler){
Connection connection
=
JdbcUtils.createConnection();
PreparedStatement ps
=
JdbcUtils.createPreparedStatement(sql,connection);
ResultSet rs
=
null
;
Object o
=
null
;
try
{
rs
=
ps.executeQuery();
rs.next();
o
=
handler.processRow(rs);
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
JdbcUtils.closeStatement(ps);
JdbcUtils.relaseConnection(connection);
}
return
o;
}
public
void
executeUpdate(String sql,PreparedStatementCallBack pareparedStatementCallBack){
Connection connection
=
JdbcUtils.createConnection();
PreparedStatement ps
=
JdbcUtils.createPreparedStatement(sql,connection);
try
{
pareparedStatementCallBack.doInStatement(ps);
ps.executeUpdate();
}
catch
(SQLException e) {
e.printStackTrace();
}
finally
{
JdbcUtils.closeStatement(ps);
JdbcUtils.relaseConnection(connection);
}
}
}
//
回调接口RowCallBackHandler
package
edu.jlu.fuliang.library.jdbc;
import
java.sql.ResultSet;
public
interface
RowCallBackHandler {
public
Object processRow(ResultSet rs);
}
//
/回调接口PreparedStatementCallBack
package
edu.jlu.fuliang.library.jdbc;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
public
interface
PreparedStatementCallBack {
public
void
doInStatement(PreparedStatement stmt)
throws
SQLException;
}
//
ORMaping接口
package
edu.jlu.fuliang.library.jdbc;
import
java.sql.ResultSet;
public
interface
ORMaping {
public
Object mapping(ResultSet rs,Object o);
}
//
JdbcUtils.java
package
edu.jlu.fuliang.library.jdbc;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
java.sql.Statement;
public
class
JdbcUtils {
public
static
Connection createConnection(){
//
为了简单,进行了硬编码
String user
=
"
root
"
;
String passwd
=
"
123456
"
;
String url
=
"
jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312
"
;
Connection connection
=
null
;
try
{
Class.forName(
"
com.mysql.jdbc.Driver
"
).newInstance();
connection
=
DriverManager.getConnection(url,user,passwd);
}
catch
(Exception e){
e.printStackTrace();
}
return
connection;
}
public
static
PreparedStatement createPreparedStatement(String sql,Connection connection){
PreparedStatement stmt
=
null
;
try
{
stmt
=
connection.prepareStatement(sql);
}
catch
(Exception e){
e.printStackTrace();
}
return
stmt;
}
public
static
void
closeStatement(Statement s){
try
{
s.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
public
static
void
relaseConnection(Connection connection){
try
{
connection.close();
}
catch
(SQLException e) {
e.printStackTrace();
}
}
}
//
以下是数据访问对象:
//
BorrowerDao.java
package
edu.jlu.fuliang.library.dao;
import
edu.jlu.fuliang.library.domain.Borrower;
public
interface
BorrowerDao {
public
Borrower queryByLastName(String lastName);
public
void
updateBorrower(Borrower borrower);
public
void
insertBorrower(Borrower borrower);
public
void
deleteBorrowerByLastName(String lastName);
}
//
BookDao.java
package
edu.jlu.fuliang.library.dao;
import
java.util.List;
import
edu.jlu.fuliang.library.domain.Book;
public
interface
BookDao {
public
Book queryByTitle(String title);
public
List queryByAuthor(String author);
public
List queryByBorrowerLastName(String firstName);
public
void
updateBook(Book book);
public
void
insertBook(Book book);
public
void
deleteBookByTile(String title);
}
//
BorrowOrReturnDao.java
package
edu.jlu.fuliang.library.dao;
public
interface
BorrowOrReturnDao {
public
void
borrowBook(String borrowerLastName,String bookTile);
public
void
returnBook(String borrowerLastName,String bookTile);
}
//
抽象工厂:
package
edu.jlu.fuliang.library.dao;
public
interface
DaoFactory {
public
BookDao createBookDao();
public
BorrowerDao createBorrowerDao();
public
BorrowOrReturnDao createBorrowOrReturnDao();
}
//
具体工厂:
package
edu.jlu.fuliang.library.dao;
import
edu.jlu.fuliang.library.dao.mysql.BookDaoImpl;
import
edu.jlu.fuliang.library.dao.mysql.BorrowerDaoImpl;
public
class
MysqlDaoFactory
implements
DaoFactory{
public
BookDao createBookDao() {
return
new
BookDaoImpl();
}
public
BorrowerDao createBorrowerDao() {
return
new
BorrowerDaoImpl();
}
public
BorrowOrReturnDao createBorrowOrReturnDao() {
return
new
BorrowOrReturnDaoImpl();
}
}
//
BookDao的具体实现:
//
BookDaoImpl.java
package
edu.jlu.fuliang.library.dao.mysql;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.util.List;
import
edu.jlu.fuliang.library.dao.BookDao;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.jdbc.
*
;
public
class
BookDaoImpl
implements
BookDao{
private
JdbcTemplate jdbcTemplate
=
new
JdbcTemplate();
public
Book queryByTitle(String title) {
String sql
=
"
select * from book where title =
"
+
title;
return
(Book)jdbcTemplate.queryForObject(sql,
new
RowCallBackHandler(){
public
Object processRow(ResultSet rs) {
BookMaping map
=
new
BookMaping();
Book book
=
(Book)map.mapping(rs,
new
Book());
return
book;
}
});
}
public
List queryByAuthor(String author) {
String sql
=
"
select * from book where author =
"
+
author;
return
jdbcTemplate.query(sql,
new
RowCallBackHandler(){
public
Object processRow(ResultSet rs) {
BookMaping map
=
new
BookMaping();
Book book
=
(Book)map.mapping(rs,
new
Book());
return
book;
}
});
}
//
Borrower的具体实现:
//
BookDaoImpl.java
package
edu.jlu.fuliang.library.dao.mysql;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
edu.jlu.fuliang.library.dao.BorrowerDao;
import
edu.jlu.fuliang.library.domain.Borrower;
import
edu.jlu.fuliang.library.jdbc.JdbcTemplate;
import
edu.jlu.fuliang.library.jdbc.PreparedStatementCallBack;
import
edu.jlu.fuliang.library.jdbc.RowCallBackHandler;
public
class
BorrowerDaoImpl
implements
BorrowerDao{
private
JdbcTemplate jdbcTemplate
=
new
JdbcTemplate();
public
Borrower queryByLastName(String lastName) {
String sql
=
"
select * from borrower where lastName =
"
+
lastName;
return
(Borrower)jdbcTemplate.queryForObject(sql,
new
RowCallBackHandler(){
public
Object processRow(ResultSet rs) {
BorrowerMaping borrowerMaping
=
new
BorrowerMaping();
return
borrowerMaping.mapping(rs,
new
Borrower());
}
});
}
public
void
updateBorrower(
final
Borrower borrower) {
String sql
=
"
update borrower set lastname = ?,firstname=?
"
+
"
,address =?,city =?,zip=?,state=? where lastname = ?
"
;
jdbcTemplate.executeUpdate(sql,
new
PreparedStatementCallBack(){
public
void
doInStatement(PreparedStatement stmt)
throws
SQLException {
stmt.setString(
1
,borrower.getLastName());
stmt.setString(
2
,borrower.getFirstName());
stmt.setString(
3
,borrower.getAddress());
stmt.setString(
4
,borrower.getCity());
stmt.setString(
5
,borrower.getZip());
stmt.setString(
6
,borrower.getState());
stmt.setString(
7
,borrower.getLastName());
}
});
}
public
void
insertBorrower(
final
Borrower borrower) {
String sql
=
"
insert into borrower values(?,?,?,?,?,?)
"
;
jdbcTemplate.executeUpdate(sql,
new
PreparedStatementCallBack(){
public
void
doInStatement(PreparedStatement stmt)
throws
SQLException {
stmt.setString(
1
,borrower.getLastName());
stmt.setString(
2
,borrower.getFirstName());
stmt.setString(
3
,borrower.getAddress());
stmt.setString(
4
,borrower.getCity());
stmt.setString(
5
,borrower.getZip());
stmt.setString(
6
,borrower.getState());
}
});
}
public
void
deleteBorrowerByLastName(
final
String lastName) {
String sql
=
"
delete from borrower where lastName = ?
"
;
jdbcTemplate.executeUpdate(sql,
new
PreparedStatementCallBack(){
public
void
doInStatement(PreparedStatement stmt)
throws
SQLException {
stmt.setString(
1
,lastName);
}
});
}
}
//
BorrowOrReturnDaoImpl.java
package
edu.jlu.fuliang.library.dao.mysql;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
import
edu.jlu.fuliang.library.dao.BorrowOrReturnDao;
import
edu.jlu.fuliang.library.jdbc.JdbcTemplate;
import
edu.jlu.fuliang.library.jdbc.PreparedStatementCallBack;
public
class
BorrowOrReturnDaoImpl
implements
BorrowOrReturnDao{
private
JdbcTemplate jdbcTemplate
=
new
JdbcTemplate();
public
void
borrowBook(
final
String borrowerLastName,
final
String bookTitle) {
String sql
=
"
insert into book_borrower values(?,?)
"
;
jdbcTemplate.executeUpdate(sql,
new
PreparedStatementCallBack(){
public
void
doInStatement(PreparedStatement stmt) {
try
{
stmt.setString(
1
,borrowerLastName);
stmt.setString(
2
,bookTitle);
}
catch
(SQLException e){
e.printStackTrace();
}
}
});
}
public
void
returnBook(
final
String borrowerLastName,
final
String bookTitle) {
String sql
=
"
delete from book_borrower where lastname = ?,title = ?
"
;
jdbcTemplate.executeUpdate(sql,
new
PreparedStatementCallBack(){
public
void
doInStatement(PreparedStatement stmt) {
try
{
stmt.setString(
1
,borrowerLastName);
stmt.setString(
2
,bookTitle);
}
catch
(SQLException e){
e.printStackTrace();
}
}
});
}
}
//
book的ORMaping的实现:
//
BookMaping.java
package
edu.jlu.fuliang.library.dao.mysql;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.jdbc.ORMaping;
public
class
BookMaping
implements
ORMaping{
public
Object mapping(ResultSet rs, Object o) {
Book book
=
(Book)o;
try
{
book.setTitleName(rs.getString(
"
titleName
"
));
book.setAuthor(rs.getString(
"
author
"
));
book.setISBN(rs.getString(
"
isbn
"
));
book.setType(rs.getInt(
"
type
"
));
book.setItemsAvailable(rs.getInt(
"
itemAvailable
"
));
}
catch
(SQLException e) {
e.printStackTrace();
}
return
book;
}
}
////
borrower的ORMaping的实现:
//
BorrowerMaping.java
package
edu.jlu.fuliang.library.dao.mysql;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
edu.jlu.fuliang.library.domain.Borrower;
import
edu.jlu.fuliang.library.jdbc.ORMaping;
public
class
BorrowerMaping
implements
ORMaping{
public
Object mapping(ResultSet rs, Object o) {
Borrower borrower
=
(Borrower)o;
try
{
borrower.setLastName(rs.getString(
1
));
borrower.setFirstName(rs.getString(
2
));
borrower.setAddress(rs.getString(
3
));
borrower.setCity(rs.getString(
4
));
borrower.setZip(rs.getString(
5
));
borrower.setState(rs.getString(
6
));
}
catch
(SQLException e) {
e.printStackTrace();
}
return
borrower;
}
}
//
业务层:
//
Service接口
//
Service.java
package
edu.jlu.fuliang.library.service;
import
java.util.List;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.domain.Borrower;
public
interface
Service {
public
Book queryBookByTitle(String title);
public
List queryBookByAuthor(String author);
public
List queryBookByBorrowerLastName(String firstName);
public
void
updateBook(Book book);
public
void
insertBook(Book book);
public
void
deleteBookByTile(String title);
public
Borrower queryBorrowerByLastName(String lastName);
public
void
updateBorrower(Borrower borrower);
public
void
insertBorrower(Borrower borrower);
public
void
deleteBorrowerByLastName(String lastName);
public
void
borrowBook(String borrowerLastName,String bookTitle);
public
void
returnBook(String borrowerLastName,String bookTitle);
}
//
Service的具体实现
//
LibraryService.java
package
edu.jlu.fuliang.library.service;
import
java.util.List;
import
edu.jlu.fuliang.library.dao.BookDao;
import
edu.jlu.fuliang.library.dao.BorrowOrReturnDao;
import
edu.jlu.fuliang.library.dao.BorrowerDao;
import
edu.jlu.fuliang.library.dao.DaoFactory;
import
edu.jlu.fuliang.library.dao.MysqlDaoFactory;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.domain.Borrower;
public
class
LibraryService
implements
Service{
private
BookDao bookDao;
private
BorrowerDao borrowerDao;
private
BorrowOrReturnDao borrowOrReturnDao;
public
LibraryService(){
DaoFactory daoFactory
=
new
MysqlDaoFactory();
bookDao
=
daoFactory.createBookDao();
borrowerDao
=
daoFactory.createBorrowerDao();
borrowOrReturnDao
=
daoFactory.createBorrowOrReturnDao();
}
public
Book queryBookByTitle(String title) {
return
bookDao.queryByTitle(title);
}
public
List queryBookByAuthor(String author) {
return
bookDao.queryByAuthor(author);
}
public
List queryBookByBorrowerLastName(String firstName) {
return
bookDao.queryByBorrowerLastName(firstName);
}
public
void
updateBook(Book book) {
bookDao.updateBook(book);
}
public
void
insertBook(Book book) {
bookDao.insertBook(book);
}
public
void
deleteBookByTile(String title) {
bookDao.deleteBookByTile(title);
}
public
Borrower queryBorrowerByLastName(String lastName) {
return
borrowerDao.queryByLastName(lastName);
}
public
void
updateBorrower(Borrower borrower) {
borrowerDao.updateBorrower(borrower);
}
public
void
insertBorrower(Borrower borrower) {
borrowerDao.insertBorrower(borrower);
}
public
void
deleteBorrowerByLastName(String lastName) {
borrowerDao.deleteBorrowerByLastName(lastName);
}
public
void
borrowBook(String borrowerLastName, String bookTitle) {
borrowOrReturnDao.borrowBook(borrowerLastName,bookTitle);
}
public
void
returnBook(String borrowerLastName, String bookTitle) {
borrowOrReturnDao.returnBook(borrowerLastName,bookTitle);
}
}
//
view层:
//
主界面:
package
edu.jlu.fuliang.library.view;
import
java.awt.Color;
import
java.awt.Graphics;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.JFrame;
import
javax.swing.JMenu;
import
javax.swing.JMenuBar;
import
javax.swing.JMenuItem;
import
javax.swing.JPanel;
public
class
LibraryMainFrame
extends
JFrame{
private
JPanel myPanel
=
new
MyPanel();
private
JMenu functionMenu,infoMenu,maintenanceMenu;
private
JMenuItem lendMenuItem,returnMenuItem;
private
JMenuItem titleMenuItem,borrowerMenuItem,browseAllMenuItem;
private
JMenuItem insertBookMenuItem,updateBookMenuItem;
private
JMenuItem insertBorrowerMenuItem,updateBorrowerMenuItem;
public
LibraryMainFrame(){
functionMenu
=
new
JMenu(
"
Function
"
);
infoMenu
=
new
JMenu(
"
Inforation
"
);
maintenanceMenu
=
new
JMenu(
"
Maintenance
"
);
lendMenuItem
=
new
JMenuItem(
"
Lend items...
"
);
returnMenuItem
=
new
JMenuItem(
"
Return items...
"
);
functionMenu.add(lendMenuItem);
functionMenu.add(returnMenuItem);
titleMenuItem
=
new
JMenuItem(
"
Search book...
"
);
borrowerMenuItem
=
new
JMenuItem(
"
Search Borrower...
"
);
browseAllMenuItem
=
new
JMenuItem(
"
Browse All...
"
);
infoMenu.add(titleMenuItem);
infoMenu.add(borrowerMenuItem);
infoMenu.add(browseAllMenuItem);
insertBookMenuItem
=
new
JMenuItem(
"
Insert Title...
"
);
updateBookMenuItem
=
new
JMenuItem(
"
Update Item...
"
);
insertBorrowerMenuItem
=
new
JMenuItem(
"
Insert Borrower...
"
);
updateBorrowerMenuItem
=
new
JMenuItem(
"
Update Borrower...
"
);
maintenanceMenu.add(insertBookMenuItem);
maintenanceMenu.add(updateBookMenuItem);
maintenanceMenu.add(insertBorrowerMenuItem);
maintenanceMenu.add(updateBorrowerMenuItem);
JMenuBar menuBar
=
new
JMenuBar();
menuBar.add(functionMenu);
menuBar.add(infoMenu);
menuBar.add(maintenanceMenu);
setJMenuBar(menuBar);
add(myPanel);
setSize(
300
,
300
);
setVisible(
true
);
lendMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
BorrowDialog(LibraryMainFrame.
this
,
"
Lend book
"
,
true
);
}
});
returnMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
ReturnDialog(LibraryMainFrame.
this
,
"
Return book
"
,
true
);
}
});
titleMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
SearchBookDialog(LibraryMainFrame.
this
,
"
Search book
"
,
true
);
}
});
borrowerMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
BorrowerInformationDialog(LibraryMainFrame.
this
,
"
Borrower Information
"
,
true
);
}
});
browseAllMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
}
});
insertBookMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
InsertBookDialog(LibraryMainFrame.
this
,
"
Insert book dialog
"
,
true
);
}
});
updateBookMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
UpdateBookDialog(LibraryMainFrame.
this
,
"
Update book dialog
"
,
true
);
}
});
insertBorrowerMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
InsertBorrowerDialog(LibraryMainFrame.
this
,
"
Insert borrower dialog
"
,
true
);
}
});
updateBorrowerMenuItem.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
new
UpdateBorrowerDialog(LibraryMainFrame.
this
,
"
Update borrower dialog
"
,
true
);
}
});
}
private
class
MyPanel
extends
JPanel{
public
void
paint(Graphics g){
g.setColor(Color.green);
g.drawString(
"
Welcome to Library System!
"
,
40
,
40
);
}
}
public
static
void
main(String []args){
LibraryMainFrame mainFrame
=
new
LibraryMainFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
package
edu.jlu.fuliang.library.view;
import
java.awt.GridLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.JButton;
import
javax.swing.JDialog;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.JTextField;
import
edu.jlu.fuliang.library.controller.InsertBorrowerController;
import
edu.jlu.fuliang.library.domain.Borrower;
public
class
InsertBorrowerDialog
extends
JDialog{
JTextField lastNameField;
JTextField firstNameField;
JTextField addressField;
JTextField cityField;
JTextField zipField;
JTextField stateField;
JButton addButton,exitButton;
public
InsertBorrowerDialog(JFrame f,String s,
boolean
b){
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
7
,
2
));
panel.add(
new
JLabel(
"
Last Name:
"
));
lastNameField
=
new
JTextField(
10
);
panel.add(lastNameField);
firstNameField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
First Name:
"
));
panel.add(firstNameField);
addressField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
Address:
"
));
panel.add(addressField);
cityField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
City:
"
));
panel.add(cityField);
zipField
=
new
JTextField(
5
);
panel.add(
new
JLabel(
"
Zip:
"
));
panel.add(zipField);
stateField
=
new
JTextField(
5
);
panel.add(
new
JLabel(
"
State:
"
));
panel.add(stateField);
addButton
=
new
JButton(
"
Add
"
);
exitButton
=
new
JButton(
"
Exit
"
);
panel.add(addButton);
panel.add(exitButton);
add(panel);
this
.setSize(
200
,
200
);
this
.setVisible(
true
);
addButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
Borrower borrower
=
new
Borrower();
borrower.setLastName(lastNameField.getText());
borrower.setFirstName(firstNameField.getText());
borrower.setAddress(addressField.getText());
borrower.setCity(cityField.getText());
borrower.setZip(zipField.getText());
borrower.setState(stateField.getText());
InsertBorrowerController controller
=
new
InsertBorrowerController();
controller.execute(borrower);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
InsertBorrowerDialog.
this
.setVisible(
false
);
}
});
}
}
//
InsertBookDialog.java
//
package edu.jlu.fuliang.library.view;
import
javax.swing.
*
;
import
edu.jlu.fuliang.library.controller.InsertBookController;
import
edu.jlu.fuliang.library.domain.Book;
import
java.awt.
*
;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
public
class
InsertBookDialog
extends
JDialog{
private
JTextField titleNameField,authorField,iSBNField;
private
JTextField typeField,itemAvailableField;
private
JButton addButton , exitButton;
public
InsertBookDialog(JFrame f,String s,
boolean
b){
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
6
,
2
));
panel.add(
new
JLabel(
"
Tile:
"
));
titleNameField
=
new
JTextField();
panel.add(titleNameField);
panel.add(
new
JLabel(
"
Author:
"
));
authorField
=
new
JTextField();
panel.add(authorField);
panel.add(
new
JLabel(
"
ISBN:
"
));
iSBNField
=
new
JTextField();
panel.add(iSBNField);
panel.add(
new
JLabel(
"
Type:
"
));
typeField
=
new
JTextField();
panel.add(typeField);
panel.add(
new
JLabel(
"
Item available:
"
));
itemAvailableField
=
new
JTextField();
panel.add(itemAvailableField);
addButton
=
new
JButton(
"
Add
"
);
exitButton
=
new
JButton(
"
Exit
"
);
panel.add(addButton);
panel.add(exitButton);
add(panel);
setSize(
200
,
200
);
setVisible(
true
);
addButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
Book book
=
new
Book();
book.setTitleName(titleNameField.getText());
book.setAuthor(authorField.getText());
book.setISBN(iSBNField.getText());
book.setType(Integer.parseInt(typeField.getText()));
book.setItemsAvailable(Integer.parseInt(itemAvailableField.getText()));
InsertBookController controller
=
new
InsertBookController();
controller.execute(book);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent event){
InsertBookDialog.
this
.setVisible(
false
);
}
});
}
}
//
UpdateBookDialog.java
package
edu.jlu.fuliang.library.view;
import
java.awt.GridLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.JButton;
import
javax.swing.JDialog;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.JTextField;
import
edu.jlu.fuliang.library.controller.UpdateBookController;
import
edu.jlu.fuliang.library.domain.Book;
public
class
UpdateBookDialog
extends
JDialog{
private
JTextField titleNameField,authorField,iSBNField;
private
JTextField typeField,itemAvailableField;
private
JButton updateButton , exitButton;
public
UpdateBookDialog(JFrame f,String s,
boolean
b){
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
6
,
2
));
panel.add(
new
JLabel(
"
Tile:
"
));
titleNameField
=
new
JTextField();
panel.add(titleNameField);
panel.add(
new
JLabel(
"
Author:
"
));
authorField
=
new
JTextField();
panel.add(authorField);
panel.add(
new
JLabel(
"
ISBN:
"
));
iSBNField
=
new
JTextField();
panel.add(iSBNField);
panel.add(
new
JLabel(
"
Type:
"
));
typeField
=
new
JTextField();
panel.add(typeField);
panel.add(
new
JLabel(
"
Item available:
"
));
itemAvailableField
=
new
JTextField();
panel.add(itemAvailableField);
updateButton
=
new
JButton(
"
Update:
"
);
exitButton
=
new
JButton(
"
Exit
"
);
panel.add(updateButton);
panel.add(exitButton);
add(panel);
setSize(
200
,
200
);
setVisible(
true
);
updateButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
Book book
=
new
Book();
book.setTitleName(titleNameField.getText());
book.setAuthor(authorField.getText());
book.setISBN(iSBNField.getText());
book.setType(Integer.parseInt(typeField.getText()));
book.setItemsAvailable(Integer.parseInt(itemAvailableField.getText()));
UpdateBookController controller
=
new
UpdateBookController();
controller.execute(book);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent event){
UpdateBookDialog.
this
.setVisible(
false
);
}
});
}
}
//
UpdateBorrowerDialog.java
package
edu.jlu.fuliang.library.view;
import
java.awt.GridLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.JButton;
import
javax.swing.JDialog;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.JTextField;
import
edu.jlu.fuliang.library.controller.InsertBorrowerController;
import
edu.jlu.fuliang.library.controller.UpdateBorrowerController;
import
edu.jlu.fuliang.library.domain.Borrower;
public
class
UpdateBorrowerDialog
extends
JDialog{
private
JTextField lastNameField;
private
JTextField firstNameField;
private
JTextField addressField;
private
JTextField cityField;
private
JTextField zipField;
private
JTextField stateField;
JButton updateButton,exitButton;
public
UpdateBorrowerDialog(JFrame f,String s,
boolean
b){
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
7
,
2
));
panel.add(
new
JLabel(
"
Last Name:
"
));
lastNameField
=
new
JTextField(
10
);
panel.add(lastNameField);
firstNameField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
First Name:
"
));
panel.add(firstNameField);
addressField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
Address:
"
));
panel.add(addressField);
cityField
=
new
JTextField(
10
);
panel.add(
new
JLabel(
"
City:
"
));
panel.add(cityField);
zipField
=
new
JTextField(
5
);
panel.add(
new
JLabel(
"
Zip:
"
));
panel.add(zipField);
stateField
=
new
JTextField(
5
);
panel.add(
new
JLabel(
"
State:
"
));
panel.add(stateField);
updateButton
=
new
JButton(
"
Update
"
);
exitButton
=
new
JButton(
"
Exit
"
);
panel.add(updateButton);
panel.add(exitButton);
add(panel);
this
.setSize(
200
,
200
);
this
.setVisible(
true
);
updateButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
Borrower borrower
=
new
Borrower();
borrower.setLastName(lastNameField.getText());
borrower.setFirstName(firstNameField.getText());
borrower.setAddress(addressField.getText());
borrower.setCity(cityField.getText());
borrower.setZip(zipField.getText());
borrower.setState(stateField.getText());
UpdateBorrowerController controller
=
new
UpdateBorrowerController();
controller.execute(borrower);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
UpdateBorrowerDialog.
this
.setVisible(
false
);
}
});
}
}
//
SearchBookDialog.java
package
edu.jlu.fuliang.library.view;
import
java.awt.GridLayout;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.util.Map;
import
javax.swing.JButton;
import
javax.swing.JDialog;
import
javax.swing.JFrame;
import
javax.swing.JLabel;
import
javax.swing.JPanel;
import
javax.swing.JTextField;
import
edu.jlu.fuliang.library.controller.QueryBookInfoController;
import
edu.jlu.fuliang.library.domain.Book;
public
class
SearchBookDialog
extends
JDialog{
private
JTextField titleNameField,authorField,iSBNField;
private
JTextField typeField,itemAvailableField;
private
JButton searchButton , exitButton;
public
SearchBookDialog(JFrame f,String s,
boolean
b){
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
6
,
2
));
panel.add(
new
JLabel(
"
Tile:
"
));
titleNameField
=
new
JTextField();
panel.add(titleNameField);
panel.add(
new
JLabel(
"
Author:
"
));
authorField
=
new
JTextField();
authorField.setEditable(
false
);
panel.add(authorField);
panel.add(
new
JLabel(
"
ISBN:
"
));
iSBNField
=
new
JTextField();
iSBNField.setEditable(
false
);
panel.add(iSBNField);
panel.add(
new
JLabel(
"
Type:
"
));
typeField
=
new
JTextField();
typeField.setEditable(
false
);
panel.add(typeField);
panel.add(
new
JLabel(
"
Item available:
"
));
itemAvailableField
=
new
JTextField();
itemAvailableField.setEditable(
false
);
panel.add(itemAvailableField);
searchButton
=
new
JButton(
"
Search
"
);
exitButton
=
new
JButton(
"
Exit
"
);
panel.add(searchButton);
panel.add(exitButton);
add(panel);
setSize(
200
,
200
);
setVisible(
true
);
searchButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
String titleName
=
titleNameField.getText();
QueryBookInfoController controller
=
new
QueryBookInfoController();
Map map
=
controller.execute(titleName);
Book book
=
(Book)map.get(
"
book
"
);
authorField.setText(book.getAuthor());
iSBNField.setText(book.getISBN());
typeField.setText(String.valueOf(book.getType()));
itemAvailableField.setText(String.valueOf(book.getItemsAvailable()));
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent event){
SearchBookDialog.
this
.setVisible(
false
);
}
});
}
}
//
BorrowDialog.java
package
edu.jlu.fuliang.library.view;
import
javax.swing.
*
;
import
edu.jlu.fuliang.library.controller.BorrowController;
import
java.awt.
*
;
import
java.awt.event.
*
;
import
java.util.
*
;
import
java.util.List;
public
class
BorrowDialog
extends
JDialog{
private
JTextField borrowerLastNameField,bookTitleField;
private
JButton borrowButton,exitButton;
public
BorrowDialog(JFrame frame,String title,
boolean
b){
borrowerLastNameField
=
new
JTextField();
bookTitleField
=
new
JTextField();
borrowButton
=
new
JButton(
"
Borrow
"
);
exitButton
=
new
JButton(
"
Exit
"
);
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
3
,
2
));
panel.add(
new
JLabel(
"
Borrower Last Name:
"
));
panel.add(borrowerLastNameField);
panel.add(
new
JLabel(
"
Book title:
"
));
panel.add(bookTitleField);
panel.add(borrowButton);
panel.add(exitButton);
add(panel);
setSize(
250
,
100
);
setVisible(
true
);
borrowButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
String borrowerLastName
=
borrowerLastNameField.getText();
String bookTitle
=
bookTitleField.getText();
BorrowController controller
=
new
BorrowController();
List list
=
new
ArrayList(
2
);
list.add(borrowerLastName);
list.add(bookTitle);
controller.execute(list);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
BorrowDialog.
this
.setVisible(
false
);
}
});
}
}
//
ReturnDialog.java
package
edu.jlu.fuliang.library.view;
import
java.awt.GridLayout;
import
java.util.ArrayList;
import
java.util.List;
import
java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
javax.swing.
*
;
import
edu.jlu.fuliang.library.controller.ReturnController;
public
class
ReturnDialog
extends
JDialog{
private
JTextField borrowerLastNameField,bookTitleField;
private
JButton borrowButton,exitButton;
public
ReturnDialog(JFrame frame,String title,
boolean
b){
borrowerLastNameField
=
new
JTextField();
bookTitleField
=
new
JTextField();
borrowButton
=
new
JButton(
"
Borrow
"
);
exitButton
=
new
JButton(
"
Exit
"
);
JPanel panel
=
new
JPanel();
panel.setLayout(
new
GridLayout(
3
,
2
));
panel.add(
new
JLabel(
"
Borrower Last Name:
"
));
panel.add(borrowerLastNameField);
panel.add(
new
JLabel(
"
Book title:
"
));
panel.add(bookTitleField);
panel.add(borrowButton);
panel.add(exitButton);
add(panel);
setSize(
250
,
100
);
setVisible(
true
);
borrowButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
String borrowerLastName
=
borrowerLastNameField.getText();
String bookTitle
=
bookTitleField.getText();
ReturnController controller
=
new
ReturnController();
List list
=
new
ArrayList(
2
);
list.add(borrowerLastName);
list.add(bookTitle);
controller.execute(list);
}
});
exitButton.addActionListener(
new
ActionListener(){
public
void
actionPerformed(ActionEvent e) {
ReturnDialog.
this
.setVisible(
false
);
}
});
}
}
//
Controller:
//
Controller接口
//
Controller.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
public
interface
Controller{
public
Map execute();
}
//
FormController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
public
interface
FormController {
public
Map execute(Object o);
}
//
InsertBookController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
InsertBookController {
private
Service service
=
new
LibraryService();
public
Map execute(Object o) {
Book book
=
(Book)o;
service.insertBook(book);
return
null
;
}
}
//
QueryBookInfoController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.HashMap;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
QueryBookInfoController
implements
FormController{
private
Service service
=
new
LibraryService();
public
Map execute(Object o){
String title
=
(String)o;
Book book
=
service.queryBookByTitle(title);
Map map
=
new
HashMap();
map.put(
"
book
"
,book);
return
map;
}
}
//
UpdateBookController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Book;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
UpdateBookController
implements
FormController{
private
Service service
=
new
LibraryService();
public
Map execute(Object o){
Book book
=
(Book)o;
service.updateBook(book);
return
null
;
}
}
//
InsertBorrowerController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Borrower;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
InsertBorrowerController
implements
FormController{
private
Service service
=
new
LibraryService();
public
Map execute(Object o) {
Borrower borrower
=
(Borrower)o;
service.insertBorrower(borrower);
return
null
;
}
}
//
QueryBorrowerController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.HashMap;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Borrower;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
QueryBorrowerController {
private
Service service
=
new
LibraryService();
public
Map execute(Object o){
String lastName
=
(String)o;
Borrower brrower
=
service.queryBorrowerByLastName(lastName);
Map map
=
new
HashMap();
map.put(
"
brrower
"
,brrower);
return
map;
}
}
//
UpdateBorrowerController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.Map;
import
edu.jlu.fuliang.library.domain.Borrower;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
UpdateBorrowerController
implements
FromController{
private
Service service
=
new
LibraryService();
public
Map execute(Object o){
Borrower borrower
=
(Borrower)o;
service.updateBorrower(borrower);
return
null
;
}
}
//
BorrowController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.
*
;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
BorrowController
implements
FormController{
private
Service service
=
new
LibraryService();
public
Map execute(Object o) {
List list
=
(List)o;
String borrowerFirstName
=
(String) list.get(
0
);
String bookTitle
=
(String)list.get(
1
);
service.borrowBook(borrowerFirstName,bookTitle);
return
null
;
}
}
//
ReturnController.java
package
edu.jlu.fuliang.library.controller;
import
java.util.List;
import
java.util.Map;
import
edu.jlu.fuliang.library.service.LibraryService;
import
edu.jlu.fuliang.library.service.Service;
public
class
ReturnController {
private
Service service
=
new
LibraryService();
public
Map execute(Object o) {
List list
=
(List)o;
String borrowerFirstName
=
(String) list.get(
0
);
String bookTitle
=
(String)list.get(
1
);
service.returnBook(borrowerFirstName,bookTitle);
return
null
;
}
}
//
domain
//
Book.java
package
edu.jlu.fuliang.library.domain;
import
java.util.List;
public
class
Book {
private
String titleName;
private
String author;
private
String ISBN;
private
int
type;
private
int
itemsAvailable;
private
List borrowers;
public
String getAuthor() {
return
author;
}
public
void
setAuthor(String author) {
this
.author
=
author;
}
public
String getISBN() {
return
ISBN;
}
public
void
setISBN(String isbn) {
ISBN
=
isbn;
}
public
int
getItemsAvailable() {
return
itemsAvailable;
}
public
void
setItemsAvailable(
int
itemsAvailable) {
this
.itemsAvailable
=
itemsAvailable;
}
public
String getTitleName() {
return
titleName;
}
public
void
setTitleName(String titleName) {
this
.titleName
=
titleName;
}
public
int
getType() {
return
type;
}
public
void
setType(
int
type) {
this
.type
=
type;
}
public
List getBorrowers() {
return
borrowers;
}
public
void
setBorrowers(List borrowers) {
this
.borrowers
=
borrowers;
}
}
//
Borrower.java
package
edu.jlu.fuliang.library.domain;
import
java.util.List;
public
class
Borrower {
private
String lastName;
private
String firstName;
private
String address;
private
String city;
private
String zip;
private
String state;
private
List books;
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address
=
address;
}
public
String getCity() {
return
city;
}
public
void
setCity(String city) {
this
.city
=
city;
}
public
String getFirstName() {
return
firstName;
}
public
void
setFirstName(String firstName) {
this
.firstName
=
firstName;
}
public
String getLastName() {
return
lastName;
}
public
void
setLastName(String lastName) {
this
.lastName
=
lastName;
}
public
String getState() {
return
state;
}
public
void
setState(String state) {
this
.state
=
state;
}
public
String getZip() {
return
zip;
}
public
void
setZip(String zip) {
this
.zip
=
zip;
}
public
List getBooks() {
return
books;
}
public
void
setBooks(List books) {
this
.books
=
books;
}
}