推薦學習:《》
1.輸入姓名
2.選擇身份
3.根據身份列印不同功能選單
book(包)
operation(功能包)
user(使用者包)
package book;/** * User: gu'jiu * Date: 2022-05-18 * Time: 14:45 * Description:書 */public class Book { private String name;//書名 private String author;//作者 private double price;//價格 private String type;//型別 private boolean state;//書的狀態(已借出or未借出) //對書進行初始化,書的狀態不用進行初始化(預設未借出) public Book(String name, String author, double price, String type) { this.name = name; this.author = author; this.price = price; this.type = type; this.state = state; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getType() { return type; } public void setType(String type) { this.type = type; } public boolean isState() { return state; } public void setState(boolean state) { this.state = state; } @Override public String toString() { return "[" + "name='" + name + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ", " + (state == true ? "已借出" : "未借出") + ']'; }}
package book;/** * User: gu'jiu * Date: 2022-05-18 * Time: 14:52 * Description:書架 */public class BookList { private Book[] books = new Book[10]; private int usedSize; public BookList() { books[0] = new Book("三國演義","羅貫中",19.9,"小說"); books[1] = new Book("西遊記","吳承恩",29.9,"小說"); books[2] = new Book("紅樓夢","曹雪芹",26.9,"小說"); usedSize = 3; } public Book getBooks(int pos) {//輸入下標獲取該下標的這本書 return books[pos]; } public void setBooks(int pos, Book book) {//輸入下標和一本書,在該下標放入一本書 this.books[pos] = book; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; }}
package operation;import book.BookList;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:07 * Description:介面 */public interface IOperation { void work(BookList bookList);}
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:09 * Description:查詢書籍資訊 */public class FindOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("查詢圖書"); System.out.println("------------------------"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入書的名稱:"); String name = scanner.nextLine(); //獲取書架中圖書數量 int size = bookList.getUsedSize(); //根據名字查詢 for (int i = 0; i < size; i++) { Book book = bookList.getBooks(i); if (book.getName().equals(name)) { System.out.println(book); return; } } System.out.println("未找到該書。"); }}
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:04 * Description:新增書籍 */public class AddOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("新增圖書"); System.out.println("------------------------"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入書名:"); String name = scanner.nextLine(); System.out.print("請輸入作者:"); String author = scanner.nextLine(); System.out.print("請輸入書的型別:"); String type = scanner.nextLine(); System.out.print("請輸入價格:"); double price = scanner.nextDouble(); Book book = new Book(name, author, price, type); bookList.setBooks(bookList.getUsedSize(), book);//將書放入書架 bookList.setUsedSize(bookList.getUsedSize()+1);//書籍數量+1 }}
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:05 * Description:刪除書籍 */public class DelOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("刪除圖書"); System.out.println("------------------------"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入要刪除的書名:"); String name = scanner.nextLine(); //獲取書架中現在有幾本書 int size = bookList.getUsedSize(); int i = 0; //根據書名找到這個本書的下標 for (i = 0; i < size; i++) { Book book = bookList.getBooks(i); if (book.getName().equals(name)) { break; } } if (i == size) { System.out.println("刪除失敗,未找到該書。"); return; } //通過前移將書刪除 for (i = 0; i < size - 1; i++) { bookList.setBooks(i,bookList.getBooks(i + 1)); } bookList.setUsedSize(bookList.getUsedSize()-1);//書架中圖書數量-1 System.out.println("刪除成功。"); }}
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:05 * Description:借閱書籍 */public class BorrowOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("借閱圖書"); System.out.println("------------------------"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入要借書的名稱:"); String name = scanner.nextLine(); //獲取書架中現在有幾本書 int size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getBooks(i); //判斷在書架中是否有這本書 if (book.getName().equals(name)) { //判斷書是否被借走 if (!book.isState()) { System.out.println("借閱成功!"); book.setState(true);//將書的狀態改為已借出 return; } else { System.out.println("借閱失敗,該書已被別人借走。"); return; } } } System.out.println("借閱失敗,未找到該書。"); }}
package operation;import book.Book;import book.BookList;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:10 * Description:歸還書籍 */public class ReturnOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("歸還圖書"); System.out.println("------------------------"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入要還書的名稱:"); String name = scanner.nextLine(); //獲取書架中現在有幾本書 int size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getBooks(i); //判斷在書架中是否有這本書 if (book.getName().equals(name)) { //判斷書是否已被借走 if (book.isState()) { System.out.println("歸還成功!"); book.setState(false);//將書的狀態改為未借出 return; } else { System.out.println("歸還失敗,該書已還未被借走。"); return; } } } System.out.println("歸還失敗,未找到該書。"); }}
package operation;import book.Book;import book.BookList;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:06 * Description:列印全部書籍資訊 */public class DisplayOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("顯示全部圖書"); System.out.println("------------------------"); int size = bookList.getUsedSize(); for (int i = 0; i < size; i++) { Book book = bookList.getBooks(i); System.out.println(book); } }}
import book.BookList;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:08 * Description:退出系統 */public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("退出系統。"); System.out.println("------------------------"); System.exit(0); }}
package user;import book.BookList;import operation.IOperation;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:11 * Description:使用者 */abstract public class User { protected String userName;//使用者姓名 protected IOperation[] iOperations;//功能陣列 public User(String userName) { this.userName = userName; } public abstract int menu(); //輸入選擇,呼叫不同的功能 public void doOperation(int choice, BookList bookList) { this.iOperations[choice].work(bookList);//動態繫結 }}
package user;import operation.*;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:13 * Description:管理員 */public class AdminUser extends User{ public AdminUser(String userName) { super(userName); //管理員功能 this.iOperations = new IOperation[] { new ExitOperation(), new FindOperation(), new AddOperation(), new DelOperation(), new DisplayOperation() }; } @Override public int menu() { System.out.println(); System.out.println("hello " + this.userName + " 歡迎來到圖書館"); while(true) { System.out.println("------------------------"); System.out.println("1.查詢圖書"); System.out.println("2.新增圖書"); System.out.println("3.刪除圖書"); System.out.println("4.顯示全部圖書"); System.out.println("0.退出系統"); System.out.println("------------------------"); System.out.print("請輸入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); //保證輸入數位有效 if (choice > 4 || choice < 0) { System.out.println("輸入有誤請重新輸入。"); } else { return choice; } } }}
package user;import operation.*;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:13 * Description:學生 */public class NormalUser extends User{ public NormalUser (String userName) { super(userName); //使用者功能 this.iOperations = new IOperation[] { new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation(), }; } @Override public int menu() { System.out.println(); System.out.println("hello "+this.userName+" 歡迎來到圖書館"); while(true) { System.out.println("------------------------"); System.out.println("1.查詢圖書"); System.out.println("2.借閱圖書"); System.out.println("3.歸還圖書"); System.out.println("0.退出系統"); System.out.println("------------------------"); System.out.print("請輸入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); //保證輸入數位有效 if (choice > 3 || choice < 0) { System.out.println("輸入有誤請重新輸入。"); } else { return choice; } } }}
import book.BookList;import user.AdminUser;import user.NormalUser;import user.User;import java.util.Scanner;/** * User: gu'jiu * Date: 2022-05-18 * Time: 15:20 * Description:圖書管理系統 */public class Test { public static User login() { System.out.println("歡迎使用圖書管理系統"); Scanner scanner = new Scanner(System.in); System.out.print("請輸入您的姓名:"); String name = scanner.nextLine(); while(true){ System.out.println("-----------------------------"); System.out.println("請選擇您的身份:1>管理員 2>普通使用者"); System.out.print("請輸入:"); int choice = scanner.nextInt(); //保證輸入數位有效 if (choice == 1) { return new AdminUser(name); } else if (choice == 2) { return new NormalUser(name); } else { System.out.println("輸入有誤,請重新輸入。"); } } } public static void main(String[] args) { BookList bookList = new BookList(); User user = login();//向上轉型 while(true){ int choice = user.menu(); user.doOperation(choice, bookList); } }}
主函數中的user.doOperation(choice, bookList);
中的doOperation
方法比較難理解,我們來解釋一下
1.user.
參照的可能是兩個物件
2.在範例化
這物件的時候,在構造方法中,我們已經將iOperations[]
準備好了。
3.根據輸入下標
(例如:2)存取相應物件,再呼叫work
方法。
推薦學習:《》
以上就是Java圖文詳解之實現圖書管理系統的詳細內容,更多請關注TW511.COM其它相關文章!