孙先森
2015-05-16 00:38
package com.immc.exception; /** * 图书信息储存 * 书名 bookName * 图书序号 bookNumber * @author Administrator * */ public class Book { private String bookName; private int bookNumber; public Book() { super(); // TODO Auto-generated constructor stub } public Book(String bookName, int bookNumber) { super(); this.bookName = bookName; this.bookNumber = bookNumber; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public int getBookNumber() { return bookNumber; } public void setBookNumber(int bookNumber) { this.bookNumber = bookNumber; } } package com.immc.exception; /* * 自定义异常: * 图书不存在异常 */ public class bookNotExistException extends Exception{ public bookNotExistException() { super(); System.out.println("图书不存在!"); // TODO Auto-generated constructor stub } } package com.immc.exception; /* * 自定义异常: * 命令序号不在范围内(只能为1或2) */ public class demandErrorException extends Exception{ public demandErrorException() { super(); System.out.println("命令序号不在范围内!"); // TODO Auto-generated constructor stub } } package com.immc.exception; import java.util.Scanner; public class userInput { public int inputDemand(){ System.out.println("输入命令:1-按名称查找图书;2-按序号查找图书"); Scanner sc = new Scanner(System.in); return sc.nextInt(); } public String inputBookName(){ System.out.println("请输入图书名称:"); Scanner sc = new Scanner(System.in); return sc.nextLine(); } public int inputBookNumber(){ System.out.println("请输入图书序号:"); Scanner sc = new Scanner(System.in); return sc.nextInt(); } } package com.immc.exception; import java.util.InputMismatchException; public class ExceptionTest { userInput us = new userInput(); public void searchBook(){ while(true){ int demand = 0; try{ demand = us.inputDemand(); demandError(demand); }catch(InputMismatchException e){ System.out.println("命令输入错误!请根据提示输入数字命令!"); continue; }catch(demandErrorException e){ continue; } switch(demand){ case 1: String str = us.inputBookName(); try{ showBook(searchByName(str)); break; }catch(bookNotExistException e){ continue; } case 2: int num = us.inputBookNumber(); try{ showBook(searchByNumber(num)); break; }catch(bookNotExistException e){ continue; } } } } private Book searchByName(String name) throws bookNotExistException{ //for (循环变量类型 循环变量名称 : 要被遍历的对象) 循环体 for(Book book : BookIinitial.book){ if(book.getBookName().equals(name)){ return book; } } throw new bookNotExistException(); } private Book searchByNumber(int number) throws bookNotExistException{ if(number>BookIinitial.book.length || number<1){ throw new bookNotExistException(); }else{ for(Book book : BookIinitial.book){ if(book.getBookNumber() == number){ return book; } } return null; } } private int demandError(int demand) throws demandErrorException{ if(demand == 1||demand == 2){ return demand; }else{ throw new demandErrorException(); } } private void showBook(Book book){ System.out.println("图书序号:"+book.getBookNumber()+ " 书名:"+book.getBookName()); } } package com.immc.exception; public class BookIinitial { static Book[] book = {new Book("高等数学",1),new Book("数据结构",2), new Book("中国语文",3),new Book("大学英语",4)}; } package com.immc.exception; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ExceptionTest ex = new ExceptionTest(); ex.searchBook(); } }
还没有人回答问题,可以看看其他问题
Java入门第三季
409792 学习 · 4340 问题
相似问题