package borrow_books;
import java.util.Scanner;
public class InitialBorrow{
static int num;
static String input;
public static void main(String[] args) throws ErrorCommandException{
System.out.println("欢迎进入借书系统!\n书目\n1-红楼梦\n2-西游记\n3-三国演义\n4-水浒传\n请输入序号或书名选择借阅!");
Scanner sc=new Scanner(System.in);
input=sc.nextLine();//input获取输入的内容
try {
InitialBorrow IB =new InitialBorrow();
IB.inStr();
} catch (Exception e) {
e.printStackTrace();
throw new ErrorCommandException();
}finally {
sc.close();
System.out.println("程序运行完毕!");
System.exit(0);
}
}
public void inNum () throws NumFailedException{
try {
num = Integer.parseInt(input);
if (num>=1 & num<=4) {
System.out.println(num+"-"+Book.getBookName(num)+"借阅成功!^-^");
}
else {
throw new BookNotExistException();
}
}catch(Exception e) {
throw new NumFailedException();
}
}
public void inStr() throws BookNotExistException{
try {
inNum();
} catch (NumFailedException e) {//使用序号判定失败后,使用NumFailedException作为信号
if (input.equals("红楼梦")||input.equals("西游记")||input.equals("三国演义")||input.equals("水浒传")) {
System.out.println(Book.getBookNum(input)+"-"+input+"借阅成功!^-^");
}else {
throw new BookNotExistException();
}
}
}
}
public class Book {
public static int getBookNum(String newinput) {
switch (newinput) {
case "红楼梦":
return 1;
case "西游记":
return 2;
case "三国演义":
return 3;
case "水浒传":
return 4;
default:
return 0;
}
}
public static String getBookName(int newinput) {
switch (newinput) {
case 1:return"红楼梦";
case 2:return"西游记";
case 3:return"三国演义";
case 4:return"水浒传";
default:return"";
}
}
}
public class ErrorCommandException extends Exception{
public ErrorCommandException() {
super("命令错误!");
}
}
public class BookNotExistException extends Exception{
public BookNotExistException() {
super("图书不存在!");
}
}
public class NumFailedException extends Exception {}