您好,登錄后才能下訂單哦!
利用Java編寫一個簡單的圖書館管理系統?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
以下為小型圖書館管理系統模式圖:
模式總體概述:
其中IBorrower接口確定Borrower類標準,Library類為單例設計模式,即圖書館只有一個。Book類為Borrower類的內部類,libraryBook類為Library類的內部類。最后利用PlatForm類進一步的封裝。對于類方法中的核心,主要是數組的“增刪查改”操作,本項目中三次運用了這些操作,分別在Borrower、Library、PlatForm類方法中。
IBorrower接口:
// package com.wu.demo; /** * @date 2020-10-30 10:08:48 * @author 一夜星塵 * @version 1.8.0 */ // 圖書借閱人標準 public interface IBorrower { public abstract void borrowBook(String bookName,String bookAuthor,float bookPrice,Library libary); public abstract void returnBook(String bookName,String bookAuthor,float bookPrice,Library libary); }
Borrower類:
// package com.wu.demo; /** * @date 2020-10-30 10:11:00 * @author 一夜星塵 * @version 1.8.0 */ public class Borrower implements IBorrower{ protected String borrowerName; protected long borrowerNumber; private int booksNumber; private static final int MAX_NUMBER_OF_BOOKS = 4; //最大借書容量 private Book[] books = new Book[MAX_NUMBER_OF_BOOKS]; public Borrower(String borrowerName,long borrowerNumber) { this.borrowerName = borrowerName; this.borrowerNumber = borrowerNumber; this.booksNumber = 0; } private class Book{ private String bookName; private String bookAuthor; private float bookPrice; public Book(String bookName,String bookAuthor,float bookPrice){ Book.this.bookName = bookName; Book.this.bookAuthor = bookAuthor; Book.this.bookPrice = bookPrice; } public String getName() { return Book.this.bookName; } public String getAuthor() { return Book.this.bookAuthor; } public float getPrice() { return Book.this.bookPrice; } } @Override /** * 借書操作 */ public void borrowBook(String bookName,String bookAuthor,float bookPrice,Library library) { if(booksNumber>=MAX_NUMBER_OF_BOOKS) { System.out.println("超過最大借書容量"); return; } books[booksNumber++] = new Book(bookName,bookAuthor,bookPrice); // 移除圖書館書籍,更新圖書館信息 library.removeBook( bookName,bookAuthor, bookPrice); } @Override /** * 還書操作 */ public void returnBook(String bookName,String bookAuthor,float bookPrice,Library library) { // 前提是有書的情況下 if(booksNumber<=0) { return; } int index = -1; for(int i = 0 ; i < books.length ; i++) { if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) && books[i].bookPrice == bookPrice) { index = i; break; } } if(index < 0) { System.out.println("你并沒有該書籍"); return; } for(int i = 0; i < booksNumber-1 ; i++) { if(i>=index) { books[i] = books[i+1]; } } booksNumber --; // 還入圖書館,更新圖書館信息 library.addBook(bookName,bookAuthor,bookPrice); } /** * 借書人信息 */ public void borrowerInfo() { int i = 0; System.out.println("------------------------------借閱人信息------------------------------"); System.out.println("借閱人姓名:"+this.borrowerName); System.out.println("借閱人學號:"+this.borrowerNumber); for(;i < booksNumber ; i++) { System.out.println("第"+(i+1)+"本書: "); System.out.println("書籍名稱:"+books[i].getName()); System.out.println("書籍作者:"+books[i].getAuthor()); System.out.println("書籍價格:"+books[i].getPrice()); } System.out.println("當前借閱書本數為: "+booksNumber+" 剩余容量為: "+(MAX_NUMBER_OF_BOOKS-i)); System.out.println("------------------------------------------------------------------------"); } }
Library類:
// package com.wu.demo; /** * @date 2020-10-30 10:32:42 * @author 一夜星塵 * @version 1.8.0 */ public class Library { private static final Library library = new Library(); private static final int MAX_NUMBER_OF_BOOKS = 120; // 圖書館最大書容量 private libraryBook[] books = new libraryBook[MAX_NUMBER_OF_BOOKS]; protected static int booksNumber = 0; // 無參構造函數私有化 private Library() {}; private class libraryBook{ private String bookName; private String bookAuthor; private float bookPrice; public libraryBook(String bookName,String bookAuthor,float bookPrice){ libraryBook.this.bookName = bookName; libraryBook.this.bookAuthor = bookAuthor; libraryBook.this.bookPrice = bookPrice; } public void setName(String bookName) { libraryBook.this.bookName = bookName; } public void setAuthor(String bookAuthor) { libraryBook.this.bookAuthor = bookAuthor; } public void setPrice(float bookPrice) { libraryBook.this.bookPrice = bookPrice; } } // 唯一返回的單例 static Library getLibrary() { return library; } /** * 增添書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void addBook(String bookName,String bookAuthor,float bookPrice) { if(booksNumber>=MAX_NUMBER_OF_BOOKS) { System.out.println("超過圖書館書籍最大容量"); return; } books[booksNumber++] = new libraryBook(bookName,bookAuthor,bookPrice); } /** * 刪除書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void removeBook(String bookName,String bookAuthor,float bookPrice) { int index = -1; for(int i = 0 ; i < booksNumber ; i++) { if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) && books[i].bookPrice == bookPrice) { index = i; break; } } if(index < 0) { System.out.println("該圖書未在庫"); return; } for(int i = 0;i < booksNumber - 1;i++) { if(index <= i) { books[i] = books[i+1]; } } booksNumber --; } /** * 查找書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void checkBook(String bookName,String bookAuthor,float bookPrice) { System.out.println(""); for(int i = 0 ; i<booksNumber ; i++) { if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) && books[i].bookPrice == bookPrice) { System.out.println("狀態:書籍在庫"); return; } } System.out.println("狀態:書籍未在庫"); System.out.println(""); } /** * 更改書籍信息 * @param bookName * @param bookAuthor * @param bookPrice */ public void changeBook(String bookName, String bookAuthor,float bookPrice,String newBookName, String newBookAuthor,float newBookPrice) { for(int i = 0 ;i<booksNumber;i++) { if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) && books[i].bookPrice == bookPrice) { books[i].setName(newBookName); books[i].setAuthor(newBookAuthor); books[i].setPrice(newBookPrice); break; } } System.out.println("更改書籍完成"); } /** * 查看當前圖書館書籍信息 */ public void booksInfo() { System.out.println("-----------------------------圖書館書籍信息-----------------------------"); for(int i = 0 ; i<booksNumber;i++) { System.out.println("在庫書籍 "+(i+1)+" : "); System.out.println("書籍名稱:"+books[i].bookName); System.out.println("書籍作者:"+books[i].bookAuthor); System.out.println("書籍價格:"+books[i].bookPrice); } System.out.println("在庫書籍總數:"+booksNumber+" 剩余可借閱書籍總數:"+(MAX_NUMBER_OF_BOOKS-booksNumber)); System.out.println("--------------------------------------------------------------------------"); } }
PlatForm類:
// package com.wu.demo; import java.util.Scanner; /** * @date 2020-10-30 14:06:34 * @author 一夜星塵 * @version 1.8.0 */ // 進一步封裝 public class PlatForm { private static final int MAX_NUMBER_OF_BORROWERS = 30; // 平臺最多可注冊30名借閱者 private Borrower[] borrowers = new Borrower[MAX_NUMBER_OF_BORROWERS]; private static int numberBorrowers = 0; private Library library = Library.getLibrary(); public PlatForm() { Scanner input = new Scanner(System.in); // 初始化Borrower成員 System.out.println("-----------------------------初始化借閱人-----------------------------"); System.out.println("按任意鍵繼續(按Q或q退出):"); String exit = input.nextLine(); System.out.println("以下輸入可借閱人員信息"); while(!exit.equalsIgnoreCase("q")) { System.out.print("姓名 學號:"); String tempName = input.next(); long tempNumber = input.nextLong(); this.borrowers[numberBorrowers++] = new Borrower(tempName,tempNumber); // 回車輸入舍棄 input.nextLine(); System.out.println("按任意鍵繼續(按Q或q退出):"); exit = input.nextLine(); if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS){ System.out.println("已達到最大注冊量"); break; } } // 初始化圖書館信息 System.out.println("---------------------------初始化圖書館圖書信息------------------------"); System.out.println("按任意鍵繼續(按Q或q退出):"); exit = input.nextLine(); System.out.println("請輸入原始書籍信息:"); while(!exit.equalsIgnoreCase("q")) { System.out.print("書籍名稱 作者 價格:"); String tempName = input.next(); String tempAuthor = input.next(); float tempPrice = input.nextFloat(); library.addBook(tempName,tempAuthor,tempPrice); // 回車輸入舍棄 input.nextLine(); System.out.println("按任意鍵繼續(按Q或q退出):"); exit = input.nextLine(); } System.out.println("-----------------------------------------------------------------------"); input.close(); } /** * 平臺管理:增添用戶 * @param borrowerName * @param borrowerNumber */ public void addClient(String borrowerName,long borrowerNumber) { if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS) { System.out.println("用戶容量已達最大值"); return; } this.borrowers[numberBorrowers++] = new Borrower(borrowerName,borrowerNumber); System.out.println("增添用戶操作成功"); } /** * 平臺管理:移除用戶 * @param borrowerName * @param borrowerNumber */ public void removeClient(String borrowerName,long borrowerNumber) { int index = -1; for(int i = 0 ; i<numberBorrowers; i++) { if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) { index = i; break; } } if(index<0) { System.out.println("未找到用戶"); return; } for(int i = 0 ; i < numberBorrowers-1;i++) { if(index <= i) { borrowers[i] = borrowers[i+1]; } } numberBorrowers--; System.out.println("移除用戶成功"); } /** * 相應借閱人的信息 * @param borrowerName * @param borrowerNumber */ public void platformBorrowerInfo(String borrowerName,long borrowerNumber) { for(int i = 0 ; i<numberBorrowers;i++) { if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) { borrowers[i].borrowerInfo(); return; } } System.out.println("該用戶不存在"); } /** * 相應借閱人借閱書籍 * @param borrowerName * @param borrowerNumber * @param bookName * @param bookAuthor * @param bookPrice */ public void platformBorrowBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor, float bookPrice) { for(int i = 0 ; i<borrowers.length;i++) { if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) { borrowers[i].borrowBook(bookName,bookAuthor, bookPrice, this.library); break; } } } /** * 相應借閱人歸還書籍 * @param borrowerName * @param borrowerNumber * @param bookName * @param bookAuthor * @param bookPrice */ public void platformRrturnBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor, float bookPrice){ for(int i = 0 ; i<borrowers.length;i++) { if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) { borrowers[i].returnBook(bookName,bookAuthor, bookPrice, library); break; } } } /** * 輸出圖書館信息 */ public void platformLibraryInfo() { library.booksInfo(); } /** * 對圖書館增添書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void platformLibraryAddBook(String bookName,String bookAuthor,float bookPrice) { library.addBook(bookName,bookAuthor,bookPrice); } /** * 對圖書館移除書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void platformLibraryRemoveBook(String bookName,String bookAuthor,float bookPrice) { library.removeBook(bookName,bookAuthor,bookPrice); } /** * 對圖書館檢索書籍 * @param bookName * @param bookAuthor * @param bookPrice */ public void platformLibraryCheckBook(String bookName,String bookAuthor,float bookPrice) { library.checkBook(bookName, bookAuthor, bookPrice); } /** * 對圖書館書籍更改 * @param bookName * @param bookAuthor * @param bookPrice * @param newBookName * @param newBookAuthor * @param newBookPrice */ public void platformLibraryChangeBook(String bookName,String bookAuthor,float bookPrice, String newBookName,String newBookAuthor,float newBookPrice) { library.changeBook(bookName, bookAuthor, bookPrice,newBookName,newBookAuthor,newBookPrice); } }
testDemo(測試):
// package com.wu.demo; /** * @date 2020-10-30 10:51:21 * @author 一夜星塵 * @version 1.8.0 */ public class testDemo { public static void main(String[] args) { PlatForm platform = new PlatForm(); // 以下為平臺管理權限的測試 platform.addClient("張三", 100000L); platform.addClient("李四", 100001L); platform.addClient("王五", 100002L); platform.addClient("趙六", 100003L); platform.platformBorrowerInfo("張三",100000L); platform.platformBorrowerInfo("李四",100000L); // 此時應該為不存在 platform.platformBorrowerInfo("王五",100002L); platform.platformBorrowerInfo("趙六",100003L); platform.removeClient("王五", 100002L); platform.platformBorrowerInfo("王五",100002L); // 此時王五不是用戶 //以下為圖書館和借閱人的測試 platform.platformLibraryAddBook("數據結構","匿名1",100.0F); platform.platformLibraryAddBook("網絡基礎","匿名2",90.0F); platform.platformLibraryAddBook("算法","匿名3",110.0F); platform.platformLibraryInfo(); // 當前圖書館里只有3本書 platform.platformLibraryAddBook("java基礎","匿名4",105.0F); platform.platformLibraryAddBook("深度學習","匿名5",129.0F); platform.platformLibraryAddBook("Linux基礎","匿名6",104.0F); platform.platformLibraryAddBook("matlab基礎","匿名7",123.0F); platform.platformLibraryAddBook("visual C++基礎","匿名8",95.0F); platform.platformLibraryAddBook("HTML CSS JS","匿名9",99.0F); platform.platformLibraryAddBook("C++基礎","匿名10",119.0F); platform.platformLibraryAddBook("python基礎","匿名11",108.0F); platform.platformLibraryAddBook("數據庫基礎","匿名12",120.0F); platform.platformLibraryAddBook("操作系統基礎","匿名13",106.0F); platform.platformLibraryInfo(); // 當前圖書館里有13本書 platform.platformLibraryRemoveBook("visual C++基礎","匿名8",95.0F); platform.platformLibraryInfo(); // 當前圖書館里有12本書 platform.platformLibraryRemoveBook("Linux基礎" ,"匿名6", 104.0F); // 檢索圖書 Linux基礎 狀態 platform.platformLibraryChangeBook("python基礎", "匿名11", 108.0F,"keras基礎","匿名11",110.0F); // 變更圖書館書籍名稱 python基礎 為 keras基礎 platform.platformBorrowBook("張三", 100000L, "java基礎", "匿名4", 105.0F); // 用戶 張三 借了書籍 java基礎 platform.platformBorrowBook("張三", 100000L, "算法", "匿名3", 110.0F); // 用戶 張三 又借了書籍 算法 platform.platformBorrowBook("張三", 100000L, "C++基礎", "匿名10", 119.0F); // 用戶 張三 又借了書籍 C++基礎 platform.platformBorrowBook("張三", 100000L, "深度學習", "匿名5", 129.0F); // 用戶 張三 又借了書籍 深度學習 platform.platformBorrowBook("趙六", 100003L, "網絡基礎", "匿名2", 84.0F); // 用戶 趙六 借了書籍 網絡基礎 platform.platformBorrowBook("李四", 100001L, "HTML CSS JS", "匿名9", 119.0F); // 用戶 李四 借了書籍 HTML CSS JS platform.platformBorrowerInfo("張三", 100000L); // 查看用戶 張三 借閱情況 當前用戶 張三 有四本書 platform.platformBorrowerInfo("趙六", 100003L); // 查看用戶 趙六 借閱情況 當前用戶 趙六 有一本書 platform.platformBorrowerInfo("李四", 100001L); // 查看用戶 李四 借閱情況 當前用戶 李四 有一本書 platform.platformRrturnBook("張三", 100000L, "算法", "匿名3", 110.0F); // 用戶 張三 還了書籍 算法 platform.platformRrturnBook("張三", 100000L, "深度學習", "匿名5", 129.0F); // 用戶 張三 又還了書籍 深度學習 platform.platformBorrowerInfo("張三", 100000L); // 查看用戶 張三 借閱情況 當前用戶 張三 只有兩本書 platform.platformLibraryRemoveBook("java基礎" ,"匿名4", 105.0F); // 檢索圖書 java基礎 狀態 當前狀態應該為 未在庫 } }
測試結果顯示:
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。