当前位置 : 主页 > 编程语言 > java >

Java实现小型图书馆管理系统

来源:互联网 收集:自由互联 发布时间:2021-04-03
本文实例为大家分享了Java实现小型图书馆管理系统的具体代码,供大家参考,具体内容如下 以下为小型图书馆管理系统模式图: 模式总体概述: 其中IBorrower接口确定Borrower类标准,

本文实例为大家分享了Java实现小型图书馆管理系统的具体代码,供大家参考,具体内容如下

以下为小型图书馆管理系统模式图:

模式总体概述:

其中IBorrower接口确定Borrower类标准,Library类为单例设计模式,即图书馆只有一个。Book类为Borrower类的内部类,libraryBook类为Library类的内部类。最后利用PlatForm类进一步的封装。对于类方法中的核心,主要是数组的“增删查改”操作,本项目中三次运用了这些操作,分别在Borrower、Library、PlatForm类方法中。

IBorrower接口:

// package com.wu.demo;

/**
* @date 2020-10-30 10:08:48 
* @author 一夜星尘
* @version 1.8.0
 */

// 图书借阅人标准
public interface IBorrower {
 public abstract void borrowBook(String bookName,String bookAuthor,float bookPrice,Library libary);
 public abstract void returnBook(String bookName,String bookAuthor,float bookPrice,Library libary);
}

Borrower类:

// package com.wu.demo;

/**
* @date 2020-10-30 10:11:00 
* @author 一夜星尘
* @version 1.8.0
 */
public class Borrower implements IBorrower{
 protected String borrowerName;
 protected long borrowerNumber;
 private int booksNumber;
 private static final int MAX_NUMBER_OF_BOOKS = 4; //最大借书容量
 private Book[] books = new Book[MAX_NUMBER_OF_BOOKS];
 
 public Borrower(String borrowerName,long borrowerNumber) {
 this.borrowerName = borrowerName;
 this.borrowerNumber = borrowerNumber;
 this.booksNumber = 0;
 }
 
 private class Book{
 private String bookName;
 private String bookAuthor;
 private float bookPrice;
 public Book(String bookName,String bookAuthor,float bookPrice){
 Book.this.bookName = bookName;
 Book.this.bookAuthor = bookAuthor;
 Book.this.bookPrice = bookPrice;
 }
 public String getName() {
 return Book.this.bookName;
 }
 public String getAuthor() {
 return Book.this.bookAuthor;
 }
 public float getPrice() {
 return Book.this.bookPrice;
 }
 }
 @Override
 /**
 * 借书操作
 */
 public void borrowBook(String bookName,String bookAuthor,float bookPrice,Library library) {
 if(booksNumber>=MAX_NUMBER_OF_BOOKS) {
 System.out.println("超过最大借书容量");
 return;
 }
 books[booksNumber++] = new Book(bookName,bookAuthor,bookPrice);
 // 移除图书馆书籍,更新图书馆信息
 library.removeBook( bookName,bookAuthor, bookPrice);
 }
 
 @Override
 /**
 * 还书操作
 */
 public void returnBook(String bookName,String bookAuthor,float bookPrice,Library library) {
 // 前提是有书的情况下
 if(booksNumber<=0) {
 return;
 }
 
 int index = -1;
 for(int i = 0 ; i < books.length ; i++) {
 if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) 
 && books[i].bookPrice == bookPrice) {
 index = i;
 break;
 }
 }
 if(index < 0) {
 System.out.println("你并没有该书籍");
 return;
 }
 for(int i = 0; i < booksNumber-1 ; i++) {
 if(i>=index) {
 books[i] = books[i+1];
 }
 }
 booksNumber --;
 // 还入图书馆,更新图书馆信息
 library.addBook(bookName,bookAuthor,bookPrice);
 }
 /**
 * 借书人信息
 */
 public void borrowerInfo() {
 int i = 0;
 System.out.println("------------------------------借阅人信息------------------------------");
 System.out.println("借阅人姓名:"+this.borrowerName);
 System.out.println("借阅人学号:"+this.borrowerNumber);
 for(;i < booksNumber ; i++) {
 System.out.println("第"+(i+1)+"本书: ");
 System.out.println("书籍名称:"+books[i].getName());
 System.out.println("书籍作者:"+books[i].getAuthor());
 System.out.println("书籍价格:"+books[i].getPrice());
 }
 System.out.println("当前借阅书本数为: "+booksNumber+" 剩余容量为: "+(MAX_NUMBER_OF_BOOKS-i));
 System.out.println("------------------------------------------------------------------------");
 }
 
}

Library类:

// package com.wu.demo;
/**
* @date 2020-10-30 10:32:42 
* @author 一夜星尘
* @version 1.8.0
 */
public class Library {
 private static final Library library = new Library();
 private static final int MAX_NUMBER_OF_BOOKS = 120; // 图书馆最大书容量
 private libraryBook[] books = new libraryBook[MAX_NUMBER_OF_BOOKS];
 protected static int booksNumber = 0;
 
 // 无参构造函数私有化
 private Library() {};
 
 private class libraryBook{
 private String bookName;
 private String bookAuthor;
 private float bookPrice;
 public libraryBook(String bookName,String bookAuthor,float bookPrice){
 libraryBook.this.bookName = bookName;
 libraryBook.this.bookAuthor = bookAuthor;
 libraryBook.this.bookPrice = bookPrice;
 }
 public void setName(String bookName) {
 libraryBook.this.bookName = bookName;
 }
 public void setAuthor(String bookAuthor) {
 libraryBook.this.bookAuthor = bookAuthor;
 }
 public void setPrice(float bookPrice) {
 libraryBook.this.bookPrice = bookPrice;
 }
 }
 // 唯一返回的单例
 static Library getLibrary() {
 return library;
 }
 /**
 * 增添书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void addBook(String bookName,String bookAuthor,float bookPrice) {
 if(booksNumber>=MAX_NUMBER_OF_BOOKS) {
 System.out.println("超过图书馆书籍最大容量");
 return;
 }
 books[booksNumber++] = new libraryBook(bookName,bookAuthor,bookPrice);
 }
 /**
 * 删除书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void removeBook(String bookName,String bookAuthor,float bookPrice) {
 int index = -1;
 for(int i = 0 ; i < booksNumber ; i++) {
 if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
 books[i].bookPrice == bookPrice) {
 index = i;
 break;
 }
 }
 if(index < 0) {
 System.out.println("该图书未在库");
 return;
 }
 
 for(int i = 0;i < booksNumber - 1;i++) {
 if(index <= i) {
 books[i] = books[i+1];
 }
 }
 booksNumber --;
 }
 /**
 * 查找书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void checkBook(String bookName,String bookAuthor,float bookPrice) {
 System.out.println("");
 for(int i = 0 ; i<booksNumber ; i++) {
 if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
 books[i].bookPrice == bookPrice) {
 System.out.println("状态:书籍在库");
 return;
 }
 }
 System.out.println("状态:书籍未在库");
 System.out.println("");
 }
 /**
 * 更改书籍信息
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void changeBook(String bookName, String bookAuthor,float bookPrice,String newBookName,
 String newBookAuthor,float newBookPrice) {
 for(int i = 0 ;i<booksNumber;i++) {
 if(books[i].bookName.equals(bookName) && books[i].bookAuthor.equals(bookAuthor) &&
 books[i].bookPrice == bookPrice) {
 books[i].setName(newBookName);
 books[i].setAuthor(newBookAuthor);
 books[i].setPrice(newBookPrice);
 break;
 }
 }
 System.out.println("更改书籍完成");
 }
 /**
 * 查看当前图书馆书籍信息
 */
 public void booksInfo() {
 System.out.println("-----------------------------图书馆书籍信息-----------------------------");
 for(int i = 0 ; i<booksNumber;i++) {
 System.out.println("在库书籍 "+(i+1)+" : ");
 System.out.println("书籍名称:"+books[i].bookName);
 System.out.println("书籍作者:"+books[i].bookAuthor);
 System.out.println("书籍价格:"+books[i].bookPrice);
 }
 System.out.println("在库书籍总数:"+booksNumber+" 剩余可借阅书籍总数:"+(MAX_NUMBER_OF_BOOKS-booksNumber));
 System.out.println("--------------------------------------------------------------------------");
 }
}

PlatForm类:

// package com.wu.demo;
import java.util.Scanner;

/**
* @date 2020-10-30 14:06:34 
* @author 一夜星尘
* @version 1.8.0
 */
// 进一步封装
public class PlatForm {
 private static final int MAX_NUMBER_OF_BORROWERS = 30; // 平台最多可注册30名借阅者
 private Borrower[] borrowers = new Borrower[MAX_NUMBER_OF_BORROWERS];
 private static int numberBorrowers = 0; 
 private Library library = Library.getLibrary();
 
 public PlatForm() {
 Scanner input = new Scanner(System.in);
 // 初始化Borrower成员
 System.out.println("-----------------------------初始化借阅人-----------------------------");
 System.out.println("按任意键继续(按Q或q退出):");
 String exit = input.nextLine();
 System.out.println("以下输入可借阅人员信息");
 while(!exit.equalsIgnoreCase("q")) {
 System.out.print("姓名 学号:");
 String tempName = input.next();
 long tempNumber = input.nextLong();
 this.borrowers[numberBorrowers++] = new Borrower(tempName,tempNumber);
 // 回车输入舍弃
 input.nextLine();
 System.out.println("按任意键继续(按Q或q退出):");
 exit = input.nextLine();
 if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS){
 System.out.println("已达到最大注册量");
 break;
 }
 }
 
 // 初始化图书馆信息
 System.out.println("---------------------------初始化图书馆图书信息------------------------");
 System.out.println("按任意键继续(按Q或q退出):");
 exit = input.nextLine();
 System.out.println("请输入原始书籍信息:");
 while(!exit.equalsIgnoreCase("q")) {
 System.out.print("书籍名称 作者 价格:");
 String tempName = input.next();
 String tempAuthor = input.next();
 float tempPrice = input.nextFloat();
 library.addBook(tempName,tempAuthor,tempPrice);
 // 回车输入舍弃
 input.nextLine();
 System.out.println("按任意键继续(按Q或q退出):");
 exit = input.nextLine();
 }
 System.out.println("-----------------------------------------------------------------------");
 input.close();
 } 
 
 /**
 * 平台管理:增添用户
 * @param borrowerName
 * @param borrowerNumber
 */
 public void addClient(String borrowerName,long borrowerNumber) {
 if(numberBorrowers >= MAX_NUMBER_OF_BORROWERS) {
 System.out.println("用户容量已达最大值");
 return;
 }
 this.borrowers[numberBorrowers++] = new Borrower(borrowerName,borrowerNumber);
 System.out.println("增添用户操作成功");
 }
 /**
 * 平台管理:移除用户
 * @param borrowerName
 * @param borrowerNumber
 */
 public void removeClient(String borrowerName,long borrowerNumber) {
 int index = -1;
 for(int i = 0 ; i<numberBorrowers; i++) {
 if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
 index = i;
 break;
 }
 }
 if(index<0) {
 System.out.println("未找到用户");
 return;
 }
 for(int i = 0 ; i < numberBorrowers-1;i++) {
 if(index <= i) {
 borrowers[i] = borrowers[i+1];
 }
 }
 numberBorrowers--;
 System.out.println("移除用户成功");
 }
 /**
 * 相应借阅人的信息
 * @param borrowerName
 * @param borrowerNumber
 */
 public void platformBorrowerInfo(String borrowerName,long borrowerNumber) {
 for(int i = 0 ; i<numberBorrowers;i++) {
 if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
 borrowers[i].borrowerInfo();
 return;
 }
 }
 System.out.println("该用户不存在");
 }
 /**
 * 相应借阅人借阅书籍
 * @param borrowerName
 * @param borrowerNumber
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void platformBorrowBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor,
 float bookPrice) {
 for(int i = 0 ; i<borrowers.length;i++) {
 if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
 borrowers[i].borrowBook(bookName,bookAuthor, bookPrice, this.library);
 break;
 }
 }
 }
 /**
 * 相应借阅人归还书籍
 * @param borrowerName
 * @param borrowerNumber
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void platformRrturnBook(String borrowerName,long borrowerNumber,String bookName,String bookAuthor,
 float bookPrice){
 for(int i = 0 ; i<borrowers.length;i++) {
 if(borrowers[i].borrowerName.equals(borrowerName) && borrowers[i].borrowerNumber == borrowerNumber) {
 borrowers[i].returnBook(bookName,bookAuthor, bookPrice, library);
 break;
 }
 }
 } 
 /**
 * 输出图书馆信息
 */
 public void platformLibraryInfo() {
 library.booksInfo();
 } 
 /**
 * 对图书馆增添书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void platformLibraryAddBook(String bookName,String bookAuthor,float bookPrice) {
 library.addBook(bookName,bookAuthor,bookPrice);
 }
 /**
 * 对图书馆移除书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void platformLibraryRemoveBook(String bookName,String bookAuthor,float bookPrice) {
 library.removeBook(bookName,bookAuthor,bookPrice);
 }
 /**
 * 对图书馆检索书籍
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 */
 public void platformLibraryCheckBook(String bookName,String bookAuthor,float bookPrice) {
 library.checkBook(bookName, bookAuthor, bookPrice);
 }
 /**
 * 对图书馆书籍更改
 * @param bookName
 * @param bookAuthor
 * @param bookPrice
 * @param newBookName
 * @param newBookAuthor
 * @param newBookPrice
 */
 public void platformLibraryChangeBook(String bookName,String bookAuthor,float bookPrice,
 String newBookName,String newBookAuthor,float newBookPrice) {
 library.changeBook(bookName, bookAuthor, bookPrice,newBookName,newBookAuthor,newBookPrice);
 }
}

testDemo(测试):

// package com.wu.demo;
/**
* @date 2020-10-30 10:51:21 
* @author 一夜星尘
* @version 1.8.0
 */
public class testDemo {
 public static void main(String[] args) {
 PlatForm platform = new PlatForm();
 
 // 以下为平台管理权限的测试
 platform.addClient("张三", 100000L);
 platform.addClient("李四", 100001L);
 platform.addClient("王五", 100002L);
 platform.addClient("赵六", 100003L);
 platform.platformBorrowerInfo("张三",100000L);
 platform.platformBorrowerInfo("李四",100000L); // 此时应该为不存在
 platform.platformBorrowerInfo("王五",100002L); 
 platform.platformBorrowerInfo("赵六",100003L);
 platform.removeClient("王五", 100002L);
 platform.platformBorrowerInfo("王五",100002L); // 此时王五不是用户
 
 //以下为图书馆和借阅人的测试
 platform.platformLibraryAddBook("数据结构","匿名1",100.0F);
 platform.platformLibraryAddBook("网络基础","匿名2",90.0F);
 platform.platformLibraryAddBook("算法","匿名3",110.0F);
 platform.platformLibraryInfo(); // 当前图书馆里只有3本书
 platform.platformLibraryAddBook("java基础","匿名4",105.0F);
 platform.platformLibraryAddBook("深度学习","匿名5",129.0F);
 platform.platformLibraryAddBook("Linux基础","匿名6",104.0F);
 platform.platformLibraryAddBook("matlab基础","匿名7",123.0F);
 platform.platformLibraryAddBook("visual C++基础","匿名8",95.0F);
 platform.platformLibraryAddBook("HTML CSS JS","匿名9",99.0F);
 platform.platformLibraryAddBook("C++基础","匿名10",119.0F);
 platform.platformLibraryAddBook("python基础","匿名11",108.0F);
 platform.platformLibraryAddBook("数据库基础","匿名12",120.0F);
 platform.platformLibraryAddBook("操作系统基础","匿名13",106.0F);
 platform.platformLibraryInfo(); // 当前图书馆里有13本书
 platform.platformLibraryRemoveBook("visual C++基础","匿名8",95.0F);
 platform.platformLibraryInfo(); // 当前图书馆里有12本书
 platform.platformLibraryRemoveBook("Linux基础" ,"匿名6", 104.0F); // 检索图书 Linux基础 状态
 platform.platformLibraryChangeBook("python基础", "匿名11", 108.0F,"keras基础","匿名11",110.0F); // 变更图书馆书籍名称 python基础 为 keras基础
 platform.platformBorrowBook("张三", 100000L, "java基础", "匿名4", 105.0F); // 用户 张三 借了书籍 java基础
 platform.platformBorrowBook("张三", 100000L, "算法", "匿名3", 110.0F); // 用户 张三 又借了书籍 算法
 platform.platformBorrowBook("张三", 100000L, "C++基础", "匿名10", 119.0F); // 用户 张三 又借了书籍 C++基础
 platform.platformBorrowBook("张三", 100000L, "深度学习", "匿名5", 129.0F); // 用户 张三 又借了书籍 深度学习
 platform.platformBorrowBook("赵六", 100003L, "网络基础", "匿名2", 84.0F); // 用户 赵六 借了书籍 网络基础
 platform.platformBorrowBook("李四", 100001L, "HTML CSS JS", "匿名9", 119.0F); // 用户 李四 借了书籍 HTML CSS JS
 platform.platformBorrowerInfo("张三", 100000L); // 查看用户 张三 借阅情况 当前用户 张三 有四本书 
 platform.platformBorrowerInfo("赵六", 100003L); // 查看用户 赵六 借阅情况 当前用户 赵六 有一本书
 platform.platformBorrowerInfo("李四", 100001L); // 查看用户 李四 借阅情况 当前用户 李四 有一本书
 platform.platformRrturnBook("张三", 100000L, "算法", "匿名3", 110.0F); // 用户 张三 还了书籍 算法
 platform.platformRrturnBook("张三", 100000L, "深度学习", "匿名5", 129.0F); // 用户 张三 又还了书籍 深度学习 
 platform.platformBorrowerInfo("张三", 100000L); // 查看用户 张三 借阅情况 当前用户 张三 只有两本书
 platform.platformLibraryRemoveBook("java基础" ,"匿名4", 105.0F); // 检索图书 java基础 状态 当前状态应该为 未在库
 }
}

测试结果显示:

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

网友评论