首页 > 基础资料 博客日记

Java之图书管理系统

2024-06-26 19:00:04基础资料围观20

本篇文章分享Java之图书管理系统,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识

🤷‍♀️🤷‍♀️🤷‍♀️ 今天给大家分享一下Java实现一个简易的图书管理系统!

清风的个人主页🎉✏️✏️ 

🌂c/java领域新星创作者

🎉欢迎👍点赞✍评论❤️收藏

😛😛😛希望我的文章能对你有所帮助,有不足的地方还请各位看官多多指教,大家一起学习交流!

动动你们发财的小手,点点关注点点赞!在此谢过啦!哈哈哈!😛😛😛


目录

 一、找到抽象化的对象

1.书类

2.书架类

二、管理员与普通用户登录

三、实现的功能

1.查找图书

2.新增图书(管理员功能)

3.删除图书(管理员功能)

4.显示图书信息

5.退出系统

6.借阅图书(普通用户功能)

7.归还图书(普通用户功能)

四、main方法



图书管理系统源码链接-满船清梦压星河的Gitee

 

 一、找到抽象化的对象

1.书类

经过分析,我们可以知道,书可以抽象成一个类型。它的属性包括:书名,作者,价格,书的类型等等...我们就先以这些为例。为了保持封装性,我们把这些属性都设置成private修饰的。

下面是书类的定义代码:
这段代码包括一些构造函数以及设置书的属性、重写String函数等。

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;

    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;
    }

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrowed==true)?"已借出!":"未借出!") +
                '}';
    }
}

2.书架类

我们可以利用一个数组来存放这些书籍,并记录当前存放书籍的数量,为后续的增删查改做准备,同时初始化有三本书籍。

下面是代码:

public class BookList {
    private Book[] books;
    private int usedSize;//记录当前书架上实际存放的书的数量

    public BookList(){
        this.books=new Book[10];
        this.books[0]=new Book("三国演义","罗贯中",18,"小说");
        this.books[1]=new Book("西游记","吴承恩",28,"小说");
        this.books[2]=new Book("红楼梦","曹雪芹",35,"小说");
        this.usedSize=3;
    }
    //获取当前存放书籍数量
    public int getUsedSize() {
        return usedSize;
    }
    
    //设置存放书籍数量
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    //返回下标为pos的书籍
    public Book getBook(int pos){
        return books[pos];
    }
    //设置下标为pos位置的书籍为book
    public void setBook(int pos,Book book){
        books[pos]=book;
    }
    //返回书籍这个数组
    public Book[] getBooks(){
        
        return books;
    }
}

二、管理员与普通用户登录

首先定义一个用户抽象类,再定义管理员与普通用户去继承抽象类并重写菜单方法。

下面是用户抽象类代码:

abstract public 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){
        ioPerations[choice].work(bookList);
    }
}

管理员类代码:

public class AdiminUser extends User{
    public AdiminUser(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("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;
    }
}

普通用户类代码:
 

public class NormalUser extends User{
    public NormalUser(String name){
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowedOperation(),
                new ReturnOperation()
                };
    }

    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;
    }
}

三、实现的功能

实现以下几个功能,可以定义一个接口,方便后续的相关操作。

public interface IOPeration {
    void work(BookList bookList);
}

1.查找图书

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.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("该书信息如下>:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("无此书!!!");
    }
}

2.新增图书(管理员功能)

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书>:");
        int cunrrentSize=bookList.getUsedSize();
        if (cunrrentSize==bookList.getBooks().length){
            System.out.println("书架已满!");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入要新增书籍>:");
        String name=scanner.nextLine();
        //检查数组当中有没有这本书
        for (int i = 0; i <cunrrentSize ; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("该书已存放,无需新增!!!");
                return;
            }
        }
        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);
        bookList.setBook(cunrrentSize,book);
        bookList.setUsedSize(cunrrentSize+1);
        System.out.println("新增书籍成功!!!");
    }
}

3.删除图书(管理员功能)

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书>:");
        int cunrrentSize=bookList.getUsedSize();
        if (cunrrentSize==bookList.getBooks().length){
            System.out.println("书架已满!");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入要新增书籍>:");
        String name=scanner.nextLine();
        //检查数组当中有没有这本书
        for (int i = 0; i <cunrrentSize ; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("该书已存放,无需新增!!!");
                return;
            }
        }
        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);
        bookList.setBook(cunrrentSize,book);
        bookList.setUsedSize(cunrrentSize+1);
        System.out.println("新增书籍成功!!!");
    }
}

4.显示图书信息

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);
        }
    }
}

5.退出系统

public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统>:");
        System.exit(0);
    }
}

6.借阅图书(普通用户功能)

public class BorrowedOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书>:");
        /**
         * 1.你要借阅哪本书?
         * 2.你借阅的书存在吗?
         * 借阅的方式是什么?
         */
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入要借阅书籍>:");
        String name=scanner.nextLine();
        int currentSize=bookList.getUsedSize();
        int i = 0;
        for (; i <currentSize ; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!!!");
                return;
            }
        }
        if(i==currentSize){
            System.out.println("该书不存在,无法借阅!!!");
        }
    }
}

7.归还图书(普通用户功能)

public class ReturnOperation implements IOPeration{
    @Override
    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 i = 0;
        for (; i <currentSize ; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!!!");
                return;
            }
        }
        if(i==currentSize){
            System.out.println("该书不存在,无需归还!!!");
        }
    }
}

四、main方法

public class Main {
    public static User login() {
        System.out.println("请输入你的姓名:>");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份:> 1.管理员  2.普通用户");
        int choice = scanner.nextInt();

        if (choice == 1) {
            //管理员
            return new AdiminUser(name);
        } else {
            //普通用户
            return new NormalUser(name);
        }

    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        //user指向哪个对象,就看返回值是什么
        User user = login();
        while (true) {
            int choice = user.menu();

            System.out.println("choice:" + choice);
            //根据choice决定调用的是哪个方法

            user.doOperation(choice, bookList);

        }
    }
}

🎉好啦,今天的分享就到这里!!

创作不易,还希望各位大佬支持一下!

👍点赞,你的认可是我创作的动力!

收藏,你的青睐是我努力的方向!

✏️评论:你的意见是我进步的财富!

 


文章来源:https://blog.csdn.net/m0_73920844/article/details/134223331
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云