写了好久,功能基本实现,也用到了异常处理的知识,不是简单地条件判断,希望大家指教。唯一不足就是当选择了哪种查找方式以后,输入非法,系统提示输入非法后又跳回重新选择查找方式,而不是重新执行刚刚已经选择好的查找方式,望高手指点。
book类
//book类表示图书库,有序号和图书两个字段
public class Book {
private int id;
private String bookname;
public Book(int id,String bookname){
this.id=id;
this.bookname=bookname;
}
public String getBookName(){
return bookname;
}
public void setBookName(String bookname){
this.bookname=bookname;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
}
(自定义)图书不存在或序号不存在异常类
public class noExist extends Exception{
public noExist(){
}
public noExist(String message){
super(message);
}
}
(自定义)命令输入有误异常类
public class wrongCommandException extends Exception{
public wrongCommandException(){
}
public wrongCommandException(String message){
super(message);
}
}
Test主类
import java.util.*;
public class Test {
private static Book[] books={new Book(1,"西游记"),new Book(2,"水浒传"),new Book(3,"三国演义"),new Book(4,"红楼梦")};
private String input=null;//用来存放命令字符串
public static void main(String[] args) {
while(true){//如果没有查找到书或输入错误程序会一直循环
try {
String input=commandForChoose();//选择查找方式输入命令
if(input.equals("1")){
System.out.println("输入图书名称:");
try {
searchBookName();
} catch (noExist e) {
System.out.println(e.getMessage());
}
}
else if(input.equals("2")){
System.out.println("输入图书序号:");
while(true){
try {
int id=commandForSearch();//通过图书序号查找时候检查命令
searchBookId(id);
} catch (noExist e) {
System.out.println(e.getMessage());
}
}
}
} catch (wrongCommandException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 选择查找方式输入命令的方法
* @return 返回选择1或2
* @throws wrongCommandException
*/
private static String commandForChoose() throws wrongCommandException{
System.out.println("输入命令:1-按照名称查找图书;2-按照序号查找图书");
Scanner scan=new Scanner(System.in);
String input=scan.nextLine();
if(!input.equals("1")&&!input.equals("2")){
throw new wrongCommandException("命令输入错误!请根据提示输入数字命令!");
}
return input;
}
/**
* 按照序号查找图书的序号,判断序号是否为数字的方法
* @return 按照序号查找图书的序号
* @throws wrongCommandException
*/
private static int commandForSearch() throws wrongCommandException{
Scanner scan=new Scanner(System.in);
String input=scan.nextLine();
int num;//用来返回
try{
num=Integer.parseInt(input);
return num;
}catch(Exception e){
throw new wrongCommandException("命令输入错误!请根据提示输入数字命令!");
}
}
/**
* 按名称查找图书函数
*
* * @throws noExist
*/
private static void searchBookName() throws noExist{
Scanner scan=new Scanner(System.in);
String input=scan.nextLine();//录入想要查找的图书名称
boolean flag=false;//控制符用来判断是否找到了该图书名称
//遍历查找看是否找到
for(int i=0;i<books.length;i++){
String bookname=books[i].getBookName();
if(input.equals(bookname)){//找到了则为true
System.out.println("book:"+bookname);//输出找到的图书名称
flag=true;
System.exit(0);
}
}
if(!flag){//没找到,抛出异常
throw new noExist("图书不存在!");
}
}
/**
* 按照序号查找图书
* @param id 序号
* @throws noExist
*/
private static void searchBookId(int id) throws noExist{
boolean flag=false;//控制符用来判断是否找到了该图书序号
//遍历查找看是否找到
for(int i=0;i<books.length;i++){
int x=books[i].getId();//x表示图书库里每次找到的id字段值
if(id==x){//找到了则为true
System.out.println("book:"+books[i].getBookName());//输出找到的图书名称
flag=true;
System.exit(0);
}
}
if(!flag){//没找到,抛出异常
throw new noExist("图书不存在!");
}
}
}
點(diǎn)擊查看更多內(nèi)容
18人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫(xiě)下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得
100積分直接送
付費(fèi)專(zhuān)欄免費(fèi)學(xué)
大額優(yōu)惠券免費(fèi)領(lǐng)