The realization of the book lending system to process the user input illegal parameters, and guide users to use the correct
Test results:
The main purpose is to practice the use of exception classes in exception handling
Related syntax for using
1 Try {2// code block 3} that could produce an exception Catch (Exception e) {4// code block for handling exceptions 5 } finally {// can select 6// Final Execution code block 7 } as required
Implementation process:
Creating the Borrowbook Class
Import java.util.InputMismatchException; Import Java.util.Scanner; Public class Borrowbook { new Scanner (system.in);}
Define three kinds of exception classes: The number entered does not correspond to the book's exception, the title of the input does not correspond to the book's exception, other normal anomalies
Private Static classCommanexceptionextendsexception{Privatecommanexception () {}}Private Static classBooknumexceptionextendsexception{Privatebooknumexception () {}}Private Static classBooknameexceptionextendsexception{Privatebooknameexception () {}}
Create throw exception method, and throw three kinds of exceptions
Private voidCommanexc ()throwscommanexception{Throw Newcommanexception ();}Private voidBooknumexc ()throwsbooknumexception{Throw Newbooknumexception ();}Private voidBooknameexc ()throwsbooknameexception{Throw Newbooknameexception ();}
How to create a box for storing books (for example, two examples)
Private Static string[] Bookbox () { new string[] {"High number", "Linear Algebra"}; return Bookbox;}
Create a library entry method
Private Static voidBorrowbook ()throwsexception{System.out.println ("Input command: 1-Find books by name; 2-Find books by serial number"); Try { intInputnum =NewBorrowbook (). Scn.nextint (); Try { if(Inputnum = = 0) {borrowbkbyname (Bookbox ()); }Else if(Inputnum = = 1) {Borrowbkbynum (Bookbox ()); }Else { NewBorrowbook (). Commanexc (); } }Catch(Exception e) {System.out.println ("Command input error, please enter the number command as prompted!" "); Borrowbook (); } }Catch(inputmismatchexception eInput) {System.out.println ("Command input error, please enter the number command as prompted!" "); Borrowbook (); }}
Create a method of borrowing books by book titles
Private Static voidBorrowbkbyname (string[] books)throwsexception{System.out.println ("Enter book name:"); String BookName=NewBorrowbook (). Scn.next (); Try { Switch(bookname) { Case"High number": System.out.println ("Book:" +bookname); Break; Case"Linear algebra": System.out.println ("Book:" +bookname); Break; default: NewBorrowbook (). Booknameexc (); } }Catch(Exception e) {System.out.println ("The book does not exist!" "); Borrowbkbyname (books); } }
Create a method of borrowing books by book serial number
Private Static voidBorrowbkbynum (string[] books)throwsexception{System.out.println ("Enter book Number:"); Try { intBooknum =NewBorrowbook (). Scn.nextint (); Try { Switch(booknum) { Case1: System.out.println ("Book:" +Books[booknum]); Break; Case2: System.out.println ("Book:" +Books[booknum]); Break; default: NewBorrowbook (). Booknumexc (); } }Catch(Exception e) {System.out.println ("The book does not exist!" "); Borrowbkbynum (books); } }Catch(inputmismatchexception e) {System.out.println ("Command input error, please enter the number command as prompted!" "); Borrowbkbynum (books); }}
The Main method for initialization
Public Static void throws exception{ Borrowbook (); New Borrowbook (). Scn.close ();}
The borrowing system of Java learning