專案1第五課——圖書管理系統測試管理員功能
阿新 • • 發佈:2018-11-12
上節課我們把管理員和書籍容器搞定了,這節課我們就來測試下他們的功能,順便把管理員登入做一下。
Login:
package com.bookmanagement; import java.util.Scanner; public class Login { public static void main(String[] args) { printMenu(); intchoose = 0; boolean flag = true; //Scannner類物件可以從控制套獲取通過鍵盤輸入的資料 //這個scanner物件只需要構造一次即可多次呼叫 Scanner scanner = new Scanner(System.in); //選擇登入使用者的型別 do { choose = scanner.nextInt(); switch (choose) {case 1: flag = false; Admin admin = loginAdmin(); AdminSystem managerSystem = toManager(admin); break; case 2: flag = false; break; default: System.out.print("請輸入1或2: "); break; } }while (flag); } private static Admin loginAdmin() { Scanner scanner = new Scanner(System.in); System.out.print("請輸入管理員使用者名稱: "); String username = scanner.next(); System.out.print("請輸入管理員密碼: "); String password = scanner.next(); Admin admin = Admin.getInstance(); if (admin.getUsername().equals(username) && admin.getPassword().equals(password)) { System.out.println("登入成功"); return admin; } else { System.out.println("登入失敗"); loginAdmin(); } return null; } public static void printMenu() { System.out.println("請選擇管理員或使用者登入:"); System.out.println("1.管理員"); System.out.println("2.使用者"); System.out.print("請選擇: "); } public static AdminSystem toManager(Admin admin) { return new AdminSystem(admin); } }
AdminSystem:
package com.bookmanagement; import java.util.Collection; import java.util.Scanner; public class AdminSystem { private BookContainer bookContainer = BookContainer.getInstance(); private Admin admin = null; public AdminSystem(Admin admin) { this.admin = admin; printAdminSystemMenu(); chooseOption(); } private void printAdminSystemMenu() { System.out.println("1.錄入書籍資訊"); System.out.println("2.刪除書籍資訊"); System.out.println("3.修改書籍資訊"); System.out.println("4.查詢書籍資訊"); } private void chooseOption() { int choose = 0; boolean flag = true; Scanner scanner = new Scanner(System.in); do { System.out.print("請輸入您想要使用的功能: "); choose = scanner.nextInt(); switch (choose) { case 1: toAddBook(); break; case 2: toDeleteBook(); break; case 3: toUpdateBook(); break; case 4: toFindBook(); break; default: break; } }while (flag); } public void toAddBook() { Scanner scanner = new Scanner(System.in); String bookId = null; do { System.out.print("請輸入書籍的編號: "); bookId = scanner.next(); }while (bookContainer.isBookExist(bookId)); //這裡要判斷系統裡是否已經存在相同的書籍id //這裡就用書籍容器完成,畢竟是系統內部操作 System.out.print("請輸入書名: "); String name = scanner.next(); System.out.print("請輸入作者: "); String author = scanner.next(); System.out.println("請輸入出版日期: "); String publishDate = scanner.next(); Book book = new Book(bookId,name,author,publishDate); //新增書籍由管理員來完成 admin.insertBook(book); System.out.println("新增完成"); printAdminSystemMenu(); chooseOption(); } public void toDeleteBook() { Scanner scanner = new Scanner(System.in); System.out.println("請輸入要刪除的書籍的id"); String bookId = scanner.next(); if (bookContainer.isBookExist(bookId)) { admin.delete(bookId); System.out.println("刪除完成"); } else { System.out.println("未找到該書籍"); } System.out.println("是否繼續刪除書籍(Y/N): "); String choose = scanner.next(); if (choose.equals("y") || choose.equals("Y")) toDeleteBook(); else { printAdminSystemMenu(); chooseOption(); } } public void toUpdateBook() { Scanner scanner = new Scanner(System.in); System.out.println("請輸入要刪除的書籍的id"); String bookId = scanner.next(); if (bookContainer.isBookExist(bookId)) { System.out.print("請輸入書名: "); String name = scanner.next(); System.out.print("請輸入作者: "); String author = scanner.next(); System.out.println("請輸入出版日期: "); String publishDate = scanner.next(); Book book = new Book(bookId,name,author,publishDate); admin.uodateBookInfo(book); } else { System.out.println("未找到該書籍"); } printAdminSystemMenu(); chooseOption(); } public void toFindBook() { System.out.println("請選擇:"); System.out.println("1.檢視所有書籍"); System.out.println("2.根據id查詢書籍"); int choose; Scanner scanner = new Scanner(System.in); boolean flag = true; do { choose = scanner.nextInt(); switch (choose) { case 1: flag = false; Collection<Book> allBooks = admin.findAllBooks(); for (Book book : allBooks) { System.out.println(book); } System.out.println("輸入任意字串返回主選單..."); scanner.next(); printAdminSystemMenu(); chooseOption(); break; case 2: flag = false; System.out.print("請輸入要查詢書籍的編號: "); String bookId = scanner.next(); Book book = admin.findBookById(bookId); if (book == null) System.out.println("書籍不存在"); else System.out.println(book); System.out.println("輸入任意字串返回主選單..."); scanner.next(); printAdminSystemMenu(); chooseOption(); break; default: break; } }while (flag); } }
BookContainer.isBookExist:
public boolean isBookExist(String bookId) { return books.containsKey(bookId); }
不美觀的地方大家可以自行修改