【无标题】

import java.time.LocalDate;

import java.util.*;

public class Main {

private static LibraryService libraryService = new LibraryService();

private static ScannerService scannerService = new ScannerService();

private static ExcelService excelService = new ExcelService();

private static Scanner scanner = new Scanner(System.in);

public static void main(String\[\] args) {

System.out.println("=== 图书管理系统启动 ===");

System.out.println("版本: 1.0 | 自动维护模式");

// 检查是否首次运行

checkFirstRun();

// 加载数据

libraryService.loadData();

boolean running = true;

while (running) {

showMainMenu();

int choice = getIntInput("请选择操作: ");

switch (choice) {

case 1:

manageBooks();

break;

case 2:

manageBorrowers();

break;

case 3:

borrowReturnBooks();

break;

case 4:

searchBooks();

break;

case 5:

importExportData();

break;

case 6:

scanBookISBN();

break;

case 7:

systemStatistics();

break;

case 0:

running = false;

System.out.println("系统退出,数据已自动保存");

break;

default:

System.out.println("无效选择,请重新输入");

}

}

scanner.close();

}

private static void checkFirstRun() {

// 检查是否为首次运行,创建必要的目录结构

String\[\] dirs = {"data", "config", "exports", "logs"};

for (String dir : dirs) {

File directory = new File(dir);

if (!directory.exists()) {

directory.mkdirs();

System.out.println("创建目录: " + dir);

}

}

}

private static void showMainMenu() {

System.out.println("\n===== 图书管理系统主菜单 =====");

System.out.println("1. 📚 图书管理");

System.out.println("2. 👥 借阅者管理");

System.out.println("3. 🔄 借书/还书");

System.out.println("4. 🔍 图书查询");

System.out.println("5. 📊 数据导入导出");

System.out.println("6. 📱 扫码录入图书");

System.out.println("7. 📈 系统统计");

System.out.println("0. ❌ 退出系统");

}

private static void manageBooks() {

boolean back = false;

while (!back) {

System.out.println("\n===== 图书管理 =====");

System.out.println("1. 添加图书");

System.out.println("2. 修改图书");

System.out.println("3. 删除图书");

System.out.println("4. 查看所有图书");

System.out.println("0. 返回主菜单");

int choice = getIntInput("请选择操作: ");

switch (choice) {

case 1:

addBook();

break;

case 2:

updateBook();

break;

case 3:

deleteBook();

break;

case 4:

listAllBooks();

break;

case 0:

back = true;

break;

default:

System.out.println("无效选择");

}

}

}

private static void addBook() {

System.out.println("\n--- 添加图书 ---");

System.out.print("请输入ISBN: ");

String isbn = scanner.nextLine();

// 检查是否已存在

if (libraryService.findBookByIsbn(isbn) != null) {

System.out.println("该ISBN的图书已存在!");

return;

}

System.out.print("请输入书名: ");

String title = scanner.nextLine();

System.out.print("请输入作者: ");

String author = scanner.nextLine();

System.out.print("请输入出版社: ");

String publisher = scanner.nextLine();

System.out.print("请输入出版日期 (YYYY-MM-DD): ");

String dateStr = scanner.nextLine();

System.out.print("请输入总册数: ");

int totalCopies = getIntInput("总册数: ");

Book book = new Book(isbn, title, author, publisher, LocalDate.parse(dateStr), totalCopies);

if (libraryService.addBook(book)) {

System.out.println("✅ 图书添加成功!");

} else {

System.out.println("❌ 图书添加失败!");

}

}

private static void updateBook() {

System.out.println("\n--- 修改图书 ---");

System.out.print("请输入要修改的图书ISBN: ");

String isbn = scanner.nextLine();

Book book = libraryService.findBookByIsbn(isbn);

if (book == null) {

System.out.println("❌ 未找到该ISBN的图书!");

return;

}

System.out.println("当前图书信息: " + book);

System.out.print("请输入新书名 (直接回车保持原值): ");

String newTitle = scanner.nextLine();

if (!newTitle.isEmpty()) {

book.setTitle(newTitle);

}

System.out.print("请输入新作者 (直接回车保持原值): ");

String newAuthor = scanner.nextLine();

if (!newAuthor.isEmpty()) {

book.setAuthor(newAuthor);

}

System.out.print("请输入新出版社 (直接回车保持原值): ");

String newPublisher = scanner.nextLine();

if (!newPublisher.isEmpty()) {

book.setPublisher(newPublisher);

}

System.out.print("请输入新总册数 (输入0保持原值): ");

int newTotal = getIntInput("新总册数: ");

if (newTotal > 0) {

book.setTotalCopies(newTotal);

}

if (libraryService.updateBook(book)) {

System.out.println("✅ 图书修改成功!");

} else {

System.out.println("❌ 图书修改失败!");

}

}

private static void deleteBook() {

System.out.println("\n--- 删除图书 ---");

System.out.print("请输入要删除的图书ISBN: ");

String isbn = scanner.nextLine();

Book book = libraryService.findBookByIsbn(isbn);

if (book == null) {

System.out.println("❌ 未找到该ISBN的图书!");

return;

}

System.out.println("将要删除的图书: " + book);

System.out.print("确认删除?(y/n): ");

String confirm = scanner.nextLine();

if ("y".equalsIgnoreCase(confirm)) {

if (libraryService.deleteBook(isbn)) {

System.out.println("✅ 图书删除成功!");

} else {

System.out.println("❌ 图书删除失败!");

}

}

}

private static void listAllBooks() {

System.out.println("\n--- 所有图书列表 ---");

List ListBook> books = libraryService.getAllBooks();

if (books.isEmpty()) {

System.out.println("暂无图书数据");

} else {

System.out.printf("%-15s %-30s %-20s %-15s %-10s%n",

"ISBN", "书名", "作者", "出版社", "库存");

for (Book book : books) {

System.out.printf("%-15s %-30s %-20s %-15s %-10s%n",

book.getIsbn(), book.getTitle(), book.getAuthor(), book.getPublisher(),

book.getAvailableCopies() + "/" + book.getTotalCopies());

}

}

}

private static void manageBorrowers() {

boolean back = false;

while (!back) {

System.out.println("\n===== 借阅者管理 =====");

System.out.println("1. 添加借阅者");

System.out.println("2. 查看所有借阅者");

System.out.println("0. 返回主菜单");

int choice = getIntInput("请选择操作: ");

switch (choice) {

case 1:

addBorrower();

break;

case 2:

listAllBorrowers();

break;

case 0:

back = true;

break;

default:

System.out.println("无效选择");

}

}

}

private static void addBorrower() {

System.out.println("\n--- 添加借阅者 ---");

System.out.print("请输入借阅者ID: ");

String id = scanner.nextLine();

System.out.print("请输入姓名: ");

String name = scanner.nextLine();

System.out.print("请输入电话: ");

String phone = scanner.nextLine();

System.out.print("请输入邮箱: ");

String email = scanner.nextLine();

Borrower borrower = new Borrower(id, name, phone, email);

if (libraryService.addBorrower(borrower)) {

System.out.println("✅ 借阅者添加成功!");

} else {

System.out.println("❌ 该ID的借阅者已存在!");

}

}

private static void listAllBorrowers() {

System.out.println("\n--- 所有借阅者列表 ---");

List ListBorrower> borrowers = libraryService.getAllBorrowers();

if (borrowers.isEmpty()) {

System.out.println("暂无借阅者数据");

} else {

System.out.printf("%-15s %-20s %-15s %-25s%n",

"借阅者ID", "姓名", "电话", "邮箱");

for (Borrower borrower : borrowers) {

System.out.printf("%-15s %-20s %-15s %-25s%n",

borrower.getId(), borrower.getName(), borrower.getPhone(), borrower.getEmail());

}

}

}

private static void borrowReturnBooks() {

boolean back = false;

while (!back) {

System.out.println("\n===== 借书/还书 =====");

System.out.println("1. 借书");

System.out.println("2. 还书");

System.out.println("3. 查看借阅记录");

System.out.println("0. 返回主菜单");

int choice = getIntInput("请选择操作: ");

switch (choice) {

case 1:

borrowBook();

break;

case 2:

returnBook();

break;

case 3:

listBorrowRecords();

break;

case 0:

back = true;

break;

default:

System.out.println("无效选择");

}

}

}

private static void borrowBook() {

System.out.println("\n--- 借书 ---");

System.out.print("请输入图书ISBN: ");

String isbn = scanner.nextLine();

Book book = libraryService.findBookByIsbn(isbn);

if (book == null) {

System.out.println("❌ 未找到该ISBN的图书!");

return;

}

if (book.getAvailableCopies() <= 0) {

System.out.println("❌ 该图书已无库存!");

return;

}

System.out.print("请输入借阅者ID: ");

String borrowerId = scanner.nextLine();

if (libraryService.borrowBook(isbn, borrowerId)) {

System.out.println("✅ 借书成功!");

} else {

System.out.println("❌ 借书失败!");

}

}

private static void returnBook() {

System.out.println("\n--- 还书 ---");

System.out.print("请输入借阅记录ID: ");

String recordId = scanner.nextLine();

if (libraryService.returnBook(recordId)) {

System.out.println("✅ 还书成功!");

} else {

System.out.println("❌ 还书失败,请检查记录ID!");

}

}

private static void listBorrowRecords() {

System.out.println("\n--- 借阅记录列表 ---");

List ListBorrowRecord> records = libraryService.getBorrowRecords();

if (records.isEmpty()) {

System.out.println("暂无借阅记录");

} else {

System.out.printf("%-15s %-15s %-15s %-12s %-12s %-8s%n",

"记录ID", "ISBN", "借阅者ID", "借书日期", "应还日期", "状态%n");

for (BorrowRecord record : records) {

System.out.printf("%-15s %-15s %-15s %-12s %-12s %-8s%n",

record.getRecordId(), record.getBookIsbn(), record.getBorrowerId(),

record.getBorrowDate(), record.getDueDate(), record.getStatus());

}

}

}

private static void searchBooks() {

System.out.println("\n--- 图书查询 ---");

System.out.print("请输入搜索关键词 (书名/作者/ISBN): ");

String keyword = scanner.nextLine();

List ListBook> results = libraryService.searchBooks(keyword);

if (results.isEmpty()) {

System.out.println("❌ 未找到相关图书!");

} else {

System.out.println("🔍 找到 " + results.size() + " 本相关图书:");

for (Book book : results) {

System.out.println("📖 " + book);

}

}

}

private static void importExportData() {

boolean back = false;

while (!back) {

System.out.println("\n===== 数据导入导出 =====");

System.out.println("1. 导出图书数据到Excel");

System.out.println("2. 从Excel导入图书数据");

System.out.println("0. 返回主菜单");

int choice = getIntInput("请选择操作: ");

switch (choice) {

case 1:

exportToExcel();

break;

case 2:

importFromExcel();

break;

case 0:

back = true;

break;

default:

System.out.println("无效选择");

}

}

}

private static void exportToExcel() {

System.out.print("请输入导出文件名 (例如: books_export.xlsx): ");

String fileName = scanner.nextLine();

if (excelService.exportBooksToExcel(libraryService.getAllBooks(), "exports/" + fileName);

System.out.println("✅ 数据导出成功!");

}

private static void importFromExcel() {

System.out.print("请输入要导入的Excel文件名: ");

String fileName = scanner.nextLine();

List ListBook> importedBooks = excelService.importBooksFromExcel(fileName);

System.out.println("✅ 成功导入 " + importedBooks.size() + " 本图书");

for (Book book : importedBooks) {

libraryService.addBook(book);

}

}

private static void scanBookISBN() {

System.out.println("\n--- 扫码录入图书 ---");

System.out.print("请扫描图书ISBN码: ");

String isbn = scanner.nextLine();

Book book = scannerService.scanBook(isbn);

if (book != null) {

System.out.println("📱 扫描成功!");

System.out.println("图书信息: " + book);

System.out.print("是否添加此图书?(y/n): ");

String confirm = scanner.nextLine();

if ("y".equalsIgnoreCase(confirm)) {

if (libraryService.addBook(book)) {

System.out.println("✅ 图书添加成功!");

} else {

System.out.println("❌ 图书添加失败!");

}

} else {

System.out.println("❌ 扫码失败,请检查ISBN码!");

}

}

private static void systemStatistics() {

System.out.println("\n===== 系统统计信息 =====");

int totalBooks = libraryService.getAllBooks().size();

int totalBorrowers = libraryService.getAllBorrowers().size();

int totalRecords = libraryService.getBorrowRecords().size();

System.out.println("📊 统计概览:");

System.out.println(" 📚 总图书数量: " + totalBooks);

System.out.println(" 👥 总借阅者数量: " + totalBorrowers);

System.out.println(" 🔄 总借阅记录: " + totalRecords);

// 计算借出中的图书数量

long borrowedCount = libraryService.getBorrowRecords().stream()

.filter(record -> "借出".equals(record.getStatus()))

.count();

System.out.println(" 📖 当前借出图书: " + borrowedCount);

List ListBook> books = libraryService.getAllBooks();

if (!books.isEmpty()) {

System.out.println("\n📈 热门图书排行:");

books.stream()

.sorted((b1, b2) -> Integer.compare(b2.getTotalCopies() - b2.getAvailableCopies(),

b1.getTotalCopies() - b1.getAvailableCopies()))

.limit(5)

.forEach(book -> System.out.println(" - " + book.getTitle() +

" (借出: " + (b1.getTotalCopies() - b1.getAvailableCopies() + ")"));

}

}

private static int getIntInput(String prompt) {

System.out.print(prompt);

while (!scanner.hasNextInt()) {

System.out.print("请输入有效数字: ");

scanner.next();

}

int result = scanner.nextInt();

scanner.nextLine(); // 清除换行符

return result;

}

}

相关推荐
wdfk_prog6 分钟前
lely-canopen-rtt:为什么推荐在 RT-Thread 上使用 Lely CANopen 构建主站
java·运维·缓存·docker·容器
Token掘金室11 分钟前
JSON模式结构化输出报错
java·服务器·json
秋饼17 分钟前
Spring AI 2.0 接入 DeepSeek V4.1 Flash 生产级实战
java·ai·技术分享·后端开发
IT枫斗者枫哥17 分钟前
Java 分批导出仍然 OOM?用 32 MiB 堆复现三种 CSV 写法
java
shehuiyuelaiyuehao25 分钟前
算法43,外观数列,模拟算法+双指针
java·算法
lhldsg1 小时前
多商户团购系统源码:技术架构选型与二次开发实战指南
java·小程序·架构
亦暖筑序1 小时前
AgentScope Java 实战:@Tool 方法里到底该不该写业务逻辑?
java·agent·ai编程
脉动数据行情11 小时前
Java SpringBoot 国际期货批量采集实践 美原油 / 黄金 / 指数期货定时落库
java·开发语言·spring boot
Zane19941 小时前
写Stream时踩过的坑:中间操作不会真正执行,直到你调用这一个方法
java·后端
EXI-小洲1 小时前
Spring AI (第二章)大模型对话上下文记忆
java·人工智能·spring