一、项目功能:
1、读者
(1)图书信息查询
(2)图书借阅
2、管理员
(1)读者信息管理
(2)图书信息管理
(3)基础信息维护
(4)用户管理
4、结构
二、代码
只贴一部分
BookDaoImpl:
package zzm.com.dao.Impl;
import zzm.com.dao.BookDao;
import zzm.com.entity.Book;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import zzm.com.entity.BookLend;
import zzm.com.util.JDBCUtils;
import javax.swing.*;
import java.util.Date;
import java.util.List;
public class BookDaoImpl implements BookDao {
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
/**
* @param book 书籍信息
* @param publicationTimes 书籍数量
* @return
*/
@Override
public int addBooks(Book book, int publicationTimes) {
try {
String sql;
int j = 0;
for (int i=0;i<publicationTimes;i++){
sql="insert into book (ISBN, name, author, press, publicationDate, price, bookType, bookStatus) values (?,?,?,?,?,?,?,?)";
j=jdbcTemplate.update(sql,book.getISBN(),book.getName(),book.getAuthor(),book.getPress(),book.getPublicationDate(),book.getPrice(),book.getBookType(),book.getBookStatus());
}
return j;
}catch (Exception e){
return 0;
}
}
public List<Book> findAllBook() {
try {
String sql = "select * from book";
List<Book> books = template.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
return books;
}catch (Exception e){
return null;
}
}
@Override
public int deleteBook(int id){
try {
String sql = "delete from book where id = ?";
int i=template.update(sql, id);
return i;
}catch (Exception e){
return 0;
}
}
@Override
public int updateBook(String choose, String factor, int id) {
try {
String sql = "update book set "+choose+" = ? where id = ?";
int i=template.update(sql, factor, id);
return i;
}catch (Exception e){
return 0;
}
}
@Override
public List<Book> findByIntBook(String choose, int parseInt) {
try {
String sql = "select * from book where "+choose+" = ?";
List<Book> books = template.query(sql, new BeanPropertyRowMapper<Book>(Book.class), parseInt);
return books;
}catch (Exception e){
return null;
}
}
@Override
public List<Book> findByDoubleBook(String choose, double parseDouble) {
try {
String sql = "select * from book where"+choose+" = ?";
List<Book> books = template.query(sql, new BeanPropertyRowMapper<Book>(Book.class), parseDouble);
return books;
}catch (Exception e){
return null;
}
}
public List<Book> findByStringBook(String choose, String factor) {
try {
String sql = "select * from book where "+choose+" = ? ";
List<Book> books = template.query(sql, new BeanPropertyRowMapper<Book>(Book.class), factor);
return books;
}catch (Exception e){
return null;
}
}
@Override
public String findBookStatus(int id) {
try {
String sql="select bookStatus from book where id=?";
String status=jdbcTemplate.queryForObject(sql, String.class,id);
return status;
}catch (Exception e){
return null;
}
}
@Override
public int findBookBorrow(int id) {
try {
//查询读者可借
String sql1="select maxNum from readertype where readerType=(select readertype from reader where id=?)";
int i=jdbcTemplate.queryForObject(sql1, Integer.class,id);
//查询读者已借
String sql2="SELECT COUNT(*) FROM booklend WHERE id=? AND returnDate=NULL";
int j=jdbcTemplate.queryForObject(sql2, Integer.class,id);
return i-j;
}catch (Exception e){
return 0;
}
}
@Override
public void borrowRecord(int bookId, int readerId, Date date) {
try {
String sql="insert into booklend(bookId, readerId, borrowDate)values (?,?,?)";
jdbcTemplate.update(sql,bookId,readerId,date);
}catch (Exception e){
return;
}
}
@Override
public List<BookLend> findRecord(int readerId) {
try {
String sql="select * from booklend where readerId=? and returnDate is null ";
List<BookLend> bookLends=jdbcTemplate.query(sql, new BeanPropertyRowMapper<BookLend>(BookLend.class),readerId);
return bookLends;
}catch (Exception e){
return null;
}
}
public List<BookLend> findAllRecord() {
try {
String sql="select * from booklend";
List<BookLend> bookLends=jdbcTemplate.query(sql, new BeanPropertyRowMapper<BookLend>(BookLend.class));
return bookLends;
}catch (Exception e){
return null;
}
}
@Override
public int updateBookLend(Date date, int id) {
try {
String sql = "update booklend set returnDate= ? where id = ?";
int i=template.update(sql, date, id);
return i;
}catch (Exception e){
return 0;
}
}
@Override
public int getReaderDate(int readerId) {
try {
String sql="select maxDate from readertype where readerType=(select readertype from reader where id=?)";
int i=jdbcTemplate.queryForObject(sql, Integer.class,readerId);;
return i;
}catch (Exception e){
return 0;
}
}
}
BooksImpl:
package zzm.com.service.Impl;
import zzm.com.dao.BookDao;
import zzm.com.dao.Impl.BookDaoImpl;
import zzm.com.dao.Impl.ManagerDaoImpl;
import zzm.com.dao.ManagerDao;
import zzm.com.entity.Book;
import zzm.com.entity.BookLend;
import zzm.com.entity.Fine;
import zzm.com.service.Books;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class BooksImpl implements Books {
BookDao bookDao = new BookDaoImpl();
ManagerDao managerDao=new ManagerDaoImpl();
@Override
public void bookAdd(Book book, int publicationTimes) {
int i= bookDao.addBooks(book, publicationTimes);
if (i!=0) {
System.out.println("书籍添加成功");
} else {
System.out.println("书籍添加失败");
}
}
@Override
public void bookDelete(String factor) {
int i=bookDao.deleteBook(Integer.parseInt(factor,10));
if (i!=0)
System.out.println("删除成功");
else
System.out.println("删除失败");
}
@Override
public void bookFindAll() {
List<Book> books = bookDao.findAllBook();
if (books != null) {
for (Book book : books) {
System.out.println("书籍编号:" + book.getId() + "\tISBN:" + book.getISBN()
+ "\t书名:" + book.getName() + "\t作者:" + book.getAuthor()
+ "\t出版社:" + book.getPress() + "\t出版日期:" + book.getPublicationDate()
+ "\t单价:" + book.getPrice() + "\t书籍类别:" + book.getBookType()
+ "\t书籍状态:" + book.getBookStatus());
}
} else
System.out.println("未找到相应结果");
}
@Override
public boolean bookFindOne(String choose, String factor) {
List<Book>books=new ArrayList<Book>();
switch (choose){
case "id":books=bookDao.findByIntBook(choose,Integer.parseInt(factor,10));break;
case "price":books=bookDao.findByDoubleBook(choose,Double.parseDouble(factor));break;
case "ISBN": case "name": case "author": case "press": case "publicationDate":
case "bookType": case "bookStatus":books=bookDao.findByStringBook(choose,factor);break;
}
if (books != null) {
for (Book book : books) {
System.out.println("书籍编号:" + book.getId() + "\tISBN:" + book.getISBN()
+ "\t书名:" + book.getName() + "\t作者:" + book.getAuthor()
+ "\t出版社:" + book.getPress() + "\t出版日期:" + book.getPublicationDate()
+ "\t单价:" + book.getPrice() + "\t书籍类别:" + book.getBookType()
+ "\t书籍状态:" + book.getBookStatus());
}
return true;
} else{
System.out.println("未找到相应结果");
return false;
}
}
@Override
public void bookUpdate(String choose, String factor, String id) {
int i=bookDao.updateBook(choose,factor,Integer.parseInt(id,10));
if (i!=0)
System.out.println("修改成功");
else
System.out.println("修改失败");
}
@Override
public void bookBorrow(String i, int readerId) {
int bookId=Integer.parseInt(i,10);
String status=bookDao.findBookStatus(bookId);
if (status.equals("正常")){
int num=bookDao.findBookBorrow(readerId);
if (num>0){
bookDao.updateBook("bookStatus","借出",bookId);
bookDao.borrowRecord(bookId,readerId,new Date());
System.out.println("借阅成功");
}else
System.out.println("已达上限");
}else
System.out.println("该书暂时无法借阅");
}
@Override
public void bookFindLend(String booksId, String readersId, String status) {
int bookId=Integer.parseInt(booksId,10);
int readerId=Integer.parseInt(readersId,10);
List<Fine> fines=managerDao.findFine();
//如果该书籍存在
Book book=bookDao.findByIntBook("id",bookId).get(0);
if (book!=null){
List<BookLend> bookLends=bookDao.findRecord(readerId);
if (bookLends!=null){
for (BookLend bookLend:bookLends)
if (bookLend.getBookId()==bookId){
Date now=new Date();
bookDao.updateBookLend(now,bookLend.getId());
bookDao.updateBook("bookStatus",status,bookId);
if (status.equals("损坏")){
System.out.println("书籍损坏,请支付赔偿"+(fines.get(0).getFines()*book.getPrice()));
}
if (status.equals("缺失")){
System.out.println("书籍缺失,请支付赔偿"+(fines.get(1).getFines()*book.getPrice()));
}
long day=(now.getTime()-bookLend.getBorrowDate().getTime())/(1000);
int days=bookDao.getReaderDate(readerId);
if (day>days){
System.out.println("借阅超时,请支付罚金"+(fines.get(2).getFines()*(day-days)));
}
System.out.println("还书成功");
return;
}
System.out.println("该读者尚未借阅该书籍");
}else {
System.out.println("该读者尚未借阅任何书籍");
}
}else {
System.out.println("该图书不存在");
}
}
public void FindLend(){
List<BookLend> bookLends=bookDao.findAllRecord();
for (BookLend bookLend:bookLends){
System.out.println(bookLend.getBookId()+" "+bookLend.getReaderId()+" "+bookLend.getBorrowDate()+" "+bookLend.getReturnDate());
}
}
}