图书馆管理系统:开发一个图书馆管理系统,包括图书的分类、借阅、归还、查询等功能。用JAVA实现
时间: 2024-05-09 10:15:17 浏览: 12
好的,首先需要设计数据库来存储图书和借阅信息。可以设计两张表,一张存储图书信息,另一张存储借阅信息。
图书信息表包括以下字段:
- book_id: 图书编号,主键
- book_name: 图书名称
- author: 作者
- category: 分类
- publisher: 出版社
- publish_date: 出版日期
- price: 价格
- status: 状态(在库、借出)
借阅信息表包括以下字段:
- borrow_id: 借阅编号,主键
- book_id: 图书编号,外键
- user_id: 借阅者编号,外键
- borrow_date: 借阅日期
- return_date: 归还日期
接下来是JAVA代码实现,大致分为以下几个模块:
1. 数据库连接模块
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
```
2. 图书管理模块
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookManager {
public static List<Book> getAllBooks() {
List<Book> books = new ArrayList<>();
try (Connection conn = DatabaseConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM book")) {
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("book_id"));
book.setName(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setCategory(rs.getString("category"));
book.setPublisher(rs.getString("publisher"));
book.setPublishDate(rs.getDate("publish_date"));
book.setPrice(rs.getDouble("price"));
book.setStatus(rs.getString("status"));
books.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
public static List<Book> searchBooks(String keyword) {
List<Book> books = new ArrayList<>();
String sql = "SELECT * FROM book WHERE book_name LIKE ? OR author LIKE ? OR category LIKE ?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, "%" + keyword + "%");
pstmt.setString(2, "%" + keyword + "%");
pstmt.setString(3, "%" + keyword + "%");
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("book_id"));
book.setName(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setCategory(rs.getString("category"));
book.setPublisher(rs.getString("publisher"));
book.setPublishDate(rs.getDate("publish_date"));
book.setPrice(rs.getDouble("price"));
book.setStatus(rs.getString("status"));
books.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
public static boolean addBook(Book book) {
String sql = "INSERT INTO book (book_name, author, category, publisher, publish_date, price, status) VALUES (?, ?, ?, ?, ?, ?, ?)";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getCategory());
pstmt.setString(4, book.getPublisher());
pstmt.setDate(5, new Date(book.getPublishDate().getTime()));
pstmt.setDouble(6, book.getPrice());
pstmt.setString(7, book.getStatus());
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean deleteBook(int bookId) {
String sql = "DELETE FROM book WHERE book_id=?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, bookId);
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean updateBook(Book book) {
String sql = "UPDATE book SET book_name=?, author=?, category=?, publisher=?, publish_date=?, price=?, status=? WHERE book_id=?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getCategory());
pstmt.setString(4, book.getPublisher());
pstmt.setDate(5, new Date(book.getPublishDate().getTime()));
pstmt.setDouble(6, book.getPrice());
pstmt.setString(7, book.getStatus());
pstmt.setInt(8, book.getId());
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
3. 借阅管理模块
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BorrowManager {
public static List<Borrow> getAllBorrows() {
List<Borrow> borrows = new ArrayList<>();
try (Connection conn = DatabaseConnection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM borrow")) {
while (rs.next()) {
Borrow borrow = new Borrow();
borrow.setId(rs.getInt("borrow_id"));
borrow.setBookId(rs.getInt("book_id"));
borrow.setUserId(rs.getInt("user_id"));
borrow.setBorrowDate(rs.getDate("borrow_date"));
borrow.setReturnDate(rs.getDate("return_date"));
borrows.add(borrow);
}
} catch (SQLException e) {
e.printStackTrace();
}
return borrows;
}
public static List<Borrow> searchBorrows(int userId) {
List<Borrow> borrows = new ArrayList<>();
String sql = "SELECT * FROM borrow WHERE user_id=?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, userId);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Borrow borrow = new Borrow();
borrow.setId(rs.getInt("borrow_id"));
borrow.setBookId(rs.getInt("book_id"));
borrow.setUserId(rs.getInt("user_id"));
borrow.setBorrowDate(rs.getDate("borrow_date"));
borrow.setReturnDate(rs.getDate("return_date"));
borrows.add(borrow);
}
} catch (SQLException e) {
e.printStackTrace();
}
return borrows;
}
public static boolean borrowBook(int bookId, int userId) {
String sql = "INSERT INTO borrow (book_id, user_id, borrow_date) VALUES (?, ?, ?)";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, bookId);
pstmt.setInt(2, userId);
pstmt.setDate(3, new Date(System.currentTimeMillis()));
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static boolean returnBook(int borrowId) {
String sql = "UPDATE borrow SET return_date=? WHERE borrow_id=?";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setDate(1, new Date(System.currentTimeMillis()));
pstmt.setInt(2, borrowId);
int rows = pstmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
```
4. 用户界面模块
```java
import java.util.List;
import java.util.Scanner;
public class UserInterface {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("欢迎使用图书馆管理系统!");
System.out.println("1. 查看所有图书");
System.out.println("2. 搜索图书");
System.out.println("3. 借阅图书");
System.out.println("4. 归还图书");
System.out.println("5. 退出系统");
System.out.print("请输入选项:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
showAllBooks();
break;
case 2:
searchBooks();
break;
case 3:
borrowBook();
break;
case 4:
returnBook();
break;
case 5:
System.out.println("再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private static void showAllBooks() {
List<Book> books = BookManager.getAllBooks();
if (books.isEmpty()) {
System.out.println("暂无图书信息!");
} else {
System.out.println("所有图书信息如下:");
for (Book book : books) {
System.out.println(book);
}
}
}
private static void searchBooks() {
System.out.print("请输入关键词:");
String keyword = scanner.next();
List<Book> books = BookManager.searchBooks(keyword);
if (books.isEmpty()) {
System.out.println("未找到符合条件的图书!");
} else {
System.out.println("搜索结果如下:");
for (Book book : books) {
System.out.println(book);
}
}
}
private static void borrowBook() {
System.out.print("请输入要借阅的图书编号:");
int bookId = scanner.nextInt();
System.out.print("请输入您的编号:");
int userId = scanner.nextInt();
boolean result = BorrowManager.borrowBook(bookId, userId);
if (result) {
System.out.println("借阅成功!");
} else {
System.out.println("借阅失败!");
}
}
private static void returnBook() {
System.out.print("请输入要归还的借阅编号:");
int borrowId = scanner.nextInt();
boolean result = BorrowManager.returnBook(borrowId);
if (result) {
System.out.println("归还成功!");
} else {
System.out.println("归还失败!");
}
}
}
```
以上就是图书馆管理系统的JAVA实现。需要注意的是,本系统仅作为示例,还有很多细节问题需要考虑,比如数据库连接池、异常处理、并发控制等等。