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("歡迎進(jìn)入借書(shū)系統(tǒng)!\n書(shū)目\n1-紅樓夢(mèng)\n2-西游記\n3-三國(guó)演義\n4-水滸傳\n請(qǐng)輸入序號(hào)或書(shū)名選擇借閱!");
????Scanner?sc=new?Scanner(System.in);
????input=sc.nextLine();//input獲取輸入的內(nèi)容
????try?{
????????InitialBorrow?IB?=new?InitialBorrow();
????????IB.inStr();
????}?catch?(Exception?e)?{
????????e.printStackTrace();
????????throw?new?ErrorCommandException();
????}finally?{
????????sc.close();
????????System.out.println("程序運(yùn)行完畢!");
????????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)?{//使用序號(hào)判定失敗后,使用NumFailedException作為信號(hào)
????????if?(input.equals("紅樓夢(mèng)")||input.equals("西游記")||input.equals("三國(guó)演義")||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?"紅樓夢(mèng)":
????????????return?1;
????????case?"西游記":
????????????return?2;
????????case?"三國(guó)演義":
????????????return?3;
????????case?"水滸傳":
????????????return?4;
????????default:
????????????return?0;
????????}
????????????
????}
????public?static?String?getBookName(int?newinput)?{
????????
????????switch?(newinput)?{
????????case?1:return"紅樓夢(mèng)";
????????case?2:return"西游記";
????????case?3:return"三國(guó)演義";
????????case?4:return"水滸傳";
????????default:return"";
????????}
????}
????????
????}
??
public?class?ErrorCommandException?extends?Exception{
????public?ErrorCommandException()?{
????????super("命令錯(cuò)誤!");
????}
}
public?class?BookNotExistException?extends?Exception{
????public?BookNotExistException()?{
????????super("圖書(shū)不存在!");
????}
}
public?class?NumFailedException?extends?Exception?{}
2021-01-13
???
2020-10-26
????