谢谢你陪着稀奇古怪的我
大家好,这里是新一,请多关照🙈🙉🙊。在本篇博客中,新一将会为大带来JAVA小程序——图书管理系统,干货满满哟。(以下结果均在IDEA中编译)希望在方便自己复习的同时也能帮助到大家。😜😜😜🥇🥈🥉
废话不多说,直接进入我们的文章。
文章目录
🥇图书管理系统
既然我们前面介绍了继承,多态,接口等等一系列知识,那么今天新一就带大家来实战一波,面向对象—— 图书管理系统。
1.🥈 基础框架介绍
首先,我们想到,既然是图书管理系统,那肯定必须要有书,书——就是一个对象,一本书吗?当然不是,我们需要的是一堆书——链表存储,存储对象为书
还有用户——对象,以及我们形形色色的功能——对象
于是我们整理出了以下内容:
一、简单的登录
1.用户名和密码输入(简易)
2.管理员判定
二、管理端
1.整理书籍(该功能为可扩展功能)
2.查阅书籍
3.增加书籍
4.删除书籍
5.打印书籍列表
6.退出
三、用户端
1.查询书籍
2.借阅书籍
3.归还书籍
4.退出
于是我们便有了思路,建立三个包,分别是book包——用来存放图书信息,operation包——用来存放功能,user包——用来存放用户这个对象与main函数入口,包以及各函数接口如下:
2.🥈 book包
这里主要存放我们图书有关的类
2.1🥉 Book类——数据有关信息
packagebook;publicclassBook{privateString name;privateString author;privateint price;privateString type;privateboolean isBorrowed;publicBook(String name,String author,int price,String type){this.name = name;this.author = author;this.price = price;this.type = type;}publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicStringgetAuthor(){return author;}publicvoidsetAuthor(String author){this.author = author;}publicintgetPrice(){return price;}publicvoidsetPrice(int price){this.price = price;}publicStringgetType(){return type;}publicvoidsetType(String type){this.type = type;}publicbooleanisBorrowed(){return isBorrowed;}publicvoidsetBorrowed(boolean borrowed){
isBorrowed = borrowed;}@OverridepublicStringtoString(){return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+(isBorrowed ==true?", 该书已借出 ":", 未借出 ")+'}';}}
2.2🥉 BookList类——链表存储书籍
packagebook;publicclassBookList{ListNode head;publicBookList(){ListNode listNode1 =newListNode(newBook("未闻花名","长井龙雪",45,"爱情"));ListNode listNode2 =newListNode(newBook("你的名字","新海诚",48,"爱情"));ListNode listNode3 =newListNode(newBook("天气之子","新海诚",56,"爱情"));ListNode listNode4 =newListNode(newBook("咒术回战","芥见下下",46,"战斗"));ListNode listNode5 =newListNode(newBook("一拳超人","ONE",43,"战斗"));ListNode listNode6 =newListNode(newBook("千与千寻","宫崎骏",42,"奇幻"));
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
listNode5.next = listNode6;this.head = listNode1;}publicListNodegetHead(){return head;}publicvoidsetHead(ListNode head){this.head = head;}publicintsize(){int count =0;ListNode cur =this.head;while(cur !=null){
count++;
cur = cur.next;}return count;}}
2.3🥉 ListNode类——链表节点信息
packagebook;publicclassListNode{publicBook val;publicListNode next;publicListNode(Book val){this.val = val;}}
3.🥈 user包
这里主要存放我们面向用户的功能以及主函数main
3.1🥉 User类——用户接口
packageuser;importbook.BookList;importoperation.IOperation;publicabstractclassUser{protectedString name;protectedIOperation[] iOperations;publicUser(String name){this.name = name;}publicabstractintmenu();publicvoiddoWork(int choice,BookList bookList){
iOperations[choice].work(bookList);}}
3.2🥉 AdminUser类——管理员
packageuser;importoperation.*;importjava.util.Scanner;publicclassAdminUserextendsUser{publicAdminUser(String name){super(name);this.iOperations =newIOperation[]{newExitOperation(),newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation(),newRemoveOperation(),};}publicintmenu(){System.out.println("===========管理员菜单===========");System.out.println("hello "+this.name +" 欢迎来到图书管理系统");System.out.println("1.查找图书");System.out.println("2.新增图书");System.out.println("3.删除图书");System.out.println("4.显示图书");System.out.println("5.清空书架");System.out.println("0.退出系统");System.out.println("==============================");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;}}
3.3🥉 NormalUser类——普通用户
packageuser;importoperation.*;importjava.util.Scanner;publicclassNormalUserextendsUser{publicNormalUser(String name){super(name);this.iOperations =newIOperation[]{newExitOperation(),newFindOperation(),newBorrowOperation(),newReturnOperation(),};}publicintmenu(){System.out.println("===========普通用户菜单===========");System.out.println("hello "+this.name +" 欢迎来到图书管理系统");System.out.println("1.查找图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("0.退出系统");System.out.println("================================");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;}}
3.4🥉 Main类——主函数入口
packageuser;importbook.BookList;importjava.util.Scanner;publicclassMain{publicstaticUserlogin(){Scanner scanner =newScanner(System.in);System.out.println("请输入用户名:");String name = scanner.nextLine();System.out.println("请输入图书馆登录密码:");int count =0;while(count <=3){String key = scanner.nextLine();if(key.equals("bityyds")){System.out.println("登录成功,请输入你的身份:one - 管理员,else number - 普通用户");int choice = scanner.nextInt();if(choice ==1){String namekey[]={"ljk","msr"};for(int i =0; i < namekey.length; i++){if(name.equals(namekey[i])){returnnewAdminUser(name);}}System.out.println("抱歉,您非管理员,以下为普通用户界面");returnnewNormalUser(name);}else{returnnewNormalUser(name);}}if(count <3){System.out.println("密码错误,请重新输入: ");}
count++;}System.out.println("密码错误次数过多,退出系统");System.exit(0);returnnull;}publicstaticvoidmain(String[] args){BookList bookList =newBookList();User user =login();while(true){int choice = user.menu();
user.doWork(choice, bookList);}}}
4.🥈 operation包
这里主要是我们的功能包。
4.1🥉 AddOperation类——添加图书
packageoperation;importbook.Book;importbook.BookList;importbook.ListNode;publicclassAddOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("新增图书");System.out.println("请输入书名");String bookname = scanner.nextLine();System.out.println("请输入作者");String bookauthor = scanner.nextLine();System.out.println("请输入类型");String booktype = scanner.nextLine();System.out.println("请输入定价");int bookprice = scanner.nextInt();Book book =newBook(bookname, bookauthor, bookprice, booktype);ListNode newHead = bookList.getHead();ListNode node =newListNode(book);
node.next = newHead;
bookList.setHead(node);System.out.println("新增成功!");}}
4.2🥉 BorrowOperation类——借阅图书
packageoperation;importbook.Book;importbook.BookList;importbook.ListNode;publicclassBorrowOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("借阅图书");System.out.println("请输入您要借阅的图书名:");String bookName = scanner.nextLine();ListNode cur = bookList.getHead();while(cur !=null){Book book = cur.val;if(bookName.equals(cur.val.getName())){
book.setBorrowed(true);System.out.println("借阅成功");System.out.println(book);return;}
cur = cur.next;}System.out.println("没有你要借阅的书");}}
4.3🥉 DelOperation类——删除图书
packageoperation;importbook.BookList;importbook.ListNode;publicclassDelOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("删除图书");System.out.println("请输入你要删除的图书名字:");String bookName = scanner.nextLine();ListNode cur = bookList.getHead();if(cur ==null){System.out.println("图书余量为空,不能删除!");return;}if(bookName.equals(cur.val.getName())){
bookList.setHead(cur.next);System.out.println("删除成功");return;}while(cur.next !=null){if(bookName.equals(cur.next.val.getName())){
cur.next = cur.next.next;System.out.println("删除成功");return;}
cur = cur.next;}System.out.println("未找到删除图书");}}
4.4🥉 DisplayOperation类——显示图书
packageoperation;importbook.Book;importbook.BookList;importbook.ListNode;publicclassDisplayOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("显示图书");ListNode cur = bookList.getHead();while(cur !=null){System.out.println(cur.val);
cur = cur.next;}}}
4.5🥉 ExitOperation类——退出系统
packageoperation;importbook.BookList;publicclassExitOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("退出系统");System.exit(0);}}
4.6🥉 FindOperation类——查找图书
packageoperation;importbook.BookList;importbook.ListNode;publicclassFindOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("查找图书");System.out.println("请输入你要查找的图书名字:");String bookName = scanner.nextLine();ListNode cur = bookList.getHead();while(cur !=null){if(bookName.equals(cur.val.getName())){System.out.println(cur.val);return;}
cur = cur.next;}System.out.println("查找书名不存在");}}
4.7🥉 RemoveOperation类——清空书架
packageoperation;importbook.BookList;importbook.ListNode;publicclassRemoveOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("清空书架!系统提示您:请谨慎操作");System.out.println("您是否要清空整个书架: one - 是 other number - 否");int index = scanner.nextInt();if(index ==1){
bookList.setHead(null);System.out.println("清空成功");return;}System.out.println("您的清空操作未执行!");}}
4.8🥉 ReturnOperation类——清空书架
packageoperation;importbook.Book;importbook.BookList;importbook.ListNode;publicclassReturnOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("归还图书");System.out.println("请输入您要归还的图书名:");String bookName = scanner.nextLine();ListNode cur = bookList.getHead();while(cur !=null){Book book = cur.val;if(bookName.equals(cur.val.getName())){
book.setBorrowed(false);System.out.println("归还成功");System.out.println(book);return;}
cur = cur.next;}System.out.println("归还信息不存在,如果您想为图书馆尽一份力,请联系管理员ljk & msr");}
4.9🥉 IOperation类——接口
packageoperation;importbook.BookList;importjava.util.Scanner;/**
* Created with IntelliJ IDEA.
* Description:
* User: mac
* Date: 2022-09-08
* Time: 13:04
*/publicinterfaceIOperation{Scanner scanner =newScanner(System.in);publicvoidwork(BookList bookList);}
5.🥈 源码下载
需要源码的兄弟们点这里就OK咯,免费的哦😜😜😜 图书管理系统源码
想对大家说的话
家人们,学到这里我们已经肝完了JAVA图书管理系统🥳🥳🥳,后续新一会持续更JAVA的有关内容,学习永无止境,技术宅,拯救世界!
版权归原作者 爱打酱油的新一 所有, 如有侵权,请联系我们删除。