Java Book query System example (simplified refining version and partial corrections)

Source: Internet
Author: User
Tags getmessage

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)

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.