• 【手把手带你学JavaSE系列】练习项目—图书管理系统


    ❤️❤️个人主页:摸鱼王胖嘟嘟
    🌟🌟作品专栏:【手把手带你学JavaSE系列】
    📑给大家推荐一款非常火的面试、刷题、学习神器
    👉牛客网
    👉点击注册一起刷题、学习、讨论收获大厂offer吧!
    在这里插入图片描述

    前言

    学习了类、抽象类、封装、继承、多态、接口等知识之后,为了更好的去理解贯彻知识,我们今天练习一个小项目—图书管理系统。

    一、项目需求

    图书管理系统,面向管理员和普通用户使用,对管理员的开放的功能有:添加图书,删除图书,查找图书,显示图书和退出程序等;对普通用户的开放的功能有:查找图书,借阅图书,归还图书和退出程序。

    二、实现思路

    首先我们想到的对象就是用户和书。

    用户包括管理员和普通用户,书要有放书的书架,不同的用户有不同的操作。

    接下来我们就要实现这些操作。

    三、代码实现

    1. book包

    Book类

    package book;
    
    import java.security.SecureRandom;
    
    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) ?  " 已经被借出 " : "  未被借出 ")+
                    '}';
        }
    }
    
    
    • 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
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    BookList类

    package book;
    
    public class BookList {
        private Book[] books = new Book[10];
        private int usedSize;
        public BookList(){
            books[0] = new Book("三国演义","罗贯中",17,"小说");
            books[1] = new Book("西游记","吴承恩",40,"小说");
            books[2] = new Book("水浒传","施耐庵",57,"小说");
            this.usedSize = 3;
        }
    
        public int getUsedSize() {
            return usedSize;
        }
    
        public void setUsedSize(int usedSize) {
            this.usedSize = usedSize;
        }
    
        /**
         * 获取到pos位置的一本书
         * @param pos
         * @return
         */
        public Book getPos(int pos){
            return this.books[pos];
        }
    
        /**
         * 设置pos下标为一本书->[添加一本书]
         * @param pos
         * @param book
         */
        public void setBook(int pos,Book book){
            this.books[pos] = book;
        }
    }
    
    
    • 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

    2. operations包

    AddOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    
    import java.util.Scanner;
    
    
    public class AddOperation implements IOperation {
    
        @Override
        public void work(BookList bookList) {
            System.out.println("新增图书");
            //输入图书信息
            System.out.println("输入新增图书名称:");
            String name = scanner.nextLine();
            System.out.println("输入新增图书作者:");
            String author = scanner.nextLine();
            System.out.println("输入新增图书价格:");
            int price = scanner.nextInt();
            scanner.nextLine();
            System.out.println("输入新增图书类型:");
            String type = scanner.nextLine();
    
            Book book = new Book(name, author, price, type);
            //获取存书位置
            int size = bookList.getUsedSize();
            //把书放到书架(数组)
            bookList.setBook(size,book);
            //书的总数加1
            bookList.setUsedSize(size+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
    • 33
    • 34
    • 35

    BorrowOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class BorrowOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("借阅图书!");
            System.out.println("请输入你要借阅的图书的名字:");
            String name = scanner.nextLine();
            int size = bookList.getUsedSize();
            for (int i = 0; i < size; i++) {
                Book book = bookList.getPos(i);
                if(name.equals(book.getName())){
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                    System.out.println(book);
                    return;
                }
            }
            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

    DelOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    
    public class DelOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("删除图书!");
            //1.根据书名找到书的位置index
            System.out.println("请输入你要删除的图书:");
            String name = scanner.nextLine();
            int currentSize = bookList.getUsedSize();
            int index = 0;//存储找到的下标
            int i = 0;
            for (; i < currentSize; i++) {
                Book book = bookList.getPos(i);
                if(book.getName().equals(name)){
                    index = i;
                    break;
                }
            }
            if(i >= currentSize){
                System.out.println("没有你要删除的这本书!");
                return;
            }
            //2.进行删除
            for (int j = 0; j < currentSize - 1; j++) {
                Book book = bookList.getPos(j + 1);
                bookList.setBook(j,book);
            }
            bookList.setBook(currentSize,null);
            bookList.setUsedSize(currentSize-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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    DisplayOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    
    public class DisplayOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("打印图书!");
            int size = bookList.getUsedSize();
            for (int i = 0; i < size; i++) {
                Book book = bookList.getPos(i);
                System.out.println(book);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    ExitOperation类

    package operations;
    
    import book.BookList;
    
    
    public class ExitOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("退出系统!");
            System.exit(0);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    FindOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    
    public class FindOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("查找图书!");
            String name = scanner.nextLine();
            int size = bookList.getUsedSize();
            for (int i = 0; i < size; i++) {
                Book book = bookList.getPos(i);
                if(name.equals(book.getName())){
                    System.out.println("找到了这本书,信息如下:");
                    System.out.println(book);
                    return;
                }
            }
            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

    ReturnOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    
    public class ReturnOperation implements IOperation{
    
        public void work(BookList bookList){
            System.out.println("归还图书!");
            System.out.println("请输入你要归还的图书的名字:");
            String name = scanner.nextLine();
            int size = bookList.getUsedSize();
            for (int i = 0; i < size; i++) {
                Book book = bookList.getPos(i);
                if(name.equals(book.getName())){
                    book.setBorrowed(false);
                    System.out.println("归还成功!");
                    System.out.println(book);
                    return;
                }
            }
            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

    IOperation接口

    package operations;
    
    import book.BookList;
    
    import java.util.Scanner;
    
    public interface IOperation {
        Scanner scanner = new Scanner(System.in);
        void work(BookList bookList);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3. user包

    AdminUser类

    package user;
    
    import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
    import operations.*;
    
    import java.util.Scanner;
    import java.util.SplittableRandom;
    
    
    public class AdminUser extends User{
    
        public AdminUser(String name){
            super(name);
            this.iOperations = new IOperation[] {
                    new ExitOperation(),
                    new FindOperation(),
                    new AddOperation(),
                    new DelOperation(),
                    new DisplayOperation()
            };
        }
    
        public int menu(){
            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("=================================");
    
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            return choice;
        }
    }
    
    
    • 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

    NormalUser类

    package user;
    
    import operations.*;
    
    import java.util.Scanner;
    
    
    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("hello" +" " + this.name + " 欢迎来到这里");
            System.out.println("1.查找图书");
            System.out.println("2.借阅图书");
            System.out.println("3.归还图书");
            System.out.println("0.退出系统");
            System.out.println("=================================");
    
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            return choice;
        }
    }
    
    • 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

    User类

    package user;
    
    import book.BookList;
    import operations.IOperation;
    
    
    public abstract class User {
        protected String name;
    
        protected IOperation[] iOperations;
    
        public User(String name){
            this.name = name;
        }
    
        public abstract int menu();
    
        public void doWork(int choice, BookList bookList){
            iOperations[choice].work(bookList);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Main类

    import book.BookList;
    import user.AdminUser;
    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->管理员。0->普通用户");
            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 = login();//向上转型
            while(true) {
                int choice = user.menu();//发生了动态绑定 -> 多态
                //根据你的choice 调用合适的操作
                user.doWork(choice, 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

    四、效果展示

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

  • 相关阅读:
    年近三十,真的卷不动了
    线性表的单链表
    linux复习笔记01(小滴课堂)
    【python】 16进制字符串转list
    磨金石教育摄影技能干货分享|古风人像修图与调色技巧
    ubuntu127.0.1.1
    echarts5.0引入地图,背景渐变色,航线图,地图阴影
    Yolov5的类激活图
    权限、认证与授权
    浅谈一下:Java当作数组的几个应用场景
  • 原文地址:https://blog.csdn.net/weixin_61341342/article/details/126650447