• 【Java】SE练习项目 —》图书管理系统


    在这里插入图片描述博客主页: XIN-XIANG荣
    系列专栏:【Java SE】
    一句短话: 难在坚持,贵在坚持,成在坚持!

    前言

    这篇博客是在学习了一部分Java基础语法之后的练习项目,通过这个小项目的练习,对Java中的类和对象,抽象类和接口,面向对象的继承、多态和封装、组合等进行熟悉理解;抽象出不同的对象,将对象进行合理的设计,完成对象之间的交互,面向对象进行编程。

    1. 项目需求

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

    2. 实现思路

    先抽象提取出不同的对象,首先想到的对象是用户和书,

    用户可分为管理员和普通用户,将管理员和普通用户共有的属性设置为一个父类User,管理员和普通用户继承User,可以先将父类设置为普通类,在写码过程如果合适可以修改为抽象类;

    系统对书进行管理,书本身就可以抽象为一个对象;对多本书进行操作管理,书架也可以抽象为一个对象;所以书和书架用两个类来实现,书架可以设计为一个数组;

    这里系统对书进行管理,其实就是对书架对应数组进行操作,不同的操作可以通过一个标准去实现,也就是可以定义一个接口,每一个操作类去实现这个接口,完成向上转型,通过接口实现多态;

    然后写代码的过程中思考完善各个对象的成员,最终实例化对象,通过不同对象之间的交互完成管理系统。

    3. 代码实现

    包的设计

    img

    book包

    Book类

    package book;
    
    public class Book {
        private String name;//书名
        private String author;//书的作者
        private int price;//书的价格
        private String type;//书的类型
        private boolean borrowed;//是否被借出,默认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) {
            this.type = type;
        }
    
        public Boolean getBorrowed() {
            return borrowed;
        }
    
        public void setBorrowed(Boolean borrowed) {
            this.borrowed = borrowed;
        }
    
        @Override
        public String toString() {
            return "Book{" +
                    "name='" + name + '\'' +
                    ", author='" + author + '\'' +
                    ", price=" + price +
                    ", type='" + type + '\'' +
                    ", borrowed=" + ((borrowed==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

    BookList类

    package book;
    
    public class BookList {
        private Book[] books = new Book[100];
        private int usedsize;
        //书架当中默认有三本书
    
        public BookList() {
            this.books[0] = new Book("三国演义","罗贯中",66,"小说");
            this.books[1] = new Book("西游记","吴承恩",68,"小说");
            this.books[2] = new Book("红楼梦","曹雪芹",64,"小说");
            this.usedsize = 3;
        }
    
        public int getUsedsize() {
            return usedsize;
        }
    
        public void setUsedsize(int usedsize) {
            this.usedsize = usedsize;
        }
    
    
        public Book getBooks(int pos){
            return this.books[pos];
        }
    
        public void setBooks(Book book, int pos) {
            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

    operations包

    IOperation接口

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

    AddOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class AddOperation implements IOperation{
        @Override
        public void work(BookList bookList) {
            System.out.println("新增图书");
            //输入图书信息
            System.out.println("输入新增图书名称:");
            Scanner scanner = new Scanner(System.in);
            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 currentSize = bookList.getUsedsize();
            //把书放到书架(数组)
            bookList.setBooks(book, currentSize);
            //书的总数加1
            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

    BorrowOperation类

    package operations;
    
    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.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.getBooks(i);
                if (name.equals(book.getName())){
                    if(book.getBorrowed()){
                        System.out.println("该书已经被借出!");
                        return;
                    }else {
                        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

    DelOperation类

    package operations;
    
    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.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 book = bookList.getBooks(i);
                if (name.equals(book.getName())){
                    index = i;
                    break;
                }
            }
            if(-1 == index) {
                System.out.println("没有找到要删除的这本书!");
            }else {
                for (int i = index; i < currentSize-1; i++) {
                    Book book = bookList.getBooks(i+1);
                    bookList.setBooks(book, i);
                }
                //每次删除,都要将原来最后一本书位置的引用置空
                bookList.setBooks(null,currentSize-1);
                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
    • 39

    DisplayOperation类

    package operations;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class DisplayOperation 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.getBooks(i);
                System.out.println(book);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ExitOperation类

    package operations;
    
    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

    FindOperation类

    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class FindOperation 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.getBooks(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
    • 25

    ReturnOperation类

    package operations;
    
    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.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.getBooks(i);
                if (name.equals(book.getName())){
                    if(book.getBorrowed()){
                        book.setBorrowed(false);
                        System.out.println("归还成功!");
                        return;
    
                    }else {
                        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

    user包

    User类

    public abstract class User {
        protected String name;
        protected IOperation[] iOperation;
    
        public User(String name) {
            this.name = name;
        }
        public abstract int menu();
        public void doOperation(int choice, BookList bookList){
            iOperation[choice].work(bookList);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    AdminUser类

    package user;
    
    import operations.*;
    
    import java.util.Scanner;
    
    public class AdminUser extends User{
        public AdminUser(String name) {
            super(name);
            this.iOperation = new IOperation[]{
                    new ExitOperation(),
                    new FindOperation(),
                    new AddOperation(),
                    new DelOperation(),
                    new DisplayOperation()
            };
        }
    
        @Override
        public int menu() {
            System.out.println("---------------------------------");
            System.out.println("Hello 管理员:>"+""+name+" 欢迎来到图书管理系统!");
            System.out.println("      1.查找图书  2.新增图书");
            System.out.println("      3.删除图书  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

    NormalUser类

    package user;
    
    import operations.*;
    
    import java.util.Scanner;
    
    public class NormalUser extends User{
        public NormalUser(String name) {
            super(name);
            this.iOperation = new IOperation[]{
                    new ExitOperation(),
                    new FindOperation(),
                    new BorrowOperation(),
                    new ReturnOperation()
            };
        }
    
        @Override
        public int menu() {
            System.out.println("---------------------------------");
            System.out.println("Hello 用户:>"+name+" 欢迎来到图书管理系统!");
            System.out.println("      1.查找图书  2.借阅图书");
            System.out.println("      3.归还图书  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

    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 userName = scanner.nextLine();
            System.out.println("请确认你的身份:> |1->管理员|0->普通用户|");
            int choice = scanner.nextInt();
            if(1==choice){
                return new AdminUser(userName);
            }else{
                return new NormalUser(userName);
            }
        }
    
        public static void main(String[] args) {
            //准备数据
            BookList bookList = new BookList();
            //登录
            User user = login();
    
            while (true) {
                int choice = user.menu();
                user.doOperation(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

    4. 实现效果

    管理员

    img

    普通用户

    img

  • 相关阅读:
    (mac M1)Flutter环境搭建
    喜报丨迪捷软件入选浙江省2023年省级产业数字化服务商
    存储过程与游标
    Redis(主从复制、哨兵模式、集群)概述及部署
    React Native实现Toast轻提示和loading
    什么是网络编程?Java如何实现?三次握手和四次挥手?
    计算机网络 交换机的安全配置
    MyBatis标签之Select resultType和resultMap
    shell编程规范与变量
    elasticsearch搜索IK分词器实现单个字搜索
  • 原文地址:https://blog.csdn.net/Trong_/article/details/126512489