答案:圖書管理系統(tǒng)通過(guò)Book類和Library類實(shí)現(xiàn)增刪查借功能,使用ArrayList管理圖書,提供菜單交互。
圖書管理系統(tǒng)是學(xué)習(xí)Java編程時(shí)常見(jiàn)的實(shí)踐項(xiàng)目,能幫助掌握面向?qū)ο缶幊?/a>、集合操作和基礎(chǔ)的控制流程。以下是實(shí)現(xiàn)一個(gè)簡(jiǎn)單圖書管理系統(tǒng)的思路與代碼示例。
每本書包含基本信息如書名、作者、ISBN編號(hào)和是否借出的狀態(tài)。
class Book { private String title; private String author; private String isbn; private boolean isBorrowed; <pre class='brush:java;toolbar:false;'>public Book(String title, String author, String isbn) { this.title = title; this.author = author; this.isbn = isbn; this.isBorrowed = false; } // Getter 和 Setter 方法 public String getTitle() { return title; } public String getAuthor() { return author; } public String getIsbn() { return isbn; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "書名:" + title + " | 作者:" + author + " | ISBN:" + isbn + " | 狀態(tài):" + (isBorrowed ? "已借出" : "可借閱"); }
}
使用ArrayList存儲(chǔ)圖書,并提供增刪查借等操作。
立即學(xué)習(xí)“Java免費(fèi)學(xué)習(xí)筆記(深入)”;
import java.util.ArrayList; import java.util.Scanner; <p>class Library { private ArrayList<Book> books = new ArrayList<>(); private Scanner scanner = new Scanner(System.in);</p><pre class='brush:java;toolbar:false;'>// 添加圖書 public void addBook() { System.out.print("輸入書名:"); String title = scanner.nextLine(); System.out.print("輸入作者:"); String author = scanner.nextLine(); System.out.print("輸入ISBN:"); String isbn = scanner.nextLine(); books.add(new Book(title, author, isbn)); System.out.println("圖書添加成功!"); } // 查找圖書(按書名或作者) public void searchBook() { System.out.print("輸入要查找的關(guān)鍵詞(書名或作者):"); String keyword = scanner.nextLine().toLowerCase(); boolean found = false; for (Book book : books) { if (book.getTitle().toLowerCase().contains(keyword) || book.getAuthor().toLowerCase().contains(keyword)) { System.out.println(book); found = true; } } if (!found) { System.out.println("未找到相關(guān)圖書。"); } } // 顯示所有圖書 public void displayAllBooks() { if (books.isEmpty()) { System.out.println("圖書館暫無(wú)圖書。"); return; } for (Book book : books) { System.out.println(book); } } // 借閱圖書 public void borrowBook() { System.out.print("輸入要借閱的ISBN:"); String isbn = scanner.nextLine(); for (Book book : books) { if (book.getIsbn().equals(isbn)) { if (!book.isBorrowed()) { book.setBorrowed(true); System.out.println("成功借閱:" + book.getTitle()); } else { System.out.println("該書已被借出。"); } return; } } System.out.println("未找到該ISBN的圖書。"); } // 歸還圖書 public void returnBook() { System.out.print("輸入要?dú)w還的ISBN:"); String isbn = scanner.nextLine(); for (Book book : books) { if (book.getIsbn().equals(isbn)) { if (book.isBorrowed()) { book.setBorrowed(false); System.out.println("成功歸還:" + book.getTitle()); } else { System.out.println("該書本未被借出。"); } return; } } System.out.println("未找到該ISBN的圖書。"); }
}
提供菜單驅(qū)動(dòng)的交互界面,用戶可通過(guò)選擇數(shù)字執(zhí)行對(duì)應(yīng)功能。
public class Main { public static void main(String[] args) { Library library = new Library(); Scanner scanner = new Scanner(System.in); int choice; <pre class='brush:java;toolbar:false;'> do { System.out.println("\n--- 圖書管理系統(tǒng) ---"); 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.print("請(qǐng)選擇操作:"); choice = scanner.nextInt(); scanner.nextLine(); // 消費(fèi)換行符 switch (choice) { case 1: library.addBook(); break; case 2: library.searchBook(); break; case 3: library.displayAllBooks(); break; case 4: library.borrowBook(); break; case 5: library.returnBook(); break; case 6: System.out.println("退出系統(tǒng)。"); break; default: System.out.println("無(wú)效選擇,請(qǐng)重試。"); } } while (choice != 6); scanner.close(); }
}
基本上就這些。通過(guò)這個(gè)結(jié)構(gòu),你可以輕松擴(kuò)展功能,比如加入文件讀寫保存數(shù)據(jù)、用戶權(quán)限管理或圖形界面。核心是理解類的設(shè)計(jì)與集合的使用。不復(fù)雜但容易忽略細(xì)節(jié),比如Scanner的換行處理和字符串匹配的大小寫問(wèn)題。建議運(yùn)行后多測(cè)試邊界情況。
以上就是如何使用Java實(shí)現(xiàn)圖書管理系統(tǒng)的基本功能的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個(gè)人都需要一臺(tái)速度更快、更穩(wěn)定的 PC。隨著時(shí)間的推移,垃圾文件、舊注冊(cè)表數(shù)據(jù)和不必要的后臺(tái)進(jìn)程會(huì)占用資源并降低性能。幸運(yùn)的是,許多工具可以讓 Windows 保持平穩(wěn)運(yùn)行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)