// Borrow.java
package MainPro;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import PublicModule.*;
public class Borrow extends Frame {
private static final long serialVersionUID = -1036076990599464079L;
String SepLine = "-------------------------------------------------";
Label lbbookid = new Label("图书编号");
Label lbreaderid = new Label("读者编号");
TextField tf_bookid = new TextField();
TextField tf_readerid = new TextField();
Button queryBtn = new Button("查询");
Label lbbookinfo = new Label(SepLine + "图书信息" + SepLine);
Label lbbookname = new Label("图书名称:");
Label tf_bookname = new Label("xx");
Label lbauthor = new Label("作者:");
Label tf_author = new Label("xx");
Label lbpublisher = new Label("出版社:");
Label tf_publisher = new Label("xx");
Label lbpublish_time = new Label("出版时间:");
Label tf_publish_time = new Label("xx");
Label lbprice = new Label("定价:");
Label tf_price = new Label("xx");
Label lbstock = new Label("库存数量:");
Label tf_stock = new Label("xx");
Label lbreaderinfo = new Label(SepLine + "读者信息" + SepLine);
Label lbreadername = new Label("读者姓名:");
Label tf_readername = new Label("xx");
Label lbreadertype = new Label("读者类型:");
Label tf_readertype = new Label("xx");
Label lbmax_num = new Label("最大可借数:");
Label tf_max_num = new Label("xx");
Label lbdays_num = new Label("最大可借天数:");
Label tf_days_num = new Label("xx");
Label lbborrowinfo = new Label(SepLine + "借阅信息" + SepLine);
Label lbborrowednum = new Label("该读者已借图书数量:");
Label tf_borrowednum = new Label("xx");
Label lbif_borrow = new Label("该读者是否可借所选图书:");
Label tf_if_borrow = new Label("xx");
Label lbborrow_date = new Label("借阅日期:");
Label tf_borrow_date = new Label("xx");
Button borrowBtn = new Button("借出");
Button closeBtn = new Button("关闭");
public Borrow() {
setLayout(null);
setTitle("借阅图书");
setSize(500, 420);
this.setForeground(Color.BLACK); // 设置前景色为黑色
lbbookid.setBounds(30, 40, 50, 25); // 图书编号
tf_bookid.setBounds(90, 40, 90, 20);
lbreaderid.setBounds(200, 40, 50, 25); // 读者编号
tf_readerid.setBounds(260, 40, 90, 20);
queryBtn.setBounds(370, 40, 80, 25); // 查询按钮
lbbookinfo.setBounds(30, 70, 440, 25); // 图书信息提示条
lbbookname.setBounds(30, 100, 60, 25); // 图书名称
tf_bookname.setBounds(90, 100, 200, 25);
lbauthor.setBounds(310, 100, 60, 25); // 作者
tf_author.setBounds(370, 100, 90, 25);
lbpublisher.setBounds(30, 125, 60, 25); // 出版社
tf_publisher.setBounds(90, 125, 200, 25);
lbpublish_time.setBounds(310, 125, 60, 25); // 出版时间
tf_publish_time.setBounds(370, 125, 90, 25);
lbprice.setBounds(30, 150, 60, 25); // 定价
tf_price.setBounds(90, 150, 200, 25);
lbstock.setBounds(310, 150, 60, 25); // 库存数量
tf_stock.setBounds(370, 150, 90, 25);
lbreaderinfo.setBounds(30, 180, 440, 25); // 读者信息提示条
lbreadername.setBounds(30, 205, 60, 25); // 读者姓名
tf_readername.setBounds(90, 205, 90, 25);
lbreadertype.setBounds(310, 205, 60, 25); // 读者类型
tf_readertype.setBounds(370, 205, 90, 25);
lbmax_num.setBounds(30, 230, 75, 25); // 最大可借数
tf_max_num.setBounds(105, 230, 90, 25);
lbdays_num.setBounds(310, 230, 85, 25); // 最大可借天数
tf_days_num.setBounds(395, 230, 70, 25);
lbborrowinfo.setBounds(30, 260, 440, 25); // 借阅信息提示条
lbborrowednum.setBounds(30, 285, 120, 25);// 已借图书数量
tf_borrowednum.setBounds(150, 285, 50, 25);
lbif_borrow.setBounds(30, 310, 145, 25); // 是否可借
tf_if_borrow.setBounds(175, 310, 50, 25);
lbborrow_date.setBounds(30, 335, 60, 25);// 借书日期
tf_borrow_date.setBounds(90, 335, 100, 25);
borrowBtn.setBounds(160, 365, 80, 25);// 借出按钮
borrowBtn.setEnabled(false); // 开始时禁用借出按钮
closeBtn.setBounds(260, 365, 80, 25);// 关闭按钮
queryBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_querywActionPerformed(e);
}
});
borrowBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btn_borrowActionPerformed(e);
}
});
closeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setForeground(Color.BLACK); // 设置前景色为黑色
dispose(); // 关闭窗体
}
});
/* 关闭窗口 */
this.addWindowListener(new WindowAdapter() {
// 重写windowClosing()方法
public void windowClosing(WindowEvent e) {
setForeground(Color.BLACK); // 设置前景色为黑色
dispose(); // 关闭窗体
}
});
add(lbbookid);
add(lbreaderid);
add(queryBtn);
add(lbbookinfo);
add(lbbookname);
add(lbauthor);
add(lbpublisher);
add(lbpublish_time);
add(lbprice);
add(lbstock);
add(lbreaderinfo);
add(lbreadername);
add(lbreadertype);
add(lbmax_num);
add(lbdays_num);
add(lbborrowinfo);
add(lbborrowednum);
add(lbif_borrow);
add(lbborrow_date);
add(borrowBtn);
add(closeBtn);
setLocationRelativeTo(null); // 使窗体在屏幕上居中放置
setVisible(true); // 使窗体可见
setForeground(Color.RED); // 设置前景色为红色
add(tf_bookid);
add(tf_readerid);
add(tf_bookname);
add(tf_author);
add(tf_publisher);
add(tf_publish_time);
add(tf_price);
add(tf_stock);
add(tf_readername);
add(tf_readertype);
add(tf_max_num);
add(tf_days_num);
add(tf_borrowednum);
add(tf_if_borrow);
add(tf_borrow_date);
}
// 图书和读者查询
private void btn_querywActionPerformed(ActionEvent e) {
String bookid = tf_bookid.getText();
String readerid = tf_readerid.getText();
// 如果图书编号或读者编号两者均为空,或者有一个为空,则返回
if (bookid.equals("") || readerid.equals("")) {
JOptionPane.showMessageDialog(null, "图书编号和读者编号均不能为空!");
init(); // 重新初始化各参数并禁止借出按钮
return;
}
// 按编号查询图书,结果存入book对象中
Book book = BookSelect.SelectBookById(bookid);
// 如果查询到结果,将其显示在各文本框中
if (book != null) {
tf_bookname.setText(book.getBookname());
tf_author.setText(book.getAuthor());
tf_publisher.setText(book.getPublisher());
tf_publish_time.setText(book.getPublish_time().toString());
tf_price.setText(String.valueOf((book.getPrice())));
tf_stock.setText(String.valueOf(book.getStock()));
} else {
JOptionPane.showMessageDialog(null, "图书编号有误,查无此书!");
init(); // 重新初始化各参数并禁止借出按钮
return;
}
if (book.getStock() == 0) {
JOptionPane.showMessageDialog(null, "图书已无库存,无法借阅!");
init(); // 重新初始化各参数并禁止借出按钮
return;
}
// 按编号查询读者,结果存入reader对象中
Reader reader = ReaderSelect.selectReaderById(readerid);
// 如果查询到结果,将其显示在各文本框中
if (reader != null) {
tf_readername.setText(reader.getReadername());
tf_readertype.setText(reader.getReadertype());
tf_max_num.setText(String.valueOf(reader.getMax_num()));
tf_days_num.setText(String.valueOf(reader.getDays_num()));
} else {
JOptionPane.showMessageDialog(null, "读者编号有误,查无此人!");
init(); // 重新初始化各参数并禁止借出按钮
return;
}
// 查询指定读者是否已借过指定图书且未归还
if (IfBorrowBack.findbook(bookid, readerid)) {
JOptionPane.showMessageDialog(null, "该读者已借阅所选图书,且未归还!");
init(); // 重新初始化各参数并禁止借出按钮
return;
}
// 统计读者所借图书数量
int borrowednum = statborrowednum(readerid);
tf_borrowednum.setText(String.valueOf(borrowednum));
// 如果读者已借图书尚未超出其允许最大借书量,则允许其继续借阅所选图书
if (borrowednum < reader.getMax_num()) {
tf_if_borrow.setText("是");
// 创建一个简单日期格式对象,注意:MM一定要用大写