1.核心源码
package cn.io;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Book {
private int id;
private String name;
private String author;
private double price;
public Book() {
}
static List<Book> list = new ArrayList<>();
public Book(int id, String name, String author, double price) {
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
public int getId() {
return id;
}
public void steId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 0;
while (true) {
i = list.size() + 1;
System.out.printf("请输入第%d本书籍名称 作者 价格: ", i);
String n = sc.next();
String a = sc.next();
double p = sc.nextDouble();
Book bk = new Book(i, n, a, p);
list.add(bk);
if (list.size() == 10) {
System.out.println("10 书籍输入完成。 进入程序查询");
break;
}
System.out.println();
}
System.out.println();
System.out.println("----------------------------------------");
while(true)
{
System.out.print("请输入书籍名称(退出quit):");
String n = sc.nextLine();
if ("quit".equalsIgnoreCase(n)) break;
for (Book b : list) {
if (b.getName().contains(n)) {
System.out.println(b);
}
}
System.out.println();
}
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}
2.运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b0e1513619120eab4dba56dcbea728ce.png)