📄 returnbooks.java
字号:
package lib;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Calendar;
import java.sql.*;
public class ReturnBooks 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("图书信息");
//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 = {" 填写图书ID号码:", " 填写用户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;
//for creating an Internal Panel in the center panel
private JPanel returnButtonPanel = new JPanel();
//for creating the buton
private JButton returnButton = new JButton("归还",new ImageIcon(ClassLoader.getSystemResource("images/import.gif")));
//for creating the panel
private JPanel southPanel = new JPanel();
//for creating the button
private JButton cancelButton = new JButton("取消",new ImageIcon(ClassLoader.getSystemResource("images/exit.gif")));
//for creating an object
private Books book;
private Members member;
private Borrow borrow;
private String URL = "jdbc:odbc:JLibrary";
private static float payPerDay = 0.1f;
//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);
}
public void isOverTime(Date dayOfReturn)
{
Date today = new Date();
System.out.println("today:"+today+"dayofreturn:"+dayOfReturn);
if (today.after(dayOfReturn))
JOptionPane.showMessageDialog(null, "图书超时!", "警告", JOptionPane.WARNING_MESSAGE);
}
//constructor of returnBooks
public ReturnBooks() {
//for setting the title for the internal frame
super("归还图书", 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("Default", 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(2, 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("Default", Font.BOLD, 13));
informationPanel.add(informationTextField[i] = new JTextField());
informationTextField[i].setFont(new Font("Default", Font.PLAIN, 13));
}
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("Default", Font.BOLD, 13));
//for adding the internal panel to the panel
centerPanel.add("South", returnButtonPanel);
//for setting the border
centerPanel.setBorder(BorderFactory.createTitledBorder("归还图书:"));
//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("Default", Font.BOLD, 13));
//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(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();
borrow.connection("SELECT * FROM Borrow WHERE BookID = " +data[0] + " AND MemberID = "+data[1]);
Date dayOfReturn = borrow.getDayOfReturn();
Date today = new Date();
if(borrow.getBookID() == Integer.parseInt(data[0])&&borrow.getMemberID ()==Integer.parseInt(data[1]))
{
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();
if (today.after(dayOfReturn)){
int total = 0;
int year;
Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
cal.setTime(today);
cal1.setTime(dayOfReturn);
while(cal.get(Calendar.YEAR) > cal1.get(Calendar.YEAR)+1){
year = cal1.get(Calendar.YEAR);
if((year%4==0&&year%100!=0)||(year%400==0)) total+=366;
else total+=365;
cal1.roll(Calendar.YEAR,1);
}
year = cal1.get(Calendar.YEAR);
if(year != cal.get(Calendar.YEAR))
if((year%4==0&&year%100!=0)||(year%400==0))
total += 366 - cal1.get(Calendar.DAY_OF_YEAR)+ cal.get(Calendar.DAY_OF_YEAR);
else
total += 365 -cal1.get(Calendar.DAY_OF_YEAR) + cal.get(Calendar.DAY_OF_YEAR);
else
total += -cal1.get(Calendar.DAY_OF_YEAR) + cal.get(Calendar.DAY_OF_YEAR);
float totalPay = member.getMoney()+ payPerDay*total;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException cnfe) {
System.out.println("ReturnBooks.java\n" + cnfe.toString());
}
catch (Exception e) {
System.out.println("ReturnBooks.java\n" + e.toString());
}
try {
Connection connection = DriverManager.getConnection(URL);
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = statement.executeQuery("SELECT * FROM Members WHERE MemberID = " +data[1]);
rs.absolute(1);
rs.updateFloat("Money",totalPay);
rs.updateRow();
rs.close();
statement.close();
connection.close();
}
catch (SQLException SQLe) {
System.out.println("ReturnBooks.java\n" + SQLe.toString());
}
JOptionPane.showMessageDialog(null, "图书超时", "警告", JOptionPane.WARNING_MESSAGE);
Log log =new Log("\n"+"Return:"+data[0] + "," + data[1]);
}
else{
Log log =new Log("\n"+"Return:"+data[0] + "," + data[1]);
JOptionPane.showMessageDialog(null, "数据操作成功!", "信息", JOptionPane.YES_OPTION);
}
}
}
else
JOptionPane.showMessageDialog(null, "图书没有被借出", "警告", JOptionPane.WARNING_MESSAGE);
}
};
runner.start();
}
//if there is a missing data, then display Message Dialog
else
JOptionPane.showMessageDialog(null, "请将信息填写完整", "警告", 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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -