考虑到大家看着一大堆冗长且无趣的专业术语和代码就没有了学习的兴趣。 我特地学会了新技能 就是画思维导图!! 这样能够让大家更加容易地去理解其中地概念和内容
那么随着我们的JavaSE初阶的学习博客完结到达尾声,这一篇带着大家做一个小项目,然后借此串联之前所学的内容,达到复习和巩固的效果
首先我们来构建一个大致框架
一个图书管理系统,要有书,放书的地方,管理图书的功能,以及使用者
我们就分别创建三个包和一个Main的程序的入口类,写对应的代码
BOOK
Book介绍
那么我们先从书开始搭建
对于书这个类来说,它有一些属性,比如书名,作者,价格,以及它的类型
它也有一些状态,这里我就用borrowed(即是否被借出的状态)
我们为了达到封装的目的,就要把属性都带上private
那么这里外部类使用的时候就不能访问到相关属性了
怎么办呢?
那就开放一些接口,比如getName用来得到姓名的信息,setName用来修改name的值
然后我们再重写一下Book类的父类Object中的toString方法,以此让用户更方便地获取书籍的信息
Book源码
package book;
/**
* @ Author 12629
* @ Date 2022/4/10 11:41
* @ Description:
*/
public class Book {
private String name;//书名
private String author;//作者
private int price;//价格
private String type;//类型
private int id;//书号
private boolean isBorrowed;//是否被借出
public Book(int id,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;
}
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介绍
那么一个书单(哈哈哈就当作是书架吧)需要书,那我们就给它new几本书出来,但这个时候books其实是一个空的对象数组,它就相当于一个空的书架,我们需要往上面加几本书,那么正好可以利用我们的构造函数初始化一下,让它一开始就有三本书。
再定义一个usedSize,表示书架上书籍的数量
那么同样我们也要设置一些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介绍
这个包主要就是写好多好多功能
那么我们为了达到代码复用的理念,这里就要用到接口了
我们在之前有专门对接口进行讲解的博客,不太了解的小伙伴们可以去看看哦 👉一篇带你吃透Java接口
IOpreation(接口)
那么我们要实现的所有功能,都需要去操作,我们可以称为“可操作的”那么需要实现这个功能,我们可以写一个统一的接口,来提高代码的耦合度和复用性,也能够让大家更加熟悉接口的概念。
那么我们的接口中就放一个work方法就OK了
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
那么我们要输入新增图书的各类信息,导入之后,再去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
源码如下
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
源码
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
源码
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
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了
package user;
import book.BookList;
import operation.IOperation;
/**
* @ Author 12629
* @ Date 2022/4/10 12:04
* @ Description:user的抽象类,用来被继承
*/
abstract public class User {
protected String name;
protected IOperation[] IOperations;
public User(String name) {
this.name = name;
}
abstract public int menu();
public void doOperation(int choice, BookList bookList) {
IOperations[choice].work(bookList);
}
}
AdminUser
管理员用户和普通用户能做的事情不一样,他们的menu也不一样
我们需要在管理员用户的类中继承User的抽象类并重写其中的一些方法
再往接口数组里面放一些功能性的对象
源码👇
package user;
import operation.*;
import java.util.Scanner;
/**
* @ Author 12629
* @ Date 2022/4/10 12:05
* @ Description:管理员
*/
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
this.IOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new DisplayOperation()
};
}
public int menu() {
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("请输入你的操作:");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
}
NormalUser
普通用户同理
源码👇
package user;
import operation.*;
import java.util.Scanner;
/**
* @ Author 12629
* @ Date 2022/4/10 12:05
* @ Description:普通用户
*/
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.IOperations = new IOperation[] {
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
public int menu() {
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("请输入你的操作:");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
}
Main
那么我们需要在这个程序进入的地方设置一个登录方法,然后在main方法中调用,运用while达到循环操作,while中通过choice来判断进入哪个user界面
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
/**
* @ Author 12629
* @ Date 2022/4/10 11:38
* @ Description:程序进入的Main类
*/
public class Main {
public static User login() {
System.out.println("请输入姓名:");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.println("请输入你的身份:1-》管理员,0-》普通用户");
int choice = scan.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
//准备好了书
BookList bookList = new BookList();
User user = login();//user到底引用哪个对象 需要看
while(true) {
int choice = user.menu();
//user引用哪个对象?
// 选择了1之后,调用的是user引用的对象的具体操作,这个怎么做??
user.doOperation(choice,bookList);
}
}
}
运行示例
E:\develop\Java\jdk-11\bin\java.exe "-javaagent:E:\IDEA\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=59768:E:\IDEA\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath E:\JAVAcode\gyljava\TestBook\out\production\TestBook Main
请输入姓名:
geyuli
请输入你的身份:1-》管理员,0-》普通用户
1
hello geyuli 欢迎来到图书小练习
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
请输入你的操作:
1
查找书籍!
请输入要查找书籍的书名:
三国演义
Book{name='三国演义', author='罗贯中', price=90, type='小说', id=0, isBorrowed=false}
共找到 1 本相同书籍!
hello geyuli 欢迎来到图书小练习
1.查找图书
2.新增图书
3.删除图书
4.显示图书
0.退出系统
请输入你的操作:
0
退出系统!
Process finished with exit code 0
那么简单的图书管理系统就完成了
画图不易码字不易,希望大家多多支持和指点错误
感谢阅读~