作业做完了,继续巩固了自定义异常知识点,但是没有用到异常链。本系统的执行过程是一旦用户输入有任何错误就一直提示再输,输入正确则返回书名,并终止运行
package com.lib;
//define a book class, which contain two attributes:name and id
public class Book {
private String name;
private int id;
public Book(String name,int id){ //constructor
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
public int getID(){
return id;
}
}
package com.lib;
//Define an exception in case the customer input an invalid command
public class InvalidCommandException extends Exception {
public String toString(){
return "Incorrect command, please input based on the prompt number: \n";
}
}
package com.lib;
//Define a exception in case the customer input invalid book id or book name
public class NoExistingException extends Exception {
public String toString(){
return "the book isn't existing, please try again: \n";
}
}
package com.lib;
import java.util.Scanner;
public class LibraryDemo {
public static void main(String[] args){
Book[] book = {new Book("高数",1),new Book("数据结构",2)}; //Initialization
System.out.println("Command:1. search book by name; 2. search book by ID");
int search=0;
boolean searchFlag=false;
//try-catch block to handle 2 kind of input exception
//the exception may be an integer input out of boarder, or non-integer
//do-while loop keep asking an input if the previous one is invalid
do{
try{
Scanner input = new Scanner(System.in);
search = input.nextInt();
checkCommand(search); //
searchFlag=true;
}catch (InvalidCommandException e){ //exception out of boarder
System.out.println(e);
}catch (Exception e){ //exception with non-integer
System.out.println("Incorrect command, please input based on the prompt number: ");
}
}while (searchFlag==false);
LibraryDemo lib = new LibraryDemo();
if (search==1){
lib.searchBookFromName(book); //search by name
}else if(search==2){
lib.searchBookFromID(book); //search by id
}
System.out.println("Thanks for using our system!");
}
//search book by name, and handle he possible exception
public void searchBookFromName(Book[] book){
boolean searchFlag=false;
String bookname=null;
LibraryDemo lib = new LibraryDemo();
System.out.println("Please input the book name:");
//do-while loop keeps asking an input if customer give an invalid input
//try-block handle the non-existing exception
do{
try{
Scanner input = new Scanner(System.in);
bookname=input.next();
searchFlag=lib.findBookFromName(book, bookname);//check if the given book is existing
System.out.println("book: "+bookname);
}catch (NoExistingException e){ //non-existing exception
System.out.println(e);
}
}while (searchFlag==false);
}
//search book by ID
//do-while loop keeps asking an input if customer give an invalid input
//try-block handle two kinds of exception, which may be an non-integer input, or out of boarder input
public void searchBookFromID(Book[] book){
int id=0;
String bookname=null;
boolean searchFlag=false;
LibraryDemo lib=new LibraryDemo();
System.out.println("Please input the book ID: ");
do{
try{
Scanner input = new Scanner(System.in);
id = input.nextInt();
bookname = lib.findBookFromID(book, id);
searchFlag = true;
System.out.println("book: "+bookname);
}catch (NoExistingException e){
System.out.println(e);
}catch (Exception e){
System.out.println("Invalid input, try again(the ID should be an integer): ");
}
}while (searchFlag==false);
}
//throw an exception when a invalid command was given
private static void checkCommand(int search) throws InvalidCommandException{
if ((search>2)||(search<1)){
throw new InvalidCommandException();
}
}
//based on given ID, check if the book is existing. if no, throw an exception
public String findBookFromID(Book[] book,int id)throws NoExistingException{
//boolean flag = true;
for (int i=0;i<book.length;i++)
{
if (id==book[i].getID()){
return book[i].getName();
}
}
throw new NoExistingException();
}
//based on given bookname, check if the book is existing. if no, throw an exception
public boolean findBookFromName(Book[] book, String name)throws NoExistingException{
String bookName;
for(int i=0;i<book.length;i++){
bookName=book[i].getName();
if (name.equals(bookName)){
return true;
}
}
throw new NoExistingException();
}
}