前言
觉得有用的老铁留下你的三连!
学习了Java的面向对象,我还是有点懵逼的,不知道面向对象有多好,直到写了图书管理系统才知道,和面向过程比代码并没有少多少,最主要的是每个动作都被打包起来了,让人很好检查,接下来让我介绍一下图书管理系统并且实现吧!
文章目录
分析出所有的对象
1.书籍—Book
2.书架—BookList
3.管理员操作和普通用户操作
具体为:
管理员操作:
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出
普通用户操作:
查询书籍
借阅书籍
归还书籍
退出
4.使用者—User
具体主要流程是:使用者通过调用操作类操作书架中的书籍
Book包
Book类
publicclassBook{privateString name;//书名privateString author;//作者privateint price;//价格privateString type;//类型privateboolean isBorrowed=false;//是否被借publicBook(String name,String author,int price,String type){this.name=name;this.author=author;this.price=price;this.type=type;}@OverridepublicStringtoString(){//输出书籍信息return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+", isBorrowed="+(isBorrowed==false?"\'未被借出\'":"\'已被借出\'")+'}';}publicStringgetName(){//用来查找书籍return name;}publicvoidsetBorrowed(boolean borrowed){//用来修改状态
isBorrowed = borrowed;}publicbooleanisBorrowed(){//获取状态return isBorrowed;}}
BookList类
publicclassBookList{//静态顺序表privateBook[] books=newBook[10];//存放10本书privateint size=3;//存有书的个数publicBookList(){//初始化书籍
books[0]=newBook("三国演义","罗贯中",100,"小说");
books[1]=newBook("水浒传","施耐庵",100,"小说");
books[2]=newBook("西游记","吴承恩",100,"小说");}publicBookgetBook(int pos){return books[pos];}publicvoidsetBook(int pos,Book book){
books[pos]=book;}publicintgetSize(){return size;}publicvoidsetSize(int size){this.size = size;}}
Operation包
这里通过接口来实现操作功能的,利用类来实例化接口,达成操作过程,至于我们如何调用这些功能,我们可以创建一个接口数组来存放这些类,这个在User包中具体实现!
当然你也可以创建一个操作(抽象)类,让具体操作类来继承他,然后创建一个子类数组,这里操作和接口数组一样,当然我没试过,这只是个人想法!
IOperation接口
publicinterfaceIOperation{publicvoidwork(BookList booklist);}
AddOperation类
publicclassAddOperationimplementsIOperation{publicvoidwork(BookList booklist){Scanner scanner=newScanner(System.in);System.out.println("增加图书");System.out.println("请输入图书的名字:");String name=scanner.nextLine();System.out.println("请输入图书的作者");String author=scanner.nextLine();System.out.println("请输入图书的类型:");String type=scanner.nextLine();System.out.println("请输入图书的价格:");int price= scanner.nextInt();Book book=newBook(name,author,price,type);int size=booklist.getSize();
booklist.setBook(size,book);
booklist.setSize(size+1);System.out.println("图书增加成功!");};}
BorrowOperation类
publicclassBorrowOperationimplementsIOperation{publicvoidwork(BookList booklist){System.out.println("请输入你要借的书名:");Scanner scanner=newScanner(System.in);String name=scanner.nextLine();for(int i =0; i < booklist.getSize(); i++){if(name.equals(booklist.getBook(i).getName())){if(booklist.getBook(i).isBorrowed()==true){System.out.println("你想借的书已被他人借出!");return;}
booklist.getBook(i).setBorrowed(true);System.out.println("成功借出!");return;}}System.out.println("没找到你需要的书,请借阅下面显示未借出的书籍!");for(int i =0; i < booklist.getSize(); i++){System.out.println(booklist.getBook(i).toString());}}}
DelOperation类
publicclassDelOperationimplementsIOperation{publicvoidwork(BookList booklist){int cur=0;//记录要删除图书的位置int size=booklist.getSize();//记录原生图书个数System.out.println("请输入你要删除图书的名字:");Scanner scanner=newScanner(System.in);String name=scanner.nextLine();for(int i =0; i < booklist.getSize(); i++){if(name.equals(booklist.getBook(i).getName())){
cur=i;for(int j = cur; j <booklist.getSize(); j++){
booklist.setBook(j,booklist.getBook(j+1));//覆盖}
booklist.setSize(size-1);System.out.println("删除成功!");return;}}System.out.println("没找到你要删除的书!");};}
DisplayOperation类
publicclassDisplayOperationimplementsIOperation{publicvoidwork(BookList booklist){for(int i =0; i < booklist.getSize(); i++){System.out.println(booklist.getBook(i).toString());}};}
ExitOperation类
publicclassExitOperationimplementsIOperation{publicvoidwork(BookList booklist){System.out.println("退出程序!");exit(-1);};}
FindOperation类
publicclassFindOperationimplementsIOperation{publicvoidwork(BookList booklist){System.out.println("请输入你要查找的书名:");Scanner scanner=newScanner(System.in);String name=scanner.nextLine();for(int i =0; i < booklist.getSize(); i++){if(name.equals(booklist.getBook(i).getName())){System.out.println(booklist.getBook(i).toString());return;}}System.out.println("未找到你查找的书籍!");};}
ReturnOperation类
publicclassReturnOperationimplementsIOperation{publicvoidwork(BookList booklist){System.out.println("请输入你归还的书名:");Scanner scanner=newScanner(System.in);String name=scanner.nextLine();for(int i =0; i < booklist.getSize(); i++){if(name.equals(booklist.getBook(i).getName())){
booklist.getBook(i).setBorrowed(false);System.out.println("成功归还!");return;}}System.out.println("你归还的书不是本馆的图书!");};}
User包
User类——父类
abstractpublicclassUser{protectedString name;protectedIOperation[] operations;abstractpublicintmenu();//菜单publicvoiddooperation(int choice,BookList bookList){//调用功能
operations[choice].work(bookList);};}
NormalUser类——子类
publicclassNormalUserextendsUser{publicNormalUser(String name){//构造函数this.name=name;this.operations=newIOperation[]{newExitOperation(),newFindOperation(),newBorrowOperation(),newReturnOperation()};}@Overridepublicintmenu(){//重写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("=============================");System.out.println("请输入您的选择: ");Scanner scanner=newScanner(System.in);int choice=scanner.nextInt();return choice;}@Overridepublicvoiddooperation(int choice,BookList bookList){//重写super.dooperation(choice, bookList);}}
Admin 类——子类
publicclassAdminextendsUser{@Overridepublicintmenu(){//重写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("=============================");System.out.println("请输入您的选择: ");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;}publicAdmin(String name){this.name=name;this.operations=newIOperation[]{newExitOperation(),newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation()};}}
Main函数(重点)
publicclassMain{publicstaticvoidmain(String[] args){// 1. 准备基本的数据BookList bookList =newBookList();User user=login();//向上转型,动态绑定,实现多态while(true){int choice=user.menu();
user.dooperation(choice, bookList);}}publicstaticUserlogin(){Scanner scanner =newScanner(System.in);System.out.println("请输入你的名字:");String name=scanner.nextLine();System.out.println("请选择你的身份:0->管理员 1->普通用户");int who = scanner.nextInt();if(who==0){returnnewAdmin(name);}else{returnnewNormalUser(name);}}}
很多东西我都写在注释中,大家得仔细看!
版权归原作者 相知- 所有, 如有侵权,请联系我们删除。