Appearance
全局配置
Appearance
// test.java
package com.southwind.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String[] bookName = {"三国演义","西游记","水浒传","红楼梦","七龙珠","海贼王"};
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
System.out.println(Arrays.binarySearch(bookName, name));
}
}
// Book.java
package com.southwind.test;
import java.util.Arrays;
import java.util.Scanner;
public class Book {
public static void main(String[] args) {
//初始化数据
String[] bookName = {"三国演义","西游记","水浒传","红楼梦","七龙珠","海贼王"};
String[] state = {"可借","可借","可借","可借","可借","可借"};
int[] count = {0,0,0,0,0,0};
Scanner scanner = new Scanner(System.in);
int num = 0;
do {
System.out.println("欢迎使用图书借阅系统");
System.out.println("-----------------------------");
System.out.println("1.查看图书");
System.out.println("2.新增图书");
System.out.println("3.删除图书");
System.out.println("4.借阅图书");
System.out.println("5.归还图书");
System.out.println("6.退出系统");
System.out.println("-----------------------------");
System.out.print("请选择:");
num = scanner.nextInt();
switch(num) {
case 1:
System.out.println("---》查看图书");
System.out.println("序号\t\t名称\t\t状态\t\t借出次数");
for(int i = 1;i <= bookName.length;i++) {
System.out.print(i);
System.out.print("\t\t"+bookName[i-1]);
System.out.print("\t\t"+state[i-1]);
System.out.println("\t\t"+count[i-1]);
}
break;
case 2:
System.out.println("---》新增图书");
//判断数组中是否有位置来存放新书
if(bookName[bookName.length-1] == null) {
System.out.print("请输入书名:");
String name = scanner.next();
//检测数组中是否已经存在
//默认数组中没有用户输入的书
boolean flag = false;
for(String name2:bookName) {
if(name.equals(name2)) {
flag = true;
break;
}
}
if(flag) {
System.out.println("<<"+name+">>已存在,请勿重复添加");
}else {
for(int i = 0; i < bookName.length; i++) {
if(bookName[i] == null) {
bookName[i] = name;
break;
}
}
System.out.println("<<"+name+">>添加成功!");
}
}else {
System.out.println("书架已满!!!");
}
break;
case 3:
System.out.println("--》删除图书");
//1.检测bookName是否为空
boolean flag = false;
for(String name:bookName) {
if(name != null) {
flag = true;
break;
}
}
if(flag) {
//2.检测用户输入的书是否存在于书架中
System.out.print("请输入要删除的书名:");
String name = scanner.next();
boolean flag2 = false;
for(String name2:bookName) {
if(name.equals(name2)) {
flag2 = true;
break;
}
}
if(flag2) {
//找到被删除的书在数组中的下标
int index = 0;
for(int i = 0; i < bookName.length; i++) {
if(name.equals(bookName[i])) {
index = i;
break;
}
}
//index之后的数据向前提
for(int i = index;i < bookName.length-1;i++) {
bookName[i] = bookName[i+1];
}
//将数组的最后一位置为null
bookName[bookName.length-1] = null;
System.out.println("<<"+name+">>成功删除!");
}else {
System.out.println("您输入的书名不存在,请确认!");
}
}else {
System.out.println("书架已空,没有可以删除的书籍!");
}
break;
case 4:
System.out.println("--》借阅图书");
//1.检测bookName是否为空
flag = false;
for(String name:bookName) {
if(name != null) {
flag = true;
break;
}
}
if(flag) {
//2.检测用户输入的书是否存在于书架中
System.out.print("请输入要借阅的书名:");
String name = scanner.next();
boolean flag2 = false;
for(String name2:bookName) {
if(name.equals(name2)) {
flag2 = true;
break;
}
}
if(flag2) {
//将图书对应的状态改为已借出
//将图书对应的数量进行+1
int index = 0;
for(int i = 0; i < bookName.length; i++) {
if(name.equals(bookName[i])) {
index = i;
break;
}
}
if(state[index].equals("已借出")) {
System.out.println("<<"+name+">>已借出,请选择其他书籍!");
}else {
state[index] = "已借出";
count[index] = ++count[index];
System.out.println("<<"+name+">>借阅成功!");
}
}else {
System.out.println("您输入的书名不存在,请确认!");
}
}else {
System.out.println("书架已空,没有可以借阅的书籍!");
}
break;
case 5:
System.out.println("--》归还图书");
System.out.print("请输入要归还的书名:");
String name = scanner.next();
boolean flag2 = false;
for(String name2:bookName) {
if(name.equals(name2)) {
flag2 = true;
break;
}
}
if(flag2) {
int index = 0;
for(int i = 0; i < bookName.length; i++) {
if(name.equals(bookName[i])) {
index = i;
break;
}
}
if(state[index].equals("可借")) {
System.out.println("<<"+name+">>已经归还!");
}else {
state[index] = "可借";
System.out.println("<<"+name+">>归还成功!");
}
}else {
System.out.println("您输入的书名不存在,请确认!");
}
break;
case 6:
System.out.println("退出系统");
return;
}
System.out.print("输入0返回:");
num = scanner.nextInt();
while(num != 0) {
System.out.print("输入0返回:");
num = scanner.nextInt();
}
}while(num == 0);
}
}
// Arrays.java
package com.southwind.test;
public class Arrays {
public static int binarySearch(String[] array,String name) {
int index = -1;
for(int i = 0; i < array.length; i++) {
if(name.equals(array[i])) {
index = i;
break;
}
}
return index;
}
}