public class BorrowBook {
public static String[] books = {"第一行代码", "thinking in java", "安卓探索艺术"};
public static Scanner sc;
public static boolean flag = true;
public static boolean isIndexOrName = false;
public static void main(String[] args) {
System.out.println("***欢迎来到模拟图书系统***");
System.out.println("请输入您要查找图书的方式:1.按图书序号查找" + "\t" + "2.按图书名称查找");
System.out.println("请输入一个有效查找方式:");
find2IndexOrName();
}
private static void find2IndexOrName() {
sc = new Scanner(System.in);
int input = sc.nextInt();
try {
if (1 == input) {
System.out.println("请输入有效图书序号:");
int index = sc.nextInt();
find2Index(index);
} else if (2 == input) {
System.out.println("请输入正确的书名:");
String name = sc.next();
find2Name(name);
} else {
throw new Exception();
}
} catch (Exception e) {
System.out.println("请输入正确指令:");
find2IndexOrName();
}
}
public static void find2Name(String name) {
isIndexOrName = true;
try {
for (int i = 0; i < books.length; i++) {
if (name.equals(books[i])) {
showBook(name, i);
break;
} else {
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("图书不存在,请重新输入:");
String s = sc.next();
find2Name(s);
}
}
public static void find2Index(int index) {
isIndexOrName = false;
try {
for (int i = 0; i < books.length; i++) {
if (index == i) {
showBook(index);
break;
} else if (index > 2) {
throw new Exception();
}
}
} catch (Exception e) {
System.out.println("图书不存在,请重新输入:");
int i = sc.nextInt();
find2Index(i);
}
}
/**
* 通过书名查找图书
*
* @param name 书名
* @param index 书名对应的下标
*/
private static void showBook(String name, int index) {
System.out.println("***查找到以下内容***");
if (books[index].equals(name)) {
System.out.println("第一行代码");
} else if (books[index].equals(name)) {
System.out.println("thinking in java");
} else if (books[index].equals(name)) {
System.out.println("安卓探索艺术");
}
System.out.println("***确定借阅吗?(Y/N)***");
String s = sc.next();
isBorrow(s);
}
/**
* 通过图书序号查找
*
* @param index 图书的序号
*/
private static void showBook(int index) {
System.out.println("***查找到以下内容***");
System.out.println(books[index]);
System.out.println("确认借阅本书吗?(Y/N)");
String s = sc.next();
isBorrow(s);
}
private static void isBorrow(String s) {
if (s.equals("Y") || s.equals("y")) {
System.out.println("恭喜您借阅成功");
} else if (s.equals("N") || s.equals("n")) {
System.out.println("您以取消操作!请重新操作!");
if (flag) {
System.out.println("请输入一个有效查找方式:"+"\n"+"1.按序号查找"+"\n"+"2.按书名查找");
find2IndexOrName();
}
}
}
}