一、效果展示
1)界面
![](https://i-blog.csdnimg.cn/blog_migrate/65744241c9313ca8d922aff8c4ef4db2.png)
2)[1]新增图书
逻辑要求:当六本书时提示已满,无需添加
![](https://i-blog.csdnimg.cn/blog_migrate/104347aec9451de60e81dc8deb3c53dc.png)
3)[2]查看图书
逻辑要求:遍历字符串二维数组
![](https://i-blog.csdnimg.cn/blog_migrate/c40a1eae3356c3f74e2c1e2bc7b7e04d.png)
4)[3]删除图书
逻辑要求:当图书状态为已借出时,图书不可删除;处理好空指针异常问题;
![](https://i-blog.csdnimg.cn/blog_migrate/e66c32fe318909d0f291c0660896fbcb.png)
5)[4]借出图书
逻辑要求:更新状态;热度自增;
![](https://i-blog.csdnimg.cn/blog_migrate/b81899aa567c8481633d799012671209.png)
6)[5]归还图书
逻辑要求:更新状态
![](https://i-blog.csdnimg.cn/blog_migrate/ba03a7d3f1e7eb477e2f337ba9aea7d7.png)
7)[6]提出系统
![](https://i-blog.csdnimg.cn/blog_migrate/65ded2e4ca3355c3639c7897863856e8.png)
二、源码
main()方法
package com.bdqn.图书管理系统;
import java.util.Scanner;
public class MainLibrarySystem {
public static void main(String[] args) {
/*
* 图书管理系统 新增图书 查看图书 删除图书 借出图书 归还图书 退出图书系统
*/
// 实例化对象
Scanner sc = new Scanner(System.in);
Function function = new Function();
while(true){
// 首页展示
function.showHome();
// 用户选择
function.select();
}
//sc.close();
}
}
function类
package com.bdqn.图书管理系统;
import java.util.Scanner;
public class Function {
Scanner sc = new Scanner(System.in);
private String bookName;
private String bookAuthor;
private String bookPrice;
private String[][] books = { { "百年孤独", "马尔克斯", "¥99", "67", "未借出" },
{ "红高粱", "莫言", "¥99", "67", "未借出" },
{ null, null, null, "0", "未借出" }, { null, null, null, "0", "已借出" },
{ null, null, null, "0", "未借出" }, { null, null, null, "0", "未借出" } };
// A主界面
public void showHome() {
// 16个- 新增图书 查看图书 删除图书 借出图书 归还图书 退出图书系统
System.out.println("----------------图书管理系统----------------");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[1]新增图书\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[2]查看图书\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[3]删除图书\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[4]借出图书\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[5]归还图书\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t[6]退出系统\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("*\t\t\t\t\t*");
System.out.println("----------------图书管理系统----------------");
}
// B操作选项
public void select() {
// 第一步 键盘录入操作数
System.out.print("请输入你要执行的功能:");
int choose = sc.nextInt();
// 第二部 选择对应的功能
if (choose == 1) {
// 功能1 新增图书
addBooks();
} else if (choose == 2) {
showBooks();
} else if (choose == 3) {
delete();
} else if (choose == 4) {
lend();
} else if (choose == 5) {
repay();
} else if (choose == 6) {
exit();
}
}
// C返回上一级方法 返回值布尔类型 true为继续 false返回
public boolean back() {
while (true) {
System.out.println("\n继续请按[1]\t返回请按[0]");
int input = sc.nextInt();
if (input == 1) {
// 即继续
return true;
} else if (input == 0) {
// 即返回
return false;
} else {
System.out.print("请入错误!请重新输入:");
// 返回循环头重来
continue;
}
}
}
// [1]新增图书 图书信息 0书名 1作者 2价格 3受欢迎度
public void addBooks() {
while (true) {
// 第一步 寻找插入的下标
int insertIndex = 0;
for (int i = 0; i < books.length; i++) {
if (books[i][0] == null) {
// 找到下标
insertIndex = i;
// 结束循环
break;
}
}
// 第二步 判断是否已满,即下标==5
if (insertIndex == 5) {
System.out.println("图书已满,无需添加!");
} else {
// 1.键盘录入
System.out.print("请输入图书名称:");
bookName = sc.next();
System.out.print("请输入图书作者:");
bookAuthor = sc.next();
System.out.print("请输入图书价格:");
bookPrice = sc.next();
// 2.插入数据 0书名 1作者 2价格 3受欢迎度
books[insertIndex][0] = bookName;
books[insertIndex][1] = bookAuthor;
books[insertIndex][2] = "¥" + bookPrice;
System.out.println("插入成功!");
}
// 第三步 继续 返回操作
showBooks();
if (back()) {
// 继续添加
continue;
} else {
// 结束循环 回到上一级
break;
}
}
}
// [2]查看图书
public void showBooks() {
// 遍历books数组
System.out.println("序号\t\t书名\t\t作者\t\t价格\t\t热度\t\t借出状态");
for (int i = 0; i < books.length && books[i][0] != null; i++) {
// 序号
System.out.print((i + 1) + "\t\t");
for (int j = 0; j < books[i].length && books[i][j] != null; j++) {
System.out.print(books[i][j] + "\t\t");
}
System.out.println();
}
// 返回上一级
System.out.print("任意键返回...");
sc.next();
}
// [3]删除图书
public void delete() {
while (true) {
// 第一步 键盘录入要删除的书名
System.out.print("请输入您要删除的图书[书名]:");
String bookName = sc.next();
// 第二步 寻找最后一条记录的下标
int endIndex = 0;
for (int i = 0; i < books.length; i++) {
if (books[i][0] == null) {
// 找到下标 i自减1才是最后一条记录
endIndex = --i;
// 结束循环
break;
}
}
// 第三步
// 1.寻找要删除的下标
int deleteIndex = 0;
for (int i = 0; i < books.length; i++) {
if (books[i][0].equals(bookName)) {
deleteIndex = i;
break;
}
}
// 2.判断要删除的记录的状态 已借出时
if ("已借出".equals(books[deleteIndex][4])) {
System.out.println("该书已被借出,可以选择换一本!");
} else {
// 3.未借出时 判断删除的下标是不是最后一个
if (deleteIndex == endIndex) {
for (int col = 0; col < books[col].length; col++) {
// 将要删除的一条记录中的每项赋值为空 即删除
books[deleteIndex][col] = null;
}
System.out.println("操作成功!");
// 不是最后一个时 row < books.length-1
} else {
for (int row = deleteIndex; row < books.length - 1; row++) {
for (int col = 0; col < books[row].length; col++) {
// 后向前覆盖
books[row][col] = books[row + 1][col];
}
}
System.out.println("操作成功!");
}
}
// 返回或继续删除 返回true则继续
if (back()) {
continue;
} else {
break;
}
}
}
// [4] 借出图书
public void lend() {
while (true) {
System.out.println("请输入您要借的书[书名]:");
String bookName = sc.next();
// 遍历找到书名对应的记录
for (int i = 0; i < books.length && books[i][0] != null; i++) {
if (books[i][0].equals(bookName)) {
if (books[i][4].equals("未借出")) {
books[i][4] = "已借出";
System.out.print("借出成功!\n\n任意键退出...");
//成功借出 则热度加1
books[i][3] = Integer.parseInt(books[i][3])+1+"";
sc.next();
return;
} else {
System.out.println("书籍已借出!");
System.out.print("任意键退出...");
sc.next();
return;
}
}
}
System.out.println("书名输入错误!");
// 继续或返回
if (back()) {
continue;
} else {
break;
}
}
}
// [5] 归还图书
public void repay() {
while (true) {
System.out.println("请输入您要还的书[书名]:");
String bookName = sc.next();
// 遍历找到书名对应的记录
for (int i = 0; i < books.length && books[i][0] != null; i++) {
if (books[i][0].equals(bookName)) {
if (books[i][4].equals("已借出")) {
books[i][4] = "未借出";
System.out.print("归还成功!\n\n任意键退出...");
sc.next();
return;
} else {
System.out.println("书籍已归还无需还书!");
System.out.print("任意键退出...");
sc.next();
return;
}
}
}
System.out.println("书名输入错误!");
// 继续或返回
if (back()) {
continue;
} else {
break;
}
}
}
// [6]退出系统
public void exit() {
System.out.println("欢迎下次使用哦!");
System.exit(0);
}
}