• 图书管理系统


    在这里插入图片描述

    作者简介: zoro-1,目前大一,正在学习Java,数据结构
    作者主页: zoro-1的主页
    欢迎大家点赞 👍 收藏 ⭐ 加关注哦!💖💖

    book包

    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=" + 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

    BookList

    public class BookList {
        private Book[] books ;
        private int usedSize;//记录当前书架上 实际存放的书的 数量
        private static final int DEFAULT_CAPACITY = 10;
    
        public BookList() {
            this.books = new Book[DEFAULT_CAPACITY];
            //放好书!!
            this.books[0] = new Book("三国演义","罗贯中",10,"小说");
            this.books[1] = new Book("西游记","吴承恩",9,"小说");
            this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
    
            this.usedSize = 3;
        }
    
        public int getUsedSize() {
            return usedSize;
        }
    
        public void setUsedSize(int usedSize) {
            this.usedSize = usedSize;
        }
    
        public Book getBook(int pos) {
            return books[pos];
        }
    
        public void setBooks(int pos,Book book) {
            books[pos] = book;
        }
    
    
        public Book[] getBooks() {
            return books;
        }
    }
    
    • 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

    operation包

    Add

    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class Add implements Io{
        @Override
        public void work(BookList bookList) {
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入新增图书的名字");
            String name=scanner.nextLine();
            System.out.println("请输入新增图书的作者");
            String author=scanner.nextLine();
            System.out.println("请输入新增图书的价格");
            int price=scanner.nextInt();
            System.out.println("请输入新增图书的类型");
            String type=scanner.nextLine();
            Book book=new Book(name,author,price,type);
            int size=bookList.getUsedSize();
            if (size==bookList.getBooks().length){
                System.out.println("书架满了");
                return;
            }
            for(int i=0;i<size;i++)
            {
                if (bookList.getBook(i).getName().equals(name)){
                    System.out.println("已经有这本书了");
                    return;
                }
            }
            bookList.setBooks(size,book);
            bookList.setUsedSize(size+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

    Borrow

    import book.BookList;
    
    import java.util.Scanner;
    
    public class Borrow implements Io{
        @Override
        public void work(BookList bookList) {
            System.out.println("请输入您要借的书名");
            Scanner sc=new Scanner(System.in);
            String name=sc.nextLine();
            int size=bookList.getUsedSize();
            for(int i=0;i<size;i++){
                if(bookList.getBook(i).getName().equals(name)){
                    System.out.println("有您要借的书");
                    System.out.println(bookList.getBook(i));
                }
            }
            System.out.println("您要借的书不存在");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    Exit

    import book.BookList;
    
    public class Exit implements Io{
        @Override
        public void work(BookList bookList) {
            System.out.println("退出系统!");
            System.exit(0);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    Find

    import book.Book;
    import book.BookList;
    
    import java.util.Scanner;
    
    public class Find implements Io{
        @Override
        public void work(BookList bookList) {
            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)) {
                    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

    Io

    import book.BookList;
    
    public interface Io {
        void work(BookList bookList);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Return

    import book.BookList;
    
    import java.util.Scanner;
    
    public class Return implements Io{
        @Override
        public void work(BookList bookList) {
            System.out.println("请输入您要归还图书名");
            Scanner sc=new Scanner(System.in);
            String name=sc.nextLine();
            int szie=bookList.getUsedSize();
            for(int i=0;i<szie;i++){
                if(bookList.getBook(i).getName().equals(name)){
                    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

    Show

    import book.BookList;
    
    public class Show implements Io{
        @Override
        public void work(BookList bookList) {
            System.out.println("打印图书");
            int size=bookList.getUsedSize();
            for(int i=0;i<size;i++){
                System.out.println(bookList.getBook(i));
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    user包

    Max

    import operation.*;
    
    import java.util.Scanner;
    
    public class Max extends Users{
        public Max(String name){
            //父类先进行构造
            super(name);
            this.io = new Io[]{
                    new Exit(),
                    new Find(),
                    new Add(),
                    new Del(),
                    new Show(),
            };
        }
        @Override
        public int menu() {
            System.out.println("**********管理员用户*****");
            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);
            System.out.println("请输入你的操作:");
            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

    Min

    import operation.*;
    
    import java.util.Scanner;
    
    public class Min extends Users{
        public Min(String name){
            super(name);
            this.io = new Io[]{
                    new Exit(),
                    new Find(),
                    new Borrow(),
                    new Return()
    
            };
        }
        @Override
        public int menu() {
            System.out.println("**********普通用户******");
            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);
            System.out.println("请输入你的操作:");
            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

    Users

    import book.BookList;
    import operation.Io;
    
    public abstract class Users  {
    private String name;
    public Io io[];
    public Users(String name){
        this.name=name;
    }
    public abstract int menu();
    public void choice(int choice, BookList bookList){
        io[choice].work(bookList);
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    主函数

    import book.BookList;
    import user.Max;
    import user.Users;
    
    import java.util.Scanner;
    
    public class Main {
        public static Users shenfen(){
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入您的身份");
            String name= sc.nextLine();
            if(name.equals("Max")){
                return new Max(name);
            }
            if(name.equals("Min")) {
                return new Max(name);
            }
            return null;
        }
        public static void main(String[] args) {
            Users users=shenfen();
            Scanner sc=new Scanner(System.in);
            BookList bookList=new BookList();
            while (true){
                int chioce=users.menu();
                users.choice(chioce,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

    今天分享到这里就结束了,希望大家支持一下
    在这里插入图片描述

  • 相关阅读:
    【Java 进阶篇】深入理解 SQL 聚合函数
    STM32F407的时钟
    【0x01】机器学习-数据集
    MASM-环境搭建篇
    【微电网】具有柔性结构的孤岛直流微电网的分级控制(Malab代码实现)
    基于24位Δ-ΣADC和FPGA的高精度数据采集系统开发
    将中文名格式化输出为英文名
    原水的分类有哪些?
    c++学习-STL常用函数
    长文本拆分
  • 原文地址:https://blog.csdn.net/ltzoro/article/details/132646817