考虑到大家看着一大堆冗长且无趣的专业术语和代码就没有了学习的兴趣。
我特地学会了新技能
就是画思维导图!!
这样能够让大家更加容易地去理解其中地概念和内容文章目录
那么随着我们的JavaSE初阶的学习博客完结到达尾声,这一篇带着大家做一个小项目,然后借此串联之前所学的内容,达到复习和巩固的效果
首先我们来构建一个大致框架
一个图书管理系统,要有书,放书的地方,管理图书的功能,以及使用者
我们就分别创建三个包和一个Main的程序的入口类,写对应的代码
BOOK
Book介绍
那么我们先从书开始搭建
对于书这个类来说,它有一些属性,比如书名,作者,价格,以及它的类型
它也有一些状态,这里我就用borrowed(即是否被借出的状态)
我们为了达到封装的目的,就要把属性都带上private
那么这里外部类使用的时候就不能访问到相关属性了
怎么办呢?
那就开放一些接口,比如getName用来得到姓名的信息,setName用来修改name的值
然后我们再重写一下Book类的父类Object中的toString方法,以此让用户更方便地获取书籍的信息
Book源码
packagebook;/**
* @ Author 12629
* @ Date 2022/4/10 11:41
* @ Description:
*/publicclassBook{privateString name;//书名privateString author;//作者privateint price;//价格privateString type;//类型privateint id;//书号privateboolean isBorrowed;//是否被借出publicBook(int id,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;}publicintgetId(){return id;}publicvoidsetId(int id){this.id = id;}@OverridepublicStringtoString(){return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+", id="+ id +", isBorrowed="+ isBorrowed +'}';}}
BookList介绍
那么一个书单(哈哈哈就当作是书架吧)需要书,那我们就给它new几本书出来,但这个时候books其实是一个空的对象数组,它就相当于一个空的书架,我们需要往上面加几本书,那么正好可以利用我们的构造函数初始化一下,让它一开始就有三本书。
再定义一个usedSize,表示书架上书籍的数量
那么同样我们也要设置一些get和set的方法来访问到这些属性
BookList源码
packagebook;/**
* @ Author 12629
* @ Date 2022/4/10 11:45
* @ Description:书架类
*/publicclassBookList{privateBook[] books =newBook[10];//这个书架的大小privateint usedSize;//数组中放了几本书publicBookList(){
books[0]=newBook(1,"三国演义","罗贯中",90,"小说");
books[1]=newBook(2,"西游记","吴承恩",78,"小说");
books[2]=newBook(3,"红楼梦","曹雪芹",89,"小说");this.usedSize =3;}/**
* 获取当前数组当中的元素的个数
* @ return
*/publicintgetUsedSize(){return usedSize;}/**
* 修改当前数组中元素的个数
* @ param usedSize
*/publicvoidsetUsedSize(int usedSize){this.usedSize = usedSize;}//可以再这里面 写所有对数组的操作 借书 还书......./**
* 获取pos下标的书
* @ param pos
* @ return
*/publicBookgetPos(int pos){return books[pos];}/**
* 给数组的pos位置 放一本书
* @param pos
* @param book
*/publicvoidsetBooks(int pos,Book book){
books[pos]= book;}/* dpublic void work() {
System.out.println("新增图书!");
}*/}
OPERATION
Operation介绍
这个包主要就是写好多好多功能
那么我们为了达到代码复用的理念,这里就要用到接口了
我们在之前有专门对接口进行讲解的博客,不太了解的小伙伴们可以去看看哦
👉一篇带你吃透Java接口
IOpreation(接口)
那么我们要实现的所有功能,都需要去操作,我们可以称为“可操作的”那么需要实现这个功能,我们可以写一个统一的接口,来提高代码的耦合度和复用性,也能够让大家更加熟悉接口的概念。
那么我们的接口中就放一个work方法就OK了
IOperation源码
packageoperation;importbook.BookList;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination:接口,用来实现各类操作
*/publicinterfaceIOperation{voidwork(BookList bookList);}
AddOperation
那么我们要输入新增图书的各类信息,导入之后,再去BookList上把书塞进去
AddOpreation源码
packageoperation;importbook.BookList;/**
* @ Author 12629
* @ Date 2022/4/10 11:54
* @ Description:增加图书
*/importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassAddOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("新增一本书籍!");System.out.println("请输入书籍的ID");Scanner scanner =newScanner(System.in);int id = scanner.nextInt();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(id,name,author, price,type);
bookList.setBooks(bookList.getUsedSize(), book);
bookList.setUsedSize(bookList.getUsedSize()+1);}}
BorrowOperation
源码如下
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;/**
* @ Author 12629
* @ Date 2022/4/10 11:56
* @ Description:
*/publicclassBorrowOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("借阅书籍!");System.out.println("请输入要借阅的书籍的id: ");Scanner scanner =newScanner(System.in);int id = scanner.nextInt();for(int i =0; i < bookList.getUsedSize(); i++){Book book = bookList.getPos(i);if(!(id == book.getId())){continue;}if(book.isBorrowed()){System.out.println("此书已经被借走了!");break;}
book.setBorrowed(true);System.out.println("借阅成功");}}}
DelOperation
源码
packageoperation;importbook.Book;importbook.BookList;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 删除书籍
*/importjava.util.Scanner;publicclassDelOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("删除书籍!");System.out.println("请输入您想要删除书籍的id: ");Scanner scanner =newScanner(System.in);int id = scanner.nextInt();int i =0;for(; i < bookList.getUsedSize(); i++){Book book = bookList.getPos(i);if(book.getId()==id){break;}}if(i >= bookList.getUsedSize()){System.out.println("未找到要找的书籍!");return;}Book lastBook = bookList.getPos(bookList.getUsedSize()-1);
bookList.setBooks(i, lastBook);
bookList.setUsedSize(bookList.getUsedSize()-1);System.out.println("删除成功!");}}
DisplayOperation
比较简单,直接上代码
packageoperation;importbook.Book;importbook.BookList;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 查看所有图书
*/publicclassDisplayOperationimplementsIOperation{publicvoidwork(BookList bookList){System.out.println("显示图书!");int currentSize = bookList.getUsedSize();for(int i =0; i < currentSize; i++){Book book = bookList.getPos(i);System.out.println(book);}}}
ExitOperation
packageoperation;importbook.BookList;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 退出系统
*/publicclassExitOperationimplementsIOperation{publicvoidwork(BookList bookList){//有可能 需要销毁,或者使用到 这个数组当中的所有的数据System.out.println("退出系统!");/*int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBooks(i,null);
}*/System.exit(0);}}
FindOperation
源码
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:Gremmie
* Date: -04-12
* Destination: 查找书籍
*/publicclassFindOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("查找书籍!");System.out.println("请输入要查找书籍的书名: ");Scanner scanner =newScanner(System.in);String name = scanner.next();int count =0;for(int i =0; i < bookList.getUsedSize(); i++){Book book = bookList.getPos(i);if(book.getName().equals(name)){System.out.println(book);
count++;}}if(count ==0){System.out.println("没找到此书籍!");}else{System.out.println("共找到 "+ count +" 本相同书籍!");}}}
RetuenOperation
packageoperation;importbook.BookList;importbook.Book;importjava.util.Scanner;/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination:还书
*/publicclassReturnOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("归还书籍!");System.out.println("请输入要归还书籍的id: ");Scanner scanner =newScanner(System.in);int id = scanner.nextInt();int i =0;for(; i < bookList.getUsedSize(); i++){Book book = bookList.getPos(i);if(book.getId()!=id){continue;}if(!book.isBorrowed()){System.out.println("这本书未借出去,归还失败!");}
book.setBorrowed(false);}}}
USER
那么user可以分为普通用户和管理员用户,这里可以将用户设置为一个抽象类或者是接口
抽象类很好写,只要写他们之间的共性就OK了
packageuser;importbook.BookList;importoperation.IOperation;/**
* @ Author 12629
* @ Date 2022/4/10 12:04
* @ Description:user的抽象类,用来被继承
*/abstractpublicclassUser{protectedString name;protectedIOperation[]IOperations;publicUser(String name){this.name = name;}abstractpublicintmenu();publicvoiddoOperation(int choice,BookList bookList){IOperations[choice].work(bookList);}}
AdminUser
管理员用户和普通用户能做的事情不一样,他们的menu也不一样
我们需要在管理员用户的类中继承User的抽象类并重写其中的一些方法
再往接口数组里面放一些功能性的对象
源码👇
packageuser;importoperation.*;importjava.util.Scanner;/**
* @ Author 12629
* @ Date 2022/4/10 12:05
* @ Description:管理员
*/publicclassAdminUserextendsUser{publicAdminUser(String name){super(name);this.IOperations=newIOperation[]{newExitOperation(),newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation()};}publicintmenu(){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 sca =newScanner(System.in);int choice = sca.nextInt();return choice;}}
NormalUser
普通用户同理
源码👇
packageuser;importoperation.*;importjava.util.Scanner;/**
* @ Author 12629
* @ Date 2022/4/10 12:05
* @ Description:普通用户
*/publicclassNormalUserextendsUser{publicNormalUser(String name){super(name);this.IOperations=newIOperation[]{newExitOperation(),newFindOperation(),newBorrowOperation(),newReturnOperation()};}publicintmenu(){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 sca =newScanner(System.in);int choice = sca.nextInt();return choice;}}
Main
那么我们需要在这个程序进入的地方设置一个登录方法,然后在main方法中调用,运用while达到循环操作,while中通过choice来判断进入哪个user界面
importbook.BookList;importuser.AdminUser;importuser.NormalUser;importuser.User;importjava.util.Scanner;/**
* @ Author 12629
* @ Date 2022/4/10 11:38
* @ Description:程序进入的Main类
*/publicclassMain{publicstaticUserlogin(){System.out.println("请输入姓名:");Scanner scan =newScanner(System.in);String name = scan.nextLine();System.out.println("请输入你的身份:1-》管理员,0-》普通用户");int choice = scan.nextInt();if(choice ==1){returnnewAdminUser(name);}else{returnnewNormalUser(name);}}publicstaticvoidmain(String[] args){//准备好了书BookList bookList =newBookList();User user =login();//user到底引用哪个对象 需要看while(true){int choice = user.menu();//user引用哪个对象?// 选择了1之后,调用的是user引用的对象的具体操作,这个怎么做??
user.doOperation(choice,bookList);}}}
运行示例
E:\develop\Java\jdk-11\bin\java.exe "-javaagent:E:\IDEA\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=59768:E:\IDEA\IntelliJ IDEA Community Edition 2021.3.2\bin"-Dfile.encoding=UTF-8-classpath E:\JAVAcode\gyljava\TestBook\out\production\TestBookMain
请输入姓名:
geyuli
请输入你的身份:1-》管理员,0-》普通用户
1
hello geyuli 欢迎来到图书小练习
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
请输入你的操作:
1
查找书籍!
请输入要查找书籍的书名:
三国演义
Book{name='三国演义', author='罗贯中', price=90, type='小说', id=0, isBorrowed=false}
共找到 1 本相同书籍!
hello geyuli 欢迎来到图书小练习
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
请输入你的操作:
0
退出系统!
Process finished withexit code 0
那么简单的图书管理系统就完成了
画图不易码字不易,希望大家多多支持和指点错误
感谢阅读~
版权归原作者 Gremmie102 所有, 如有侵权,请联系我们删除。