当前位置: 首页 > article >正文

Java : 图书管理系统

图书管理系统的作用:

高效的图书管理 图书管理系统通过自动化管理,实现了图书的采编、编目、流通管理等操作的自动化处理,大大提高了图书管理的效率和准确性。 工作人员可以通过系统快速查找图书信息,实时掌握图书的借还情况,避免了繁琐的手工操作和人为错误。

主要功能:

  1. 图书管理:添加、删除、修改图书信息。
  2. 用户管理:用户注册、登录功能。
  3. 借阅与归还:用户可以借阅和归还书籍。
  4. 查询功能:可以根据书名或作者查询书籍信息。

通过不同包的串通和引用,来实现一个简单的图书管理系统

 

代码实现: 

//定义一个book的包,包括Book和BookList两个类
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 +
                '}';
    }
}
public class BookList {
    private Book[] books=new Book[100];
    private int usedSize;
    public BookList(){
        books[0]=new Book("三国演义","罗贯中",10,"小说");
        books[1]=new Book("西游记","吴承恩",18,"小说");
        books[2]=new Book("红楼梦","曹雪芹",14,"小说");
        this.usedSize=3;
    }

    public Book getBook(int pos) {
        return books[pos];
    }

    public void setBook(int pos, Book book) {
        this.books[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }
}


//再定义一个user包,包括管理员的类和普通用户的类

public abstract class User {
    public String name;
    public IOperation[] iOperations;

    public User(String name){
        this.name=name;
    }
    public abstract int menu();
    public void doIOperation(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

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

    @Override
    public int menu() {
        System.out.println("****管理员菜单****");
        System.out.println("****************");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        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 BorrowOperation(),
                new ReturnOperation(),
        };
    }

    @Override
    public int menu() {
        System.out.println("****普通用户菜单****");
        System.out.println("******************");
        System.out.println("0.退出系统");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("******************");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的操作");
        int choice=scanner.nextInt();
        return choice;
    }

}

在定义一个operation包,包括一个接口和各种操作的类

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


public class AddOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        System.out.println("添加图书");
        int currentSize= bookList.getUsedSize();
        if(currentSize==bookList.getBooks().length){
            System.out.println("放满了,放不下了");
            return;
        }

        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);
        for (int i = 0; i < currentSize; i++) {
            Book book1=bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("有这本书不能插入");
                return;
            }
        }
        bookList.setBook(currentSize,book);
        bookList.setUsedSize(currentSize+1);
    }
}

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)){
                if (book.isBorrowed()){
                    System.out.println("该书已被借出");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有你要借阅的书本");
    }
}

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 pos=-1;
        int i = 0;
        for (; i <currentSize; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(name)){
                pos=i;
                break;
            }
        }
        if (pos==currentSize){
            System.out.println("没有你要删除的书籍");
            return;
        }

        for (int j = 0; j < currentSize; j++) {
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");

    }
}

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

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 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("没有找到这本书");
    }
}

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();
        for (int i = 0; i < currentSize; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("没有你要归还的书本");
    }
}

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

最后一个类来串通整个操作过程

public class Main {
    public static User login(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入你的姓名");
        String name=scanner.nextLine();
        System.out.println("请输入你的身份   1.管理员   2.普通用户");
        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();
            user.doIOperation(choice, bookList);
        }
    }
}

希望能对大家有所帮助!!!!!

 

 

 

 


http://www.kler.cn/a/316784.html

相关文章:

  • 虚幻引擎 CEO 谈元宇宙:发展、策略与布局
  • Unity学习笔记(4):人物和基本组件
  • Pycharm PyQt5 环境搭建创建第一个Hello程序
  • Spring框架之观察者模式 (Observer Pattern)
  • 前端知识点---Javascript的对象(Javascript)
  • Java 堆内存管理详解:`-Xms` 和 `-Xmx` 参数的使用与默认内存设置
  • ArcGIS Pro SDK (十四)地图探索 6 图形与工具
  • AIGC7: 高通骁龙AIPC开发者沙龙过程记录A
  • 力扣刷题之2398.预算内的最多机器人数目
  • Shelly实测天工的音乐创作功能,写了一首歌,来听听效果
  • 学习笔记JVM篇(四)
  • python教程修订版
  • Redis 集群策略详解
  • oracle查询历史操作记录
  • 行为型设计模式的全面解析
  • 中小企业体系技术抽象沉淀-异地灾备篇
  • Android中如何调用DLL文件
  • 通信工程学习:什么是VM虚拟机
  • 在交互式系统中,非剥夺是不是一个好的策略?为什么?
  • kettle从入门到精通 第八十六课 ETL之kettle kettle调用https接口忽略SSL校验
  • 设计原则模式概览
  • Java项目实战II基于Java+Spring Boot+MySQL的房屋租赁管理系统的设计与实现
  • 编写webpack插件自动上传sourceMap
  • MySQL高阶1831-每天的最大交易
  • 通过spring-boot创建web项目
  • 数据爬虫中遇到验证码的解决方法