The borrowing system of Java learning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.