這篇文章將為大家詳細(xì)講解有關(guān)如何使用java實(shí)現(xiàn)圖書館管理系統(tǒng),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到寧都網(wǎng)站設(shè)計(jì)與寧都網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋寧都地區(qū)。
具體內(nèi)容如下
思路:所有包都在book_manage包里
利用面向?qū)ο蟮亩鄳B(tài)特性去除了很多if-else的判斷,不同的子類在父類所對(duì)應(yīng)的方法不同。
1.首先建立一個(gè)book包
包里面有2個(gè)類,一個(gè)是Book,這個(gè)類里面包含一本書的全部信息另外一個(gè)類是BookList,這個(gè)類是用來管理每一個(gè)書,通過這個(gè)類來尋找每一本書。private Book[] books = new Book[100];Book數(shù)組里面存放所有的書。
2.再建立一個(gè)包Operation 這個(gè)類里面有一個(gè)OI接口,通過對(duì)接口里面的Work方法重寫,來實(shí)現(xiàn)管理員身份和普通用戶身份的不同操作。
3.最后建立一個(gè)User包,里面有三個(gè)類,User,Admin,NormalUser
Admin和NormalUser都繼承自User.User里秒你有一個(gè)數(shù)組protected IO[] operation;這個(gè)數(shù)組里面包含了用戶或者管理員所具備的操作。通過對(duì)數(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("請(qǐng)輸入書名"); String name =scanner.next(); System.out.println("請(qǐng)輸入序號(hào)"); String id = scanner.next(); System.out.println("請(qǐng)輸入作者"); String author =scanner.next(); System.out.println("請(qǐng)輸入價(jià)格"); int price = scanner.nextInt(); System.out.println("請(qǐng)輸入類型"); 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("請(qǐng)輸入需要借閱的書名"); String name = scan.next(); for(;i<bookList.getSize();i++){ if(name.equals(bookList.getBook(i).getName())){ if(bookList.getBook(i).isBorrow()==false){ System.out.println("借閱成功"); flag=1; bookList.getBook(i).setBorrow(true); } } } if(flag==0){ System.out.println("不好意思,借閱失敗"); } }}
Delete類
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("請(qǐng)輸入想要?jiǎng)h除的編號(hào)"); String id = scanner.next(); for(int i=0;i<bookList.getSize();i++){ if(bookList.getBook(i).getId().equals(id)){ bookList.setBooks(i,bookList.getBook(bookList.getSize())); bookList.setSize(bookList.getSize()-1); System.out.println("刪除成功"); } else{ System.out.println("刪除失敗"); } } }}
Exit類
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("請(qǐng)輸入需要查找的書名"); String name = scan.next(); for(;i<bookList.getSize();i++){ if(name.equals(bookList.getBook(i).getName())){ count++; } } if(count==0){ System.out.println("不好意思,沒有找到"); } else{ System.out.println("找到了,共計(jì)"+count+"本"); } }}
IO接口
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;i<bookList.getSize();i++){ System.out.println(bookList.getBook(i)); } }}
Return類
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("請(qǐng)輸入需要?dú)w還的ID"); String id = scan.next(); for(;i<bookList.getSize();i++){ if(id.equals(bookList.getBook(i).getId())){ if(bookList.getBook(i).isBorrow()==true){ System.out.println("歸還成功"); bookList.getBook(i).setBorrow(false); flag=1; } else{ System.out.println("歸還失敗"); } } } if(flag==0){ System.out.println("不好意思,沒有此書"); } }}
user包:
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("請(qǐng)輸入您的選擇: "); 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("請(qǐng)輸入您的選擇: "); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); // close 本質(zhì)上是在關(guān)閉 System.in // 由于后面還需要用到 System.in, 此處不能盲目關(guān)閉. // scanner.close(); return choice; }}
最后還有一個(gè)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("請(qǐng)輸入你的姓名"); String name = scanner.next(); System.out.println("請(qǐng)輸入你的身份"); 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)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)站欄目:如何使用java實(shí)現(xiàn)圖書館管理系統(tǒng)
分享鏈接:http://www.rwnh.cn/article4/gpoioe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、定制開發(fā)、外貿(mào)建站、網(wǎng)站建設(shè)、定制網(wǎng)站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)