• JavaSE项目练习:图书管理系统


    1.简介

    通过前面知识:类,抽象类,封装,继承,多态,接口的学习,这篇文章将会通过对学习的知识点的整合运用,写一个小项目——图书管理系统,来帮助大家更好的掌握前面学习的知识点。

    2.设计需求

    1. 根据用户信息进行登录(选择身份)

    2. 管理员身份

    • 查找图书
    • 新增图书
    • 删除图书
    • 显示图书
    • 退出系统
    1. 普通用户身份
    • 查找图书
    • 借阅图书
    • 归还图书
    • 退出系统

    3.相关类的设计

    根据设计需求,我们分析所需要的类.

    1. 首先是图书类 -> 先创建 package book包 -> 创建 Book 类, 表示一本书,创建 BookList 类, 用来保存 N 本书.

    2. 接着是操作类 ->先创建 package operation包 -> 根据功能创建操作类

       							AddOperation  -> 新增图书
       							BrowOperation -> 借阅图书
       							DelOperation -> 删除图书
       							ExitOperation -> 退出系统
       							FindOperation	-> 查找图书							
       							ReturnOperation -> 归还图书
       							ShowOperation -> 显示图书
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    根据操作类来设计一个接口IOPeration

    1. 然后创建用户类 -> 先创建 package user包 -> 创建 User 类, 这是一个抽象类,创建普通用户类, 是 User 的子类创建管理员用户类。

    2. 最后创建测试类Main 来测试下代码的基本框架是否存在问题。

    4.图书类

    1.Book类

    package book;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:书籍信息
     * User: 28144
     * Date: 2022-11-27
     * Time: 18:59
     */
    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 double 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
    • 71
    • 72
    • 73
    • 74
    • 75

    2. BookList类

    package book;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:书架
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:06
     */
    public class BookList {
        private Book[] books = new Book[10];
    
        private int usedSize;//来记录当前books数组当中有多少本书
    
        public BookList() {
            books[0] = new Book("三国演义","罗贯中",89,"小说");
            books[1] = new Book("西游记","吴承恩",59,"小说");
            books[2] = new Book("红楼梦","曹雪芹",189,"小说");
            this.usedSize = 3;
        }
    
        public Book getBook(int pos) {
            return this.books[pos];
        }
    
        public void setBook(Book book) {
            this.books[usedSize] = book;
        }
    
        public void setBook(int pos,Book book) {
            this.books[pos] = book;
        }
    
        public int getUsedSize() {
            return usedSize;
        }
    
        public void setUsedSize(int usedSize) {
            this.usedSize = usedSize;
        }
    }
    
    • 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

    5.操作类

    操作的接口:

    package opera;
    
    import book.BookList;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:16
     */
    public interface IOPeration {
        void work(BookList bookList);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    1.新增图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:18
     */
    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 book = new Book(name,author,price,type);
            int currentSize = bookList.getUsedSize();
            for (int i = 0; i < currentSize; i++) {
                Book tmp = bookList.getBook(i);
                if(tmp.getName().equals(name)) {
                    System.out.println("这本书已经存在,不能再放入!");
                    return;
                }
            }
            bookList.setBook(book);
            //修改usedSize
            bookList.setUsedSize(currentSize + 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    在这里插入图片描述

    2.借阅图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:25
     */
    public class BrowOperation implements IOPeration{
        @Override
        public void work(BookList bookList) {
            System.out.println("借阅图书!");
            System.out.println("输入你要借阅的图书:");
            Scanner scanner = new Scanner(System.in);
            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.isBorrowed()) {
                    book.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
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    3.删除图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:21
     */
    public class DelOperation implements IOPeration{
        @Override
        public void work(BookList bookList) {
            System.out.println("删除图书!");
            System.out.println("请输入你要删除图书的名字:");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            int currentSize = bookList.getUsedSize();
            int index = -1;
            for (int i = 0; i < currentSize; i++) {
                Book tmp = bookList.getBook(i);
                if(tmp.getName().equals(name)) {
                    index = i;
                    break;
                }
            }
            //挪动数据
            for (int j = index; j < currentSize - 1; j++) {
                Book book = bookList.getBook(j + 1);
                bookList.setBook(j,book);
            }
            //修改Size
            bookList.setUsedSize(currentSize - 1);
            //因为删除的是对象,所以把最后一个置为null
            bookList.setBook(currentSize - 1,null);
            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

    在这里插入图片描述

    4.查找图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:20
     */
    public class FindOperation implements IOPeration{
        @Override
        public void work(BookList bookList) {
            System.out.println("查找图书!");
            System.out.println("请输入你要查找的图书的名字:");
            Scanner sc = new Scanner(System.in);
            String name = sc.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("没有这本书!");
        }
    }
    
    • 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

    在这里插入图片描述

    在这里插入图片描述

    5.归还图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:25
     */
    public class ReturnOperation implements IOPeration{
        @Override
        public void work(BookList bookList) {
            System.out.println("归还图书!");
            System.out.println("输入你要归还的图书:");
            Scanner scanner = new Scanner(System.in);
            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.isBorrowed()) {
                    book.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
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述

    6.显示图书

    package opera;
    
    import book.Book;
    import book.BookList;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:22
     */
    public class ShowOperation implements IOPeration{
        @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);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    7.退出系统

    package opera;
    
    import book.BookList;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:24
     */
    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
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    6.用户类

    1.User类

    创建 User类, 这是一个抽象类

    package user;
    
    import book.BookList;
    import opera.IOPeration;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:12
     */
    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) {
            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
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2.普通用户类

    创建普通用户类, 是 User 的子类.

    package user;
    
    import opera.*;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:14
     */
    public class NormalUser extends User{
    
        public NormalUser(String name) {
            super(name);
            this.ioPerations = new IOPeration[] {
                new ExitOperation(),
                new FindOperation(),
                new BrowOperation(),
                new ReturnOperation(),
            };
        }
    
        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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    在这里插入图片描述

    3.管理员类

    创建管理员用户类.

    package user;
    
    import opera.*;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:13
     */
    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("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
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    在这里插入图片描述

    7.测试类

    import book.Book;
    import book.BookList;
    import user.AdminUser;
    import user.NormalUser;
    import user.User;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * Description:
     * User: 28144
     * Date: 2022-11-27
     * Time: 19:32
     */
    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来确定调用那个对象的那个操作。
                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
    • 34
    • 35
    • 36
    • 37
    • 38

    在这里插入图片描述

  • 相关阅读:
    【正点原子STM32连载】第五十章 照相机实验 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
    网络防御--防火墙
    我让虚拟DOM的diff算法过程动起来了
    Maven安装与配置
    [1164]python用numpy计算均值,方差,标准差
    山西电力市场日前价格预测【2023-10-29】
    有了低代码,二次开发都不是事!
    大数据-Storm流式框架(二)--wordcount案例
    NPM 常用命令(一)
    VB遍历所有窗体名
  • 原文地址:https://blog.csdn.net/m0_66030479/article/details/128087874