当前位置: 首页 > news >正文

图书馆管理系统(Java编写,思路及源代码)

        如果你已经学习了Java的三大特性(封装、继承、多态)及接口,那么你就可以尝试这个编写这个图书馆管理系统小项目,这个小项目主要的作用还是用来巩固Java的三大特性及接口的学习。(我前边的几个博客中也详细介绍了三大特性及接口:Java封装_是小辰的博客-CSDN博客   Java继承_是小辰的博客-CSDN博客   Java多态_是小辰的博客-CSDN博客 Java 接口_是小辰的博客-CSDN博客)

        当这个图书馆管理系统运行的时候,首先会进入图书管理系统,然后要求使用者首先输入自己的名字,输入完成后选择进入系统的身份,有管理员和普通用户两种身份,如果身份是管理员要有增添图书,删除图书,查找图书,展示图书和退出系统五种功能;而是普通用户则要求有查找图书,借阅图书,归还图书和退出系统四种功能。

        为了使代码清晰简洁,我们可以把不同功能的板块分成不同包,例如:把与书相关的都在book包中实现,把与身份有关的都在user包中实现,把与功能有关的都在function包中实现。而我们还需要一个程序入口,那么我们就可以再创建一个Main类。

        如图:

        

一、book包

   book包中我们可以实现创建图书对象及信息,管理图书,所以我们可以分为Book类和Book List类。

             

    Book类

        我们发现不论是管理员还是普通用户,他们除了退出系统之外的功能都是对书进行操作;所以我们首先创建一个Book类,在Book类使书具有String类型的书名(name),String类型的作者(author),int类型的价钱(price),String类型的书的类型(type)和Boolean类型的借阅状态(isBorrow)五种属性,在Book类中需要重写toString方法,需要注意的时要根据书的isBorrow的状态来打印已借出(true)或者未借出(false)。

package book;

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

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

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

    public Book() {

    }

    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 isBorrow() {
        return isBorrow;
    }

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }
}

     BookList类

        BookList类主要事为了方便操作Book类,我们可以把Book类的对象保存在Booklist类的Book类型的books数组中,我们使用实例代码块来创建几个Book类的对象并且存放在books数组中,为了方便管理,我们需要设置一个书的数量的成员变量并且仅对本类可见,所以我们是使用private关键字对其进行修饰,并且为其设置get和set方法。

     

package book;

public class BookList {
    private Book[] books = new Book[10];
    private int usedSize;//记录当前书架上有几本书

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",20,"小说");
        books[1] = new Book("西游记","吴承恩",30,"小说");
        books[2] = new Book("水浒传","施耐庵",25,"小说");
        this.usedSize = 3;
    }

    public Book getBook(int pos) {
        return books[pos];
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }
}

二、function

        funtion包中实现书籍的增加、删除、查找、展示、借阅、归还以及系统的退出,这些实现统一用一个接口完成,所以:

        

   IFunction接口

Ifunction接口是所有功能类的公共规范,所以它应该具有抽象 方法work,而且这个方法的操作对象应该是Booklist类的对象;

package function;

public interface IFunction {
    void work(BookList bookList);
}

    AddFunction类

package function;

public class AddFunction implements IFunction{
        @Override
        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.getUsedSize();
            bookList.setBooks(currentSize,book);
            bookList.setUsedSize(currentSize+1);
            System.out.println("新增图书成功!!");
            //scanner.close();
        }
}

        DeleteFunction类

package function;


public class DeleteFunction implements IFunction{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        //1、找到你要删除的图书是否存在?
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书名字:");
        String name = scanner.nextLine();//水浒传

        int currentSize = bookList.getUsedSize();
        int delIndex = -1;
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                delIndex = i;
                break;
            }
        }

        if (i == currentSize) {
            System.out.println("没有你删除的这本书!");
            return;
        }

        for (int j = delIndex; j < currentSize - 1; j++) {
            //[j] = [j+1]
            Book book = bookList.getBook(j + 1);
            bookList.setBooks(j, book);
        }

        bookList.setBooks(currentSize - 1, null);

        bookList.setUsedSize(currentSize - 1);

        System.out.println("删除图书成功!");
    }
}

        BorrowFunction类

package function;

public class BorrowFunction implements IFunction{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要借阅的图书的名字:");
        String name = scanner.nextLine();//水浒传

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                if(book.isBorrowed()) {
                    System.out.println("该书已经被借出!");
                }else{
                    book.setBorrowed(true);
                }
                return;
            }
        }
        System.out.println("没有你要借阅的图书!");
    }
}

        FindFunction类

package function;

public class FindFunction implements IFunction{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要查找的图书姓名:");
        String name = scanner.nextLine();//水浒传

        int currentSize = bookList.getUsedSize();
        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("没有你要查找的这本书!");
    }
}

        ReturnFunction类

package function;

public class ReturnFunction implements IFunction{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要归还的图书的名字:");
        String name = scanner.nextLine();//水浒传

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                book.setBorrowed(false);
                return;
            }
        }
        System.out.println("没有你要归还的图书!");
    }
}

        ShowFunction类

package function;

public class ShowFunction implements IFunction{
    @Override
    public void work(BookList bookList) {
        System.out.println("打印所有图书!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

        ExitFunction类

package function;

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

三、user包

        用户分为两种,所以:

        

         User类

User类应该有保存功能类的Ifunction类型的数组、抽象方法main方法、根据用户选择对booklist对象进行操作的dofunction方法的功能。

package user;

import function.IFunction;

public abstract class User {
    protected String name;
    public IFunction[] iFunctions;//这里我没有分配空间

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doOperation(int choice, BookList bookList){
        this.iFunctions[choice].work(bookList);
    }
}

             AdminUser类

此类继承User类,需要根据对应的功能重写main方法,并在构造方法中对其Ifunction数组做出赋值选择其需要实现的功能。

package user;
import java.util.Scanner;
import function.*;

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iFunctions = new IFunction[]{
                new ExitFunction(),
                new FindFunction(),
                new AddFunction(),
                new DeleteFunction(),
                new ShowFunction()
        };
    }

    public int menu() {
        System.out.println("管理员菜单!");
        System.out.println("****************************");
        System.out.println("hello " + this.name +" 欢迎来到图书小练习");
        System.out.println("1. 查找图书");
        System.out.println("2. 新增图书");
        System.out.println("3. 删除图书");
        System.out.println("4. 显示图书");
        System.out.println("0. 退出系统!");
        System.out.println("****************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

            NormalUser类

package user;
import java.util.Scanner;
import function.*;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iFunctions = new IFunction[] {
                new ExitFunction(),
                new FindFunction(),
                new BorrowFunction(),
                new ReturnFunction()
        };
    }


    public int menu() {
        System.out.println("普通用户的菜单!");
        System.out.println("****************************");
        System.out.println("hello " + this.name +" 欢迎来到图书小练习");
        System.out.println("1. 查找图书");
        System.out.println("2. 借阅图书");
        System.out.println("3. 归还图书");
        System.out.println("0. 退出系统!");
        System.out.println("****************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 算法练习:动态规划(最长公共子串问题)
  • 【lm401】解决malloc动态申请内存时内存不足的问题
  • 【Python】ChineseCalendar包简介
  • 前端八股——JS高级学习
  • 【CSS系列】第二章 · CSS选择器
  • vue尚品汇商城项目-day04【27.分页器静态组件(难点)】
  • JavaScript技术干货第二弹,有需自取!
  • 华为OD机试用java实现 -【关联子串】
  • 走过最长的路是ChatGPT的套路,信过最真的话是Adobe的Firefly
  • 基于蓄电池进行调峰和频率调节研究【超线性增益的联合优化】(Matlab代码实现)
  • 华为OD机试用Python实现 -【打折买水果】
  • 深入浅出 Fast DDS网络协议(入门篇)
  • 【简陋Web应用2】人脸检测——基于Flask和PaddleHub
  • 基于springboot实现数码论坛系统设计与实现演示【附项目源码+论文说明】
  • 深入理解NLP中的文本匹配任务
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【跃迁之路】【477天】刻意练习系列236(2018.05.28)
  • Angular 响应式表单之下拉框
  • express.js的介绍及使用
  • flask接收请求并推入栈
  • Github访问慢解决办法
  • JavaScript函数式编程(一)
  • laravel with 查询列表限制条数
  • 测试开发系类之接口自动化测试
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 构建二叉树进行数值数组的去重及优化
  • 聊聊redis的数据结构的应用
  • 爬虫模拟登陆 SegmentFault
  • 前端存储 - localStorage
  • 驱动程序原理
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 设计模式走一遍---观察者模式
  • 我的业余项目总结
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 职业生涯 一个六年开发经验的女程序员的心声。
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 阿里云服务器如何修改远程端口?
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • 整理一些计算机基础知识!
  • ​LeetCode解法汇总2182. 构造限制重复的字符串
  • #1014 : Trie树
  • $$$$GB2312-80区位编码表$$$$
  • $GOPATH/go.mod exists but should not goland
  • (1) caustics\
  • (3)nginx 配置(nginx.conf)
  • (arch)linux 转换文件编码格式
  • (C)一些题4
  • (ISPRS,2021)具有遥感知识图谱的鲁棒深度对齐网络用于零样本和广义零样本遥感图像场景分类
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • (计算机网络)物理层
  • (每日一问)基础知识:堆与栈的区别
  • (十五)使用Nexus创建Maven私服
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (四)鸿鹄云架构一服务注册中心
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)