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

📄 returnbookaction.java

📁 一个简单的图书馆的管理系统,该系统主要是针对学校的图书馆而做的
💻 JAVA
字号:
package librarymanagement.action.dialogAction;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import librarymanagement.common.SetCommitStyle;
import librarymanagement.dao.ReturnBookDao;
import librarymanagement.dao.common.DbException;
import librarymanagement.dao.dialogInterface.ReturnBookInterface;
import librarymanagement.view.dialog.ReturnBookDialog;
import librarymanagement.vo.ReaderBackVO;

public class ReturnBookAction implements ActionListener {

	private ReturnBookDialog returnBook;
	private Vector v;
	private JTable table = null;
	private ReturnBookInterface dao = null;

	public ReturnBookAction(ReturnBookDialog returnBook) {
		this.returnBook = returnBook;
	}

	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("搜索")) {
			actionPerformedNew();
		}
		if (name.equals("归还书籍")) {
			table = returnBook.getTable();
			int row = table.getSelectedRow();
			if (row < 0) {
				JOptionPane.showMessageDialog(null, "请选择读者所借书籍信息");
			} else {
				long backReaderID =Long.parseLong(table.getValueAt(row, 0).toString());
				long backBookID = Long.parseLong(table.getValueAt(row, 2).toString());
				int ch = JOptionPane.showConfirmDialog(null, "你确定要删除该条记录吗?",
						"删除提示", JOptionPane.YES_NO_OPTION);
				if (ch == JOptionPane.YES_OPTION) {
					dao = new ReturnBookDao();
					if (dao.deleteVector(backReaderID, backBookID)) {
						JOptionPane.showMessageDialog(null, "数据删除成功");
						DefaultTableModel model = (DefaultTableModel) table
								.getModel();
						model.removeRow(row);
					}
				}
			}
		}
		if (name.equals("提交数据")) {
			SetCommitStyle.setCommit_RollBack(name);
		}
		if (name.equals(" 取    消 ")) {
			if (returnBook.getTextStudentID().getText().trim().equals("")
					&&returnBook.getTextBookID().getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "请先搜索出信息");
				return;
			} else {
				actionPerformedNew();
				SetCommitStyle.setCommit_RollBack(name);
			}
		}
	}

	public void actionPerformedNew() {
		try {
			if (returnBook.getTextStudentID().isEditable()) {
				if (!(returnBook.getTextStudentID().getText().trim().equals(""))) {
					if (returnBook.getTextStudentID().getText().trim().matches(
							"\\d{0,10}")) {
						v = commonActionByReader(dao);

					} else {
						JOptionPane.showMessageDialog(null, "借书证号只能0~10位的数字");
						returnBook.getTextStudentID().setText("");
						return;
					}
				} else {
					JOptionPane.showMessageDialog(null, "借书证号不能为空");
					return;
				}
			}
			if (returnBook.getTextBookID().isEditable()) {
				if (!(returnBook.getTextBookID().getText().trim().equals(""))) {
					if (returnBook.getTextBookID().getText().trim().matches(
							"\\d*")) {
						v = commonActionByBook(dao);
					} else {
						JOptionPane.showMessageDialog(null, "图书编号只能为整数");
						returnBook.getTextBookID().setText("");
						return;
					}
				} else {
					JOptionPane.showMessageDialog(null, "图书编号不能为空");
					return;
				}
			}
			table = returnBook.createJTable();
			initiaJTableValues(table, v);
			table.addMouseListener(new MouseAdapter(){
			      public void mouseClicked(MouseEvent e) {
			        int ch = e.getClickCount();
			        if(ch == 1){
			          int columns = table.getColumnCount();
			          int row = table.getSelectedRow();
			          String[] data = new String[columns];
			          for(int i = 0; i < columns; i++){
			            data[i] = table.getValueAt(row, i).toString();
			          }
			          initTableUpdate(data);
			        }
			      }
			    });
		} catch (DbException ex) {
			System.out.println(ex.getMessage());
		}
	}

	public Vector commonActionByReader(ReturnBookInterface dao)
			throws DbException {
		dao = new ReturnBookDao();
		ReaderBackVO vo = returnBook.getVo();
		vo.setReader_id(Long.parseLong(returnBook.getTextStudentID().getText()
				.trim()));
		v = dao.selectByReader_ID(vo.getReader_id());
		return v;
	}

	public Vector commonActionByBook(ReturnBookInterface dao)
			throws DbException {
		dao = new ReturnBookDao();
		ReaderBackVO vo = returnBook.getVo();
		vo.setBook_id(Long.parseLong(returnBook.getTextBookID().getText()
				.trim()));
		v = dao.selectByBook_ID(vo.getBook_id());
		return v;
	}

	private void initiaJTableValues(JTable table, Vector v) {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		int rows = model.getRowCount();
		for (int i = rows - 1; i >= 0; i--) {
			model.removeRow(i);
		}
		Iterator iter = v.iterator();
		while (iter.hasNext()) {
			ReaderBackVO value = (ReaderBackVO) iter.next();
			changeText(value);
			Object[] data = { new Long(value.getReader_id()),
					value.getReader_name(), new Long(value.getBook_id()),
					value.getBook_name(), value.getBorrow_date(),
					value.getShould_return_date() };
			model.addRow(data);

		}
	}

	private void changeText(ReaderBackVO value) {
		returnBook.getTextStudentName().setText(value.getReader_name());
		returnBook.getTextBookLabelID().setText(
				String.valueOf(value.getReader_id()));
		returnBook.getTextBookName().setText(value.getBook_name());
		returnBook.getTextClass().setText(String.valueOf(value.getBook_id()));
		returnBook.getTextBookTime().setText(value.getBorrow_date());
		returnBook.getTextData().setText(value.getShould_return_date());
		

	}
  private void initTableUpdate(String[] name){
//	    returnBook.getTextStudentName().setText(name[1]);
//		returnBook.getTextBookLabelID().setText(name[0]);
		returnBook.getTextClass().setText(name[2]);
		returnBook.getTextBookTime().setText(name[4]);
		returnBook.getTextData().setText(name[5]);
		returnBook.getTextBookName().setText(name[3]);
	  
	  
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -