Java實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖書(shū)館借閱系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
Main.java
package com.src1.booksystem; import com.src1.booksystem.booklist.BookList; import com.src1.booksystem.users.AdminUser; import com.src1.booksystem.users.NormalUser; import com.src1.booksystem.users.User; import java.util.Scanner; public class Main { ?? ?public static void main(String[] args) { ?? ??? ?//1.準(zhǔn)備書(shū)籍 ?? ??? ?BookList bookList = new BookList(); ?? ??? ?//2登錄 ?? ??? ?User user = login(); ?? ??? ?while ( true ) { ?? ??? ??? ?int chioce = user.menu(); ?? ??? ??? ?//3.根據(jù)選擇調(diào)用方法 ?? ??? ??? ?user.doOperation(chioce, bookList); ?? ??? ?} ?? ?} ?? ?public static User login() { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入用戶名:"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?if ( "admin".equals(name) ) { ?? ??? ??? ?System.out.println("請(qǐng)輸入管理員密碼:"); ?? ??? ??? ?String password = scanner.nextLine(); ?? ??? ??? ?if ( "admin".equals(password) ) { ?? ??? ??? ??? ?System.out.println("hello! 尊敬的管理員! 歡迎來(lái)到藏經(jīng)閣"); ?? ??? ??? ??? ?return new AdminUser(name); ?? ??? ??? ?} else { ?? ??? ??? ??? ?System.out.println("密碼錯(cuò)誤,進(jìn)入用戶端!"); ?? ??? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來(lái)到藏經(jīng)閣"); ?? ??? ??? ??? ?return new NormalUser(name); ?? ??? ??? ?} ?? ??? ?}else { ?? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來(lái)到藏經(jīng)閣"); ?? ??? ??? ?return new NormalUser(name); ?? ??? ?} ?? ?} }
package user
AdminUser.java
package com.src1.booksystem.users; import com.src1.booksystem.operation.*; import java.util.Scanner; public class AdminUser extends User { ?? ?@Override ?? ?public int menu() { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?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)"); ?? ??? ?int chioce = scanner.nextInt(); ?? ??? ?return chioce; ?? ?} ?? ?public AdminUser(String name) { ?? ??? ?super(name); ?? ??? ?this.operations = new IOperation[] { ?? ??? ??? ??? ?new ExitOperation(), ?? ??? ??? ??? ?new FindOperation(), ?? ??? ??? ??? ?new AddOperation(), ?? ??? ??? ??? ?new DelOperation(), ?? ??? ??? ??? ?new DisplayOperation() ?? ??? ?}; ?? ?} }
NormalUser.java
package com.src1.booksystem.users; import com.src1.booksystem.operation.*; import java.util.Scanner; public class NormalUser extends User { ?? ?@Override ?? ?public int menu() { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?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)"); ?? ??? ?int chioce = scanner.nextInt(); ?? ??? ?return chioce; ?? ?} ?? ?public NormalUser(String name) { ?? ??? ?super(name); ?? ??? ?this.operations = new IOperation[] { ?? ??? ??? ??? ?new ExitOperation(), ?? ??? ??? ??? ?new FindOperation(), ?? ??? ??? ??? ?new BorrowOperation(), ?? ??? ??? ??? ?new ReturnOperation(), ?? ??? ??? ??? ?new DisplayOperation() ?? ??? ?}; ?? ?} }
User.java
package com.src1.booksystem.users; import com.src1.booksystem.booklist.BookList; import com.src1.booksystem.operation.IOperation; abstract public class User { ?? ?public String name; ?? ?public IOperation[] operations; ?? ?/** ?? ? * 如果沒(méi)有menu 就不能通過(guò)user訪問(wèn) ?? ? */ ?? ?public abstract int menu() ; ?? ?public void doOperation(int chioce, BookList bookList){ ?? ??? ?//數(shù)組當(dāng)中元素的對(duì)象,通過(guò).調(diào)用對(duì)應(yīng)operation方法 ?? ??? ?operations[chioce].work(bookList); ?? ?} ?? ?public User(String name) { ?? ??? ?this.name = name; ?? ?} }
package book
book.java
package com.src1.booksystem.book; public class Book { ?? ?private String name; ?? ?private String author; ?? ?private int price; ?? ?private String type; ?? ?private boolean isBorrowed; ?? ?public Book(String name, String author, int price) { ?? ??? ?this.name = name; ?? ??? ?this.author = author; ?? ??? ?this.price = price; ?? ?} ?? ?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 int getPrice() { ?? ??? ?return price; ?? ?} ?? ?public void setPrice(int price) { ?? ??? ?this.price = price; ?? ?} ?? ?public boolean isBorrowed() { ?? ??? ?return isBorrowed; ?? ?} ?? ?public void setBorrowed(boolean borrowed) { ?? ??? ?isBorrowed = borrowed; ?? ?} ?? ?@Override ?? ?public String toString() { ?? ??? ?return "[" + ?? ??? ??? ??? ?"書(shū)名:" + name + ?? ??? ??? ??? ?", 作者:" + author + ?? ??? ??? ??? ?", 價(jià)格:" + price + ","+ ?? ??? ??? ??? ?(isBorrowed ? "已借出":"未借出")+"]"; ?? ?} }
package booklist
BookList.java
package com.src1.booksystem.booklist; import com.src1.booksystem.book.Book; public class BookList { ?? ?private ? Book[] books; ?? ?private int usedSize; ?? ?public BookList() { ?? ??? ?this.books = new Book[10]; ?? ??? ?this.books[0] = new Book("西游記","吳承恩",13); ?? ??? ?this.books[1] = new Book("水滸傳","施耐庵",14); ?? ??? ?this.books[2] = new Book("三國(guó)演義","羅貫中",15); ?? ??? ?this.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 setBooks(int pos,Book book) { ?? ??? ?this.books[pos] = book; ?? ?} }
package operation
AddOperation
package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class AddOperation implements ?IOperation{ ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入書(shū)名"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?System.out.println("請(qǐng)輸入作者"); ?? ??? ?String author = scanner.nextLine(); ?? ??? ?System.out.println("請(qǐng)輸入價(jià)格"); ?? ??? ?int price = scanner.nextInt(); ?? ??? ??? ?Book book = new Book(name, author, price); ?? ??? ??? ?int curSize = bookList.getUsedSize(); ?? ??? ??? ?bookList.setBooks(curSize, book); ?? ??? ??? ?bookList.setUsedSize(curSize + 1); ?? ??? ??? ?System.out.println("新增成功"); ?? ?} }
BorrowOperation
package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class BorrowOperation implements IOperation { ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?System.out.println("借閱書(shū)籍"); ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入你要借閱的圖書(shū):"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(name)) { ?? ??? ??? ??? ?if(book.isBorrowed()) { ?? ??? ??? ??? ??? ?System.out.println("此書(shū)已經(jīng)借出了!"); ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?book.setBorrowed(true); ?? ??? ??? ??? ?System.out.println("借閱成功!"); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("沒(méi)系統(tǒng)未收錄這本書(shū)!"); ?? ?} }
DelOperation
未完善
package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; import com.src1.booksystem.book.Book; public class DelOperation implements IOperation{ ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)名:"); ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?String delbook = scanner.nextLine(); ?? ??? ?int size = bookList.getUsedSize(); ?? ??? ?for (int i = 0; i < size; i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(delbook)) { ?? ??? ??? ??? ?if(i+1<size) { ?? ??? ??? ??? ??? ?bookList.setBooks(i, bookList.getBook(i + 1)); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?bookList.setUsedSize(size-1); ?? ??? ??? ??? ?if(i+2 < size) { ?? ??? ??? ??? ??? ?bookList.setBooks(i+1, bookList.getBook(i + 2)); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("刪除成功!"); ?? ?} }
修改
public class DelOperation implements IOperation{ ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?System.out.println("請(qǐng)輸入要?jiǎng)h除的書(shū)名:"); ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?String delbook = scanner.nextLine(); ?? ??? ?int size = bookList.getUsedSize(); ?? ??? ?int pos = 0; ?? ??? ?int i = 0; ?? ??? ?for ( i = 0; i < size; i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(delbook)) { ?? ??? ??? ??? ?pos = i; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if(i == size){ ?? ??? ??? ?System.out.println("系統(tǒng)中沒(méi)有這本書(shū)"); ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?for(int j = pos; j < size-1; j++) { ?? ??? ??? ?bookList.setBooks(j, bookList.getBook(j + 1)); ?? ??? ?} ?? ??? ?bookList.setUsedSize(size-1); ?? ??? ?System.out.println("刪除成功!"); ?? ?} }
DisplayOperation
import com.src1.booksystem.booklist.BookList; public class DisplayOperation implements IOperation{ ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) { ?? ??? ??? ?System.out.println(bookList.getBook(i)); ?? ??? ?} ?? ?} }
ExitOperation
package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public class ExitOperation implements IOperation { ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?System.exit(0); ?? ?} }
FindOperation
package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class FindOperation implements IOperation{ ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入要查找的書(shū)名"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?for( int i = 0; i < bookList.getUsedSize(); i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(name)) { ?? ??? ??? ??? ?System.out.println(book); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ?} }
ReturnOperation
package com.src1.booksystem.operation; import com.src1.booksystem.book.Book; import com.src1.booksystem.booklist.BookList; import java.util.Scanner; public class ReturnOperation implements IOperation { ?? ?@Override ?? ?public void work(BookList bookList) { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入你要?dú)w還的書(shū)名:"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(name)) { ?? ??? ??? ??? ?//說(shuō)明有這本書(shū) ?? ??? ??? ??? ?book.setBorrowed(false); ?? ??? ??? ??? ?System.out.println("歸還成功!"); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("系統(tǒng)未找到本書(shū)的相關(guān)信息!"); ?? ?} }
接口 IOperation
package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public interface IOperation { ?? ? void work(BookList bookList); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線購(gòu)書(shū)商城系統(tǒng)的實(shí)現(xiàn)流程
- Java實(shí)戰(zhàn)之圖書(shū)管理系統(tǒng)的實(shí)現(xiàn)
- java實(shí)現(xiàn)簡(jiǎn)單圖書(shū)管理系統(tǒng)
- java實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)借閱系統(tǒng)
- Java實(shí)現(xiàn)簡(jiǎn)易圖書(shū)借閱系統(tǒng)
- Java超詳細(xì)教你寫(xiě)一個(gè)網(wǎng)絡(luò)購(gòu)書(shū)系統(tǒng)案例
相關(guān)文章
基于Java SSM框架實(shí)現(xiàn)簡(jiǎn)易的評(píng)教系統(tǒng)
這篇文章主要介紹了通過(guò)Java SSM框架實(shí)現(xiàn)一個(gè)簡(jiǎn)易的評(píng)教系統(tǒng)的示例代碼,文中的代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-02-02Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種
本文主要介紹了Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Sentinel熱門(mén)詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門(mén)詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07springboot項(xiàng)目打成jar包后無(wú)法獲取static下的靜態(tài)資源文件的問(wèn)題分析
這篇文章主要介紹了springboot項(xiàng)目打成jar包后無(wú)法獲取static下的靜態(tài)資源文件的問(wèn)題分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java管道流實(shí)現(xiàn)線程間通信過(guò)程解析
這篇文章主要介紹了Java管道流實(shí)現(xiàn)線程間通信過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03SpringBoot實(shí)現(xiàn)物品點(diǎn)贊功能
這篇文章主要介紹了SpringBoot物品點(diǎn)贊功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04