圖書管理系統——面向對象程序設計

1、登錄系統模塊的實現
本模塊主要是用戶經過圖書館管理系統的首頁進入該系統。用戶輸入正確的用戶名和密碼,若是登陸信息有錯誤,則系統提示登陸錯誤信息,而且禁止系統用戶進行任何操做。若登陸信息正確,系統會根據用戶的身份進行相應權限的判斷,讀者進入前臺系統,管理員進入後臺系統。
2、圖書管理系統代碼:
1.(1)圖書館系統登陸主頁面
Action層:
public String login() { boolean flag = iss.login(admin); if(flag) { return "success"; }
message = "用戶名或口令錯誤!";
return "fail"; }
Service層:
public boolean login(TAdmin admin) { TAdmin a = isd.login(admin); if(a != null) { return true; }
return false; }
Dao層:
public TAdmin login(TAdmin admin) {
Session session =
this.getHibernateTemplate().getSessionFactory().openSession(); Query q = session.createQuery("from TAdmin a where a.adminName = ?
and a.adminPassword = ?").setString(0,
admin.getAdminName()).setString(1, admin.getAdminPassword()); TAdmin a = (TAdmin)q.uniqueResult();
return a;
}
2.圖書管理模塊的實現
圖書管理模塊主要分爲圖書入庫、查看圖書、統計圖書,其中圖書入庫是往圖書管理數據庫中添加圖書信息。看圖書功能中能夠查看圖書詳細信息,並對圖書信息進行修改或刪除某些廢棄圖書信息。
(1)圖書入庫功能的實現
點擊添加圖書功能,填寫圖書基本信息,爲圖書選擇類型,類型是與數據庫交互動態生成的下拉列表.
public String addInfoBook() {
this.sortList = ibs.listBookSort();
return "add"; }
public List listBookSort() { return this.getHibernateTemplate().find("from TSort tsort"); }
public String addSaveBook() { ibs.saveBook(book); book = null; bookList = this.listBook(); return "Book"; }
public void saveBook(TBook book) { Date now = new Date(); book.setBuyDate(now); book.setCurrentNum(book.getTotalNum()); ibd.saveBook(book); }
public void saveBook(TBook book) {
數據庫

this.getHibernateTemplate().save(book); }
3.(1)圖書維護功能的實現
點擊圖書維護功能,展現全部圖書的信息列表,在每一個圖書信息行後提供刪除和修改操做功能,而且能夠單擊選定某一個圖書信息後的詳細信息進行詳細查看圖書信息。
public String listAllBook() { bookList = this.listBook(); return "Book"; }
public List listBook(TBook book, Page page) { String hql = "from TBook tb where 1=1";
if(book!=null) {
if(book.getBookNum() != null
&& !"".equals(book.getBookNum().trim())) {
hql = hql + " and tb.bookNum = '" + book.getBookNum()+"'"; }
if(book.getBookName() != null
&& !"".equals(book.getBookName().trim())) {
hql = hql + " and tb.bookName = '" + book.getBookName()+"'";
}
if(book.getWriter() != null
&& !"".equals(book.getWriter().trim())) { hql = hql + " and tb.writer = '" + book.getWriter()+"'"; }
if(!book.getSortId().getSortId().trim().equals("-1")) {
hql = hql + " and tb.sortId.sortId = '" + book.getSortId().getSortId()+"'";
} hql = hql + " order by tb.bookName"; } else { hql = hql + " order by tb.buyDate desc"; }
Session session =
this.getHibernateTemplate().getSessionFactory().openSession(); Query q = session.createQuery(hql); q.setFirstResult(page.getStartRow()); q.setMaxResults(5); List list = q.list();
return list;
(2)刪除圖書功能的實現
public String deleteOneBook() { ibs.delBookById(bookNum); bookList = this.listBook(); return "Book"; }
public String deleteMoreBook() { ibs.delMoreBook(bookNums);
bookList = this.listBook();
return "Book"; }
public void delBookById(String bookNum) {
this.getHibernateTemplate().delete(this.getHibernateTemplate().
get(TBook.class, bookNum)); }
public void delMoreBook(String[] bookNums) {
for(int i=0; i<bookNums.length;i++) {
this.getHibernateTemplate().delete(this.getHibernateTemplate().
get(TBook.class, bookNums[i])); }
}
(3)修改圖書信息功能的實現
public String modifyInfoBook() {
this.sortList = ibs.listBookSort(); book = ibs.queryOneBookById(bookNum); return "modifyInfo"; }
public String modifySaveBook() { ibs.modifyBook(book); book = null; bookList = this.listBook(); return "Book";
}
public TBook queryOneBookById(String bookNum) {
TBook book = (TBook)this.getSession().createQuery("from TBook tb where tb.bookNum = ?").setString(0, bookNum).uniqueResult(); this.getSession().close(); return book;
}
public void modifyBook(TBook book) { this.getHibernateTemplate().update(book);
}
(4)查看圖書詳細信息功能的實現
public String infoDetailBook() { book = ibs.queryOneBookById(bookNum); return "Detail";
}
public TBook queryOneBookById(String bookNum) {
TBook book = (TBook)this.getSession().createQuery("from TBook tb where tb.bookNum = ?").setString(0, bookNum).uniqueResult(); this.getSession().close(); return book;
}
session

  1. 學生管理模塊的實現
    學生管理模塊主要分爲添加學生、查看學生、修改學生,查看學生的詳細信息。
    (1)學生添加功能的實現
    public String addInfoStudent() { this.academyList = iss.listAllAcademy(); return "add"; }
    public void addStudent(TStudent student) { Date now = new Date(); student.setCreateDate(now); student.setLendedNum(0);
    isd.saveStudent(student); } 其實現的代碼以下:
    public String addInfoStudent() { this.academyList = iss.listAllAcademy(); return "add"; }
    public void addStudent(TStudent student) { Date now = new Date(); student.setCreateDate(now); student.setLendedNum(0);
    isd.saveStudent(student); }
    public List listAllAcademy() { return this.getHibernateTemplate().find("from TAcademy ta"); }
    public List listAllClass() { return this.getHibernateTemplate().find("from TClass tc"); }
    public void saveStudent(TStudent student) {
    this.getHibernateTemplate().save(student); }
    (2)學生維護功能的實現
    public String listAllStudent() { studentList = this.getStudentListByPage();
    return "Student"; }
    public List listAllStudent(Page page) {
    Session session =
    this.getHibernateTemplate().getSessionFactory().openSession(); Query q = session.createQuery("from TStudent ts"); q.setFirstResult(page.getStartRow()); q.setMaxResults(5); List list = q.list();
    return list; }
    5.(1)刪除學生功能的實現
    管理員刪除讀者,刪除時可刪除一個,也可同時多選刪除。
    public String deleteOneStudent() { iss.deleteOneStudent(studentNum); studentList = this.getStudentListByPage(); return "Student"; }
    public String deleteMoreStudent() { iss.deleteMoreStudent(studentNums); studentList = this.getStudentListByPage();
    return "Student"; }
    public void deleteOneStudent(String studentNum) {
    this.getHibernateTemplate().delete(this.getHibernateTemplate().get(TStudent.class, studentNum)); }
    public void deleteMoreStudent(String[] studentNums) { for(int i=0;i<studentNums.length;i++) {
    this.getHibernateTemplate().delete(this.getHibernateTemplate().get(TStudent.class, studentNums[i])); }
    }
    (2)修改學生信息功能的實現
    public String modifyInfoStudent() { this.student = iss.getStudentByid(studentNum); this.academyList = iss.listAllAcademy();
    this.classList =
    iss.listClassById(student.getAcademyId().getAcademyId()); return "modifyInfo"; }
    public String modifySaveStudent() { iss.modifyStudentByid(student); student = null; studentList = this.getStudentListByPage(); return "Student";
    }
    public TStudent getStudentByid(String studentNum) {
    return (TStudent)this.getHibernateTemplate().get(TStudent.class, studentNum); }
    public void modifyStudentByid(TStudent student) { this.getHibernateTemplate().update(student);
    }
    (3)查看讀者詳細信息功能的實現
    public String infoDetailStudent() { student = iss.getOneStudentById(studentNum);
    return "Detail"; }
    public TStudent queryOneStudentById(String studentNum) {
    TStudent student = (TStudent)this.getSession().createQuery("from TStudent ts where ts.studentNum = ?").setString(0, studentNum).uniqueResult(); this.getSession().close();
    return student;
    }
  2. 借閱信息管理模塊的實現
    借書信息管理模塊主要分爲辦理圖書借閱、辦理圖書歸還、借閱信息查看,其中借閱信息查看可按圖書編號和借書證編號查看。
    (1)辦理圖書借閱功能的實現
    public String saveBookStudent() { message = ils.saveBookStudent(bookStudent); return "success"; }
    public String saveBookStudent(TBookStudent bookStudent) { TBook book = ild.getOneBook(bookStudent.getBookId()); if(book.getCurrentNum() == 0) { return "該書已被借完!"; } TStudent student = ild.getOneStudent(bookStudent.getStudentId()); if(student.getLendedNum() == 9) { return "借書數量已達到最大,不能在借!"; } Date borrowDate = new Date(); long l = borrowDate.getTime(); long ll = l + 302460601000L;
    Date returnDate = new Date(ll);
    bookStudent.setBorrowDate(borrowDate); bookStudent.setReturnDate(returnDate); bookStudent.setMoney(0d); //補上所欠金額的計算 ild.saveBookStudent(bookStudent); book.setCurrentNum(book.getCurrentNum()-1); ild.modifyBook(book); student.setLendedNum(student.getLendedNum()+1); ild.modifyStudent(student); return "借書成功!"; }
    public void saveBookStudent(TBookStudent bookStudent) {
    this.getHibernateTemplate().save(bookStudent); }
    (2)辦理圖書歸還功能的實現
    public String delBookStudent() { message = ils.delBookStudent(bookStudent); return "success"; }
    public String delBookStudent(TBookStudent bookStudent) { TBook book = ild.getOneBook(bookStudent.getBookId()); TStudent student = ild.getOneStudent(bookStudent.getStudentId());
    boolean flag = ild.delBookStudent(bookStudent.getStudentId(), bookStudent.getBookId()); if(!flag) { return "沒有該 記錄!"; } book.setCurrentNum(book.getCurrentNum()+1); student.setLendedNum(student.getLendedNum()-1);
    return "還書成功"; }
    public boolean delBookStudent(String studentId, String bookId) {
    Session session =
    this.getHibernateTemplate().getSessionFactory().openSession(); Query q = session.createQuery("from TBookStudent tbs where tbs.studentId = ? and tbs.bookId = ?").setString(0, studentId).setString(1, bookId); List list = q.list(); if(list.size() != 0) { TBookStudent bookStudent = list.get(0); this.getHibernateTemplate().delete(bookStudent); return true; }
    return false;
    }
    7.(1) 圖書檢索模塊的實現
    本模塊主要是用戶可以輸入本身想要查詢圖書的條件來快速的檢索。
    public List listBook() { this.sortList = ibs.listBookSort(); if(page == null) { page = new Page(); page.setCurrentPage(1); } page.setTotalRow(ibs.getBookNum(book)); page.setTotalPage((page.getTotalRow()+4)/5); page.setStartRow((page.getCurrentPage()-1) * 5); return ibs.listBook(book, page); }
    public List listBook(TBook book, Page page) { String hql = "from TBook tb where 1=1"; if(book!=null) {
    if(book.getBookNum() != null
    && !"".equals(book.getBookNum().trim())) { hql = hql + " and tb.bookNum = '" + book.getBookNum()+"'";
    }
    if(book.getBookName() != null
    && !"".equals(book.getBookName().trim())) {
    hql = hql + " and tb.bookName = '" + book.getBookName()+"'"; } if(book.getWriter() != null
    && !"".equals(book.getWriter().trim())) { hql = hql + " and tb.writer = '" + book.getWriter()+"'"; } if(!book.getSortId().getSortId().trim().equals("-1")) {
    hql = hql + " and tb.sortId.sortId = '" +
    book.getSortId().getSortId()+"'"; } hql = hql + " order by tb.bookName"; } else { hql = hql + " order by tb.buyDate desc"; } Session session =
    this.getHibernateTemplate().getSessionFactory().openSession(); Query q = session.createQuery(hql); q.setFirstResult(page.getStartRow()); q.setMaxResults(5);
    List list = q.list(); return list;

}
3、程序截圖
this

相關文章
相關標籤/搜索