答案:通过设计Book和BorrowRecord类,使用Map统计借阅次数,结合Stream排序获取热门图书,并按用户维度分析借阅与超期情况,实现完整的图书借阅统计功能。

在Java中实现图书借阅记录统计,核心是设计合理的数据结构和统计逻辑。重点在于记录每本书的借阅信息,并支持按不同维度进行统计,比如借阅次数、热门图书、用户借阅情况等。
先定义基本的实体类,包括Book(图书)和BorrowRecord(借阅记录)。
每个借阅记录应包含图书ID、用户ID、借阅时间和归还时间(未归还可为空)。
class Book {
private String bookId;
private String title;
public Book(String bookId, String title) {
this.bookId = bookId;
this.title = title;
}
// getter 方法
public String getBookId() { return bookId; }
public String getTitle() { return title; }
}
class BorrowRecord {
private String bookId;
private String userId;
private long borrowTime;
private Long returnTime; // 未归还可为 null
public BorrowRecord(String bookId, String userId, long borrowTime) {
this.bookId = bookId;
this.userId = userId;
this.borrowTime = borrowTime;
this.returnTime = null;
}
public void setReturnTime(Long returnTime) {
this.returnTime = returnTime;
}
// getter 方法
public String getBookId() { return bookId; }
public String getUserId() { return userId; }
public long getBorrowTime() { return borrowTime; }
public Long getReturnTime() { return returnTime; }
public boolean isReturned() { return returnTime != null; }
}
使用Map来统计每本书被借阅的总次数,遍历所有借阅记录即可。
立即学习“Java免费学习笔记(深入)”;
import java.util.*;
public class LibraryStats {
private List<BorrowRecord> records = new ArrayList<>();
private Map<String, Book> books = new HashMap<>();
public void addRecord(BorrowRecord record) {
records.add(record);
}
public Map<String, Integer> getBorrowCountByBook() {
Map<String, Integer> countMap = new HashMap<>();
for (BorrowRecord record : records) {
countMap.merge(record.getBookId(), 1, Integer::sum);
}
return countMap;
}
}
调用getBorrowCountByBook()返回一个映射,键是图书ID,值是借阅次数。
基于借阅次数排序,找出借阅最多的前N本书。
public List<Book> getTopNBorrowedBooks(int n) {
Map<String, Integer> borrowCount = getBorrowCountByBook();
return borrowCount.entrySet().stream()
.sorted((a, b) -> b.getValue().compareTo(a.getValue()))
.limit(n)
.map(entry -> books.get(entry.getKey()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
这个方法返回最受欢迎的前N本图书对象列表,前提是books映射已加载图书元数据。
可以统计每个用户借了多少次书,甚至判断是否有未归还的记录。
public Map<String, Integer> getBorrowCountByUser() {
Map<String, Integer> userCount = new HashMap<>();
for (BorrowRecord record : records) {
userCount.merge(record.getUserId(), 1, Integer::sum);
}
return userCount;
}
public List<String> getUsersWithOverdueBooks(long currentTime) {
Map<String, Boolean> hasOverdue = new HashMap<>();
for (BorrowRecord record : records) {
if (!record.isReturned() && record.getBorrowTime() + 2_592_000_000L < currentTime) { // 假设借期30天
hasOverdue.put(record.getUserId(), true);
}
}
return hasOverdue.keySet().stream().collect(Collectors.toList());
}
上面的方法可识别出可能超期未还的用户(示例中30天为期限)。
基本上就这些。通过封装记录、使用集合统计和流处理,就能实现灵活的图书借阅分析功能。实际项目中可结合数据库或文件持久化存储记录。不复杂但容易忽略细节,比如空值处理和时间逻辑。
以上就是在Java中如何实现图书借阅记录统计的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号