這篇文章將為大家詳細(xì)講解有關(guān)java如何實(shí)現(xiàn)圖書館管理系統(tǒng),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
在魏都等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需求定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都營(yíng)銷網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè),魏都網(wǎng)站建設(shè)費(fèi)用合理。
具體內(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(;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("請(qǐng)輸入想要?jiǎng)h除的編號(hào)"); 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("請(qǐng)輸入需要查找的書名"); 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("請(qǐng)輸入需要?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("請(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)把它分享出去讓更多的人看到。
名稱欄目:java如何實(shí)現(xiàn)圖書館管理系統(tǒng)
網(wǎng)頁網(wǎng)址:http://weahome.cn/article/pdhcgh.html