Java实现图书馆借阅系统

本文实例为大家分享了Java实现图书馆借阅系统的具体代码,供大家参考,具体内容如下
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.准备书籍BookList bookList = new BookList(); //2登录User user = login(); while ( true ) {int chioce = user.menu(); //3.根据选择调用方法user.doOperation(chioce, bookList); }}public static User login() {Scanner scanner = new Scanner(System.in); System.out.println("请输入用户名:"); String name = scanner.nextLine(); if ( "admin".equals(name) ) {System.out.println("请输入管理员密码:"); String password = scanner.nextLine(); if ( "admin".equals(password) ) {System.out.println("hello! 尊敬的管理员! 欢迎来到藏经阁"); return new AdminUser(name); } else {System.out.println("密码错误,进入用户端!"); System.out.println("hello! "+ name+" 欢迎来到藏经阁"); return new NormalUser(name); }}else {System.out.println("hello! "+ name+" 欢迎来到藏经阁"); 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 {@Overridepublic 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.退出系统"); 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 {@Overridepublic 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.退出系统"); 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){//数组当中元素的对象,通过.调用对应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; }@Overridepublic String toString() {return "[" +"书名:" + name +", 作者:" + author +", 价格:" + price + ","+(isBorrowed ? "已借出":"未借出")+"]"; }}

package booklist
BookList.java
package com.src1.booksystem.booklist; import com.src1.booksystem.book.Book; public class BookList {privateBook[] 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; }publicvoid 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 implementsIOperation{@Overridepublic void work(BookList bookList) {Scanner scanner = new Scanner(System.in); System.out.println("请输入书名"); String name = scanner.nextLine(); System.out.println("请输入作者"); String author = scanner.nextLine(); System.out.println("请输入价格"); 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 {@Overridepublic void work(BookList bookList) {System.out.println("借阅书籍"); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要借阅的图书:"); 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("此书已经借出了!"); return; }book.setBorrowed(true); System.out.println("借阅成功!"); return; }}System.out.println("没系统未收录这本书!"); }}

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{@Overridepublic void work(BookList bookList) {System.out.println("请输入要删除的书名:"); 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
修改
public class DelOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("请输入要删除的书名:"); 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("系统中没有这本书"); 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{@Overridepublic 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 {@Overridepublic 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{@Overridepublic void work(BookList bookList) {Scanner scanner = new Scanner(System.in); System.out.println("请输入要查找的书名"); 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 {@Overridepublic void work(BookList bookList) {Scanner scanner = new Scanner(System.in); System.out.println("请输入你要归还的书名:"); 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("系统未找到本书的相关信息!"); }}

接口 IOperation
package com.src1.booksystem.operation; import com.src1.booksystem.booklist.BookList; public interface IOperation {void work(BookList bookList); }

【Java实现图书馆借阅系统】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读