本篇內(nèi)容介紹了“如何使用Java實(shí)現(xiàn)圖書管理系統(tǒng)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站推廣、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為水電改造等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。
1.輸入姓名
2.選擇身份
3.根據(jù)身份打印不同功能菜單
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;//價(jià)格 private String type;//類型 private boolean state;//書的狀態(tài)(已借出or未借出) //對書進(jìn)行初始化,書的狀態(tài)不用進(jìn)行初始化(默認(rèn)未借出) 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) {//輸入下標(biāo)獲取該下標(biāo)的這本書 return books[pos]; } public void setBooks(int pos, Book book) {//輸入下標(biāo)和一本書,在該下標(biāo)放入一本書 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(); //獲取書架中圖書數(shù)量 int size = bookList.getUsedSize(); //根據(jù)名字查找 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("請輸入價(jià)格:"); double price = scanner.nextDouble(); Book book = new Book(name, author, price, type); bookList.setBooks(bookList.getUsedSize(), book);//將書放入書架 bookList.setUsedSize(bookList.getUsedSize()+1);//書籍?dāng)?shù)量+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("請輸入要?jiǎng)h除的書名:"); String name = scanner.nextLine(); //獲取書架中現(xiàn)在有幾本書 int size = bookList.getUsedSize(); int i = 0; //根據(jù)書名找到這個(gè)本書的下標(biāo) 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);//書架中圖書數(shù)量-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(); //獲取書架中現(xiàn)在有幾本書 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);//將書的狀態(tài)改為已借出 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(); //獲取書架中現(xiàn)在有幾本書 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);//將書的狀態(tài)改為未借出 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:退出系統(tǒng) */public class ExitOperation implements IOperation{ @Override public void work(BookList bookList) { System.out.println(); System.out.println("退出系統(tǒng)。"); 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;//功能數(shù)組 public User(String userName) { this.userName = userName; } public abstract int menu(); //輸入選擇,調(diào)用不同的功能 public void doOperation(int choice, BookList bookList) { this.iOperations[choice].work(bookList);//動(dòng)態(tài)綁定 }}
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.退出系統(tǒng)"); System.out.println("------------------------"); System.out.print("請輸入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); //保證輸入數(shù)字有效 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:學(xué)生 */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.退出系統(tǒng)"); System.out.println("------------------------"); System.out.print("請輸入你的操作:"); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); //保證輸入數(shù)字有效 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:圖書管理系統(tǒng) */public class Test { public static User login() { System.out.println("歡迎使用圖書管理系統(tǒng)"); 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(); //保證輸入數(shù)字有效 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();//向上轉(zhuǎn)型 while(true){ int choice = user.menu(); user.doOperation(choice, bookList); } }}
主函數(shù)中的user.doOperation(choice, bookList);
中的doOperation
方法比較難理解,我們來解釋一下
1.user.
引用的可能是兩個(gè)對象
2.在實(shí)例化
這對象的時(shí)候,在構(gòu)造方法中,我們已經(jīng)將iOperations[]
準(zhǔn)備好了。
3.根據(jù)輸入下標(biāo)
(例如:2)訪問相應(yīng)對象,再調(diào)用work
方法。
“如何使用Java實(shí)現(xiàn)圖書管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!