• Java 图书管理系统


    💖欢迎来阅读子豪的博客(Java实操篇🍱)
    👉有什么宝贵的意见或建议可以在留言区留言 💬
    👽欢迎 素质三连 点赞+关注+收藏💯
    🧑‍🚀码云仓库: 补集王子的代码仓库🧑‍🔧


    项目实现
    创几个包:用来组织类(分类)
    在这里插入图片描述

    🍔1. 每本书数据【book】

    Book类

    public class Book {}
    
    • 1

    设置字段(书的属性)

    public class Book {
        private String name;     //书名
        private String author;   //作者
        private int price;       //价格
        private String type;     //类型
        private boolean isBorrowed;//是否被借出  默认false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    提供构造

    实例化对象,设置几个属性,因为字段是private修饰的就给每属性get set方法
    在加个构造和toString方法

    //构造
        public Book(String name, String author, int price, String type) {
            this.name = name;
            this.author = author;
            this.price = price;
            this.type = type;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    get+set方法,便于操作数据

       //get+set
        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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    重写toString方法,便于打印显示,(借出处略加修改)

       //toString
        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    ", author='" + author + '\'' +
                    ", price=" + price +
                    ", type='" + type + '\'' +
                    ((isBorrowed==true)?"已经借出":"未借出") +
                    '}';
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    🍟2. 书架储存书【bookList】

    BookList类

    public class BookList{}
    
    • 1

    设置书架大小

        //对多放十本
        private Book[] books = new Book[3];
        private int usedSize ;  //实时记录 目前有几本书
    
    • 1
    • 2
    • 3

    初始化书架,类构造方法

        public BookList() {
            books[0] = new Book("西游记","吴承恩",10,"小说");
            books[1] = new Book("三国演义","施耐庵",11,"小说");
            books[2] = new Book("红楼梦","曹雪芹",12,"小说");
            usedSize = 3;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    getBook,拿出pos处的书

    //拿出pos处的书
        public Book getBook(int pos){
            return books[pos];
        }
    
    • 1
    • 2
    • 3
    • 4

    setBook,给pos下标放一本书

    // 给pos下标放一本书
        public void setBook(int pos, Book book){
            books[pos] = book;
        }
    
    • 1
    • 2
    • 3
    • 4

    getUesSize,setUesSize,获取实时书本数,用于修改

    //获取实时书本数,用于修改
        public int setUesSize(int size){
            return (usedSize = size);
        }
        public int getUesSize(){
            return this.usedSize;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    🍕3. 设置两个角色【user】

    管理员【AdmainUser】

    public class AdmainUser extends User{}
    
    • 1

    构造方法,提供构造方法帮助父类构造

    //提供构造方法帮助父类构造
        public AdmainUser(String name) {
            super(name);
            //在new管理人员这个对象时,就把这些功能一并写入
            this.iOperations = new IOperation[]{
                    new ExitOperation(),//0
                    new FindOperation(),
                    new AddOperation(),
                    new DelOperation(),
                    new DisplayOperation()
            };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    菜单

        //设置一个开始菜单
        public int menu (){
            System.out.println("hello"+this.name+"欢迎使用BMS!");
            System.out.println("1.查找图书");
            System.out.println("2.增添图书");
            System.out.println("3.删除图书");
            System.out.println("4.显示图书");
            System.out.println("0.退出BNS");
            Scanner scanner = new Scanner(System.in);
            return scanner.nextInt();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    普通用户【NormalUser】

    public class NormalUser extends User{}
    
    • 1

    菜单【menu】

       //设置一个开始菜单
        public int menu (){
            System.out.println("hello"+this.name+"欢迎使用BMS!");
    
            System.out.println("1.查找图书");
            System.out.println("2.借阅图书");
            System.out.println("3.归还图书");
            System.out.println("0.退出BNS");
            Scanner scanner = new Scanner(System.in);
            return scanner.nextInt();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    构造方法,提供构造方法帮助父类构造

    //提供构造方法帮助父类构造
        public NormalUser(String name) {
            super(name);
            //在new一般人员这个对象时,就把这些功能一并写入
            this.iOperations = new IOperation[]{
                    new ExitOperation(),//0
                    new FindOperation(),
                    new BorrowOperation(),
                    new ReturnOperation()
            };
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    🌭4. 每个角色对应功能列表不一样

    身份不同,拥有的功能不一样

    管理员一般用户
    1.查找图书1.查找图书
    2.增添图书2.借阅图书
    3.删除图书3.归还图书
    4.显示图书
    0.退出BNS0.退出BNS

    🍜5. 实现每一具体功能【operation】

    接口【interface】

    功能标准化【IOperation】

    public interface IOperation {
        //实现功能
        public abstract void work(BookList bookList);
    }
    
    • 1
    • 2
    • 3
    • 4

    增添图书【AddOperation】

    1. implements实现接口
    2. 录入书本信息
    3. 用getUesSize(),在书架的size位置插入这本书

    Tips:

    要先输入字符串String,再输入整数int!
    先输入整数再输字符串的话回车会被读进去

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class AddOperation implements IOperation{
        @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 newbook = new Book(name,author,price,type);
            bookList.setBook(bookList.getUesSize(),newbook);
            bookList.setUesSize(bookList.getUesSize()+1);   //更新数量
    
            System.out.println("新增书记成功!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    借阅图书【BorrowOperation】

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class BorrowOperation implements IOperation{
    
        @Override
        public void work(BookList bookList) {
            System.out.println("借出图书!");
    
            System.out.print("请输入借阅图书的名字:");
            int currentSize = bookList.getUesSize();
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            for (int i = 0 ; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if (book.getName().equals(name)) {
                    System.out.println("找到了!");
                    book.setBorrowed(true); //改状态
                    System.out.println("借阅成功!");
                    return;
                }
            }
            System.out.println("没有这本书!");
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    删除图书【DelOperation】

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     *覆盖
     */
    public class DelOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("删除图书!");
            System.out.print("请输入图书的名字:");
            int currentSize = bookList.getUesSize();
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            int index = -1;//存下标
            int i = 0;
            for ( ; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if (book.getName().equals(name)) {
                    System.out.println("找到了!");
                    index = i;
                    System.out.println(book);
                    System.out.println();
                }
            }
            if (i >= currentSize) {
                System.out.println("找不到此书");
                return;
            }
            //直接覆盖
            for (int j = index; j < currentSize-1; i++) {
                bookList.setBook(j,bookList.getBook(j +1));
            }
            bookList.setUesSize(bookList.getUesSize()-1);   //更新数量
            bookList.setBook(currentSize-1,null);  //置为空   相当于free
            System.out.println("删除成功!");
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    显示图书【DisplayOperation】

    package operation;
    
    import book.BookList;
    
    public class DisplayOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("显示图书!");
            int currentSize = bookList.getUesSize();
            for (int i = 0; i < currentSize; i++) {
                System.out.println(bookList.getBook(i));
            }
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    归还图书【ReturnOperation】

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class ReturnOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("归还图书!");
    
            System.out.print("请输入归还图书的名字:");
            int currentSize = bookList.getUesSize();
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            for (int i = 0 ; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if (book.getName().equals(name)) {
                    System.out.println("找到了!");
                    book.setBorrowed(true); //改状态
                    System.out.println("归还成功!");
                    return;
                }
            }
            System.out.println("没有这本书!");
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    查找图书【FindOperation】

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class FindOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.print("请输入图书的名字:");
            int currentSize = bookList.getUesSize();
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            for (int i = 0 ; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if (book.getName().equals(name)) {
                    System.out.println("找到了!");
                    System.out.println(book);
                    System.out.println();
                    return;
                }
            }
            System.out.println("没有这本书!");
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    退出系统【ExitOperation】

    package operation;
    
    import book.BookList;
    
    public class ExitOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("退出系统!");
            System.exit(0); // 状态码  零代表正常结束,负数异常结束
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    🍚6. 主函数 【Main】

    整理所有功能

    import book.Book;
    import book.BookList;
    import user.AdmainUser;
    import user.NormalUser;
    import user.User;
    
    import java.util.Scanner;
    
    //登录
    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 choise = scanner.nextInt();
            if(choise == 1){
                return new AdmainUser(name);
            }else {
                return new NormalUser(name);
            }
        }
        public static void main(String[] args) {
            //整合!
            BookList bookList = new BookList();//准备图书
            //登录,
            User user = login();
            while(true) {
                int choise = user.menu();//父类的引用想访问子类的方法,此方法必须为抽象方法,动态绑定
                user.doOperator(choise, bookList);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    🥡7. 总结

    我亦无他,惟手熟尔!,多动手实操!在实践中进步!

    欢迎大家,点赞+关注+收藏!

  • 相关阅读:
    虚拟机软件Parallels Desktop 18 mac(pd虚拟机) 中文功能
    JAVA 浏览器下载excel,自定义样式:合并单元格,设置多种背景填充颜色,冻结窗格
    配置iptables防火墙(二)
    linux 出现结构需要清理-Structure needs cleaning
    2020年12月 Scratch(二级)真题解析#中国电子学会#全国青少年软件编程等级考试
    MACOS系统Qt配置MySQL
    TiUP 镜像参考指南
    手把手教你实现法玛三因子模型
    骨干全光网技术发展趋势探讨
    springboot 使用RocketMQ客户端生产消费消息DEMO
  • 原文地址:https://blog.csdn.net/qq_60021974/article/details/126031710