1. Create a large class of books
public class Book {private string name;private int num, public book (String name,int num) {this.name=name;this.num=num;} Public String GetName () {return name;} public void SetName (String name) {this.name=name;} public int Getnum () {return num;} public void setnum (int num) {this.num=num;}}
2. Custom Exceptions
Public class Noexistexception extends Exception{public noexistexception (String message) {super (message); Public noexistexception () {}}
3. Main program
Import java.util.*;p Ublic class Test2 {private Scanner input = new Scanner (system.in); Book[] book={new book ("Language", 1), New book ("Mathematics", 2), new book ("English", 3), new book ("Specialized Course", 4)};//public list<book> Listboo ks;//public Test2 () {//This.listbooks = new arraylist<book> (),//}//public void Listbooksadd ( {//Listbooks.addall (Arrays.aslist (book)),//} public void Printbook () {System.out.println ("= = = Welcome to use = "); System.out.println ("===== book information as follows: ====="); SYSTEM.OUT.PRINTLN ("Serial number" + "\ T" + "title"); for (book Bk:book) {System.out.println (Bk.getnum () + "\ T" +bk.getname ());}} public static void Main (string[] args) {Test2 tc=new Test2 (),//tc.listbooksadd (); Tc.printbook (); while (true) {System.ou T.PRINTLN ("Please select Search Method 1-by serial number 2-through the title:"), switch (Tc.back ()) {case 1:try{System.out.println ("Your book is:" +tc.fbid ()); catch (Exception e) {System.out.println (E.getmessage ()); continue;} Case 2:try{system.out.println ("Your book is:" +TC.FBBN ()); catch (Exception e) {System.out.println (e.getmessage ()); continue;} DEFAULT:SYSTEM.OUT.PRINTLN ("Input Error! Please re-enter!!! "); continue;} Break;}} Public String Fbid () throws Noexistexception{system.out.println ("Please enter the number of books:"); int in=input.nextint (); for (book Bk:book {if (In==bk.getnum ()) {return book[in-1].getname ();}} throw new Noexistexception ("re-enter" if the serial number is out of bounds);} Public String FBBN () throws Noexistexception{system.out.println ("Please enter the title of the book:"); String In=input.next (); for (book Bk:book) {if (In.equals (Bk.getname ())) {return bk.getname ();}} throw new Noexistexception ("This book does not exist");} public int back () {int in=input.nextint (); return in;}}
Java Book query System example (simplified refining version and partial corrections)