• 入门必写项目之图书管理系统(分析详解+完美运行+代码可拿)


    一、需求分析

    本次就将使用java语言中的多态,继承,封装,接口,数组,动态绑定等知识点,来综合性的设计与实现出一个简易的图书管理系统.

    二、思路分析

      1. 我们需要将书的属性进行封装,同时设立一个书架类,即存放的是书的数组。
      1. 我们需要实现管理员能够添加书籍,查找书籍,删除书籍以及显示书籍;
      1. 读者能够实现查找书籍,借阅书籍以及归还书籍
      1. 以上功能都是对书架进行操作的,所以我们可以设置一个接口实现
      1. 我们需要区分出管理员与普通用户,不同的用户所展示的界面不同,利用继承和多态可以实现这一思路;
      1. 我们根据功能分配包便于清楚分析问题

    三、包分类

    在这里插入图片描述

    四、模块代码展示

    1.1书籍类(Book)实现

    package Book;
    
    public class Book {
        private String name;
        private String author;
        private int price;
        private String type;
        private boolean isBorrowed = false;//默认未被借出
    
        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) {
            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 +
                    '}';
        }
    }
    
    • 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

    1.2书架类(BookList类)实现

    package Book;
    
    public class BookList {
        private int BooksNum;//图书种类数
        private static final int BOOKSNUM = 10;
        private Book[] book = new Book[BOOKSNUM];
    
        public BookList()
        {
            book[0]=new Book("三国演义","罗贯中",89,"名著");
            book[1]=new Book("斗破苍穹","天蚕土豆",10,"玄幻小说");
            book[2]=new Book("高等数学上","同济大学",45,"教科书");
            this.BooksNum=3;
        }
    
        public int getBooksNum() {
            return BooksNum;
        }
    
        public void setBooksNum(int booksNum) {
            BooksNum = booksNum;
        }
    
        public Book getBook(int x) {
            return book[x];
        }
    
        public void setBook(Book book,int pos) {
            this.book[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

    2.1功能接口(IOperation)实现

    package Method;
    import Book.BookList;
    
    public interface IOperation {
        void work(BookList bookList);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2增加图书(Addoperation)实现

    package Method;
    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 sc =new Scanner(System.in);
            System.out.println("请输入书名:");
            String name =sc.nextLine();
            System.out.println("请输入作者:");
            String author =sc.nextLine();
            System.out.println("请输入类型:");
            String type =sc.nextLine();
            System.out.println("请输入价格:");
            int price =sc.nextInt();
    
            Book newbook = new Book(name,author,price,type);
            int tmp=bookList.getBooksNum();
            for (int i = 0; i < tmp; i++) {
                if(bookList.getBook(i).getName().equals(name)){
                    System.out.println("该书已经存在,不可重复添加!");
                    return ;
                }
            }
    
            bookList.setBook(newbook,tmp);
            bookList.setBooksNum(tmp+1);
        }
    }
    
    • 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

    2.3删除图书(DelOperation)实现

    package Method;
    
    import Book.Book;
    import Book.BookList;
    import java.util.Scanner;
    
    public class DelOperation implements IOperation{
        @Override
        public void work(BookList bookList){
            System.out.println("请输入删除图书名称:");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
    
            for(int i=0;i<bookList.getBooksNum();i++){
                if(bookList.getBook(i).getName().equals(name)){
                    int j=0;
                    for (j = i; j < bookList.getBooksNum()-1; j++) {
                        bookList.setBook(bookList.getBook(j+1),j);
                    }
                    bookList.setBooksNum(bookList.getBooksNum()-1);
                    bookList.setBook(null,j);
                    System.out.println("删除成功!");
                    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

    2.4查找图书(FindOperation)实现

    package Method;
    
    import Book.BookList;
    import java.util.Scanner;
    
    public class FindOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("请输入你要查找的图书:");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            int tmp= bookList.getBooksNum();
            for (int i = 0; i < tmp; i++) {
                if (bookList.getBook(i).getName().equals(name) ) {
                    System.out.println(bookList.getBook(i));
                    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

    2.5阅览书架(ShowOperation)实现

    package Method;
    
    import Book.BookList;
    
    public class ShowOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("藏书展示!");
            for(int i=0;i< bookList.getBooksNum();i++) {
                System.out.println(bookList.getBook(i));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.6借阅图书(BrrowOperation)实现

    package Method;
    
    import Book.Book;
    import Book.BookList;
    import java.util.Scanner;
    
    public class BrrowOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            Scanner sc = new Scanner(System.in);
            System.out.println("借阅图书:");
            System.out.println("请输入你要借阅的图书:");
            String name = sc.nextLine();
    
            for(int i=0;i< bookList.getBooksNum();i++){
                if(bookList.getBook(i).getName().equals(name)&&(!bookList.getBook(i).isBorrowed())){
                    bookList.getBook(i).setBorrowed(true);
                    System.out.println("借阅成功!");
                    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

    2.7归还图书(ReturnOperation)实现

    package Method;
    
    import Book.BookList;
    
    import java.util.Scanner;
    
    public class ReturnOperation implements IOperation {
        @Override
        public void work(BookList bookList) {
            System.out.println("请输入你要归还的图书:");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            for (int i = 0; i < bookList.getBooksNum(); i++) {
                if (bookList.getBook(i).getName().equals(name) && (bookList.getBook(i).isBorrowed())) {
                    bookList.getBook(i).setBorrowed(false);
                    System.out.println("归还成功!");
                    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

    2.8退出系统(Exit)实现

    package Method;
    
    import Book.BookList;
    
    public class Exit implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("退出系统");
            System.exit(-1);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.1抽象类(User)实现

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

    3.2管理员类(AdminUser)实现

    package User;
    import Method.*;
    import Method.IOperation;
    
    import java.util.Scanner;
    
    public class AdminUser extends User{
        public AdminUser(String name) {
            super(name);
            this.iOperations = new IOperation[]{
                    new Exit(),
                    new FindOperation(),
                    new Addoperation(),
                    new DelOperation(),
                    new ShowOperation()
            };
        }
    
        @Override
        public int menu() {
            System.out.println("****************************");
            System.out.println("hello "+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;
        }
    
    • 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

    3.3用户类(NormalUser )实现

    package User;
    
    import Method.*;
    import Method.IOperation;
    
    import java.util.Scanner;
    
    public class NormalUser extends User {
        public NormalUser(String name) {
            super(name);
            this.iOperations = new IOperation[]{
                    new Exit(),
                    new FindOperation(),
                    new BrrowOperation(),
                    new ReturnOperation(),
                    new DelOperation()
            };
        }
    
        public int menu() {
            System.out.println("****************************");
            System.out.println("hello "+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;
        }
    }
    
    • 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

    4、主方法类(Main)实现

    package Main;
    
    import Book.BookList;
    import User.AdminUser;
    import User.NormalUser;
    import User.User;
    
    import java.util.Scanner;
    
    public class Main {
        public static User func() {
            System.out.println("请输入你的姓名:");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            System.out.println("请选择你的身份:1.管理员;2.用户");
            int choice = sc.nextInt();
            if (choice==1){
                return new AdminUser(name);
            } else {
                return new NormalUser(name);
            }
        }
    
        public static void main(String[] args) {
            User user = func();
            BookList bookList = new BookList();
            while (true) {
                int choice = user.menu();
                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

    五、效果展示

    具体功能较多,就不一一演示了,我们给出部分运行结果如下:
    在这里插入图片描述

  • 相关阅读:
    glide set gif start stop
    接口自动化框架(四):组装请求数据并通过requests库发送请求
    App Inventor 2 如何比较两个日期/时间?
    QT设置闹钟超时播报
    【算法小记】深度学习——时间序列数据分析 Time series Data Analysis
    pytorch线性代数的基本操作
    时间序列算法总结:单变量模型
    【Linux】进程概念讲解
    windows平台下的mysql启动等基本操作
    【Linux】搞懂进程地址空间
  • 原文地址:https://blog.csdn.net/m0_65038072/article/details/127950802