图书管理系统项目说明
项目简介
这是一个基于Java的简单图书管理系统,包含了书籍的增删查改功能。用户可以通过控制台与系统进行交互,管理图书信息。
项目结构
Book.java
- 书籍类Library.java
- 图书馆类LibraryManagementSystem.java
- 主程序
功能介绍
书籍类 (Book.java
)
- 属性:
id
(int): 书籍IDtitle
(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();
}
}