Java实现小型图书馆管理系统
程序员文章站
2022-06-22 08:17:48
本文实例为大家分享了java实现小型图书馆管理系统的具体代码,供大家参考,具体内容如下以下为小型图书馆管理系统模式图:模式总体概述:其中iborrower接口确定borrower类标准,library...
本文实例为大家分享了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基础 状态 当前状态应该为 未在库 } }
测试结果显示:
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。