作業(yè)—模擬借書(shū)系統(tǒng)
package com.imooc;
import java.util.Scanner;
public class BookLending {
// 定義圖書(shū)數(shù)組
static Books[] booksToInquery = { new Books(1, "Java開(kāi)發(fā)實(shí)戰(zhàn)修煉"),
new Books(2, "JavaWeb開(kāi)發(fā)實(shí)戰(zhàn)經(jīng)典"), new Books(3, "瘋狂android講義"),
new Books(4, "android權(quán)威指南"), new Books(5, "HeadFirst設(shè)計(jì)模式") };
static Scanner sc = new Scanner(System.in);
// 主查詢方法
public static void inquery() throws Exception {
System.out.println("歡迎登錄模擬借書(shū)系統(tǒng)!"); // 進(jìn)入系統(tǒng)
System.out.println("輸入命令:1-按照名稱查找圖書(shū);2-按照序號(hào)查找圖書(shū)"); // 選擇查詢類型
try {
int input = inputCommand(); // 將控制臺(tái)輸入的數(shù)字、字符、字符串轉(zhuǎn)化1,2,-1
switch (input) {
case 1: // 輸入1,則按照書(shū)名查詢
System.out.println("輸入圖書(shū)名稱:");
String bookName = sc.next();
checkName(bookName);
break;
case 2: // 輸入2,則按照編號(hào)查詢
checkSerial();
break;
case -1: // 輸入字符或字符串,則提示錯(cuò)誤,重新輸入
System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
inquery();
default: // 輸入其他類型,提示錯(cuò)誤
System.out.println("命令輸入錯(cuò)誤!");
inquery();
}
} catch (Exception e) {
// 捕獲”圖書(shū)不存在異?!皶r(shí),要求重新輸入命令
System.out.println(e.getMessage());
inquery();
}
}
// 從控制臺(tái)輸入命令,用于選擇查詢類型以及選擇查詢編號(hào)
public static int inputCommand() {
try {
int input = sc.nextInt();
return input;
} catch (Exception e) {
// 若輸入字符型或者字符串,則拋出異常,捕獲該異常,拋出"錯(cuò)誤命令異常"
sc = new Scanner(System.in);
return -1;
}
}
// 按照書(shū)名查找圖書(shū)
public static void checkName(String st) throws Exception {
for (int i = 0; i < booksToInquery.length; i++) {
// 如輸入書(shū)名正確,則返回相應(yīng)位置圖書(shū)
if (st.equals(booksToInquery[i].getName())) {
System.out.println("序號(hào): " + booksToInquery[i].serial + "\t"
+ "書(shū)名: " + booksToInquery[i].name);
System.exit(0);
}
}
throw new Exception("圖書(shū)不存在哦!");
}
// 按照編號(hào)查找圖書(shū)
public static void checkSerial() throws Exception {
System.out.println("輸入圖書(shū)序號(hào):");
try {
int bookSerial = inputCommand(); // 將輸入的序號(hào)做一個(gè)轉(zhuǎn)化,輸入數(shù)字則返回?cái)?shù)字,輸入字符和字符串則返回-1
// 如果輸入字符及字符型,則提示錯(cuò)誤,重新輸入
if (bookSerial == -1) {
System.out.println("命令輸入錯(cuò)誤!請(qǐng)根據(jù)提示輸入數(shù)字命令!");
checkSerial();
}
// 如輸入序號(hào)正確,則返回相應(yīng)位置圖書(shū)
for (int i = 0; i < booksToInquery.length; i++) {
if (bookSerial == booksToInquery[i].serial) {
System.out.println("序號(hào): " + booksToInquery[i].serial + "\t"
+ "書(shū)名: " + booksToInquery[i].name);
System.exit(0);
}
}
} catch (ArrayIndexOutOfBoundsException e) {
// 輸入的序號(hào)不存在(引發(fā)"數(shù)組下標(biāo)越界異常"),則拋出"圖書(shū)不存在異常"
Exception bookNotExists = new Exception("圖書(shū)不存在哦!");
bookNotExists.initCause(e);
throw bookNotExists;
}
}
public static void main(String[] args) throws Exception {
inquery();
}
}
package com.imooc;
public class Books {
public String name;
public int serial;
public Books(int serial, String name) {
this.name = name;
this.serial = serial;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSerial() {
return serial;
}
public void setSerial(int serial) {
this.serial = serial;
}
}
2015-12-15
!!!!