Java实现图书管理系统
⚡️前言⚡️
本篇文章是对目前Java专栏已有内容的一个总结练习,希望各位小主们在学习完面向对象的知识后,可以阅览本篇文章后,自己也动手实现一个这样的demo来加深总结应用已经学到知识并进行巩固。
🍉博客主页:****🍁【如风暖阳】🍁
🍉精品Java专栏【Javase】、【Java数据结构】
🍉欢迎点赞 👍 收藏 ⭐留言评论 📝私信必回哟😁🍉本文由 【如风暖阳】 原创,首发于 CSDN🙉
🍉博主将持续更新学习记录收获,友友们有任何问题可以在评论区留言
🍉博客中涉及源码及博主日常练习代码均已上传码云(gitee)
📍内容导读📍
🍉1.系统导读
Java最主要的特征便是面向对象,这个小练习就是让我们加强对面向对象的理解,所以系统的实现肯定是通过分多个包和类来共同实现系统功能的。
该系统的通过四部分来实现:
1.book包下为图书信息的存储
2.user包下为用户的信息及操作逻辑
3.Main文件下为整个系统的逻辑实现
4.operation包下为系统功能的具体实现
首先是登陆页面,输入姓名后选择身份,然后跳转到不同的菜单(管理员和普通用户两份菜单),然后在用户进一步选择功能以后去实现对应的功能。
🍉2.创建系统所需类
2.1图书信息(book包)
该系统是通过顺序表来实现的,在顺序表的数组中存储的是每一本书的信息,然后通过顺序表进行增删查改功能的实现(顺序表了解可以博主之前的文章【Java数据结构之顺序表】)
Book.java
书需要包含书名、作者、价格、类型、是否被借出等信息
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(){//以下getter、setter实现通过idea中的快捷键alt+insert来实现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;}@Override//重写toString()方法是为了打印书的具体信息,如果不重写不会打印出书的具体内容publicStringtoString(){return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+((isBorrowed==true)?"已被借出":"未被借出")+'}';}}
BookList.java
packagebook;publicclassBookList{privateBook[] books=newBook[10];//顺序表数组,暂时先将长度设小,随着接下来的学习,该部分还会继续改进privateint useSize;//记录目前书的本数publicBookList(){//在构造方法中先存储三本书的信息
books[0]=newBook("三国演义","罗贯中",17,"小说");
books[1]=newBook("西游记","吴承恩",47,"小说");
books[2]=newBook("水浒传","施耐庵",37,"小说");this.useSize=3;}publicintgetUseSize(){return useSize;}publicvoidsetUseSize(int useSize){this.useSize = useSize;}//以下两个方法为手敲(非快捷键)publicBookgetPos(int pos){returnthis.books[pos];}//得到角标为pos位置的书publicvoidsetBook(int pos,Book book){this.books[pos]= book;}//将角标为pos位置的书改为函数传参传过来的book.}
2.2用户信息(user包)
因为使用者有管理者和普通用户两个对象,但两者多有相同属性且有细致区别的方法,所以将相同代码抽取出来创建抽象类User,再建立AdminUser和NormalUser分别继承User。
User.java
packageuser;importbook.BookList;importoperation.IOperation;publicabstractclassUser{String name;//使用者姓名IOperation[] iOperations;//创建使用者操作数组,数组内用于存储多项功能publicUser(String name){this.name = name;}publicabstractintmenu();//两个对象的菜单不同,所以创建抽象方法publicvoiddoWork(int choice,BookList bookList){
iOperations[choice].work(bookList);}//这个方法是在用户进行选择后,根据对应操作数组的选择进行不同操作}
AdminUser.java
packageuser;importoperation.*;importjava.util.Scanner;publicclassAdminUserextendsUser{publicAdminUser(String name){super(name);this.iOperations =newIOperation[]{newExitOperation(),newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation()};//构造方法内实例化不同对象对应的不同操作菜单}//打印不同菜单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("0.退出系统");System.out.println("==============================");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;//用户进行选择,选择后将choice值返回,调用实现对应功能}}
NormalUser.java
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.系统逻辑(Main.java)
importbook.BookList;importuser.AdminUser;importuser.NormalUser;importuser.User;importjava.util.Scanner;publicclassMain{//无论对象是谁,都需要先展示登录界面publicstaticUserlogin(){System.out.println("请输入你的姓名:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();System.out.println("请输入你的身份:1-》管理员,0-》普通用户");int choice = scanner.nextInt();if(choice ==1){//根据choice的不同值,实例化不同对象并将其返回returnnewAdminUser(name);}else{returnnewNormalUser(name);}}publicstaticvoidmain(String[] args){BookList bookList=newBookList();User user=login();while(true){int choice = user.menu();//发生了动态绑定 -》 多态//根据你的choice 调用合适的操作
user.doWork(choice, bookList);}}}
🍉4.功能实现(operation包)
因为系统有多项功能,所以在这个包下,将每一个功能都独立创建为一个类来具体清晰实现,为了这些类中的代码复用,又抽象出来一个接口(多个类共有的功能)
Ioperation.java
packageoperation;importbook.BookList;importjava.util.Scanner;publicinterfaceIOperation{Scanner scanner =newScanner(System.in);voidwork(BookList bookList);}
AddOperation.java
新增图书需要new图书并将信息初始化,然后将其放在顺序表最后位置
packageoperation;importbook.Book;importbook.BookList;publicclassAddOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("新增图书!");System.out.println("请输入书名:");String name = scanner.next();System.out.println("请输入作者:");String author= scanner.next();System.out.println("请输入类型:");String type=scanner.next();System.out.println("请输入价格:");int price=scanner.nextInt();Book book=newBook(name, author,price,type);//实例化新增图书int size= bookList.getUseSize();//导包后调用bookList.getUseSize();函数确定新增图书存放位置
bookList.setBook(size,book);//新增图书
bookList.setUseSize(size+1);//有效书目加一System.out.println("新增图书成功!");}}
BorrowOperation.java
输入借阅图书书名后遍历数组查找,找到后将图书借阅情况更改
packageoperation;importbook.Book;importbook.BookList;publicclassBorrowOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("借阅图书!");System.out.println("请输入要借阅图书的名字");String name=scanner.nextLine();int size=bookList.getUseSize();for(int i =0; i < size; i++){Book book=bookList.getPos(i);if(name.equals(book.getName())){
book.setBorrowed(true);System.out.println("借阅成功");System.out.println(book);return;}}System.out.println("没有你要借阅的书");}}
DelOperation.java
packageoperation;importbook.Book;importbook.BookList;publicclassDelOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("删除图书!");System.out.println("请输入要删除的图书");String name=scanner.next();int size= bookList.getUseSize();//设置循环先进行书目查找for(int i =0; i < size; i++){Book book=bookList.getPos(i);//找到后数组前移覆盖此书if(name.equals(book.getName())){for(int j = i; j < size-1; j++){Book bookNext=bookList.getPos(j+1);
bookList.setBook(j,bookNext);}
bookList.setBook(size,null);
bookList.setUseSize(size-1);//删除书目后有效书目-1;System.out.println("删除完成");return;}}System.out.println("该书不存在");}}
DisplayOperation.java
把已有书目信息全部打印
packageoperation;importbook.Book;importbook.BookList;importjava.util.Arrays;publicclassDisplayOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("浏览图书!");int size= bookList.getUseSize();for(int i =0; i < size; i++){Book book=bookList.getPos(i);System.out.println(book);}}}
ExitOperation.java
退出系统
packageoperation;importbook.BookList;publicclassExitOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("退出系统!");System.exit(0);}}
FindOperation.java
packageoperation;importbook.Book;importbook.BookList;publicclassFindOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("查找图书!");System.out.println("请输入要查找的图书:");String name=scanner.next();int size= bookList.getUseSize();for(int i =0; i < size; i++){Book book = bookList.getPos(i);if(name.equals(book.getName())){System.out.println("找到了这本书,书的信息如下:");System.out.println(book);return;}}System.out.println("没找到");}}
ReturnOperation.java
packageoperation;importbook.Book;importbook.BookList;publicclassReturnOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("归还图书!");System.out.println("请输入要归还图书的名字");String name=scanner.nextLine();int size=bookList.getUseSize();for(int i =0; i < size; i++){Book book=bookList.getPos(i);if(name.equals(book.getName())){
book.setBorrowed(true);System.out.println("归还成功");System.out.println(book);return;}}System.out.println("没有你要归还的书");}}
⚡️最后的话⚡️
总结不易,希望uu们不要吝啬你们的👍哟(^U^)ノ~YO!!如有问题,欢迎评论区批评指正😁
版权归原作者 如风暖阳 所有, 如有侵权,请联系我们删除。