其他分享
首页 > 其他分享> > 图书管理系统

图书管理系统

作者:互联网

项目说明

项目内容

项目功能如下:
(1)读者信息管理:包括读者信息添加和读者信息查询与修改功能。用户登录成功之后,可以浏览所有读者的信息,也可以检索特定读者的信息;同时,可以对读者信息进行维护,包括增加、删除及修改。具体信息包括读者类型(读者的类型决定着他可以借到书的最大数量和最大归还天数)、读者姓名、出生日期、性别、电话、所在院系、注册日期等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(2)图书信息管理:包括图书信息添加和图书信息查询与修改功能。用户登录成功之后,可以浏览所有图书信息和检索特定图书的信息;也可以对图书信息进行维护。包括添加图书、删除图书以及修改图书信息。具体信息包括:图书ISBN、图书名称、作者、出版社、出版日期、印刷次数、单价、图书类别等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(3)图书借阅管理:包括图书借阅和图书归还功能。图书借阅功能,先输入读者的编号,然后输入要借阅的图书的信息,记录系统当前时间即借阅时间;图书归还功能,输入读者的编号,选择其名下已借阅的图书,判断当前日期即归还日期与借阅日期的差值是否超过了规定的期限,计算罚金,从而进行图书的归还操作。具体信息包括:借阅日期、归还日期、罚金。由于要计算罚金,故需要知道该读者的读者类型,根据类型判断其可借图书天数、可借图书数量等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(4)基础信息维护:包括图书类别设置、读者类别设置及罚金设置。图书类别设置,可以对图书的类别进行增加、删除、修改和查询;读者类别设置可以对读者的类别进行增加、删除、修改和查询;罚金设置,可以指定超期一天的罚金标准。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(5)用户管理:包括修改密码、用户添加和删除。修改密码,是指当前用户修改自己的密码;用户添加和删除,是对新增和去除系统用户时对用户信息的维护。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)

实例代码:

主要讲解重点借阅操作的代码段

借书和还书的实体类: 

package domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;

public class BorrowDomain implements Serializable {
    private static final long serialVersionUID = 42L;
    private Reader reader;
    private HashSet<Book> books;
    private String borrowdate;

    public BorrowDomain(Reader reader, HashSet<Book> books, String borrowdate) {
        this.reader = reader;
        this.books = books;
        this.borrowdate = borrowdate;
    }

    public BorrowDomain() {
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Reader getReader() {
        return reader;
    }

    public void setReader(Reader reader) {
        this.reader = reader;
    }

    public HashSet<Book> getBooks() {
        return books;
    }

    public void setBooks(HashSet<Book> books) {
        this.books = books;
    }

    public String getBorrowdate() {
        return borrowdate;
    }

    public void setBorrowdate(String borrowdate) {
        this.borrowdate = borrowdate;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BorrowDomain that = (BorrowDomain) o;
        return Objects.equals(reader, that.reader) &&
                Objects.equals(books, that.books) &&
                Objects.equals(borrowdate, that.borrowdate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(reader, books, borrowdate);
    }
}

  

 

package domain;

import java.io.Serializable;
import java.util.Objects;

public class ReturnBook implements Serializable {
    private static final long serialVersionUID = 42L;
    private BorrowDomain borrow;
    private String returnbook;
    private int moeny;
    private String book;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ReturnBook that = (ReturnBook) o;
        return moeny == that.moeny &&
                Objects.equals(borrow, that.borrow) &&
                Objects.equals(returnbook, that.returnbook) &&
                Objects.equals(book, that.book);
    }

    @Override
    public int hashCode() {
        return Objects.hash(borrow, returnbook, moeny, book);
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public BorrowDomain getBorrow() {
        return borrow;
    }

    public void setBorrow(BorrowDomain borrow) {
        this.borrow = borrow;
    }

    public String getReturnbook() {
        return returnbook;
    }

    public void setReturnbook(String returnbook) {
        this.returnbook = returnbook;
    }

    public int getMoeny() {
        return moeny;
    }

    public void setMoeny(int moeny) {
        this.moeny = moeny;
    }

    public String getBook() {
        return book;
    }

    public void setBook(String book) {
        this.book = book;
    }

    public ReturnBook(BorrowDomain borrow, String returnbook, int moeny, String book) {
        this.borrow = borrow;
        this.returnbook = returnbook;
        this.moeny = moeny;
        this.book = book;
    }

    public ReturnBook() {
    }
}

借书还书的操作类: 

package Service;

import domain.Book;
import domain.BorrowDomain;
import domain.Reader;
import domain.ReturnBook;
import org.w3c.dom.ls.LSOutput;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;

public class BorrrowBookService {
   private static HashSet<BorrowDomain> hashSets=new HashSet<>();
   private static HashSet<ReturnBook> returnBookHashSet=new HashSet<>();
   public void getAllBorrow() throws IOException, ClassNotFoundException {
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
       Object o = ois.readObject();
       HashSet<BorrowDomain> hs=(HashSet<BorrowDomain>) o;
       for(BorrowDomain b:hs){
           for(Book books: b.getBooks()) {
               if(books==null) {
                   System.out.println("学号:" + b.getReader().getReaderid() + " 姓名:" + b.getReader().getName() + " 类别:" + b.getReader().getType().getTypename()+"  借阅情况:"+"无借阅");
               }
               else {
                   System.out.println("学号:" + b.getReader().getReaderid() + " 姓名:" + b.getReader().getName() + " 类别:" + b.getReader().getType().getTypename()+"  借阅书名:"+books.getBookname()+" 借阅时间:"+b.getBorrowdate());
               }
           }
           System.out.println("=====================================");
       }
       TSUtility.readReturn();

   }
   public void borrowBook() throws IOException, ClassNotFoundException {
       new ReaderService().getallReader();
       Reader r=selectBorrowByid();
       HashSet<Book> borrowBookList = new HashSet<>();



       ObjectInputStream readIO = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
       Object o1 = readIO.readObject();
       HashSet<BorrowDomain> hb=(HashSet<BorrowDomain>) o1;
       readIO.close();
       for(BorrowDomain b:hb){
               if(b.getReader().equals(r)) {
                   for (Book xb : b.getBooks()) {
                       borrowBookList.add(xb);
                   }
               }
               else {
                   hashSets.add(b);
               }
       }


       new BookService().gatAllBook();
       System.out.println("请输入你要借阅的图书编号:");
       String borrowId=TSUtility.readKeyBoard(20,false);
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Book.txt"));
       Object o = ois.readObject();
       HashSet<Book> newSet=(HashSet<Book>) o;
       ois.close();
       int num=0;
       for (Book b:newSet){
           if(b.getBookid().equals(borrowId)){
               if(borrowBookList.size()>=r.getType().getMaxborrownum()){
                   System.out.println("已达到最大借书量,不能再借了");
               }
               else {
                   borrowBookList.add(b);
                   num++;
                   Date date = new Date();
                   SimpleDateFormat spd = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
                   String s = spd.format(date);
                   BorrowDomain bd = new BorrowDomain(r, borrowBookList, s);
                   hashSets.add(bd);
                   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
                   oos.writeObject(hashSets);
                   oos.close();
                   System.out.println("借阅成功");

                   BufferedWriter bw = new BufferedWriter(new FileWriter("TheTeam_2\\src\\Log.txt",true));
                   Date date2 = new Date();
                   SimpleDateFormat spd2= new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
                   String dengluTime=spd.format(date2);
                   bw.write(dengluTime+"   读者:"+r.getName()+" 借阅了书本:"+b.getBookname());
                   bw.newLine();
                   bw.flush();
                   bw.close();

               }
           }
       }
       if(num==0){
           System.out.println("你查找的图书不存在");
       }

   }
   public Reader selectBorrowByid() throws IOException, ClassNotFoundException {
       System.out.println("请输入读者学号");
       String id=TSUtility.readKeyBoard(11,false);
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Reader.txt"));
       Object o = ois.readObject();
       HashSet<Reader> newArr=(HashSet<Reader>) o;
       int num=0;
       for (Reader r:newArr){
           if(r.getReaderid().equals(id)){
               num++;
               return r;
           }
       }
       if(num==0) {
           System.out.println("未找到该学号的学生!");
       }
       TSUtility.readReturn();
       return null ;
   }
   public void returnBook() throws IOException, ClassNotFoundException, ParseException {
       getAllBorrow();
       System.out.println("请输入读者学号");
       String id=TSUtility.readKeyBoard(11,false);
//       Reader reader=null;
       BorrowDomain bd=null;

       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
       Object o = ois.readObject();
       HashSet<BorrowDomain> newArr=(HashSet<BorrowDomain>) o;
       int num=0;
       ois.close();
       for (BorrowDomain rr:newArr){
           if(rr.getReader().getReaderid().equals(id)){
               num++;
               bd=rr;
//               reader=rr.getReader();
               for(Book books:rr.getBooks()) {
                   System.out.println("学号:" + rr.getReader().getReaderid() + " 姓名:" + rr.getReader().getName() + " 类别:" + rr.getReader().getType().getTypename() + "  借阅书名:" + books.getBookname() + " 借阅时间:" + rr.getBorrowdate());
               }
           }
       }
       if(num==0) {
           System.out.println("未找到该学生的借阅信息!");
       }
       if(bd!=null){
           System.out.println("请输入要归还的图书名称");
           String returnbookname=TSUtility.readKeyBoard(20,false);
           int numnum=0;
           for(Book boo:bd.getBooks()){
               if(boo.getBookname().equals(returnbookname)){
                   numnum++;

                   ObjectInputStream oiss = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Return.txt"));
                   Object q = oiss.readObject();
                   HashSet<ReturnBook> hs=(HashSet<ReturnBook>) q;
                   oiss.close();
                   for(ReturnBook rb:hs){
                       returnBookHashSet.add(rb);
                   }

                 /*  Date date = new Date();
                   SimpleDateFormat sad= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                   String back=sad.format(date);*/
                   System.out.println("请输入归还日期(yyyy年MM月dd日)");
                   String back=TSUtility.readKeyBoard(21,false);
                   SimpleDateFormat sad= new SimpleDateFormat("yyyy年MM月dd日");
                   Date date=sad.parse(back);

                   String borrowtime=bd.getBorrowdate();
                   SimpleDateFormat spd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                   Date dd = spd.parse(borrowtime);
                   long time=(date.getTime()-dd.getTime())/(1000*3600*24);
                   int givemoney=0;
                   System.out.println(time);
                   if(time>bd.getReader().getType().getLimit()){
                       Properties prp = new Properties();
                       BufferedInputStream bis = new BufferedInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Money.txt"));
                       prp.load(bis);
                       bis.close();
                       givemoney=(int)(time-bd.getReader().getType().getLimit())*Integer.parseInt(prp.getProperty(bd.getReader().getType().getTypename()));
                       System.out.println(givemoney);
                   }
                   ReturnBook returnBook = new ReturnBook(bd, back, givemoney,boo.getBookname());
                   returnBookHashSet.add(returnBook);
                   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("TheTeam_2\\src\\TypeTxt\\Return.txt"));
                   oos.writeObject(returnBookHashSet);
                   oos.close();

                   HashSet<Book> returnBookList = new HashSet<>();
                   ObjectInputStream readIO = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
                   Object o1 = readIO.readObject();
                   HashSet<BorrowDomain> hb=(HashSet<BorrowDomain>) o1;
                   readIO.close();
                   for(BorrowDomain b:hb){
                       if(b.getReader().equals(bd.getReader())) {
                           if(b.getBooks().size()>1) {
                               for (Book xb : b.getBooks()) {
                                   if (!xb.getBookname().equals(boo.getBookname())) {
                                       returnBookList.add(xb);
                                   }
                               }
                               hashSets.add(new BorrowDomain(b.getReader(), returnBookList, b.getBorrowdate()));
                           }
                       }
                       else {
                           hashSets.add(b);
                       }
                   }
                   ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("TheTeam_2\\src\\TypeTxt\\Borrow.txt"));
                   oos1.writeObject(hashSets);
                   oos1.close();


                   System.out.println("归还成功");

                   BufferedWriter bw = new BufferedWriter(new FileWriter("TheTeam_2\\src\\Log.txt",true));
                   Date date3 = new Date();
                   SimpleDateFormat spd3= new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
                   String dengluTime=spd.format(date3);
                   bw.write(dengluTime+"   读者:"+bd.getReader().getName()+" 归还了书本:"+returnbookname);
                   bw.newLine();
                   bw.flush();
                   bw.close();

               }
           }
           if(numnum==0){
               System.out.println("该图书不存在");
           }
           TSUtility.readReturn();
       }

   }
   public void lookReturn() throws IOException, ClassNotFoundException {
       ObjectInputStream ois = new ObjectInputStream(new FileInputStream("TheTeam_2\\src\\TypeTxt\\Return.txt"));
       Object o = ois.readObject();
       HashSet<ReturnBook> hs = (HashSet<ReturnBook>) o;
       ois.close();
       for (ReturnBook rb:hs){
           System.out.println("学生姓名:"+rb.getBorrow().getReader().getName()+" 借阅图书:"+rb.getBook()+" 借阅日期:"+rb.getBorrow().getBorrowdate()+" 归还日期:"+rb.getReturnbook()+" 逾期支付:"+rb.getMoeny());
       }
       TSUtility.readReturn();
   }

    public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
        new BorrrowBookService().returnBook();
    }

}

主界面

package View;

import Service.*;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TheMainView {
    public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
        System.out.println("******************************");
        System.out.println("*                            *");
        System.out.println("******* 欢迎来到图书管理系统*******");
        System.out.println("*                            *");
        System.out.println("******************************");
        System.out.println("*                            *");
        System.out.println("*                            *");
        System.out.println("*     ——请您先进行登录——        *");

        String UseName=null;
        UsersService use = new UsersService();
        for(int i=1;i<=3;i++){
            boolean s=use.check();
            if(s==true){
                UseName=use.getname();
                BufferedWriter bw = new BufferedWriter(new FileWriter("TheTeam_2\\src\\Log.txt",true));
                Date date = new Date();
                SimpleDateFormat  spd= new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
                String dengluTime=spd.format(date);
                bw.write(dengluTime+"   用户:"+UseName+" 进入了图书管理系统");
                bw.newLine();
                bw.flush();
                bw.close();
               break;
            }
            else {
                if(i==3){
                    System.out.println("次数耗尽");
                    System.exit(0);
                }
                System.out.println("你还剩"+(3-i)+"次机会");
            }
        }

        boolean flag=true;
        while (flag){
            System.out.println("1.读者信息管理");
            System.out.println("2.图书信息管理");
            System.out.println("3.图书借阅信息管理");
            System.out.println("4.基本信息维护");
            System.out.println("5.用户管理");
            System.out.println("6.退出");
            int i=TSUtility.readInt();
            if(i==1){
                new ReaderView().meth();
            }
            else if(i==2){
                new BookView().view();
            }
            else if(i==4){
                boolean flag2=true;
                while (flag2) {
                    System.out.println("1.读者类型管理");
                    System.out.println("2.图书类型管理");
                    System.out.println("3.罚金管理");
                    System.out.println("4.退出");
                    int i2 = TSUtility.readInt();
                    if (i2 == 1) {
                        new ReaderTypeView().view();
                    }
                    else if (i2 == 2) {
                        new BookTypeView().view();
                    }
                    else if (i2 == 4) {
                        flag2=false;
                    }
                    else if(i2==3){
                        boolean f=true;
                        while (f) {
                            System.out.println("1.罚金修改");
                            System.out.println("2.罚金查看");
                            System.out.println("3.退出");
                            int ii=TSUtility.readInt();
                            if(ii==1){
                                new MoneyService().changeMoney();
                            }
                            else if(ii==2){
                                new MoneyService().look();
                            }
                            else if(ii==3){
                                f=false;
                            }
                            else {
                                System.out.println("选择有误");
                            }
                        }
                    }
                    else {
                        System.out.println("你的输入有误,请重新输入");
                    }
                }
            }
            else if(i==3){
                new BorrowView().view();
            }
            else if(i==5){
                boolean ss=true;
                while (ss) {
                    System.out.println("1.用户增加");
                    System.out.println("2.用户删除");
                    System.out.println("3.查看用户");
                    System.out.println("4.修改用户信息");
                    System.out.println("5.退出");
                    int qq = TSUtility.readInt();
                    if (qq == 1) {
                        use.addUser();
                    }
                    else if (qq == 2) {
                       use.delecteUsers();
                    }
                    else if (qq == 3) {
                        use.getAllUser();
                    }
                    else if(qq==4){
                        use.changeUser();
                    }
                    else if(qq==5){
                        ss=false;
                    }
                    else {
                        System.out.println("你的输入有误,请重新输入");
                    }
                }
            }
            else if(i==6){
                BufferedWriter bw = new BufferedWriter(new FileWriter("TheTeam_2\\src\\Log.txt",true));
                Date date = new Date();
                SimpleDateFormat  spd= new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss");
                String dengluTime=spd.format(date);
                bw.write("-------------------------------");
                bw.newLine();
                bw.flush();
                bw.close();

                flag=false;
            }
            else {
                System.out.println("输入有误,请重新输入");
            }
        }
    }
}

标签:HashSet,管理系统,println,new,借阅,public,图书
来源: https://blog.csdn.net/weixin_54226473/article/details/121628352