這篇文章將為大家詳細(xì)講解有關(guān)java如何實(shí)現(xiàn)圖書館管理系統(tǒng),小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
網(wǎng)站制作、成都網(wǎng)站建設(shè)的關(guān)注點(diǎn)不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)一個展示的機(jī)會來證明自己,這并不會花費(fèi)您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗(yàn),一切以用戶為中心。具體內(nèi)容如下
思路:所有包都在book_manage包里
利用面向?qū)ο蟮亩鄳B(tài)特性去除了很多if-else的判斷,不同的子類在父類所對應(yīng)的方法不同。
1.首先建立一個book包
包里面有2個類,一個是Book,這個類里面包含一本書的全部信息
另外一個類是BookList,這個類是用來管理每一個書,通過這個類來尋找每一本書。
private Book[] books = new Book[100];
Book數(shù)組里面存放所有的書。
2.再建立一個包Operation 這個類里面有一個OI接口,通過對接口里面的Work方法重寫,來實(shí)現(xiàn)管理員身份和普通用戶身份的不同操作。
3.最后建立一個User包,里面有三個類,User,Admin,NormalUser
Admin和NormalUser都繼承自User.
User里秒你有一個數(shù)組
protected IO[] operation;
這個數(shù)組里面包含了用戶或者管理員所具備的操作。
通過對數(shù)組的索引來確定具體需要調(diào)用的操作方法。
下面來看看代碼吧:
book包
Book類
package book_manager.book; public class Book { private String name; private String id; private String author; private int price; private String type; private boolean isBorrow; public Book(String name, String id, String author, int price, String type, boolean isBorrow) { this.name = name; this.id = id; this.author = author; this.price = price; this.type = type; this.isBorrow = isBorrow; } @Override //Object中內(nèi)置的類,用來格式化打印book的信息 public String toString() { return "Book{" + "name='" + name + '\'' + ", id='" + id + '\'' + ", author='" + author + '\'' + ", price=" + price + ", type='" + type + '\'' + ", isBorrow=" + isBorrow + '}'; } public String getName(){ return name; } public boolean isBorrow(){ return isBorrow; } public void setBorrow(boolean bool){ this.isBorrow=bool; } public String getId(){ return id; } }
BookList類
package book_manager.book; import java.util.Arrays; public class BookList { private Book[] books = new Book[100]; private int size; public BookList(){ books[0] = new Book("金瓶梅", "001", "蘭陵笑笑生", 100, "古典名著", false); books[1] = new Book("水滸傳", "002", "施耐庵", 100, "古典名著", false); books[2] = new Book("西游記", "003", "吳承恩", 100, "古典名著", false); size = 3; } public int getSize(){ return size; } public void setBooks(int index,Book book){ books[index]=book; } public void setSize(int size){ this.size=size; } public Book getBook(int index){ return books[index]; } }
Operation包:
ADD類
package book_manager.Operation; import book_manager.book.*; import java.util.Scanner; public class ADD implements IO{ @Override public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("請輸入書名"); String name =scanner.next(); System.out.println("請輸入序號"); String id = scanner.next(); System.out.println("請輸入作者"); String author =scanner.next(); System.out.println("請輸入價格"); int price = scanner.nextInt(); System.out.println("請輸入類型"); String type = scanner.next(); Book book = new Book(name, id, author, price, type, false); bookList.setBooks(bookList.getSize(),book); bookList.setSize(bookList.getSize()+1); System.out.println("添加成功"); } }
Borrow類
package book_manager.Operation; import book_manager.book.Book; import book_manager.book.BookList; import java.util.Scanner; public class Borrow implements IO{ @Override public void work(BookList bookList) { int i=0; int flag=0; Scanner scan = new Scanner(System.in); System.out.println("請輸入需要借閱的書名"); String name = scan.next(); for(;iDelete類
package book_manager.Operation; import book_manager.book.BookList; import java.util.Scanner; public class Delete implements IO{ public void work(BookList bookList){ Scanner scanner = new Scanner(System.in); System.out.println("請輸入想要刪除的編號"); String id = scanner.next(); for(int i=0;iExit類
package book_manager.Operation; import book_manager.book.BookList; public class Exit implements IO{ @Override public void work(BookList bookList) { System.out.println("退出成功"); System.exit(0); } }Find類
package book_manager.Operation; import book_manager.book.BookList; import java.util.Scanner; public class Find implements IO{ @Override public void work(BookList bookList) { int i=0; int count=0; Scanner scan = new Scanner(System.in); System.out.println("請輸入需要查找的書名"); String name = scan.next(); for(;iIO接口
package book_manager.Operation; import book_manager.book.BookList; public interface IO { abstract public void work(BookList bookList); }PrintAll類
package book_manager.Operation; import book_manager.book.BookList; public class PrintAll implements IO{ @Override public void work(BookList bookList) { for(int i=0;iReturn類
package book_manager.Operation; import book_manager.book.BookList; import java.util.Scanner; public class Return implements IO{ @Override public void work(BookList bookList) { int i=0; int flag=0; Scanner scan = new Scanner(System.in); System.out.println("請輸入需要?dú)w還的ID"); String id = scan.next(); for(;iuser包:
User類
package book_manager.user; import book_manager.Operation.*; import book_manager.Operation.IO; import book_manager.book.BookList; abstract public class User { String name; protected IO[] operation; public User(String name){ this.name=name; } abstract public int menu();//該方法被重寫 public void doOperation(int choice, BookList bookList) { operation[choice].work(bookList); } }Admin類
package book_manager.user; import book_manager.Operation.*; import java.util.Scanner; public class Admin extends User{ public Admin(String name){ super(name); operation=new IO[]{ new Exit(), new Find(), new ADD(), new Delete(), new PrintAll(), }; } public int menu() { System.out.println("============"); System.out.println("hello " + name); 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.println("請輸入您的選擇: "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }NormalUser
package book_manager.user; import book_manager.Operation.*; import java.util.Scanner; public class NormalUser extends User{ public NormalUser(String name){ super(name); operation=new IO[]{ new Exit(), new Find(), new Borrow(), new Return(), new PrintAll() }; } public int menu() { System.out.println("============"); System.out.println("hello " + name); 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.println("請輸入您的選擇: "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); // close 本質(zhì)上是在關(guān)閉 System.in // 由于后面還需要用到 System.in, 此處不能盲目關(guān)閉. // scanner.close(); return choice; } }最后還有一個Test類,里面放了main函數(shù)
package book_manager; import book_manager.book.BookList; import book_manager.user.Admin; import book_manager.user.NormalUser; import book_manager.user.User; import java.util.Scanner; public class Test { public static void main(String[] args) { BookList list = new BookList(); User user = login(); //通過不同的choice和身份調(diào)用不同的Operation方法 while(true){ int choice = user.menu(); user.doOperation(choice, list); } } public static User login(){ Scanner scanner = new Scanner(System.in); System.out.println("請輸入你的姓名"); String name = scanner.next(); System.out.println("請輸入你的身份"); System.out.println("1.普通用戶 2.管理員"); int role= scanner.nextInt(); if(role==1){ return new NormalUser(name); } else{ return new Admin(name); } } }關(guān)于“java如何實(shí)現(xiàn)圖書館管理系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
標(biāo)題名稱:java如何實(shí)現(xiàn)圖書館管理系統(tǒng)-創(chuàng)新互聯(lián)
URL分享:http://weahome.cn/article/djsojo.html