图书馆管理系统项目

项目用的是三层结构:分别是界面层,业务逻辑层,持久层。

我主要负责的是Book业务实体的三层代码编写。

1.持久层用了一个通用接口BaseDao以装各个业务实体在持久层的共同方法。

package com.lovo.dao;

import java.util.List;

import com.lovo.bean.PageBean;


public interface BaseDao<E,K> {

	/**
	 * 基础 方法1:按照对象删除
	 * 持久层
	 * 输入一个对象 删除该对象
	 * @param entity
	 */
	public boolean delete(E entity);
	
	/**
	 * 基础 方法2:按照ID删除
	 * 持久层
	 * 输入对象ID 删除该ID的对象
	 * @param id
	 */
	public boolean deleteByKey(K id);

	/**
	 * 基础 方法3:查找全部
	 * 持久层
	 * 查找全部 对象类型的数据
	 * @return 装有该类对象的容器
	 */
	public List<E> findAll();

	/**
	 * 基础 方法4:按照ID查找
	 * 持久层
	 * 按照ID 查找对象
	 * @param id
	 */
	public E findByKey(K id);

	/**
	 * 基础 方法5:给一个对象 保存(新建)该对象
	 * 持久层
	 * 输入一个对象 保存该对象
	 * @param entity
	 */
	public boolean save(E entity);

	/**
	 * 基础 方法6:给一个对象 修改该对象
	 * 持久层
	 * 输入一个对象 将数据库内 该对象的属性 修改为 输入的对象的属性
	 * @param entity
	 */
	public boolean update(E entity);
	
	/**
	 * 基础 方法7:分页查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照每页显示数量size 和 第几页page 查找书籍
	 */
	public PageBean<E> findByPage(int page, int size, Object... pramas);
	
	/**
	 * 基础 方法8:多条件查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照给定条件查找
	 */
	public List<E> findByWays(Object...params);



}
2.相应的,在dao层实现包里有一个抽象类做BaseDao的适配器

package com.lovo.dao.impl;
import java.util.List;

import com.lovo.bean.PageBean;
import com.lovo.dao.BaseDao;


public  class BaseDaoAdapter<E, K> implements BaseDao<E,K> {

	/**
	 * 构造器
	 */
	public BaseDaoAdapter(){
	}

	/**
	 * 基础 方法1:按照对象删除
	 * 持久层 适配器
	 * 输入一个对象 删除该对象
	 * @param entity
	 */
	public boolean delete(E entity){
		return false;
	}

	/**
	 * 基础 方法2:按照ID删除
	 * 持久层 适配器
	 * 输入对象ID 删除该ID的对象
	 * @param id
	 */
	public boolean deleteByKey(K id){
		return false;
	}

	/**
	 * 基础 方法3:查找全部
	 * 持久层 适配器
	 * 查找全部 对象类型的数据
	 * @return 装有该类对象的容器
	 */
	public List<E> findAll(){
		return null;
	}

	/**
	 * 基础 方法4:按照ID查找
	 * 持久层
	 * 按照ID 查找对象
	 * @param id
	 */
	public E findByKey(K id){
		return null;
	}

	/**
	 * 基础 方法5:给一个对象 保存(新建)该对象
	 * 持久层
	 * 输入一个对象 保存该对象
	 * @param entity
	 */
	public boolean save(E entity){
		return false;
	}

	/**
	 * 基础 方法6:给一个对象 修改该对象
	 * 持久层
	 * 输入一个对象 将数据库内 该对象的属性 修改为 输入的对象的属性
	 * @param entity
	 */
	public boolean update(E entity){
		return false;
	}
	
	/**
	 * 基础 方法7:分页查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照每页显示数量size 和 第几页page 查找书籍
	 */
	@Override
	public PageBean<E> findByPage(int page, int size,Object... pramas) {
		return null;
	}
	/**
	 * 基础 方法8:多条件查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照给定条件查找
	 */
	public List<E> findByWays(Object...params){
		return null;
	}






}

3.book业务实体的dao层实现代码

package com.lovo.dao.impl;
import java.util.List;

import com.lovo.bean.PageBean;
import com.lovo.dao.BaseDao;


public  class BaseDaoAdapter<E, K> implements BaseDao<E,K> {

	/**
	 * 构造器
	 */
	public BaseDaoAdapter(){
	}

	/**
	 * 基础 方法1:按照对象删除
	 * 持久层 适配器
	 * 输入一个对象 删除该对象
	 * @param entity
	 */
	public boolean delete(E entity){
		return false;
	}

	/**
	 * 基础 方法2:按照ID删除
	 * 持久层 适配器
	 * 输入对象ID 删除该ID的对象
	 * @param id
	 */
	public boolean deleteByKey(K id){
		return false;
	}

	/**
	 * 基础 方法3:查找全部
	 * 持久层 适配器
	 * 查找全部 对象类型的数据
	 * @return 装有该类对象的容器
	 */
	public List<E> findAll(){
		return null;
	}

	/**
	 * 基础 方法4:按照ID查找
	 * 持久层
	 * 按照ID 查找对象
	 * @param id
	 */
	public E findByKey(K id){
		return null;
	}

	/**
	 * 基础 方法5:给一个对象 保存(新建)该对象
	 * 持久层
	 * 输入一个对象 保存该对象
	 * @param entity
	 */
	public boolean save(E entity){
		return false;
	}

	/**
	 * 基础 方法6:给一个对象 修改该对象
	 * 持久层
	 * 输入一个对象 将数据库内 该对象的属性 修改为 输入的对象的属性
	 * @param entity
	 */
	public boolean update(E entity){
		return false;
	}
	
	/**
	 * 基础 方法7:分页查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照每页显示数量size 和 第几页page 查找书籍
	 */
	@Override
	public PageBean<E> findByPage(int page, int size,Object... pramas) {
		return null;
	}
	/**
	 * 基础 方法8:多条件查找
	 * !!!!!!!新加的!!!!!!!
	 * 持久层
	 * 按照给定条件查找
	 */
	public List<E> findByWays(Object...params){
		return null;
	}






}

4.book业务实体业务逻辑层的代码,大部分是过渡代码。

package com.lovo.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lovo.bean.PageBean;
import com.lovo.biz.BooksService;
import com.lovo.dao.BooksDao;
import com.lovo.dao.ReaderDao;
import com.lovo.dao.impl.BooksDaoDbImpl;
import com.lovo.dao.impl.ReaderDaoImpl;
import com.lovo.entity.BookType;
import com.lovo.entity.Books;
import com.lovo.entity.BorrowList;
import com.lovo.entity.Reader;
import com.lovo.util.DbSessionFactory;


public class BooksServiceImpl extends BaseServiceAdaper<Books, Integer>
		implements BooksService<Books, Integer> {

	private BooksDao<Books, Integer> booksDao = new BooksDaoDbImpl();
	private ReaderDao<Reader, Integer> readerDao = new ReaderDaoImpl();

	/**
	 * 默认无参构造器
	 */
	public BooksServiceImpl() {
	}

	/**
	 * 重写 Books专有 方法1: 借书
	 * @c 逻辑业务层 输入读者和书的ID 生成 借书单的对象
	 * @param readerID
	 * @param bookID
	 * @return 成功返回true
	 */
	@Override
	public BorrowList borrow(int readerID, int bookID) {
		BorrowList blist = new BorrowList();
		try {
			DbSessionFactory.openSession();
			Books book = booksDao.findByKey(bookID);
			book.borrowOut();
			booksDao.updateNumber(book);
			Reader reader = readerDao.findByKey(readerID);
			reader.borrowOut();
			readerDao.updateCurrentNumber(reader);
			blist.setBorrowID((int) (System.currentTimeMillis()-100000));
			blist.setBook(book);
			blist.setReader(reader);
			blist.setOperatorID(6001);
			blist.setBorrowDate(System.currentTimeMillis());
			blist.setIsBack("未");
			booksDao.saveBorrowList(blist);
		} finally {
			DbSessionFactory.closeSession();
		}
		return blist;

	}

	/**
	 * 重写 Books专有 方法2:还书
	 * @c 逻辑业务层
	 * @c 输入读者和书的ID 查找并完成借书单据
	 * @param readerID
	 * @param bookID
	 */
	@Override
	public BorrowList bookBack(int readerID, int bookID) {
		BorrowList blist = new BorrowList();
		try {
			DbSessionFactory.openSession();	
			blist=booksDao.findByTwoKey(readerID,bookID);
			blist.setBackDate(System.currentTimeMillis());
			blist.setBackManagerID(6002);
			blist.setIsBack("已");
			booksDao.updateBorrowList(blist);
			Books book = booksDao.findByKey(bookID);
			book.bookBack();
			booksDao.updateNumber(book);
			Reader reader = readerDao.findByKey(readerID);
			reader.bookBack();
			readerDao.updateCurrentNumber(reader);
		} finally {
			DbSessionFactory.closeSession();
		}
		return blist;
	}

	/**
	 * 重写 Books专业 方法3:查找书籍的详细信息
	 * @c 逻辑业务层
	 * @c 输入bookID 返回书籍详细信息的对象
	 */
	@Override
	public Books findAllInfo(int bookID) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 重写 Books专有 方法4:按照多属性查询书籍
	 * @c 逻辑业务层
	 * @param 代替多个对象
	 * @param params
	 */
	@Override
	public List<Books> findByWays(Object... params) {
		List<Books> list = null;
		try {
			DbSessionFactory.openSession();
			list = booksDao.findByWays(params);
		} finally {
			DbSessionFactory.closeSession();
		}
		return list;
	}

	
	/**
	 * 重写 Books专有方法5:查询所有基本书本类型
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List<List> findAllBookType() {
		 try {
				DbSessionFactory.openSession();
				return booksDao.findAllBookType();
			} finally {
				DbSessionFactory.closeSession();
			}
	}
	
	/**
	 * 重写 Books专有方法6:查询所有次级书本类型
	 * @return
	 */
	@Override
	public List<BookType> findAllBaseType() {
		 try {
				DbSessionFactory.openSession();
				return booksDao.findAllBaseType();
			} finally {
				DbSessionFactory.closeSession();
			}
	}
	
	/**
	 * 重写 基础 方法7:分页查找
	 * @c!!!!!!!新加的!!!!!!!
	 * @c逻辑业务层 按照每页显示数量size 和 第几页page 查找书籍
	 */
	@Override
	public PageBean<Books> findByPage(int page,int size, Object... pramas) {
		DbSessionFactory.openSession();
		// 查询所有书本
		PageBean<Books> bean = booksDao.findByPage(page,size,pramas);
		DbSessionFactory.closeSession();
		return bean;
	}
	
	/**
	 * 重写 基础 方法2:按照ID删除
	 * 业务逻辑层 总接口
	 * 输入对象ID 删除该ID的对象
	 * @param id
	 */
	@Override
	public boolean deleteByKey(Integer id) {
		try {
			DbSessionFactory.openSession();
			return booksDao.deleteByKey(id);
		} finally {
			DbSessionFactory.closeSession();
		}
	}
	
	/**
	 * 重写 基础 方法5:给一个对象 保存(新建)该对象
	 * 业务逻辑层 总接口
	 * 输入一个对象 保存该对象
	 * @param entity
	 */
	@Override
	public boolean save(Books entity) {
		 try {
				DbSessionFactory.openSession();
				return booksDao.save(entity);
			} finally {
				DbSessionFactory.closeSession();
			}
	}

	/**
	 * 重写 基础 方法6:给一个对象 修改该对象
	 * 业务逻辑层 总接口
	 * 输入一个对象 将数据库内 该对象的属性 修改为 输入的对象的属性
	 * @param entity
	 */
	@Override
	public boolean update(Books entity){
		try {
			DbSessionFactory.openSession();
			return booksDao.update(entity);
		} finally {
			DbSessionFactory.closeSession();
		}
	}
	@Override
	public PageBean<BorrowList> findBorrowList(int page, int size, int readerid) {
		PageBean<BorrowList> pageBean=null;
		try {
			DbSessionFactory.openSession();
			pageBean= booksDao.findBorrowList(page,size,readerid);
			Map<Integer, Books> booksMap = new HashMap<Integer, Books>();
			List<Books> booksList = booksDao.findAll();
			for (Books temp : booksList) {
				booksMap.put(temp.getBookID(), temp);
			}
			List<BorrowList> borrowList = pageBean.getList();
			for (BorrowList temp : borrowList) {
				temp.setBook(booksMap.get(temp.getBook().getBookID()));
			}
			
			Map<Integer, Reader> map = new HashMap<Integer, Reader>();
			List<Reader> readerList = readerDao.findAll();
			for (Reader temp : readerList) {
				map.put(temp.getReaderID(), temp);
			}
			List<BorrowList> borrowList1 = pageBean.getList();
			for (BorrowList temp : borrowList1) {
				temp.setReader(map.get(temp.getReader().getReaderID()));
			}
		} finally {
			DbSessionFactory.closeSession();
		}
		return pageBean;
	}
	
	@Override
	public Books findByKey(Integer id) {
		try {
			DbSessionFactory.openSession();
			return booksDao.findByKey(id);
		} finally {
			DbSessionFactory.closeSession();
		}
	}
	
	@Override
	public PageBean<BorrowList> findBorrowListByBook(int page, int size,
			int readerid, int bookid) {
		PageBean<BorrowList> pageBean=null;
		try {
			DbSessionFactory.openSession();
			pageBean= booksDao.findBorrowListByBook(page,size,readerid,bookid);
			Map<Integer, Books> booksMap = new HashMap<Integer, Books>();
			List<Books> booksList = booksDao.findAll();
			for (Books temp : booksList) {
				booksMap.put(temp.getBookID(), temp);
			}
			List<BorrowList> borrowList = pageBean.getList();
			for (BorrowList temp : borrowList) {
				temp.setBook(booksMap.get(temp.getBook().getBookID()));
			}
			
			Map<Integer, Reader> map = new HashMap<Integer, Reader>();
			List<Reader> readerList = readerDao.findAll();
			for (Reader temp : readerList) {
				map.put(temp.getReaderID(), temp);
			}
			List<BorrowList> borrowList1 = pageBean.getList();
			for (BorrowList temp : borrowList1) {
				temp.setReader(map.get(temp.getReader().getReaderID()));
			}
		} finally {
			DbSessionFactory.closeSession();
		}
		return pageBean;
	}

	public BorrowList findTwoKey(int readerID,int bookID) {
		try {
			DbSessionFactory.openSession();
			return booksDao.findByTwoKey(readerID, bookID);
		} finally {
			DbSessionFactory.closeSession();
		}
	}
	
}

5.新增,修改书籍界面代码

package com.lovo.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;

import com.lovo.biz.impl.BooksServiceImpl;
import com.lovo.entity.BookType;
import com.lovo.entity.Books;


@SuppressWarnings("serial")
public class BookSaveJDialog extends JDialog implements ActionListener{
	private JLabel[] labs = new JLabel[11];
	private JLabel photoLabel;
	private JTextField[] txts = new JTextField[10];
	private JTextArea  bookTxt;
	private JComboBox<BookType> supperComboBox,typeComboBox;
	private JButton okBtn,cancleBtn,imageBtn;
	
	private JFileChooser imageChooser = new JFileChooser();
	
	private Map<String, Integer> typeMap = new HashMap<String, Integer>();
	@SuppressWarnings("unused")
	private Map<String,Integer> baseMap = new HashMap<String, Integer>();
	
	private String[] strs = {"书籍编号:","书籍ISBN:", "书籍名称:", "作者:", "译者:", "出版社:", "出版日期:",
			"库存数量:","价格:", "日租金:", "书籍类型:" };
	
	private  Books book;
	private Image photo;
	
	private JDialog fatherJDialog;

	public BookSaveJDialog(Books tempBook,JDialog fatherJDialog) {
		this.setModal(true);
		this.fatherJDialog = fatherJDialog;
		this.book = tempBook;
		this.setTitle("新增书籍");
		this.setSize(650, 750);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

		this.setLayout(null);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				loadData();
				if(book != null) {
					BookSaveJDialog.this.setTitle("修改书籍");
					txts[0].setText(String.valueOf(book.getBookID()));
					txts[1].setText(book.getBookISBN());
					txts[2].setText(book.getBookName());
					txts[3].setText(book.getAuthor());
					txts[4].setText(book.getTranslator());
					txts[5].setText(book.getPress());
					txts[6].setText(String.valueOf(book.getPublicationDate()));
					txts[7].setText(String.valueOf(book.getBookNumber()));
					txts[8].setText(String.valueOf(book.getPrice()));
					txts[9].setText(String.valueOf(book.getRentPerDay()));
					
					bookTxt.setText(book.getFullInfo());
					supperComboBox.setSelectedIndex(0);
					typeComboBox.setSelectedIndex(0);

					
					okBtn.setText("确认修改");
					

				}
			}
		});

		initComponents();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void loadData() {
		//级联下拉列表 
		List<BookType> baseTypeList = new BooksServiceImpl().findAllBaseType();
		Vector<BookType> baseTypeVector = new Vector<BookType>(baseTypeList);
		ComboBoxModel baseTypeModel = new DefaultComboBoxModel(baseTypeVector);
		supperComboBox.setModel(baseTypeModel);
		
		final List<List> fatherList = new BooksServiceImpl().findAllBookType();
		Vector<BookType> bookTypeVector = new Vector<BookType>(fatherList.get(0));
		ComboBoxModel bookTypeModel = new DefaultComboBoxModel(bookTypeVector);
		typeComboBox.setModel(bookTypeModel);
		
		for(int i = 0; i < fatherList.get(0).size(); i++) {
			typeMap.put(( (BookType) fatherList.get(0).get(i)).getTypeName(), i);
		}
		//添加菜单选项改变监听器事件
		supperComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				int index = supperComboBox.getSelectedIndex();
				typeComboBox.removeAllItems();
					Vector<BookType> bookTypeVector = new Vector<BookType>(fatherList.get(index));
					ComboBoxModel bookTypeModel = new DefaultComboBoxModel(bookTypeVector);
					typeComboBox.setModel(bookTypeModel);
					for(int j = 0 ; j < fatherList.get(index).size();j++){
						typeMap.put(((BookType) fatherList.get(index).get(j)).getTypeName(), j);
					}
				}
		});
		
		
	}

	private void initComponents() {
		for (int i = 0; i < labs.length; i++) {
			labs[i] = new JLabel(strs[i],JLabel.RIGHT);
			labs[i].setBounds(10, 30 + 60 * i, 100, 30);
			this.add(labs[i]);
		}
		
		photoLabel = new JLabel("", JLabel.CENTER);
		photoLabel.setBounds(380, 30, 200, 240);
		photoLabel.setBorder(new LineBorder(Color.BLACK));
		this.add(photoLabel);
		
		for (int i = 0; i < txts.length; i++) {
			txts[i] = new JTextField();
			txts[i].setBounds(130, 30 + 60 * i, 200,30);
			this.add(txts[i]);
		}
		
		bookTxt = new JTextArea("请输入书籍的详细信息:");
		bookTxt.setBounds(380, 340, 210,240);
		bookTxt.addFocusListener(new FocusAdapter() {

			@Override
			public void focusGained(FocusEvent e) {
				if(bookTxt.getText().equals("请输入书籍的详细信息:")){
				bookTxt.setText("");
				}
			}
			
		});
		this.add(bookTxt);
		
		supperComboBox = new JComboBox<BookType>();
		supperComboBox.setBounds(130, 630, 100,30);
		this.add(supperComboBox);
		
		typeComboBox = new JComboBox<BookType>();
		typeComboBox.setBounds(230, 630, 100,30);
		this.add(typeComboBox);
		
		okBtn = new JButton("确认新增");
		okBtn.setBounds(180, 680, 100, 30);
		this.add(okBtn);
		
		cancleBtn = new JButton("取消");
		cancleBtn.setBounds(370, 680, 100, 30);
		this.add(cancleBtn);
		
		imageBtn = new JButton("添加图片");
		imageBtn.setBounds(420, 280, 100, 30);
		this.add(imageBtn);
		
		
		
		
		Font f = new Font("微软雅黑", Font.PLAIN, 14);
		for(Component c : this.getContentPane().getComponents()) {
			c.setFont(f);				
			if(c instanceof JButton) {	
				((JButton) c).addActionListener(this);
			}
		}
		
			imageChooser.setFileFilter(new FileFilter() {
			
			@Override
			public String getDescription() {
				return "图片文件";
			}
			
			@Override
			public boolean accept(File f) {
				if(f.isFile()) {
					String filename = f.getName();
					int dotIndex = filename.lastIndexOf(".");
					if(dotIndex > 0 && dotIndex < filename.length() - 1) {
						String ext = filename.substring(dotIndex + 1);
						return ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("png") 
								|| ext.equalsIgnoreCase("bmp") || ext.equalsIgnoreCase("gif");
					}
				}
				return f.isDirectory();
			}
		});
	}

//	public static void main(String[] args) {
//	new SaveFrame(book).setVisible(true);
//}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("确认新增")){
			doAddBook();
		}
		else if(command.equals("确认修改")) {
			doUpdatBook();
		}
		else if(command.equals("取消")) {
			this.dispose();			
			fatherJDialog.setVisible(true);
		}
		else if(command.equals("添加图片")) {
			if(imageChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
				repaint();
			}
		}
		
	}

	//修改书籍
	private void doUpdatBook() {
		int bookId =Integer.parseInt(txts[0].getText());
		String bookISBN=txts[1].getText().trim();
		String bookName=txts[2].getText().trim();
		String author=txts[3].getText().trim();
		String translator=txts[4].getText().trim();
		String press =txts[5].getText().trim();
		long publicationDate=Long.parseLong(txts[6].getText().trim());
		int number = Integer.parseInt(txts[7].getText().trim());
		double price = Double.parseDouble(txts[8].getText().trim());
		double rentpayday = Double.parseDouble(txts[9].getText().trim());
		String fullInfo =bookTxt.getText();
		BookType baseType=(BookType) supperComboBox.getItemAt(supperComboBox.getSelectedIndex());
		BookType bookType=(BookType) typeComboBox.getItemAt(typeComboBox.getSelectedIndex());	
		book.setBookID(bookId);
		book.setBookISBN(bookISBN);
		book.setBookName(bookName);
		book.setAuthor(author);
		book.setTranslator(translator);
		book.setPress(press);
		book.setPublicationDate(publicationDate);
		book.setBookNumber(number);
		book.setPrice(price);
		book.setRentPerDay(rentpayday);
		book.setFullInfo(fullInfo);
		book.setBaseType(baseType);
		book.setBookType(bookType);
		
		File f = imageChooser.getSelectedFile();
		if(f != null) {
			InputStream in = null;
			byte[] buffer = new byte[(int) f.length()];
			try {
				in = new FileInputStream(f);
				in.read(buffer);
				book.setPicture(buffer);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				if(in != null) {
					try {
						in.close();
					}
					catch (IOException e) {
					}
				}
			}
		}
		if(new BooksServiceImpl().update(book)) {
			JOptionPane.showMessageDialog(null, "修改成功");
			this.dispose();
			fatherJDialog.setVisible(true); 
		}
		else {
			JOptionPane.showMessageDialog(null, "修改失败", "错误", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	//新增书籍
	private void doAddBook() {
		int bookId = Integer.parseInt(txts[0].getText().trim());
		String bookISBN = txts[1].getText().trim();
		String bookName = txts[2].getText().trim();
		String author = txts[3].getText().trim();
		String translator = txts[4].getText().trim();
		String press = txts[5].getText().trim();
		long publicationDate = Long.parseLong(txts[6].getText().trim());
		int number = Integer.parseInt(txts[7].getText().trim());
		double price = Double.parseDouble(txts[8].getText().trim());
		double rentpayday = Double.parseDouble(txts[9].getText().trim());
		String fullInfo = bookTxt.getText();
		
		BookType baseType=(BookType) supperComboBox.getItemAt(supperComboBox.getSelectedIndex());
		BookType bookType=(BookType) typeComboBox.getItemAt(typeComboBox.getSelectedIndex());
		Books book1 = new Books();
		book1.setBookID(bookId);
		book1.setBookISBN(bookISBN);
		book1.setBookName(bookName);
		book1.setAuthor(author);
		book1.setTranslator(translator);
		book1.setPress(press);
		book1.setPublicationDate(publicationDate);
		book1.setBookNumber(number);
		book1.setPrice(price);
		book1.setRentPerDay(rentpayday);
		book1.setFullInfo(fullInfo);
		book1.setBaseType(baseType);
		book1.setBookType(bookType);
		
		File f = imageChooser.getSelectedFile();
		if(f != null) {
			InputStream in = null;
			byte[] buffer = new byte[(int) f.length()];
			try {
				in = new FileInputStream(f);
				in.read(buffer);
				book.setPicture(buffer);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				if(in != null) {
					try {
						in.close();
					}
					catch (IOException e) {
					}
				}
			}
		}
		if(new BooksServiceImpl().save(book1)){
			JOptionPane.showMessageDialog(null, "添加成功");
			this.dispose();
//			this.setVisible(false);
			fatherJDialog.setVisible(true);
		}
		else {
			JOptionPane.showMessageDialog(null, "添加失败", "错误", JOptionPane.ERROR_MESSAGE);
		}
		
		
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		File f = imageChooser.getSelectedFile();
		if(photo != null) {
			g.drawImage(photo, 380, 30, 200, 240, null);
		}
		if(f != null) {
			Image image = Toolkit.getDefaultToolkit().getImage(f.getAbsolutePath());
			g.drawImage(image, 380, 30, 200, 240, null);
		}
	}
}

6.借书界面

package com.lovo.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.lovo.bean.PageBean;
import com.lovo.biz.impl.BooksServiceImpl;
import com.lovo.biz.impl.ReaderServiceImpl;
import com.lovo.entity.Books;
import com.lovo.entity.BorrowList;
import com.lovo.entity.Reader;
import com.lovo.util.MyUtil;

/**
 * 查询数据窗口模型
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class BorrowDialog extends JDialog implements ActionListener {
	final Font NORMAL_FONT = new Font("微软雅黑", Font.PLAIN, 16);
	final Font BIG_FONT = new Font("微软雅黑", Font.PLAIN, 70);

	private JTable dataTable;
	private JScrollPane dataPane;
	private int page = 1;
	private int size = 20;
	private int totalPage = 1;

	private BooksServiceImpl bookService = new BooksServiceImpl();
	private ReaderServiceImpl readerService = new ReaderServiceImpl();

	private JLabel[] labs = new JLabel[7];
	private JTextField[] txts = new JTextField[7];
	private JButton[] chooseButtons = new JButton[4];
	private JButton readerBtn,bookBtn,okBtn,cancleBtn;
	private JLabel hintLabel;
	
	private String[] labelStrs={"读者编号:","读者姓名:","借书数量:","书籍编号:",
			"书名:","数量:","日租金:"};

	/**
	 * 构造器初始化
	 */
	public BorrowDialog() {
		this.setSize(870, 580);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(null);

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				startLoadData();
			}
		});

		initComponents();
		
	}

	/**
	 * 初始化方法
	 */
	public void initComponents() {
		for(int i=0;i<labs.length;i++){
			labs[i] = new JLabel(labelStrs[i],JLabel.RIGHT);
			labs[i].setBounds(10,100+50*i,100,30);
			this.add(labs[i]);
		}
		
		for(int i=0;i<txts.length;i++){
			txts[i] = new JTextField();
			txts[i].setBounds(110, 100+50*i, 150, 30);
			this.add(txts[i]);
		}
		txts[1].setEnabled(false);
		txts[2].setEnabled(false);
		txts[4].setEnabled(false);
		txts[5].setEnabled(false);
		txts[6].setEnabled(false);
		
		
		readerBtn = new JButton();
		readerBtn.setBounds(260, 100, 20, 20);
		readerBtn.addActionListener(this);
		this.add(readerBtn);
		
		bookBtn = new JButton();
		bookBtn.setBounds(260, 250, 20, 20);
		bookBtn.addActionListener(this);
		this.add(bookBtn);
		
		okBtn = new JButton("确认借出");
		okBtn.setBounds(80, 500, 80, 30);
		okBtn.addActionListener(this);
		this.add(okBtn);
		
		cancleBtn = new JButton("取消借出");
		cancleBtn.setBounds(180, 500, 80, 30);
		cancleBtn.addActionListener(this);
		this.add(cancleBtn);

		// 表格内容以及格式设置
		dataTable = new JTable();
		dataTable.getTableHeader().setReorderingAllowed(false);
		dataTable.getTableHeader().setResizingAllowed(true);

		dataPane = new JScrollPane(dataTable);
		dataPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		dataPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		dataPane.setBounds(330, 100, 470, 350);

		this.add(dataPane);

		JPanel SouthPanel = new JPanel();
		this.add(SouthPanel, BorderLayout.SOUTH);
		
		// 下方按钮
		String[] str = { "首页", "上一页", "下一页", "末页"};
		for (int i = 0; i < chooseButtons.length; i++) {
			chooseButtons[i] = new JButton(str[i]);
			chooseButtons[i].addActionListener(this);
			chooseButtons[i].setFont(NORMAL_FONT);
			chooseButtons[i].setBounds(100 + i * 100, 550, 80, 30);
			this.add(chooseButtons[i]);
		}
		
		hintLabel= new JLabel();
		hintLabel.setBounds(100, 450, 200, 30);
		hintLabel.setEnabled(false);
		this.add(hintLabel);
		
	}
	
	/**
	 * 方法:刷新页面
	 */
	public void pageComboBoxFlash() {

	}
	
	/**
	 * 方法:主界面获得信息数据 并刷新界面
	 * 
	 * @return
	 * @return
	 */
	public void loadData() {
		// 这里的fNames必须和类里面的 严格对应!!!! 数据库里可以忽略大小写
		String[] colNames = { "编号", "读者编号", "读者姓名", "书籍编号","书名", "操作员", "借出日期", "是否归还"};
		String[] fNames = { "borrowID", "reader.readerID", "reader.readerName", "book.bookID",
				"book.bookName","operatorID", "borrowDate", "isBack"};
		// 调用业务逻辑层封装好的事务脚本
		PageBean<BorrowList> bean = bookService.findBorrowList(page,size,Integer.parseInt(txts[0].getText()));
		if(bean==null){
			JOptionPane.showMessageDialog(null, "此用户并不存在");
		}
		else{
		List<BorrowList> borrowList = bean.getList();
		Object[][] data = new Object[borrowList.size()][colNames.length];

		// 反射~~已知对象属性的名称 已知对象 通过反射获得对象属性的值
		for (int i = 0; i < data.length; i++) {
			BorrowList temp = borrowList.get(i);
			for (int j = 0; j < colNames.length; j++) {
				data[i][j] = MyUtil.getValue(temp, fNames[j]);
			}
		}
		// 设置模式,刷新模式同时刷新了主界面
		TableModel model = new DefaultTableModel(data, colNames) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		dataTable.setModel(model);

		for (int i = 0; i < chooseButtons.length; i++) {
			chooseButtons[i].setEnabled(false);
		}
		if (page > 1) {
			chooseButtons[0].setEnabled(true);
			chooseButtons[1].setEnabled(true);
		}
		if (page < totalPage) {
			chooseButtons[2].setEnabled(true);
			chooseButtons[3].setEnabled(true);
		}
		}
		
	}
	
	public void startLoadData(){
		
		String[] colNames = { "编号", "读者编号", "读者姓名", "书籍编号","书名", "操作员", "借出日期", "是否归还"};
		Object[][] data = new Object[10][colNames.length];
		TableModel model = new DefaultTableModel(data, colNames) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		dataTable.setModel(model);
	}
	
	/**
	 * 重写监听器方法
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj == chooseButtons[0]) {
			page = 1;
			loadData();
		} else if (obj == chooseButtons[1]) {
				page--;
				loadData();
		} else if (obj == chooseButtons[2]) {
				page++;
				loadData();
		} else if (obj == chooseButtons[3]) {
			page = totalPage;
			loadData();
		} else if(obj == readerBtn){
			Reader reader=readerService.findByKey(Integer.parseInt(txts[0].getText()));
			loadData();
			txts[1].setText(reader.getReaderName());
			txts[2].setText(String.valueOf(reader.getCurrentNumber()));
			if(reader.getCurrentNumber()>=reader.getMaxNumber()){
				okBtn.setEnabled(false);
				hintLabel.setText("该读者可借书数量已满!");
				
			}
		} else if(obj==bookBtn){
			Books book = bookService.findByKey(Integer.parseInt(txts[3].getText()));
			txts[4].setText(book.getBookName());
			txts[5].setText(String.valueOf(book.getBookNumber()));
			txts[6].setText(String.valueOf(book.getRentPerDay()));
			if(book.getBookNumber()<=0){
				okBtn.setEnabled(false);
				hintLabel.setText("该书籍的数量为0!");
			}
		}else if(obj==okBtn){
			bookService.borrow(Integer.parseInt(txts[0].getText()), Integer.parseInt(txts[3].getText()));
			loadData();
			Reader reader=readerService.findByKey(Integer.parseInt(txts[0].getText()));
			Books book = bookService.findByKey(Integer.parseInt(txts[3].getText()));
			txts[2].setText(String.valueOf(reader.getCurrentNumber()));
			txts[5].setText(String.valueOf(book.getBookNumber()));
		}
		
	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new BorrowDialog().setVisible(true);
	}

}

7.还书界面

package com.lovo.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.lovo.bean.PageBean;
import com.lovo.biz.impl.BooksServiceImpl;
import com.lovo.biz.impl.ReaderServiceImpl;
import com.lovo.entity.Books;
import com.lovo.entity.BorrowList;
import com.lovo.util.MyUtil;

/**
 * 查询数据窗口模型
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("serial")
public class bookBackDialog extends JDialog implements ActionListener {
	final Font NORMAL_FONT = new Font("微软雅黑", Font.PLAIN, 16);
	final Font BIG_FONT = new Font("微软雅黑", Font.PLAIN, 70);

	private JTable dataTable;
	private JScrollPane dataPane;
	private int page = 1;
	private int size = 20;
	private int totalPage = 1;
	private int time;

	private JLabel readerLabel, bookLabel, hintLabel, moneyLabel, damageLabel;
	private JTextField readerField, bookField, hintField, moneyField;
	private JButton readerButton, bookButton, okButton, cancelButton;
	private JRadioButton yesRadio, noRadio;

	private BooksServiceImpl bookService = new BooksServiceImpl();
	private ReaderServiceImpl readerService = new ReaderServiceImpl();

	/**
	 * 构造器初始化
	 */
	public bookBackDialog() {
		this.setTitle("归还书籍");
		this.setSize(1000, 580);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLayout(null);

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				startLoadData();
			}
		});

		initComponents();

	}

	/**
	 * 初始化方法
	 */
	public void initComponents() {

		readerLabel = new JLabel("请输入读者的编号:", JLabel.RIGHT);
		readerLabel.setBounds(270, 40, 180, 30);
		this.add(readerLabel);

		bookLabel = new JLabel("请输入归还书籍的编号:", JLabel.RIGHT);
		bookLabel.setBounds(270, 100, 180, 30);
		this.add(bookLabel);

		readerField = new JTextField();
		readerField.setBounds(460, 40, 140, 30);
		this.add(readerField);

		bookField = new JTextField();
		bookField.setBounds(460, 100, 140, 30);
		this.add(bookField);

		readerButton = new JButton();
		readerButton.setBounds(600, 50, 20, 20);
		readerButton.addActionListener(this);
		this.add(readerButton);

		bookButton = new JButton();
		bookButton.setBounds(600, 110, 20, 20);
		bookButton.addActionListener(this);
		this.add(bookButton);

		// 表格内容以及格式设置
		dataTable = new JTable();
		dataTable.getTableHeader().setReorderingAllowed(false);
		dataTable.getTableHeader().setResizingAllowed(true);

		dataPane = new JScrollPane(dataTable);
		dataPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		dataPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		dataPane.setBounds(100, 150, 800, 230);

		this.add(dataPane);

		JPanel SouthPanel = new JPanel();
		this.add(SouthPanel, BorderLayout.SOUTH);

		damageLabel = new JLabel("书籍是否损坏:", JLabel.RIGHT);
		damageLabel.setBounds(200, 390, 140, 30);
		this.add(damageLabel);

		ButtonGroup bg = new ButtonGroup();
		yesRadio = new JRadioButton("是");
		noRadio = new JRadioButton("否");
		yesRadio.setBounds(340, 390, 50, 30);
		noRadio.setBounds(390, 390, 50, 30);
		bg.add(yesRadio);
		bg.add(noRadio);
		this.add(yesRadio);
		this.add(noRadio);

		hintLabel = new JLabel("超期提示:", JLabel.RIGHT);
		hintLabel.setBounds(500, 390, 100, 30);
		this.add(hintLabel);

		hintField = new JTextField();
		hintField.setBounds(600, 390, 140, 30);
		hintField.setEditable(false);
		this.add(hintField);

		moneyLabel = new JLabel("应付金额:", JLabel.RIGHT);
		moneyLabel.setBounds(270, 440, 180, 30);
		this.add(moneyLabel);

		moneyField = new JTextField();
		moneyField.setBounds(450, 440, 140, 30);
		moneyField.setEditable(false);
		this.add(moneyField);

		okButton = new JButton("确认归还");
		okButton.setBounds(280, 490, 100, 30);
		okButton.addActionListener(this);
		this.add(okButton);

		cancelButton = new JButton("取消归还");
		cancelButton.setBounds(580, 490, 100, 30);
		cancelButton.addActionListener(this);
		this.add(cancelButton);

	}

	/**
	 * 方法:主界面获得信息数据 并刷新界面
	 * 
	 * @return
	 * @return
	 */
	public void loadData() {
		// 这里的fNames必须和类里面的 严格对应!!!! 数据库里可以忽略大小写
		String[] colNames = { "编号", "读者编号", "读者姓名", "书籍编号", "书名", "书籍原价",
				"借出管理员", "借出日期", "还书日期", "是否归还", "日租金" };
		String[] fNames = { "borrowID", "reader.readerID", "reader.readerName",
				"book.bookID", "book.bookName", "book.price", "operatorID",
				"borrowDate", "backDate", "isBack", "book.rentPerDay" };
		// 调用业务逻辑层封装好的事务脚本
		PageBean<BorrowList> bean = bookService.findBorrowList(totalPage, size,
				Integer.parseInt(readerField.getText()));

		List<BorrowList> borrowList = bean.getList();
		Object[][] data = new Object[borrowList.size()][colNames.length];

		// 反射~~已知对象属性的名称 已知对象 通过反射获得对象属性的值
		for (int i = 0; i < data.length; i++) {
			BorrowList temp = borrowList.get(i);
			for (int j = 0; j < colNames.length; j++) {
				data[i][j] = MyUtil.getValue(temp, fNames[j]);
			}
		}
		// 设置模式,刷新模式同时刷新了主界面
		TableModel model = new DefaultTableModel(data, colNames) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		dataTable.setModel(model);

	}

	public void BookloadData() {
		// 这里的fNames必须和类里面的 严格对应!!!! 数据库里可以忽略大小写
		String[] colNames = { "编号", "读者编号", "读者姓名", "书籍编号", "书名", "书籍原价",
				"借出管理员", "借出日期", "还书日期", "是否归还", "日租金" };
		String[] fNames = { "borrowID", "reader.readerID", "reader.readerName",
				"book.bookID", "book.bookName", "book.price", "operatorID",
				"borrowDate", "backDate", "isBack", "book.rentPerDay" };
		// 调用业务逻辑层封装好的事务脚本
		PageBean<BorrowList> bean = bookService.findBorrowListByBook(page,
				size, Integer.parseInt(readerField.getText()),
				Integer.parseInt(bookField.getText()));

		List<BorrowList> borrowList = bean.getList();
		Object[][] data = new Object[borrowList.size()][colNames.length];

		// 反射~~已知对象属性的名称 已知对象 通过反射获得对象属性的值
		for (int i = 0; i < data.length; i++) {
			BorrowList temp = borrowList.get(i);
			for (int j = 0; j < colNames.length; j++) {
				data[i][j] = MyUtil.getValue(temp, fNames[j]);
			}
		}
		// 设置模式,刷新模式同时刷新了主界面
		TableModel model = new DefaultTableModel(data, colNames) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		dataTable.setModel(model);

	}

	public void startLoadData() {

		String[] colNames = { "编号", "读者编号", "读者姓名", "书籍编号", "书名", "书籍原价",
				"借出管理员", "借出日期", "还书日期", "是否归还", "日租金" };
		Object[][] data = new Object[10][colNames.length];
		TableModel model = new DefaultTableModel(data, colNames) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		dataTable.setModel(model);
	}

	/**
	 * 重写监听器方法
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object obj = e.getSource();
		if (obj == readerButton) {
			loadData();
		} else if (obj == bookButton) {
			BookloadData();
			BorrowList blist = bookService.findTwoKey(
					Integer.parseInt(readerField.getText()),
					Integer.parseInt(bookField.getText()));
			if (blist == null) {
				JOptionPane.showMessageDialog(null, "没有未还的这本书");
			} else {
				double money = 0.0;
				time = (int) ((System.currentTimeMillis() - blist
						.getBorrowDate()) / (3600 * 24 * 1000));
				if (time <= 30) {
					hintField.setText("未超期");
					if (yesRadio.isSelected()) {
						money = blist.getBook().getPrice();
					} else if (noRadio.isSelected()) {
						money = 0.0;
					}
				} else {
					hintField.setText("超期" + (time - 30));
					if (yesRadio.isSelected()) {
						money = blist.getBook().getPrice()
								+ blist.getBook().getRentPerDay()
								* (time - 30)
								* blist.getReader().getReaderType()
										.getDisciunt();
					} else {
						money = blist.getBook().getRentPerDay()
								* (time - 30)
								* blist.getReader().getReaderType()
										.getDisciunt();
						;
					}
				}
				moneyField.setText(String.valueOf(money));
				if (yesRadio.isSelected()) {
					money = blist.getBook().getPrice();
				}
			}
		} else if (obj == okButton) {
			bookService.bookBack(Integer.parseInt(readerField.getText()),
					Integer.parseInt(bookField.getText()));
			loadData();
		}

	}

	public static void main(String[] args) throws Exception {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new bookBackDialog().setVisible(true);
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值