• 图书管理系统—Java


    1.书类

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

    2.图书列表

    package book;
    
    public class BookList {
        public Book[] bookList = new Book[10];
        private int usedSize;//当前书架上书的数目
        public BookList() {
            bookList[0] = new Book("三国演义","罗贯中",18,"小说");
            bookList[1] = new Book("水浒传","施耐庵",48,"小说");
            bookList[2] = new Book("西游记","吴承恩",28,"小说");
            this.usedSize = 3;
        }
    
        public int getUsedSize() {
            return usedSize;
        }
    
        public void setUsedSize(int usedSize) {
            this.usedSize = usedSize;
        }
        //把书放到pos位置
        public void setBookList(int pos,Book book) {
            bookList[pos] = book;
        }
        //获取pos位置的书
        public Book getBook(int pos) {
            return bookList[pos];
        }
        //这里本质上是可以写借阅书籍等操作的
    }
    
    • 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

    3.新增图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class AddOperation implements IOperation {
        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();
            bookList.setBookList(currentSize,book);
            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

    4.借阅图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class BorrowOperation implements IOperation {
        public void work(BookList bookList) {
            System.out.println("借阅图书!");
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入你要借阅的书籍:");
            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.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

    5.删除图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class DelOperation implements IOperation {
        public void work(BookList bookList) {
            System.out.println("删除图书!");
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入你要删除书籍:");
            String name = scanner.nextLine();
            int currentSize = bookList.getUsedSize();
            int index = -1;
            for (int i = 0; i < currentSize; i++) {
                Book book = bookList.getBook(i);
                if (book.getName().equals(name)) {
                    index = i;
                    break;
                }
            }
            //如果index==-1,说明没有这本书
            if (index == -1) {
                System.out.println("没有你要删除的图书");
                return;
            }
            //从这里开始删除
            for (int i = 0; i < currentSize-1; i++) {
                Book book = bookList.getBook(i + 1);
                bookList.setBookList(i,book);
            }
            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

    6.显示图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    public class DisplayOperation implements IOperation {
        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

    7.退出系统

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

    8.查找图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class FindOperation implements IOperation {
        public void work(BookList bookList) {
            System.out.println("查找图书!");
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入你要查找的书籍:");
            String name = scanner.nextLine();
            int size = bookList.getUsedSize();
            for (int i = 0; i < size; 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

    9.接口

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

    10.归还图书

    package operation;
    
    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class ReturnOperation implements IOperation {
        public void work(BookList bookList) {
            System.out.println("归还图书!");
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入你要归还的书籍:");
            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.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

    11.管理员菜单

    package user;
    
    import operation.*;
    
    import java.util.Scanner;
    
    public class AdiminUser extends User {
        public AdiminUser(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

    12.普通用户菜单

    package user;
    
    import operation.*;
    
    import java.util.Scanner;
    
    public class NormaUser extends User {
        public NormaUser(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

    13.用户类

    package user;
    
    import book.BookList;
    import operation.IOperation;
    
    public abstract class User {
        protected String name;
        protected IOperation[] iOperations;
    
        public User(String name) {
            this.name = name;
        }
    
        public abstract int menu();
    
        public void doOperation(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

    14.主函数

    import book.BookList;
    import user.AdiminUser;
    import user.NormaUser;
    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 AdiminUser(name);
            }else{
                return new NormaUser(name);
            }
        }
    
        public static void main(String[] args) {
            BookList bookList = new BookList();
            User user = login();
            //User user = AdiminUser(name);
            //User user = NormaUser(name);
            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
  • 相关阅读:
    图片压缩软件大全-免费图片压缩软件排名
    【前端】关于Flow中的联结类型如何使用
    SpringBoot SpringBoot 基础篇(第一篇) 第2章 SpringBoot 全局配置 2.3 yaml 读取数据
    OA会议管理系统
    一文了解Spring Boot启动类SpringApplication
    信息学奥赛一本通:1155:回文三位数
    1.3 Apache Hadoop的重要组成-hadoop-最全最完整的保姆级的java大数据学习资料
    Web渗透测试_目录遍历
    JDK中动态库加载路径问题,一文讲清
    Flume环境搭建
  • 原文地址:https://blog.csdn.net/m0_61963422/article/details/123937649