Java實(shí)現(xiàn)圖書館借閱系統(tǒng)
本文實(shí)例為大家分享了Java實(shí)現(xiàn)圖書館借閱系統(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)備書籍 ?? ??? ?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! 尊敬的管理員! 歡迎來到藏經(jīng)閣"); ?? ??? ??? ??? ?return new AdminUser(name); ?? ??? ??? ?} else { ?? ??? ??? ??? ?System.out.println("密碼錯(cuò)誤,進(jìn)入用戶端!"); ?? ??? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來到藏經(jīng)閣"); ?? ??? ??? ??? ?return new NormalUser(name); ?? ??? ??? ?} ?? ??? ?}else { ?? ??? ??? ?System.out.println("hello! "+ name+" 歡迎來到藏經(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.查找圖書"); ?? ??? ?System.out.println("2.新增書籍"); ?? ??? ?System.out.println("3.刪除書籍"); ?? ??? ?System.out.println("4.書籍列表"); ?? ??? ?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.查找圖書"); ?? ??? ?System.out.println("2.借閱書籍"); ?? ??? ?System.out.println("3.歸還書籍"); ?? ??? ?System.out.println("4.書籍列表"); ?? ??? ?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; ?? ?/** ?? ? * 如果沒有menu 就不能通過user訪問 ?? ? */ ?? ?public abstract int menu() ; ?? ?public void doOperation(int chioce, BookList bookList){ ?? ??? ?//數(shù)組當(dāng)中元素的對(duì)象,通過.調(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 "[" + ?? ??? ??? ??? ?"書名:" + 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("三國演義","羅貫中",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)輸入書名"); ?? ??? ?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("借閱書籍"); ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("請(qǐng)輸入你要借閱的圖書:"); ?? ??? ?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("此書已經(jīng)借出了!"); ?? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?book.setBorrowed(true); ?? ??? ??? ??? ?System.out.println("借閱成功!"); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("沒系統(tǒng)未收錄這本書!"); ?? ?} }
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除的書名:"); ?? ??? ?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除的書名:"); ?? ??? ?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)中沒有這本書"); ?? ??? ??? ?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)輸入要查找的書名"); ?? ??? ?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還的書名:"); ?? ??? ?String name = scanner.nextLine(); ?? ??? ?for (int i = 0; i < bookList.getUsedSize(); i++) { ?? ??? ??? ?Book book = bookList.getBook(i); ?? ??? ??? ?if(book.getName().equals(name)) { ?? ??? ??? ??? ?//說明有這本書 ?? ??? ??? ??? ?book.setBorrowed(false); ?? ??? ??? ??? ?System.out.println("歸還成功!"); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?System.out.println("系統(tǒng)未找到本書的相關(guān)信息!"); ?? ?} }
接口 IOperation
package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public interface IOperation { ?? ? void work(BookList bookList); }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java SSM框架實(shí)現(xiàn)簡易的評(píng)教系統(tǒng)
這篇文章主要介紹了通過Java SSM框架實(shí)現(xiàn)一個(gè)簡易的評(píng)教系統(tǒng)的示例代碼,文中的代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-02-02Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種
本文主要介紹了Java中報(bào)錯(cuò)org.springframework.jdbc.UncategorizedSQLException的多種解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn)
這篇文章主要介紹了Java Swing GridBagLayout網(wǎng)格袋布局的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07springboot項(xiàng)目打成jar包后無法獲取static下的靜態(tài)資源文件的問題分析
這篇文章主要介紹了springboot項(xiàng)目打成jar包后無法獲取static下的靜態(tài)資源文件的問題分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot實(shí)現(xiàn)物品點(diǎn)贊功能
這篇文章主要介紹了SpringBoot物品點(diǎn)贊功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04