⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 borrowbooks.java

📁 图书管理系统v1.0是使用JAVA语言开发的解决图书基本管理的一个应用程序。 该系统能够实现简单的图书和用户管理
💻 JAVA
字号:
package lib;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Date;

public class BorrowBooks extends JInternalFrame {
	//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[4];
	//for creating an array of String
	private String[] informationString = {" 输入图书ID号码: ", " 输入用户ID号码: ",
	                                      " 借出日期: ", " 归还日期: "};
	//for creating an array of JTextField
	private JTextField[] informationTextField = new JTextField[4];
	//for creating the date in the String
	private String date = new SimpleDateFormat("yyyy-MM-dd", 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("借出",new ImageIcon(ClassLoader.getSystemResource("images/export.gif")));

	//for creating South 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 static int MAXNUMBEROFBORROW = 3;
	private Date dayOfReturn;
	//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();
				if(i == 3) {
				    try{
				    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd",Locale.getDefault());
				    dayOfReturn = sdf.parse(data[i]);
				    }
				    catch(java.text.ParseException ex){}
				}
			}
			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("借出图书", 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("Default", 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("Default", Font.BOLD, 13));
			if (i == 2) {
				informationPanel.add(informationTextField[i] = new JTextField(date));
				informationTextField[i].setFont(new Font("Default", Font.PLAIN, 13));
				informationTextField[i].setEnabled(false);
			}
			else {
				informationPanel.add(informationTextField[i] = new JTextField());
				informationTextField[i].setFont(new Font("Default", Font.PLAIN, 13));
			}
		}
		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("Default", Font.BOLD, 13));
		//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("借出图书:"));
		//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("Default", Font.BOLD, 13));
		//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()) {
				    Date today = new Date();
				    if(dayOfReturn.before(today)){
				        JOptionPane.showMessageDialog(null, "日期输入错误!", "警告", JOptionPane.WARNING_MESSAGE);
						informationTextField[3].setText(null);
				    }
				    else {
					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]);
							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(member.getMoney() > 0) 
								JOptionPane.showMessageDialog(null, "该用户有未缴的欠款!", "警告", JOptionPane.WARNING_MESSAGE);
							else
							if(borrow.getBookID() == Integer.parseInt(data[0])&&borrow.getMemberID ()==Integer.parseInt(data[1]))
								JOptionPane.showMessageDialog(null, "该用户已借此书!", "警告", JOptionPane.WARNING_MESSAGE);
							else 
								if(numberOfBooks > MAXNUMBEROFBORROW ) 
									JOptionPane.showMessageDialog(null, "该用户借书已超过"+MAXNUMBEROFBORROW+"本!", "警告", JOptionPane.WARNING_MESSAGE);
								else
									if (numberOfAvailbleBooks > 0) {
									    boolean avail;
									    if(numberOfAvailbleBooks>1) avail = true;
									    else avail = false; 
										numberOfAvailbleBooks -= 1;
										book.update("UPDATE Books SET NumberOfAvailbleBooks =" + numberOfAvailbleBooks +
										        ",NumberOfBorrowedBooks =" + numberOfBorrowedBooks + ",Availble = "+avail+" 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
										Log log =new Log("\n"+"Borrow:"+data[0] + "," + data[1] + "," + data[2] + "," + data[3]);
										clearTextField();
										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 + -