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

用java编写图书管理系统

一个简单的图书管理系统通常包含图书的增加、删除、查询和显示等基本功能。

以下是一个用 Java 编写的简单图书管理系统的示例代码。

import java.util.ArrayList;
import java.util.Scanner;

class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    @Override
    public String toString() {
        return "Title: " + title + ", Author: " + author;
    }
}

class Library {
    private ArrayList<Book> books;

    public Library() {
        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
        System.out.println("Book added successfully.");
    }

    public void removeBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title)) {
                books.remove(book);
                System.out.println("Book removed successfully.");
                return;
            }
        }
        System.out.println("Book not found.");
    }

    public void displayBooks() {
        if (books.isEmpty()) {
            System.out.println("No books in the library.");
        } else {
            System.out.println("Books in the library:");
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }

    public Book findBook(String title) {
        for (Book book : books) {
            if (book.getTitle().equalsIgnoreCase(title)) {
                return book;
            }
        }
        return null;
    }
}

public class LibraryManagementSystem {
    public static void main(String[] args) {
        Library library = new Library();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\nLibrary Management System");
            System.out.println("1. Add Book");
            System.out.println("2. Remove Book");
            System.out.println("3. Display Books");
            System.out.println("4. Find Book");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter book title: ");
                    String title = scanner.nextLine();
                    System.out.print("Enter book author: ");
                    String author = scanner.nextLine();
                    Book newBook = new Book(title, author);
                    library.addBook(newBook);
                    break;

                case 2:
                    System.out.print("Enter book title to remove: ");
                    String titleToRemove = scanner.nextLine();
                    library.removeBook(titleToRemove);
                    break;

                case 3:
                    library.displayBooks();
                    break;

                case 4:
                    System.out.print("Enter book title to find: ");
                    String titleToFind = scanner.nextLine();
                    Book foundBook = library.findBook(titleToFind);
                    if (foundBook != null) {
                        System.out.println("Book found: " + foundBook);
                    } else {
                        System.out.println("Book not found.");
                    }
                    break;

                case 5:
                    System.out.println("Exiting the program.");
                    System.exit(0);

                default:
                    System.out.println("Invalid choice. Please enter a valid option.");
            }
        }
    }
}

这个简单的图书管理系统允许用户添加书籍、删除书籍、显示所有书籍以及查找书籍。用户可以通过输入选项的数字来执行相应的操作。这只是一个基本示例,实际的系统可能需要更多功能和更复杂的设计。


http://www.kler.cn/news/136003.html

相关文章:

  • HDCTF2023 - Reverse方向全WP
  • 在Oracle 11g 数据库上设置透明数据加密(TDE)
  • 【SpringCloud】Eureka基于Ribbon负载均衡的调用链路流程分析
  • BLIP-2:冻结现有视觉模型和大语言模型的预训练模型
  • C#具名参数(Named Parameters)
  • Ubuntu下发送邮件
  • C#编程题分享(1)
  • 【亚马逊云科技产品测评】活动征文|aws云服务器 + 微服务Spring Cloud Nacos 实战
  • 使用Java解决快手滑块验证码
  • unity 打包exe设置分辨率
  • 线上bug-接口速度慢
  • Spring Boot - 自定义注解来记录访问路径以及访问信息,并将记录存储到MySQL
  • 解决 Python requests 库中 SSL 错误转换为 Timeouts 问题
  • 使用 Core Tools 在本地开发 Azure Functions
  • 【图数据库实战】-HugeGraph系列
  • SpringCloud 微服务全栈体系(十四)
  • 【brpc学习案例实践一】rpc服务构造基本流程
  • 彻底解决electron-builder安装问题与npm下载配置问题
  • Docker发布简单springboot项目
  • C++ 删除无头链上所有指定值为x的节点。
  • Redis设计与实现-数据结构(建设进度15%)
  • Re50:读论文 Large Language Models Struggle to Learn Long-Tail Knowledge
  • ubuntu 查看5000端口是否开放
  • 2023 极术通讯-汽车“新四化”路上,需要一片安全山海
  • 享元模式学习
  • 艾泊宇产品战略:灵感于鬼屋,掌握打造卓越用户体验的关键要素
  • C#单例模式懒汉式与饿汉式
  • CentOS 8搭建WordPress
  • 原理Redis-ZipList
  • cp: can‘t stat ‘/usr/share/zoneinfo/Asia/Shanghai‘: No such file or directory