💖欢迎来阅读子豪的博客(Java实操篇🍱)
👉有什么宝贵的意见或建议可以在留言区留言 💬
👽欢迎 素质三连 点赞+关注+收藏💯
🧑🚀码云仓库: 补集王子的代码仓库🧑🔧
BMS
项目实现
创几个包:用来组织类(分类)
🍔1. 每本书数据【book】
Book类
publicclassBook{}
设置字段(书的属性)
publicclassBook{privateString name;//书名privateString author;//作者privateint price;//价格privateString type;//类型privateboolean isBorrowed;//是否被借出 默认false
提供构造
实例化对象,设置几个属性,因为字段是private修饰的就给每属性get set方法
在加个构造和toString方法
//构造publicBook(String name,String author,int price,String type){this.name = name;this.author = author;this.price = price;this.type = type;}
get+set方法,便于操作数据
//get+setpublicStringgetName(){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;}
重写toString方法,便于打印显示,(借出处略加修改)
//toString@OverridepublicStringtoString(){return"Book{"+"name='"+ name +'\''+", author='"+ author +'\''+", price="+ price +", type='"+ type +'\''+((isBorrowed==true)?"已经借出":"未借出")+'}';}
🍟2. 书架储存书【bookList】
BookList类
publicclassBookList{}
设置书架大小
//对多放十本privateBook[] books =newBook[3];privateint usedSize ;//实时记录 目前有几本书
初始化书架,类构造方法
publicBookList(){
books[0]=newBook("西游记","吴承恩",10,"小说");
books[1]=newBook("三国演义","施耐庵",11,"小说");
books[2]=newBook("红楼梦","曹雪芹",12,"小说");
usedSize =3;}
getBook,拿出pos处的书
//拿出pos处的书publicBookgetBook(int pos){return books[pos];}
setBook,给pos下标放一本书
// 给pos下标放一本书publicvoidsetBook(int pos,Book book){
books[pos]= book;}
getUesSize,setUesSize,获取实时书本数,用于修改
//获取实时书本数,用于修改publicintsetUesSize(int size){return(usedSize = size);}publicintgetUesSize(){returnthis.usedSize;}
🍕3. 设置两个角色【user】
管理员【AdmainUser】
publicclassAdmainUserextendsUser{}
构造方法,提供构造方法帮助父类构造
//提供构造方法帮助父类构造publicAdmainUser(String name){super(name);//在new管理人员这个对象时,就把这些功能一并写入this.iOperations =newIOperation[]{newExitOperation(),//0newFindOperation(),newAddOperation(),newDelOperation(),newDisplayOperation()};
菜单
//设置一个开始菜单publicint menu (){System.out.println("hello"+this.name+"欢迎使用BMS!");System.out.println("1.查找图书");System.out.println("2.增添图书");System.out.println("3.删除图书");System.out.println("4.显示图书");System.out.println("0.退出BNS");Scanner scanner =newScanner(System.in);return scanner.nextInt();}
普通用户【NormalUser】
publicclassNormalUserextendsUser{}
菜单【menu】
//设置一个开始菜单publicint menu (){System.out.println("hello"+this.name+"欢迎使用BMS!");System.out.println("1.查找图书");System.out.println("2.借阅图书");System.out.println("3.归还图书");System.out.println("0.退出BNS");Scanner scanner =newScanner(System.in);return scanner.nextInt();}
构造方法,提供构造方法帮助父类构造
//提供构造方法帮助父类构造publicNormalUser(String name){super(name);//在new一般人员这个对象时,就把这些功能一并写入this.iOperations =newIOperation[]{newExitOperation(),//0newFindOperation(),newBorrowOperation(),newReturnOperation()};}
🌭4. 每个角色对应功能列表不一样
身份不同,拥有的功能不一样
管理员一般用户1.查找图书1.查找图书2.增添图书2.借阅图书3.删除图书3.归还图书4.显示图书0.退出BNS0.退出BNS
🍜5. 实现每一具体功能【operation】
接口【interface】
功能标准化【IOperation】
publicinterfaceIOperation{//实现功能publicabstractvoidwork(BookList bookList);}
增添图书【AddOperation】
- implements实现接口
- 录入书本信息
- 用getUesSize(),在书架的size位置插入这本书
Tips:
要先输入字符串String,再输入整数int!
先输入整数再输字符串的话回车会被读进去
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassAddOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("添加图书!");Scanner scanner =newScanner(System.in);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 newbook =newBook(name,author,price,type);
bookList.setBook(bookList.getUesSize(),newbook);
bookList.setUesSize(bookList.getUesSize()+1);//更新数量System.out.println("新增书记成功!");}}
借阅图书【BorrowOperation】
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassBorrowOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("借出图书!");System.out.print("请输入借阅图书的名字:");int currentSize = bookList.getUesSize();Scanner scanner =newScanner(System.in);String name = scanner.nextLine();for(int i =0; i < currentSize; i++){Book book = bookList.getBook(i);if(book.getName().equals(name)){System.out.println("找到了!");
book.setBorrowed(true);//改状态System.out.println("借阅成功!");return;}}System.out.println("没有这本书!");System.out.println();}}
删除图书【DelOperation】
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;/**
*覆盖
*/publicclassDelOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("删除图书!");System.out.print("请输入图书的名字:");int currentSize = bookList.getUesSize();Scanner scanner =newScanner(System.in);String name = scanner.nextLine();int index =-1;//存下标int i =0;for(; i < currentSize; i++){Book book = bookList.getBook(i);if(book.getName().equals(name)){System.out.println("找到了!");
index = i;System.out.println(book);System.out.println();}}if(i >= currentSize){System.out.println("找不到此书");return;}//直接覆盖for(int j = index; j < currentSize-1; i++){
bookList.setBook(j,bookList.getBook(j +1));}
bookList.setUesSize(bookList.getUesSize()-1);//更新数量
bookList.setBook(currentSize-1,null);//置为空 相当于freeSystem.out.println("删除成功!");System.out.println();}}
显示图书【DisplayOperation】
packageoperation;importbook.BookList;publicclassDisplayOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("显示图书!");int currentSize = bookList.getUesSize();for(int i =0; i < currentSize; i++){System.out.println(bookList.getBook(i));}System.out.println();}}
归还图书【ReturnOperation】
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassReturnOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("归还图书!");System.out.print("请输入归还图书的名字:");int currentSize = bookList.getUesSize();Scanner scanner =newScanner(System.in);String name = scanner.nextLine();for(int i =0; i < currentSize; i++){Book book = bookList.getBook(i);if(book.getName().equals(name)){System.out.println("找到了!");
book.setBorrowed(true);//改状态System.out.println("归还成功!");return;}}System.out.println("没有这本书!");System.out.println();}}
查找图书【FindOperation】
packageoperation;importbook.Book;importbook.BookList;importjava.util.Scanner;publicclassFindOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.print("请输入图书的名字:");int currentSize = bookList.getUesSize();Scanner scanner =newScanner(System.in);String name = scanner.nextLine();for(int i =0; i < currentSize; i++){Book book = bookList.getBook(i);if(book.getName().equals(name)){System.out.println("找到了!");System.out.println(book);System.out.println();return;}}System.out.println("没有这本书!");System.out.println();}}
退出系统【ExitOperation】
packageoperation;importbook.BookList;publicclassExitOperationimplementsIOperation{@Overridepublicvoidwork(BookList bookList){System.out.println("退出系统!");System.exit(0);// 状态码 零代表正常结束,负数异常结束}}
🍚6. 主函数 【Main】
整理所有功能
importbook.Book;importbook.BookList;importuser.AdmainUser;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-->管理员,2-->普通用户");int choise = scanner.nextInt();if(choise ==1){returnnewAdmainUser(name);}else{returnnewNormalUser(name);}}publicstaticvoidmain(String[] args){//整合!BookList bookList =newBookList();//准备图书//登录,User user =login();while(true){int choise = user.menu();//父类的引用想访问子类的方法,此方法必须为抽象方法,动态绑定
user.doOperator(choise, bookList);}}}
🥡7. 总结
我亦无他,惟手熟尔!,多动手实操!在实践中进步!
欢迎大家,点赞+关注+收藏!
版权归原作者 补集王子 所有, 如有侵权,请联系我们删除。