前言
较为着急的朋友们直接下滑到最后,有完整代码将放在最后,不着急的朋友们请随我慢慢思考构建这次的代码框架,这也是对抽象类,接口,数组等知识的复习。
前提
写代码的基本思路
- 一.明确对象有哪些:书和人。
- 二.与人相关的放在User类,与书相关的放在Book类。
从对象---书开始思考
思考与书相关的
-
一.我们要做的是图书管理员系统。
-
二.要有书,什么书,谁的书,书的价格--书的属性--构建书的对象。
-
三.书存放在哪:书架上--构建书架。
- 四.管理员能对书做些什么--添加,删除,查找,显示,借出,收回
- 五.普通人能对书做些什么--查找,借阅,归还
初步完善对象-书
Book.java
对Book.java进行完善 对 书 第二点的思考
Book.java
(1).对象

(2).创建构造函数

(3).创建get和set方法
原因:代码都是private类型的能是代码更加安全,但这时就需要get和set方法来帮助访问内部的参数。

(4).创建toString方法

BookList.java的完善
对BookList.java进行完善 对书第三点:书存放在哪的思考
BookList.java
1.添加图书

2.借出图书

3.删除图书

4.退出管理系统

5.查找图书

6.类型统一

7.归还图书

8.展示图书

图解

从人开始思考User.java前部分
思考与人相关的
有哪些使用者------管理员-普通人
1.使用者(父类)

- 2.普通人(子类)
- 3.管理员(子类)
-
- 2.他们的区别--对图书管理系统掌控的范围(操作的功能不同)
2.main方法

在写user.menu() 之前,我们就要将user包 中子类的menu提前写好,
写好之后发现我们通过user调用menu的时候还是会出错,
这时候我们再看向下方的User.java能否替我们解答
User.java的完善
3.menu方法
根据他们对图书管理系统掌控的范围分成
1.管理员AdminUser-menu

2.普通人NormalUser-menu

3.父类User-menu
当我们通过user调用menu的时候就会出错,源于父类一开始并没有子类的menu方法。当我们在父类写了menu方法之后,基于其内部没有具体的实现,我们将其实现为abstract方法。


思路点拨
大家写到这的时候,基本上都会遇到一个相差不大的
难点1:我们从login()中收到的choice确定我们的菜单为哪一个。

难点2:我们从menu()中收到的choice确定我们该怎么输出不同使用者的相同序号的不同功能。
难点1和2的解决方法图
1.User.Java下添加下列doOperation方法 。--通过下列第三张图进行图文并茂的方式解释,更容易理解 2.添加了目前已经存放的书籍,以及解决无法传下标的问题的三红框代码
3.上面两张图的逻辑图过程
此时我们已经实现了基本的框架了,只差这些功能的具体实现。

Operation包的具体实现
下列代码中包含大量的重复核心代码,前几张图已经添加注释,十分清楚。若有疑问可以查看前几张图的注释,以及评论区内询问,我尽力解答。
一、AddOperation

二、BorrowOperation

三、DelOperation
四、ExitOperation
五、FindOperation

六、ReturnOperation
七、ShowOperation

完整代码
book包
- 1.Book.java
typescript
package book;
public class Book {
private String name;//书名
private String author;//作者
private int price;//书的价格
private String type;//书的类型
private boolean isBorrowed;//是否被借出,默认值为false
//构造函数
public Book(String name, String author, int price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
//没有isBorrwoed的原因:默认值为false,不需要特地赋值。
}
//get和set方法
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;
}
//toString方法
@Override
public String toString() {
return "Book{" +
"name='" + name + ''' +
", author='" + author + ''' +
", price=" + price +
", type='" + type + ''' +
( (isBorrowed == true) ? ",已借出" : ",未借出" )+
//", isBorrowed=" + isBorrowed +
'}';
}
}
- 2.BookList.java
csharp
package book;
public class BookList {
private Book[] books;//用数组创建书,利于描述和添加书的属性
private int usedSize;//记录当前书架放了几本书
public BookList() {
this.books = new Book[10];
this.books[0] = new Book("蛊真人", "蛊真人", 20, "小说");
this.books[1] = new Book("西游记", "吴承恩", 9, "小说");
this.books[2] = new Book("红楼梦", "曹雪芹", 19, "小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos) {//因为books数组为private,所以需要手动传BookList的下标,
return books[pos];//返回下标
}
public void setBook(Book book, int pos) {
books[pos] = book;//下标赋值
}
}
user包
- 1.User.java
java
package user;
import Operation.IOPeration;
import book.BookList;
public abstract class User {
protected String name;
protected IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BookList bookList) {//使用菜单menu返回的choice
IOPeration ioPeration = this.ioPerations[choice];//向上转型的一种,并非接口定义变量
ioPeration.work(bookList);
//上面两行可以简化成一行代码
//this.ioPerations[choice].work(bookList);
}
}
- 2.AdminUser.java
csharp
package user;
import Operation.*;
import java.util.Scanner;
public class AdminUser.java extends User {
public AdminUser(String name) {
super(name);
this.ioPerations = new IOPeration[] {
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
public int menu() {
System.out.println("********管理员菜单********");
System.out.println("1.查找图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.显示图书");
System.out.println("0.退出系统");
System.out.println("************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
- 3.NormalUser.java
csharp
package user;
import Operation.*;
import java.util.Scanner;
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("********普通用户菜单********");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("***************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
Operation包
- 1AddOperation
ini
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration {
@Override
public void work(BookList bookList) {
System.out.println("添加图书");
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("请输入你要新增图书的类型:");
scanner.nextLine();//由于nextInt之后写nextLine会冲突,导致回车会吞掉nextLine,所以需要这一行供给回车
String type = scanner.nextLine();
//此时就可以构造出 书的对象
Book book = new Book(name,author,price,type);//书的添加样式 书名-作者-价格-类型
int currentSize = bookList.getUsedSize();//计算目前书架上书的已存放的数量
for (int i = 0; i < currentSize; i++) { //通过for循环查询是否存在这本书
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
System.out.println("存在这本书,不能重复添加!");
return;
}
}
//没有重复的书 开始新增
bookList.setBook(book,currentSize);//使当前书架的空白位置存放一本书
bookList.setUsedSize(currentSize+1);//使当前书架的已存放数量+1
}
}
- 2BorrowOperation
java
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("借出图书");
System.out.println("请写出你要借阅的图书的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();//算目前书架上书的已存放的数量
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);//获取借阅书的下标
if(book.getName().equals(name)) {
//有这本书的
book.setBorrowed(true);//更改状态 是否借出
System.out.println("借阅成功!");
return;
}
}
System.out.println("没有你要借阅的图书:"+name);
}
}
- 3DelOperation
ini
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("删除图书");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要删除图书的书名:");
int currentSize = bookList.getUsedSize();
int index = -1;//随机数值,做判断的
String name = scanner.nextLine();
int i = 0;
for (; i < currentSize; i++) {
Book tmp = bookList.getBook(i);
if(tmp.getName().equals(name)) {
index = i;//代表存在这本书
break;//记录下来了 要删除图书的姓名
}
}
//
if(i >= currentSize) {//代表:书架都翻完了都没看见
System.out.println("没有你要删除的图书!");
return;
}
//删除 (通过覆盖实现)
for (int j = index; j < currentSize-1; j++) {
//bookList[j] = bookList[j+1]
Book book = bookList.getBook(j+1); //获取下一本书的下标
bookList.setBook(book,j); //循环下标赋值 将删去图书的后一本书内容覆盖上去,促成这本书的删除
}
bookList.setBook(null,currentSize-1); //后面会空出一个位置,赋值NULL自动释放
bookList.setUsedSize(currentSize-1);
System.out.println("删除成功!");
}
}
- 4ExitOperation
java
package Operation;
import book.BookList;
public class ExitOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("退出图书管理系统");
//应该要 对 bookList 资源 手动回收
System.exit(0);
}
}
- 5FindOperation
ini
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("查找图书");
System.out.println("请写出你要查找的图书的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//Book book = bookList[i];
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("存在这本书,信息如下:");
System.out.println(book);
return;
}
}
//代码没有return(结果) ,说明没有有你要找的书
System.out.println("没有你要找的这本书,书名为:"+ name);
}
}
- 6* ReturnOperation *
java
package Operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("归还图书");
System.out.println("请写出你要归还的图书的书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {//根据书名判断是否有这本书
book.setBorrowed(false);//设置未借出
System.out.println("归还成功!");
return;
}
}
System.out.println("没有你要归还的图书:"+name);
}
}
- 7.ShowOperation
ini
package Operation;
import book.Book;
import book.BookList;
public class ShowOperation implements IOPeration {
public void work(BookList bookList) {
System.out.println("展示图书");
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
//Book book = bookList[i];
Book book = bookList.getBook(i);//获取书的下标
System.out.println(book);//打印书架上的所有书
}
}
}
- 8.IOPeration.java
java
package Operation;
import book.BookList;
public interface IOPeration {
void work(BookList bookList);
}