图书管理系统(Java实现)

图书管理系统项目说明

项目简介

这是一个基于Java的简单图书管理系统,包含了书籍的增删查改功能。用户可以通过控制台与系统进行交互,管理图书信息。

项目结构

  1. Book.java - 书籍类
  2. Library.java - 图书馆类
  3. LibraryManagementSystem.java - 主程序

功能介绍

书籍类 (Book.java)

  • 属性:
    • id (int): 书籍ID
    • title (String): 书籍标题
    • author (String): 书籍作者
    • isAvailable (boolean): 书籍是否可用
  • 方法:
    • 构造函数初始化书籍信息
    • Getter和Setter方法
    • toString 方法用于打印书籍信息

图书馆类 (Library.java)

  • 属性:
    • books (List): 存储所有书籍的列表
  • 方法:
    • addBook(Book book): 添加书籍到图书馆
    • removeBook(int id): 根据ID移除书籍
    • updateBook(int id, String title, String author): 更新书籍信息
    • listBooks(): 列出所有书籍
    • findBookById(int id): 根据ID查找书籍

主程序 (LibraryManagementSystem.java)

  • 功能:
    • main 方法:系统的入口,提供用户界面,允许用户选择操作
    • 用户选择:
      • 1. Add Book: 添加书籍
      • 2. Remove Book: 移除书籍
      • 3. Update Book: 更新书籍
      • 4. List Books: 列出所有书籍
      • 5. Exit: 退出系统

Book.java

public class Book {
    private int id;
    private String title;
    private String author;
    private boolean isAvailable;

    public Book(int id, String title, String author) {
        this.id = id;
        this.title = title;
        this.author = author;
        this.isAvailable = true;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void setAvailable(boolean isAvailable) {
        this.isAvailable = isAvailable;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                ", isAvailable=" + isAvailable +
                '}';
    }
}

Library.java

import java.util.ArrayList;
import java.util.List;

public class Library {
    private List<Book> books;

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

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

    public void removeBook(int id) {
        Book book = findBookById(id);
        if (book != null) {
            books.remove(book);
            System.out.println("Book removed: " + book);
        } else {
            System.out.println("Book not found with id: " + id);
        }
    }

    public void updateBook(int id, String title, String author) {
        Book book = findBookById(id);
        if (book != null) {
            book.setAvailable(false); // Assuming update means book is being borrowed
            System.out.println("Book updated: " + book);
        } else {
            System.out.println("Book not found with id: " + id);
        }
    }

    public void listBooks() {
        if (books.isEmpty()) {
            System.out.println("No books available.");
        } else {
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }

    public Book findBookById(int id) {
        for (Book book : books) {
            if (book.getId() == id) {
                return book;
            }
        }
        return null;
    }
}

LibraryManagementSystem.java

import java.util.Scanner;

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

        do {
            System.out.println("Library Management System");
            System.out.println("1. Add Book");
            System.out.println("2. Remove Book");
            System.out.println("3. Update Book");
            System.out.println("4. List Books");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter book id: ");
                    int id = scanner.nextInt();
                    scanner.nextLine(); // Consume newline
                    System.out.print("Enter book title: ");
                    String title = scanner.nextLine();
                    System.out.print("Enter book author: ");
                    String author = scanner.nextLine();
                    Book book = new Book(id, title, author);
                    library.addBook(book);
                    break;
                case 2:
                    System.out.print("Enter book id to remove: ");
                    int removeId = scanner.nextInt();
                    library.removeBook(removeId);
                    break;
                case 3:
                    System.out.print("Enter book id to update: ");
                    int updateId = scanner.nextInt();
                    scanner.nextLine(); // Consume newline
                    System.out.print("Enter new book title: ");
                    String newTitle = scanner.nextLine();
                    System.out.print("Enter new book author: ");
                    String newAuthor = scanner.nextLine();
                    library.updateBook(updateId, newTitle, newAuthor);
                    break;
                case 4:
                    library.listBooks();
                    break;
                case 5:
                    System.out.println("Exiting the system...");
                    break;
                default:
                    System.out.println("Invalid choice! Please enter a number between 1 and 5.");
            }
        } while (choice != 5);

        scanner.close();
    }
}
  • 15
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PeterClerk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值