import java.util.Scanner;
public class BookGeneral2 {
public static void main(String[] args) {
String[] ShuMingArray = {"数学建模","工程数学","Java入门","C语言","数据库","网页设计"};
String[] XuHaoArray = {"001","002","003","004","005","006"};
Scanner sc = new Scanner(System.in);
boolean start = true;
while(start){
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
try{
String number1 = sc.next(); //1或2
switch(number1){
case "1" : BookName(ShuMingArray,XuHaoArray);start=false;break;
case "2" : Number(ShuMingArray,XuHaoArray);start=false;break;
default : throw new SelectException("输入命令错误!请根据提示输入数字命令!");
}
}catch(SelectException e){
System.out.println(e.getMessage());
continue;
}
}
}
/*
* 自定义异常:
*/
//书名异常
public static void BookName(String ShuMingArray[],String XuHaoArray[]){
Scanner ac = new Scanner(System.in);
String Buff = "Blue";
boolean start = true;
while(start){
System.out.println("输入图书名称:");
try{
String shuming = ac.next(); //书名
for(int i=0;i<ShuMingArray.length;i++){
if(ShuMingArray[i].equals(shuming)){
System.out.println("图书信息:《"+ShuMingArray[i]+"》,作者:XXX"+",序列号:"+XuHaoArray[i]+" -查询结束");
Buff = "Red";
start=false; //结束查询
}
}
if(Buff.equals("Blue")){
throw new BookNameException("没有找到该图书!#1");
}
}catch(BookNameException e){
System.out.println(e.getMessage());
continue;
}
}
}
//序号异常
public static void Number(String ShuMingArray[],String XuHaoArray[]){
Scanner bc = new Scanner(System.in);
String Buff = "Blue";
boolean start = true;
while(start){
System.out.println("输入图书序号:");
try{
String xuhao = bc.next(); //序号
for(int j=0;j<XuHaoArray.length;j++){
if(XuHaoArray[j].equals(xuhao)){
System.out.println("图书信息:《"+ShuMingArray[j]+"》,作者:XXX"+",序列号:"+XuHaoArray[j]+" -查询结束");
Buff = "Red";
start=false; //结束查询
}
}
if(Buff.equals("Blue")){
throw new NumberException("没有找到该图书!#2");
}
}catch(NumberException e){
System.out.println(e.getMessage());
continue;
}
}
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦