【图书管理系统】(超详细附源码用java实现)

1.摘要

图书管理贯彻在我们生活当中,使用java语言中的异常处理,抽象类多态,继承,封装,接口等知识点,来综合性的设计与实现出一个简易的图书管理系统。

2.设计思想

1.首先我们需要区分出管理员与普通用户,不同的用户所展示的界面不同,利用继承和多态可以实现这一思路;
2.我们需要实现管理员能够添加书籍,查找书籍,删除书籍,显示书籍及退出系统。
3.普通用户实现查找书籍,借阅书籍,归还书籍及退出系统。
4.删除及添加书籍我们运用数组的思想,进行遍历。
5.将用户和管理员的每个操作分别封装成一个类,方便继承。

3.项目分布

在这里插入图片描述

图书类

Book类: 定义了书的一些属性(书名,作者,价格,分类,状态)并且写了属性的get、set方法

package book;

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed==true)?"已经借出":"未被借出")+
                //", isBorrowed=" + isBorrowed +
                '}';
    }
}

书架类

public class BookList {
    private Book[] books;
    private int userSize;
    public BookList(){
        this.books=new Book[10];
        //放书
        this.books[0]=new Book("三国演义","罗贯中",18,"小说");
        this.books[1]=new Book("红楼梦","曹雪芹",19,"小说");
        this.books[2]=new Book("西游记","吴承恩",22,"小说");
       this.userSize=3;
    }

    public int getUserSize() {
        return userSize;
    }

    public void setUserSize(int userSize) {
        this.userSize = userSize;
    }
    public Book getBook(int pos){
        return books[pos];
    }
    public void setBook(int pos,Book book){
        books[pos]=book;
    }

    public Book[] getBooks() {
        return books;
    }
}

操作类

增加图书

public class AddOperation implements IOPeration{
    public  void work(BookList bookList) {
        System.out.println("新增图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = scanner.nextLine();
        System.out.println("请输入作者");
        String author = scanner.nextLine();
        System.out.println("请输入类型");
        String type = scanner.nextLine();
        System.out.println("请输入价格");
        int price = scanner.nextInt();
        Book book = new Book(name, author, price,type);
        //检查数组当中有没有这本书
        int currentSize=bookList.getUserSize();
        for (int i = 0; i < currentSize; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("有这本书,不进行存放");
                return;
            }
        }
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架满了");
        }else{
            bookList.setBook(currentSize,book);
            bookList.setUserSize(currentSize+1);
        }
    }

}

借阅图书:

public class BorrowOperation implements IOPeration{
        public  void work(BookList bookList){
            System.out.println("借阅图书!");
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入你要借阅的书籍");
            String name=scanner.nextLine();

            int currentSize =bookList.getUserSize();
            for (int i = 0; i <currentSize ; i++) {
                Book book=bookList.getBook(i);
                if(book.getName().equals(name)){
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                    System.out.println(book);
                    return;
                }
            }
            System.out.println("借阅的图书不存在");
        }
    }

删除图书:

public class DelOperation implements IOPeration{
        public  void work(BookList bookList){
            System.out.println("删除图书!");
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入要删除的图书");
            String name=scanner.nextLine();
            int pos=-1;
            int currentSize=bookList.getUserSize();
            int i=0;
            for (; i < currentSize; i++) {
                Book book=bookList.getBook(i);
                if(book.getName().equals(name)){
                    pos=i;
                    break;
                }
            }
       if(i==currentSize){
    System.out.println("没有你要删除的图书!");
    return;
}
       //开始删除
            int j=pos;
            for ( j = pos; j <currentSize-1 ; j++) {
                Book book=bookList.getBook(j+1);
                bookList.setBook(j,book);
            }
            bookList.setBook(j,null);
            bookList.setUserSize(currentSize-1);
        }
    }

退出系统

public class ExitOperation  implements IOPeration{
    public  void work(BookList bookList){
        System.out.println("退出系统!");
        System.exit(0);
    }
}

查找图书:

public class FindOperation implements IOPeration {
    public void work(BookList bookList) {
        System.out.println("查找图书!");
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine();
    int currentSize = bookList.getUserSize();
        for(int i = 0;i<currentSize;i++){
        Book book = bookList.getBook(i);
        if (book.getName().equals(name)) {
            System.out.println("找到了这本书,信息如下:");
            System.out.println(book);
            return;
        }
    }
        System.out.println("没有找到");
}}

接口

package operation;

import book.BookList;

public interface IOPeration {
    void work(BookList bookList);


}

归还图书

public class ReturnOperation  implements IOPeration{
    public  void work(BookList bookList){

        System.out.println("归还图书!");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你要归还的书籍");
        String name=scanner.nextLine();

        int currentSize =bookList.getUserSize();
        for (int i = 0; i <currentSize ; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("归还成功!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("归还的图书不存在");
    }
}

打印图书

public class ShowOperation   implements IOPeration {
    public void work(BookList bookList) {
        System.out.println("打印图书!");
        int currentSize = bookList.getUserSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

管理员类

public class AdminUser extends User {
    public AdminUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()};
    }

    public int menu() {
        System.out.println("**********管理员**********");
        System.out.println("*******1.查找图书**********");
        System.out.println("*******2.新增图书*********");
        System.out.println("*******3.删除图书*********");
        System.out.println("*******4.显示图书*********");
        System.out.println("*******0.退出系统*********");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作");
        int choice = scanner.nextInt();

        return choice;
    }
}

Main类

public class Main {
    public static User login() {
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        System.out.println("请输入你的身份:1:管理员  2:普通用户 ");
        int choice = scanner.nextInt();

        if (choice == 1) {
            //管理员
            return new AdminUser(name);//向上转型
        } else {//普通用户
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        //user指向哪个对象 指向
        User user = login();
        while (true) {
            int choice = user.menu();
            System.out.println("choice:" + choice);
            //根据choice 来决定调用哪个方法
            user.doOperation(choice, bookList);
        }
    }

普通用户类

public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }
    public int menu(){
        System.out.println("*************************");
        System.out.println("*******1.查找图书**********");
        System.out.println("*******2.借阅图书*********");
        System.out.println("*******3.归还图书*********");
        System.out.println("*******0.退出系统*********");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作");
        int choice = scanner.nextInt();

        return choice;
    }
}

抽象类

public abstract class User {
    protected String name;

protected IOPeration[] ioPerations;

    public User(String name) {
        this.name = name;
    }
 public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

4.运行截图:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 21
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
图书管理系统总体上分为前台页面显示和后台管理。 前台页面(即本书图书管理系统的首页)实现了公告的显示,图书查询,留言建议三大主要功能,有读者规则查看功能,师生们可以看到图书管理人员发布的最新公告信息,并可以查询自己感兴趣的图书,查看留言提议,用户登录后还有个人资料修改、个人借阅信息查询、个人违章信息查询等功能,也可以给学校的图书管理人员留言提议。 后台的页面则集成了图书管理中所需的功能,分成图书管理人员和系统管理人员,平时管理人员的工作都是在后台中完成的。前台是为了师生显示的。相对应的后台是针对学校图书管理人员,后台的页面都加密,如果不正常登录是进入不了后台管理页面的,后台图书管理人员功能包括:借阅图书、归还图书、借书记录显示与查找、还书记录显示与查找、公告增删改查;后台系统管理员功能包括:对书籍的增删改查、对书籍分类的增删改、对借阅证的增删改查、对借阅信息的显示与查找、对借阅规则的增删改、对图书管理员的增删改、对近期借阅书籍数量的折线图显示,以及对这些表格数据进行Excel表格的输出 共包含三个大模块:用户、图书管理员、系统管理员 一、用户模块 查看公告:能看到图书管理员发布的公告信息。 图书查看:分页显示,能看到图书总览,能通过图书编号、图书名称、作者、存放位置、图书描述进行模糊查询 个人违章信息(登录后):个人历史违章信息与查询 图书馆读者留言(登录后):对图书馆的建议,或对书籍的评价 查看借阅规则:不同的规则有不同的借阅限定数量、限定时间、期费用 个人信息(登录后):对个人信息的查看与登录密码的修改 个人借阅信息(登录后):对个人借阅记录的分页查看与查询 二、图书管理员模块 借还图书:帮助用户借阅图书,并判断符不符合借阅规则、返还图书时判断有没有期,期则输出期费用再归还 借阅报表:分页显示所有借阅记录、能进行查询和输出结果到excel公告管理:能添加编辑和删除公告 还书报表:分页显示所有未还的书籍,能通过借阅证号、图书编号、借阅日期、截止日期等条件进行模糊查询 个人信息修改:修改个人信息、密码等 三、系统管理员模块 书籍管理:分页,新添图书,设置图书各种信息,编辑删除,通过编号、书名、作者、描述等进行搜索,输出结果到excel,并且能查看该书籍被谁借阅过。 分类管理:分页显示,增删改书籍分类,书籍分类方便查阅整理 借阅证管理:分页显示所有借阅证,增删改查用户借阅证,查看该借阅证所有的借阅记录 借阅规则管理:能显示、编辑、删除所有借阅规则,借阅规则将决定该图书证能借阅什么图书馆的图书,能借多少本,能借多久以及期每天的费用。 借阅信息查询:分页显示,能条件查询期没还的,根据编号、借阅证号、书籍编号、借阅日期等进行模糊查询,输出结果到excel等,系统管理员只能查询,不能增删改借阅信息,这个操作由图书管理员操作。 图书管理员管理:对图书管理员进行增删改操作,但是不能修改原来的账号 图书借阅统计:通过折线图将图书馆近30天的每天借阅书籍数量直观的显示出来。 sql表 共有十张数据库表,书籍表、借书记录表、留言表、系统管理员表、借阅证表、图书管理管理员表书籍分类表、图书馆表、借阅规则表、公告表 书籍表 编号、姓名、作者、存放的图书馆、分类编号、存放位置、是否借出、书籍描述 借书记录表 编号、借阅证编号、书籍编号、借书日期、限制日期、归还日期、违规描述、处理人编号 留言表 编号、借阅证编号、留言内容、留言日期 系统管理员表 账号、密码 借阅证表 编号、密码、借阅者姓名、规则编号、状态(丢失、可用) 图书管理员表 编号、姓名、账号、密码、邮箱 书籍分类表 编号、分类名、分类描述 图书馆表 编号、图书馆名、描述 规则表 编号、限制借阅数量、限制借阅天数、图书期每天费用 公告表 编号、标题、公告内容、发布日期
这是一个比较复杂的数据库 包含图书管理 借书还书 学生管理 老师管理 和数据连接的相关应用 代码有详细的解释 压缩包里面也有 数据库的文件 代码里设置的数据库 用户是 sa 密码是 123456 请使用的时候做相关的修改 下面给出 一部分的代码 请继续关注本资源的发布 会后面有很多实用的代码上传 using System.Windows.Forms; namespace LibraryMis { public class DatabaseAccess { /* 声明成员变量,这样这个类中的所有方法就可是使用这些变量了 */ private SqlConnection myConnection; private SqlCommand myCommand; private SqlDataAdapter myDataAdapter; private DataSet mySet = new DataSet(); /* 写该类的构造方法,该方法名要跟类名相同,无返回值 * 当new这个类时就会执行这个构造方法 */ public DatabaseAccess() { /* 获得保存连接字符串的文件名及路径 */ //获得应用程序路径 string exePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //根据路径和文件名构建FileInfo对象 string fileName = exePath + "connectionString.txt"; //建立FileInfo对象 FileInfo f = new FileInfo(fileName); //判断该文件是否存在 if (f.Exists)//文件存在 { //获得文件内容即存在文件中的连接字符串 //打开文件,获得读文件的数据流对象 StreamReader sr = f.OpenText(); //读文件到变量中 string connectionString = sr.ReadToEnd(); //关闭流 sr.Close(); //由读出的连接字符串创建Connection对象 myConnection = new SqlConnection(connectionString); //由Connection对象创建Command对象 myCommand = myConnection.CreateCommand(); //创建DataAdapter对象 myDataAdapter = new SqlDataAdapter(); myDataAdapter.SelectCommand = myCommand; //创建CommandBuilder对象 SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter); //尝试是否能够打开连接 try { myConnection.Open(); } catch (Exception ex) //打开连接出错,可能是连接字符串有问题,这里调用数据库访问设置窗体来重新设置服务器名和数据库名 { MessageBox.Show("连接不到数据库LibraryMis,请在“数据库访问设置窗体中对数据库访问进行正确的设置”" + ",取消登录后重新启动图书馆管理系统!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning ); //创建 数据库访问设置窗体,并显示 FormSetDatabase fmsd = new FormSetDatabase(); fmsd.ShowDialog(); } finally { try { myConnection.Close(); } catch (Exception ex) { } } return; } else //文件不存在 { //设置默认的连接字符串 string connectionString = "server=.;database=LibraryMis;uid=sa;pwd=123456"; //把这个字符串写入文件 StreamWriter sw = new StreamWriter(fileName); sw.Write(connectionString); sw.Close(); MessageBox.Show("文件" + fileName + "不存在,已创建该文件,请重新启动图书馆管理系统","警告",MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } /*创建查询的方法,返回数据集对象DataSet,参数SelectString表示查询的Sql语句,TableName表示要查询的表名*/ public DataSet FillDataSet(string SelectString, string TableName) { myDataAdapter.SelectCommand.CommandText = SelectString;//设置查询的Sql语句 myDataAdapter.Fill(mySet,TableName); return mySet; } /*执行插入,更新,修改的操作,参数CommandString表示Sql语句*/ public void ExeCommand(string CommandString) { myCommand.CommandText = CommandString; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning); } finally { myConnection.Close(); } } /*执行存储过程的方法,参数为Command对象*/ public void ExeStoreProcedure(SqlCommand command) { command.Connection = myConnection; myCommand = command; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { myConnection.Close(); } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值