Java順序表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)
本文實(shí)例為大家分享了Java順序表實(shí)現(xiàn)圖書(shū)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
一、簡(jiǎn)介
實(shí)現(xiàn)此項(xiàng)目的目的是鞏固并理解前面的知識(shí)點(diǎn):類(lèi),抽象類(lèi),封裝,繼承,多態(tài),接口等
二、核心需求
管理端
??查閱書(shū)籍
??增加書(shū)籍
??刪除書(shū)籍
??打印書(shū)籍列表
??退出系統(tǒng)
用戶(hù)端
??查詢(xún)書(shū)籍
??借閱書(shū)籍
??歸還書(shū)籍
??打印書(shū)籍列表
??退出系統(tǒng)
三、類(lèi)的設(shè)計(jì)
1. 創(chuàng)建圖書(shū)類(lèi)
圖書(shū)類(lèi)中包含圖書(shū)的名稱(chēng),價(jià)格,類(lèi)型,作者和是否被借出等信息,并生成構(gòu)造方法,Getter()和Setter()方法,toString方法(注意成員變量應(yīng)該盡可能使用private關(guān)鍵字修飾)
public class Book { private String name; private double price; private String type; private String author; private boolean isBorrowed; public Book(String name, double price, String type, String author) { this.name = name; this.price = price; this.type = type; this.author = author; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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 String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public boolean isBorrowed() { return isBorrowed; } public void setBorrowed(boolean borrowed) { isBorrowed = borrowed; } @Override public String toString() { return "Book{" + "name='" + name + '\'' + ", price=" + price + ", type='" + type + '\'' + ", author='" + author + '\'' + ", 狀態(tài):" +((isBorrowed) ? "已借出":"未借出")+ '}'; } }
2. 創(chuàng)建圖書(shū)列表類(lèi)
圖書(shū)列表類(lèi)用于存放圖書(shū),我們可以先在列表中初始化幾本書(shū)以方便后續(xù)測(cè)試
public class BookList { private Book[] books = new Book[10]; private int usedSize; public BookList(){ books[0] = new Book("三國(guó)演義",19,"小說(shuō)","羅貫中"); books[1] = new Book("水滸傳",29,"小說(shuō)","施耐庵"); books[2] = new Book("西游記",39,"小說(shuō)","吳承恩"); usedSize = 3; } public int getUsedSize() { return usedSize; } public void setUsedSize(int usedSize) { this.usedSize = usedSize; } public Book getBook(int pos){ return books[pos]; } public void setBook(int pos,Book book) { books[pos] = book; } }
3. 創(chuàng)建用戶(hù)類(lèi)
創(chuàng)建一個(gè)用戶(hù)類(lèi)并將其定義為抽象類(lèi),再創(chuàng)建普通用戶(hù)類(lèi)和管理員類(lèi)繼承于用戶(hù)類(lèi):
創(chuàng)建用戶(hù)類(lèi)并定義為抽象類(lèi):
public abstract class User { protected String name; protected IOperation[] iOperations; public abstract int menu(); public void doWork(int choice, BookList bookList){ iOperations[choice].work(bookList); } public User(String name) { this.name = name; } }
創(chuàng)建管理員用戶(hù)類(lèi):
public class AdminUser extends User{ public AdminUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new FindOperation(), new AddOperation(), new DisplayOperation(), new DelOperation() }; } @Override public int menu(){ System.out.println("===========管理員菜單============"); System.out.println("您好, 管理員 "+this.name+":"); System.out.println("歡迎來(lái)到圖書(shū)館!"); System.out.println("1. 查找圖書(shū)"); System.out.println("2. 新增圖書(shū)"); System.out.println("3. 顯示圖書(shū)"); System.out.println("4. 刪除圖書(shū)"); System.out.println("0. 退出系統(tǒng)"); System.out.println("================================="); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
創(chuàng)建普通用戶(hù)類(lèi):
public class NormalUser extends User{ public NormalUser(String name) { super(name); this.iOperations = new IOperation[]{ new ExitOperation(), new DisplayOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation(), }; } @Override public int menu(){ System.out.println("===========普通用戶(hù)菜單============"); System.out.println("您好,用戶(hù) "+this.name+":"); System.out.println("歡迎來(lái)到圖書(shū)館!"); System.out.println("1. 顯示圖書(shū)"); System.out.println("2. 查找圖書(shū)"); System.out.println("3. 借閱圖書(shū)"); System.out.println("4. 歸還圖書(shū)"); System.out.println("0. 退出系統(tǒng)"); System.out.println("================================="); Scanner scanner = new Scanner(System.in); int choice = scanner.nextInt(); return choice; } }
4. 創(chuàng)建操作相關(guān)的類(lèi)
首先創(chuàng)建一個(gè)接口用于實(shí)現(xiàn)多態(tài):
public interface IOperation { void work(BookList bookList); }
創(chuàng)建添加書(shū)籍類(lèi):
public class AddOperation implements IOperation{ public void work(BookList bookList) { Scanner scanner = new Scanner(System.in); System.out.println("請(qǐng)輸入圖書(shū)名稱(chēng):"); String name = scanner.nextLine(); System.out.println("請(qǐng)輸入價(jià)格:"); double price = scanner.nextDouble(); System.out.println("請(qǐng)輸入類(lèi)型:"); String type = scanner.next(); System.out.println("請(qǐng)輸入作者:"); String author = scanner.next(); Book book = new Book(name,price,type,author); int usedSize = bookList.getUsedSize(); bookList.setBook(usedSize,book); bookList.setUsedSize(++usedSize); System.out.println("添加圖書(shū)成功!"); } }
創(chuàng)建查找書(shū)籍類(lèi):
public class FindOperation implements IOperation{ public void work(BookList bookList){ System.out.println("請(qǐng)輸入書(shū)名:"); Scanner scanner = new Scanner(System.in); String name = scanner.next(); for(int i=0;i<bookList.getUsedSize();i++){ Book book = bookList.getBook(i); if(name.equals(book.getName())){ System.out.println(book); return; } } System.out.println("找不到 《"+name+"》 這本書(shū)"); } }
創(chuàng)建借閱書(shū)籍類(lèi):
public class BorrowOperation implements IOperation { public void work(BookList bookList) { System.out.println("請(qǐng)輸入你要借閱的書(shū)籍:"); Scanner scanner = new Scanner(System.in); String name = scanner.next(); int i = 0; for (i = 0; i < bookList.getUsedSize() - 1; i++) { Book book = bookList.getBook(i); if (name.equals(book.getName()) && !book.isBorrowed()) { book.setBorrowed(true); System.out.println("借閱成功!"); return; } if (name.equals(book.getName()) && book.isBorrowed()) { System.out.println("該書(shū)籍已被借出"); return; } } System.out.println("找不到你要借閱的書(shū)籍!"); } }
創(chuàng)建歸還書(shū)籍類(lèi):
public class ReturnOperation implements IOperation{ public void work(BookList bookList){ System.out.println("請(qǐng)輸入你要?dú)w還的書(shū)籍:"); Scanner scanner = new Scanner(System.in); String name = scanner.next(); int i=0; for(i=0;i<bookList.getUsedSize()-1;i++){ Book book = bookList.getBook(i); if(name.equals(book.getName())&& book.isBorrowed()){ book.setBorrowed(false); System.out.println("歸還成功!"); return; } if(name.equals(book.getName())&& !book.isBorrowed()){ System.out.println("此書(shū)處于未借出狀態(tài)!"); return; } } System.out.println("找不到你要?dú)w還的書(shū)籍!"); } }
創(chuàng)建刪除書(shū)籍類(lèi):
public class DelOperation implements IOperation{ public void work(BookList bookList) { System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)名:"); Scanner scanner = new Scanner(System.in); String name = scanner.next(); int index = 0; int i = 0; for(i=0;i<bookList.getUsedSize();i++){ Book book = bookList.getBook(i); if(name.equals(book.getName())){ index = i; break; } } if(i>=bookList.getUsedSize()) { System.out.println("找不到這本書(shū)"); return; } int j = 0; for (j = index;j< bookList.getUsedSize()-1;j++){ Book book = bookList.getBook(j+1); bookList.setBook(j,book); } bookList.setBook(bookList.getUsedSize()-1, null); bookList.setUsedSize(bookList.getUsedSize()-1); System.out.println("刪除成功!"); } }
創(chuàng)建打印書(shū)籍列表類(lèi):
public class DisplayOperation implements IOperation{ public void work(BookList bookList){ int usedSize = bookList.getUsedSize(); for (int i=0;i<usedSize;i++){ Book book = bookList.getBook(i); System.out.println(book); } } }
退出系統(tǒng)類(lèi):
public class ExitOperation implements IOperation{ public void work(BookList bookList){ System.out.println("退出系統(tǒng)!"); System.exit(0); } }
主函數(shù)類(lèi):
public class Main { public static User work(){ System.out.println("請(qǐng)輸入您的姓名:"); Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); System.out.println("請(qǐng)輸入身份: 1-> 管理員登錄 0-> 用戶(hù)登錄"); int choice = scanner.nextInt(); if(choice==1){ return new AdminUser(name); } return new NormalUser(name); } public static void main(String[] args) { BookList bookList = new BookList(); User user = work(); while (true) { int choice = user.menu(); user.doWork(choice, bookList); } } }
The end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java手寫(xiě)圖書(shū)管理基本功能附代碼
- Java實(shí)戰(zhàn)之圖書(shū)管理系統(tǒng)的實(shí)現(xiàn)
- java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)
- Java圖書(shū)管理系統(tǒng)課程設(shè)計(jì)
- JAVA大作業(yè)之圖書(shū)管理系統(tǒng)實(shí)現(xiàn)全解
- Java 實(shí)戰(zhàn)圖書(shū)管理系統(tǒng)的實(shí)現(xiàn)流程
- JAVA實(shí)現(xiàn)圖書(shū)管理系統(tǒng)項(xiàng)目
- Java快速實(shí)現(xiàn)圖書(shū)管理基本功能
相關(guān)文章
JAVA 16位ID生成工具類(lèi)含16位不重復(fù)的隨機(jī)數(shù)數(shù)字+大小寫(xiě)
這篇文章主要介紹了JAVA 16位ID生成工具類(lèi)含16位不重復(fù)的隨機(jī)數(shù)數(shù)字+大小寫(xiě),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Springmvc實(shí)現(xiàn)文件下載2種實(shí)現(xiàn)方法
這篇文章主要介紹了Springmvc實(shí)現(xiàn)文件下載2種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03SpringCloud之消息總線(xiàn)Spring Cloud Bus實(shí)例代碼
這篇文章主要介紹了SpringCloud之消息總線(xiàn)Spring Cloud Bus實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04java根據(jù)開(kāi)始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼
這篇文章主要介紹了java根據(jù)開(kāi)始時(shí)間結(jié)束時(shí)間計(jì)算中間間隔日期的實(shí)例代碼,需要的朋友可以參考下2019-05-05java實(shí)現(xiàn)Img與PDF相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Img與PDF相互轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Java基礎(chǔ)知識(shí)之BufferedReader流的使用
這篇文章主要介紹了Java基礎(chǔ)知識(shí)之BufferedReader流的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12