网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
}
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;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", id=" + id +
", isBorrowed=" + isBorrowed +
'}';
}
}
### BookList介绍
![Booklist.png](https://img-blog.csdnimg.cn/img_convert/844af533061994f620f4788ce1950ac2.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u0542ec53&margin=name=Booklist.png&originHeight=626&originWidth=1840&originalType=binary&ratio=1&rotation=0&showTitle=false&size=117811&status=done&style=none&taskId=ucd8db1a5-0be7-47fe-a3dc-1d9ebb8fb8f&title=)
那么一个书单(哈哈哈就当作是书架吧)需要书,那我们就给它new几本书出来,但这个时候books其实是一个空的对象数组,它就相当于一个空的书架,我们需要往上面加几本书,那么正好可以利用我们的构造函数初始化一下,让它一开始就有三本书。
再定义一个usedSize,表示书架上书籍的数量
![image.png](https://img-blog.csdnimg.cn/img_convert/b8447efb68d0f70cb6e6ac3dd54e0a80.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=138&id=ube122c1f&margin=name=image.png&originHeight=173&originWidth=832&originalType=binary&ratio=1&rotation=0&showTitle=false&size=32042&status=done&style=none&taskId=u3fe36588-8ffb-4c1b-b63b-892d3335210&title=&width=665.6)
那么同样我们也要设置一些get和set的方法来访问到这些属性
### BookList源码
package book;
/**
* @ Author 12629
* @ Date 2022/4/10 11:45
* @ Description:书架类
*/
public class BookList {
private Book[] books = new Book[10];//这个书架的大小
private int usedSize;//数组中放了几本书
public BookList() {
books[0] = new Book(1,"三国演义","罗贯中",90,"小说");
books[1] = new Book(2,"西游记","吴承恩",78,"小说");
books[2] = new Book(3,"红楼梦","曹雪芹",89,"小说");
this.usedSize = 3;
}
/\*\*
* 获取当前数组当中的元素的个数
* @ return
*/
public int getUsedSize() {
return usedSize;
}
/\*\*
* 修改当前数组中元素的个数
* @ param usedSize
*/
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
//可以再这里面 写所有对数组的操作 借书 还书.......
/\*\*
* 获取pos下标的书
* @ param pos
* @ return
*/
public Book getPos(int pos) {
return books[pos];
}
/\*\*
* 给数组的pos位置 放一本书
* @param pos
* @param book
*/
public void setBooks(int pos,Book book) {
books[pos] = book;
}
/\* dpublic void work() {
System.out.println(“新增图书!”);
}*/
}
## OPERATION
### Operation介绍
![OPERATION.png](https://img-blog.csdnimg.cn/img_convert/205a256fbb0426e598565fff96759dee.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u3c4d6468&margin=name=OPERATION.png&originHeight=780&originWidth=1576&originalType=binary&ratio=1&rotation=0&showTitle=false&size=252412&status=done&style=none&taskId=udfdc0bea-3e19-4faa-9a2c-d6edbce664c&title=)
>
> 这个包主要就是写好多好多功能
>
>
>
那么我们为了达到代码复用的理念,这里就要用到接口了
>
> 我们在之前有专门对接口进行讲解的博客,不太了解的小伙伴们可以去看看哦
> 👉[一篇带你吃透Java接口](https://bbs.csdn.net/topics/618668825)
>
>
>
### IOpreation(接口)
那么我们要实现的所有功能,都需要去操作,我们可以称为“可操作的”那么需要实现这个功能,我们可以写一个统一的接口,来提高代码的耦合度和复用性,也能够让大家更加熟悉接口的概念。
那么我们的接口中就放一个work方法就OK了
![image.png](https://img-blog.csdnimg.cn/img_convert/e6b3120577df07bbdb80b1cf9fcebc05.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=83&id=u7275a96e&margin=name=image.png&originHeight=104&originWidth=462&originalType=binary&ratio=1&rotation=0&showTitle=false&size=10856&status=done&style=none&taskId=u649175b2-171d-4b6a-827a-53bc9bad24e&title=&width=369.6)
**IOperation源码**
package operation;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination:接口,用来实现各类操作
*/
public interface IOperation {
void work(BookList bookList);
}
### AddOperation
![addOperation.png](https://img-blog.csdnimg.cn/img_convert/1efc31d44756713bb0edc1fd195c6bd0.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u367b8063&margin=name=addOperation.png&originHeight=1140&originWidth=1118&originalType=binary&ratio=1&rotation=0&showTitle=false&size=185236&status=done&style=none&taskId=ud228ece6-0fce-41ff-aae0-57faea11975&title=)
那么我们要输入新增图书的各类信息,导入之后,再去BookList上把书塞进去
**AddOpreation源码**
package operation;
import book.BookList;
/**
* @ Author 12629
* @ Date 2022/4/10 11:54
* @ Description:增加图书
*/
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation{
@Override
public void work(BookList bookList) {
System.out.println("新增一本书籍!");
System.out.println("请输入书籍的ID");
Scanner scanner = new Scanner(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 = new Book(id,name,author, price,type);
bookList.setBooks(bookList.getUsedSize(), book);
bookList.setUsedSize(bookList.getUsedSize() + 1);
}
}
### BorrowOperation
![BorrowOperation.png](https://img-blog.csdnimg.cn/img_convert/8a344ea03a6366c381a29de2c6b1c161.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=u82ba2b67&margin=name=BorrowOperation.png&originHeight=776&originWidth=1654&originalType=binary&ratio=1&rotation=0&showTitle=false&size=78102&status=done&style=none&taskId=u04b60f86-8875-4a8e-b560-651383dd6d3&title=)
**源码如下**
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* @ Author 12629
* @ Date 2022/4/10 11:56
* @ Description:
*/
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“借阅书籍!”);
System.out.println("请输入要借阅的书籍的id: ");
Scanner scanner = new Scanner(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
![DelOperation.png](https://img-blog.csdnimg.cn/img_convert/d2c2851a61468465ea6f43de06787045.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=ufeba7738&margin=name=DelOperation.png&originHeight=646&originWidth=2296&originalType=binary&ratio=1&rotation=0&showTitle=false&size=217426&status=done&style=none&taskId=u816c3ea3-3308-47f6-9943-b10ad7e8403&title=)
**源码**
package operation;
import book.Book;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 删除书籍
*/
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“删除书籍!”);
System.out.println("请输入您想要删除书籍的id: ");
Scanner scanner = new Scanner(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
比较简单,直接上代码
package operation;
import book.Book;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 查看所有图书
*/
public class DisplayOperation implements IOperation {
public void work(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
package operation;
import book.BookList;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination: 退出系统
*/
public class ExitOperation implements IOperation {
public void work(BookList bookList) {
//有可能 需要销毁,或者使用到 这个数组当中的所有的数据
System.out.println("退出系统!");
/\*int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
bookList.setBooks(i,null);
}*/
System.exit(0);
}
}
### FindOperation
![FindOperation.png](https://img-blog.csdnimg.cn/img_convert/bfdf27bdc9ff4c68004c5c6c692fc65e.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=uaf43837a&margin=name=FindOperation.png&originHeight=646&originWidth=1422&originalType=binary&ratio=1&rotation=0&showTitle=false&size=141145&status=done&style=none&taskId=uec9b8eb2-4c02-4bfa-ba5a-cc99e0f0823&title=)
**源码**
package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:Gremmie
* Date: -04-12
* Destination: 查找书籍
*/
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“查找书籍!”);
System.out.println("请输入要查找书籍的书名: ");
Scanner scanner = new Scanner(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
![ReturnOperation.png](https://img-blog.csdnimg.cn/img_convert/df98b73329aea2dee9c7c95786db7f68.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=uafcea4b6&margin=name=ReturnOperation.png&originHeight=566&originWidth=2520&originalType=binary&ratio=1&rotation=0&showTitle=false&size=125431&status=done&style=none&taskId=u0c4316f7-c663-48c6-ad87-927e06070c8&title=)
package operation;
import book.BookList;
import book.Book;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Description: Hello,I would appreciate your comments~
* User:
* Date: -04-12
* Destination:还书
*/
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println(“归还书籍!”);
System.out.println("请输入要归还书籍的id: ");
Scanner scanner = new Scanner(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了
![image.png](https://img-blog.csdnimg.cn/img_convert/e234c63af0db5a7c39bd4a5c10025486.png#clientId=uc1213ff2-9968-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=313&id=u1f5e33e2&margin=name=image.png&originHeight=391&originWidth=650&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43186&status=done&style=none&taskId=u19e719e4-b15b-48a8-8246-cbdf713d0de&title=&width=520)
![img](https://img-blog.csdnimg.cn/img_convert/22d2af1f4e38395b9c17ea623bcdfa85.png)
![img](https://img-blog.csdnimg.cn/img_convert/78038f3af109e4da1095c5c31c2ff98b.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
1f5e33e2&margin=name=image.png&originHeight=391&originWidth=650&originalType=binary&ratio=1&rotation=0&showTitle=false&size=43186&status=done&style=none&taskId=u19e719e4-b15b-48a8-8246-cbdf713d0de&title=&width=520)
[外链图片转存中...(img-nbItAGJL-1715551342597)]
[外链图片转存中...(img-jvYAUZg5-1715551342598)]
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**