当前位置: 首页 > news >正文

JAVA实现图书管理系统(初阶)

一.抽象出对象:

1.要有书架,图书,用户(包括普通用户,管理员用户)。根据这些我们可以建立几个包,来把繁杂的代码分开,再通过一个类来把这些,对象整合起来实现系统。说到整合,肯定缺不了,相关接口,我们再定义一个,放接口,和扩展这个接口的方法。

如图:

二.构思:

1.先在书架类上,初始化好默认书籍,其他构造方法(如:getBook,setBook(在具体的下标,放书和返回书)),具体,在写实现接口的方法时,来增加。

public class BookList {//组合的方式,初始化书架private Book[] books = new Book[10];private int usedSize;//实际放的,书的个数//初始化书架(放书)public BookList() {this.books[0] = new Book("三国演义", "罗贯中", 12, "小说");this.books[1] = new Book("红楼梦", "曹雪芹", 13, "小说");this.books[2] = new Book("西游记", "吴承恩", 14, "小说");this.usedSize = 3;}//返回一本,pos(要找的书)下标的书public Book getBook(int pos) {return books[pos];}//插入一本书的方法(相当于,要初始化好,书架原来已有的书)public void setBook(int pos, Book books) {this.books[pos] = books;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book[] getBooks() {return books;}public void setBooks(Book[] books) {this.books = books;}
}

2.在book类中写一些图书对象的,基本属性,和给成员变量初始化,的方法。

public class Book {private String name;//书籍名字private String author;//书籍作者private int price;//书籍价格private String type;//书籍类型private boolean isBorrowed;//受否被借出public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public String getType() {return type;}public void setType(String type) {this.type = type;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +"," +((isBorrowed == true) ? "已借出" : "未借出") +/*" isBorrowed=" + isBorrowed*/'}';}
}

3.在User类中,定义好name和,相关构造方法,以及接口命名的数组,为后面,到达调用,扩展了接口的类,里的方法,做铺垫。

public abstract class User {protected String name;//定义,接口命名,类型的数组,后续配合,// 【return new AdminUser(name);】就可以看出,再加上接口调用的方法,就知道,操作了哪一个方法protected IOperation[] iOperations;//要根据子类,来初始化,父类成员变量public User(String name) {this.name = name;}public abstract int menu();//这里封装一个方法,提供给,Main调用。public void DoIOperation(int choice, BookList bookList) {//这里,iOperations数组,里有我们要的对象,通过,数组里的对象,调用接口里的方法iOperations[choice].work(bookList);}
}

4.管理员类中(AdminUser)和普通用户类中(NormalUser)继承了user类,初始化好系统菜单,相关构造方法。(这个构造方法很关键,用接口作为数组相当于实例化了,扩展了接口的类,的方法,达到调用系统具体方法的作用 

public class NormalUser extends User{public NormalUser(String name) {super(name);//通过【return new AdminUser(name);】,再加上实现接口的方法,就知道,操作了哪一个方法//登录界面,选择了哪个,角色(NormalUser)或者(AdminUser),this就是哪个的引用this.iOperations = new IOperation[] {//这些对象都实现了,iOperations接口,所以不会报错//下面相当于实例化了,扩展了接口的类,的方法,达到调用系统具体方法的作用new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation(),};}public int menu() {System.out.println("欢迎" + this.name + "使用图书系统");System.out.println("********普通用户菜单********");System.out.println("1. 查找图书");System.out.println("2. 借阅图书");System.out.println("3. 归还图书");System.out.println("0. 退出系统");System.out.println("*************************");Scanner scanner = new Scanner(System.in);System.out.println("请输入你的操作:");int choice = scanner.nextInt();return choice;}}

5.在Main类中写好,登录界面,及整合一下,如何实例化对象,来操作系统。

 public static void main(String[] args) {//实例化书架BookList bookList = new BookList();//通过返回值,向上转型,确定用户//这里的user是,返回的(AdminUser),或者(NormalUser)User user = login();while (true) {//然后,通过返回信息,调用恰当的菜单int choice = user.menu();//发生了,动态绑定/*** 根据choice,返回值看看,调用了哪个方法** 1.哪个对象?* 答:User user = login();** 2.哪个方法?-》进一步,还要确定,当前对象,包含了这些方法*答:在构造方法【return new AdminUser(name)】运行时,会初始化好,对应的操作对象。** 注意:后面通过父类对象,调用方法,(int choice = user.menu();),通过choice判断* 调用了,哪个方法 ,接下来就要对,父类,进行操作*/user.DoIOperation(choice, bookList);}}}

6.初始化好,接口和,菜单里操作系统的work方法(实现了这个接口的,类就是,每个操作系统的方法)

public interface IOperation {//这个接口,有操作书架的方法,在其他类实现,就可以,操作性的区分,不同用户的方法public void work(BookList bookList);
}

7.接下来就是实现了,接口的每一个类每个操作系统的方法

以下是管理员菜单方法:

(1).查找图书:

public class FindOperation implements IOperation {@Overridepublic void work(BookList bookList) {System.out.println("查找图书");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("请输入你要查找的图书");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);return;}}System.out.println("没有你要找的书...");}
}

(2).新增图书:

public class AddOperation implements IOperation{@Overridepublic void work(BookList bookList) {//1.判断书架(数组)是否满了int currentSize = bookList.getUsedSize();if (currentSize == bookList.getBooks().length) {System.out.println("该书架满了,不能放了");return;}//2.构建对象Scanner scanner = new Scanner(System.in);System.out.println("请输入新增的书名");String name = scanner.nextLine();System.out.println("请输入新增的作者");String author = scanner.nextLine();System.out.println("请输入新增的价格");int price = scanner.nextInt();System.out.println("请输入新增的类型");String type = scanner.next();Book newBook = new Book(name, author, price, type);//3.判断书架是否,已经存在这本书for (int i = 0; i < currentSize; i++) {//遍历,书架,已初始化的书Book book = bookList.getBook(i);if (book.getName().equals(name)) {System.out.println("书已经存在了,不用再添加了");return;}}//插入图书bookList.setBook(currentSize, newBook);bookList.setUsedSize(currentSize+1);}
}

(3).删除图书:

public class DelOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("删除图书");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("请输入你要删除的图书");String name = scanner.nextLine();int pos = 0;int i = 0;for (; i < currentSize; i++) {//遍历,书架,已初始化的书Book book = bookList.getBook(i);if (book.getName().equals(name)) {//找到要删除的,位置pos = i;break;}}if (i == currentSize) {System.out.println("没有找到你要删除的图书");}//开始删除for (int j = pos; j < currentSize-1; j++) {//思路:bookList[j] = bookList[j+1];//先找到j+1,那个位置,然后覆盖Book book = bookList.getBook(j+1);bookList.setBook(j, book);}//更新下标bookList.setUsedSize(currentSize-1);System.out.println("删除成功");}
}

(4).显示图书:

public class ShowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("显示图书");int currentSize = bookList.getUsedSize();for (int i = 0; i < currentSize; i++) {//遍历下标,把找到的图书打印出来Book book = bookList.getBook(i);System.out.println(book);}}}

(5).退出系统:

public class ExitOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统");System.exit(0);}
}

以下是普通用户菜单方法:

(1).退出系统和查找图书,是普通人员和管理员的共同方法

(2)归还图书:

public class ReturnOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("归还图书");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("请输入你要归还的图书");String name = scanner.nextLine();for (int i = 0; i < currentSize; i++) {//遍历,书架,已初始化的书Book book = bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),还是false(未借出)if (book.isBorrowed()) {book.setBorrowed(false);return;}}}System.out.println("错误,没有你要归还的图书");}
}

(3)借阅图书:

public class BorrowOperation implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书");int currentSize = bookList.getUsedSize();Scanner scanner = new Scanner(System.in);System.out.println("请输入你要借阅的图书");String name = scanner.nextLine();for (int i = 0; i < currentSize; i++) {//遍历,书架,已初始化的书Book book = bookList.getBook(i);if (book.getName().equals(name)) {//看看isBorrowed返回,ture(已借出),还是false(未借出)if (book.isBorrowed()) {System.out.println("该书已经被借出");return;}book.setBorrowed(true);//置为借出System.out.println("借阅成功");return;}}System.out.println("没有找到你要借阅的那本书");}
}

相关文章:

  • LeetCode26. 删除有序数组中的重复项
  • win10/win11 优先调用大核的电源计划性能设置
  • 在vue中实现下载文件功能
  • VUE3-form表单保存附件与基本信息
  • 【C++初阶】—— 类和对象 (上)
  • 深入了解Redis的过期策略和内存淘汰机制
  • 5月27日
  • Spring Boot中如何实现定时任务?
  • el-select 组件获取整个对象
  • 模型实战(20)之 yolov8分类模型训练自己的数据集
  • yolov8+ROS+ubuntu18.04——学习记录
  • Redis篇 String
  • .Net Core 中间件与过滤器
  • 02 FreeRTOS 任务
  • PyCharm面板ctrl+鼠标滚轮放大缩小代码
  • 【comparator, comparable】小总结
  • 【css3】浏览器内核及其兼容性
  • C++11: atomic 头文件
  • CAP理论的例子讲解
  • extjs4学习之配置
  • github指令
  • JavaScript设计模式系列一:工厂模式
  • js操作时间(持续更新)
  • SQLServer插入数据
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • tweak 支持第三方库
  • Unix命令
  • Yeoman_Bower_Grunt
  • 从输入URL到页面加载发生了什么
  • 后端_ThinkPHP5
  • 实战:基于Spring Boot快速开发RESTful风格API接口
  • 用element的upload组件实现多图片上传和压缩
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (28)oracle数据迁移(容器)-部署包资源
  • (多级缓存)多级缓存
  • (简单) HDU 2612 Find a way,BFS。
  • (自用)交互协议设计——protobuf序列化
  • .gitignore文件---让git自动忽略指定文件
  • .NET delegate 委托 、 Event 事件,接口回调
  • .Net 转战 Android 4.4 日常笔记(4)--按钮事件和国际化
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .net6+aspose.words导出word并转pdf
  • /ThinkPHP/Library/Think/Storage/Driver/File.class.php  LINE: 48
  • @Documented注解的作用
  • []新浪博客如何插入代码(其他博客应该也可以)
  • [【JSON2WEB】 13 基于REST2SQL 和 Amis 的 SQL 查询分析器
  • [20160807][系统设计的三次迭代]
  • [ACP云计算]易混淆知识点(考题总结)
  • [AIGC] CompletableFuture的重要方法有哪些?
  • [CTF夺旗赛] CTFshow Web1-12 详细过程保姆级教程~
  • [Editor]Unity Editor类常用方法
  • [FFmpeg学习]从视频中获取图片
  • [Git][分支管理][上]详细讲解
  • [HarmonyOS]第一课:从简单的页面开始