✨博客主页: XIN-XIANG荣
✨系列专栏:【Java SE】
✨一句短话: 难在坚持,贵在坚持,成在坚持!
文章目录
前言
这篇博客是在学习了一部分Java基础语法之后的练习项目,通过这个小项目的练习,对Java中的类和对象,抽象类和接口,面向对象的继承、多态和封装、组合等进行熟悉理解;抽象出不同的对象,将对象进行合理的设计,完成对象之间的交互,面向对象进行编程。
1. 项目需求
图书管理系统,面向管理员和普通用户使用,对管理员的开放的功能有:添加图书,删除图书,查找图书,显示图书和退出程序等;对普通用户的开放的功能有:查找图书,借阅图书,归还图书和退出程序。
2. 实现思路
先抽象提取出不同的对象,首先想到的对象是用户和书,
用户可分为管理员和普通用户,将管理员和普通用户共有的属性设置为一个父类User,管理员和普通用户继承User,可以先将父类设置为普通类,在写码过程如果合适可以修改为抽象类;
系统对书进行管理,书本身就可以抽象为一个对象;对多本书进行操作管理,书架也可以抽象为一个对象;所以书和书架用两个类来实现,书架可以设计为一个数组;
这里系统对书进行管理,其实就是对书架对应数组进行操作,不同的操作可以通过一个标准去实现,也就是可以定义一个接口,每一个操作类去实现这个接口,完成向上转型,通过接口实现多态;
然后写代码的过程中思考完善各个对象的成员,最终实例化对象,通过不同对象之间的交互完成管理系统。
3. 代码实现
包的设计
book包
Book类
packagebook;publicclassBook{privateString name;//书名privateString author;//书的作者privateint price;//书的价格privateString type;//书的类型privateboolean borrowed;//是否被借出,默认falsepublicBook(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;}publicBooleangetBorrowed(){return borrowed;}publicvoidsetBorrowed(Boolean borrowed){this.borrowed = borrowed;}@OverridepublicStringtoString(){return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+", borrowed="+((borrowed==true)?"已借出":"未借出")+'}';}}
BookList类
packagebook;publicclassBookList{privateBook[] books =newBook[100];privateint usedsize;//书架当中默认有三本书publicBookList(){this.books[0]=newBook("三国演义","罗贯中",66,"小说");this.books[1]=newBook("西游记","吴承恩",68,"小说");this.books[2]=newBook("红楼梦","曹雪芹",64,"小说");this.usedsize =3;}publicintgetUsedsize(){return usedsize;}publicvoidsetUsedsize(int usedsize){this.usedsize = usedsize;}publicBookgetBooks(int pos){returnthis.books[pos];}publicvoidsetBooks(Book book,int pos){this.books[pos]= book;}}
operations包
IOperation接口
packageoperations;importbook.BookList;publicinterfaceIOperation{voidwork(BookList bookList);}
AddOperation类
packageoperations;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassAddOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("新增图书");//输入图书信息System.out.println("输入新增图书名称:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();System.out.println("输入新增图书作者:");String author = scanner.nextLine();System.out.println("输入新增图书价格:");int price = scanner.nextInt();
scanner.nextLine();System.out.println("输入新增图书类型:");String type = scanner.nextLine();Book book =newBook(name, author, price, type);//获取存书位置int currentSize = bookList.getUsedsize();//把书放到书架(数组)
bookList.setBooks(book, currentSize);//书的总数加1
bookList.setUsedsize(currentSize+1);System.out.println("添加成功!");}}
BorrowOperation类
packageoperations;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassBorrowOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("借阅图书");System.out.println("输入要借阅图书的名称:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedsize();for(int i =0; i < currentSize; i++){Book book = bookList.getBooks(i);if(name.equals(book.getName())){if(book.getBorrowed()){System.out.println("该书已经被借出!");return;}else{
book.setBorrowed(true);System.out.println("借阅成功!");return;}}}System.out.println("没有找到你要借阅的书");}}
DelOperation类
packageoperations;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassDelOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("删除图书");System.out.println("输入要删除图书的名称:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedsize();//找到并记录要删除书的下标int index =-1;for(int i =0; i < currentSize; i++){Book book = bookList.getBooks(i);if(name.equals(book.getName())){
index = i;break;}}if(-1== index){System.out.println("没有找到要删除的这本书!");}else{for(int i = index; i < currentSize-1; i++){Book book = bookList.getBooks(i+1);
bookList.setBooks(book, i);}//每次删除,都要将原来最后一本书位置的引用置空
bookList.setBooks(null,currentSize-1);
bookList.setUsedsize(currentSize-1);System.out.println("删除成功");}}}
DisplayOperation类
packageoperations;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassDisplayOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("显示图书");int currentSize = bookList.getUsedsize();for(int i =0; i < currentSize; i++){Book book = bookList.getBooks(i);System.out.println(book);}}}
ExitOperation类
packageoperations;importbook.BookList;publicclassExitOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("退出系统");System.exit(0);}}
FindOperation类
importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassFindOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("查找图书");System.out.println("输入要查找图书的名称:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedsize();for(int i =0; i < currentSize; i++){Book book = bookList.getBooks(i);if(name.equals(book.getName())){System.out.println("找到了,该书信息如下:");System.out.println(book);return;}}System.out.println("没有找到这本书!");}}
ReturnOperation类
packageoperations;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassReturnOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("归还图书");System.out.println("输入要归还图书的名称:");Scanner scanner =newScanner(System.in);String name = scanner.nextLine();int currentSize = bookList.getUsedsize();for(int i =0; i < currentSize; i++){Book book = bookList.getBooks(i);if(name.equals(book.getName())){if(book.getBorrowed()){
book.setBorrowed(false);System.out.println("归还成功!");return;}else{System.out.println("该书未借出!");return;}}}System.out.println("没有找到你要归还的书");}}
user包
User类
publicabstractclassUser{protectedString name;protectedIOperation[] iOperation;publicUser(String name){this.name = name;}publicabstractintmenu();publicvoiddoOperation(int choice,BookList bookList){
iOperation[choice].work(bookList);}}
AdminUser类
packageuser;importoperations.*;importjava.util.Scanner;publicclassAdminUserextendsUser{publicAdminUser(String name){super(name);this.iOperation =newIOperation[]{newExitOperation(),newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation()};}@Overridepublicintmenu(){System.out.println("---------------------------------");System.out.println("Hello 管理员:>"+""+name+" 欢迎来到图书管理系统!");System.out.println(" 1.查找图书 2.新增图书");System.out.println(" 3.删除图书 4.显示图书");System.out.println(" 0.退出系统");System.out.println("---------------------------------");System.out.println("请输入你的操作:");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;}}
NormalUser类
packageuser;importoperations.*;importjava.util.Scanner;publicclassNormalUserextendsUser{publicNormalUser(String name){super(name);this.iOperation =newIOperation[]{newExitOperation(),newFindOperation(),newBorrowOperation(),newReturnOperation()};}@Overridepublicintmenu(){System.out.println("---------------------------------");System.out.println("Hello 用户:>"+name+" 欢迎来到图书管理系统!");System.out.println(" 1.查找图书 2.借阅图书");System.out.println(" 3.归还图书 0.退出系统");System.out.println("---------------------------------");System.out.println("请输入你的操作:");Scanner scanner =newScanner(System.in);int choice = scanner.nextInt();return choice;}}
Main类
importbook.BookList;importuser.AdminUser;importuser.NormalUser;importuser.User;importjava.util.Scanner;publicclassMain{publicstaticUserlogin(){System.out.println("请输入你的姓名:");Scanner scanner =newScanner(System.in);String userName = scanner.nextLine();System.out.println("请确认你的身份:> |1->管理员|0->普通用户|");int choice = scanner.nextInt();if(1==choice){returnnewAdminUser(userName);}else{returnnewNormalUser(userName);}}publicstaticvoidmain(String[] args){//准备数据BookList bookList =newBookList();//登录User user =login();while(true){int choice = user.menu();
user.doOperation(choice, bookList);}}}
4. 实现效果
管理员
普通用户
版权归原作者 XIN-XIANG荣 所有, 如有侵权,请联系我们删除。