Java设计模式之外观模式示例详解
342
2022-07-28
java模拟实现图书检索系统 (基础版),供大家参考,具体内容如下
练习实现3个简单的功能,没有优化,可以根据需求,自行添加想要实现的功能。
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;http://
while(i 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 /*提高精度,这里用到了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]; lSgaDJCQvength++; } 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; } } } } 程序执行如下:
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 /*提高精度,这里用到了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]; lSgaDJCQvength++; } 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; } } } } 程序执行如下:
/*提高精度,这里用到了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];
lSgaDJCQvength++;
}
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;
}
}
}
}
程序执行如下:
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~