book.java
package com.demo3;public class book { private int id; private string name; private double price; public book(){} public book(int id, string name, double price) { this.id = id; this.name = name; this.price = price; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public double getprice() { return price; } public void setprice(double price) { this.price = price; } public void showmess() { system.out.println("book{" + "编号=" + getid() + ", 书名='" + getname() + ", 价格=" + getprice() + '}'); }}
tool.java
package com.demo3;import com.sun.scenario.effect.impl.sw.sse.sseblend_src_outpeer;import java.math.bigdecimal;import java.util.arrays;public class tools { private book[] books = new book[20]; private int size; public void init(){ book book1 = new book(1,"老人与海",29.8); book book2 = new book(2,"狂人日记",36.7); book book3 = new book(3,"玫瑰的葬礼",19.2); book book4 = new book(4,"宇宙源头",19.2); books[0]=book1; books[1]=book2; books[2]=book3; books[3]=book4; size=4; } public book[] getbooks() { return books; } public void setsize(int size) { this.size = size; } /*根据编号查询图书*/ public void fingbookbyid(book[] books,int id){ int i=0; while(i<size){ if(books[i].getid()== id){ system.out.println("检索信息: "); system.out.println("-------------------content----------------------"); books[i].showmess(); system.out.println("------------------------------------------------"); break; } i++; } if(i>=size){ system.out.println("暂无信息"); } } /*根据价格查找图书,返回一个新的数组*/ public book[] findbookebyprice(book[] books,double price){ book[] newbooks=new book[books.length]; int length=0; int i=0; int j=0;/*注意,这里添加一个j*/ while(i<size){ /*提高精度,这里用到了bigdecimal类*/ bigdecimal x1 = new bigdecimal(books[i].getprice()); bigdecimal x2 = new bigdecimal(price); if(x1.floatvalue()==x2.floatvalue()){ /*这里用到j,而不是下标i,如果是下标i的话,则就会出现nullpointerexception异常,因为if程序体中的 i的取值不是连续的*/ newbooks[j++]= books[i]; length++; } i++; } /*这里用到了array集合类中的copyrange静态方法*/ newbooks= arrays.copyofrange(newbooks,0,length); return newbooks; }}
booktest.java
package com.demo3;import java.util.scanner;public class booktest { public static void main(string[] args) { scanner scanner = new scanner(system.in); /*创建工具类,对其进行初始化*/ tools tool = new tools(); tool.init(); prof: while(true) { system.out.println("========图书检索系统======="); system.out.println("1,【编号检索】 2,【价格检索】 3,【退出】"); system.out.print("【输入检索【类型】: "); int num = scanner.nextint(); while (true) { switch (num) { case 1: system.out.println("进入图书【编号】检索:"); system.out.print("输入检索【编号】: "); int checkid = scanner.nextint(); tool.fingbookbyid(tool.getbooks(), checkid);break; case 2: system.out.println("进入图书【价格】检索"); system.out.print("输入检索【价格】:"); double price = scanner.nextdouble(); book[] books = tool.findbookebyprice(tool.getbooks(),price); if(books.length>0){ system.out.println("-------------------content----------------------"); for(book book :books){ book.showmess(); } system.out.println("------------------------------------------------"); };break; case 3: system.out.println("退出成功"); break prof; }; break; } } }}
程序执行如下:
以上就是java如何实现图书检索系统的详细内容。
