Java 在系统信息原型中创建搜索功能
在堆栈溢出方面也有类似的问题,但我仍然找不到解决方案。我正在为图书馆创建一个简单的系统信息原型。Java 在系统信息原型中创建搜索功能,java,search,arraylist,Java,Search,Arraylist,在堆栈溢出方面也有类似的问题,但我仍然找不到解决方案。我正在为图书馆创建一个简单的系统信息原型。 如何使我的程序具有搜索功能?Library类中的方法应该返回与给定标题、出版商或年份匹配的图书列表。此外,该方法还需要返回arraylist。这是我到目前为止所拥有的 public class Book { private String title; private String publisher; private int year; public Book(String title, Str
如何使我的程序具有搜索功能?Library类中的方法应该返回与给定标题、出版商或年份匹配的图书列表。此外,该方法还需要返回arraylist。这是我到目前为止所拥有的
public class Book {
private String title;
private String publisher;
private int year;
public Book(String title, String publisher, int year) {
this.title = title;
this.publisher = publisher;
this.year = year;
}
public String title() {
return this.title;
}
public String publisher() {
return this.publisher;
}
public int year() {
return this.year;
}
public String toString() {
return this.title + ", " + this.publisher + ", " + this.year;
}
}
import java.util.ArrayList;
public class Library {
private ArrayList<Book> list;
public Library() {
this.list = new ArrayList<Book>();
}
public void addBook(Book newBook) {
list.add(newBook);
}
public void printBooks() {
for (Book boo : this.list) {
System.out.println(boo);
}
}
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> found = new ArrayList<Book>
for (Book title : found) {
if (title.contains(title.title())) {
return title;
}
}
return found;
}
public ArrayList<Book> searchByPublisher(String publisher) {
ArrayList<Book> found = new ArrayList<Book>
//similar code to the other ArrayList method
return found;
}
public ArrayList<Book> searchByYear(String year) {
ArrayList<Book> found = new ArrayList<Book>
//similar code to the other ArrayList method
return found;
}
}
public class Main {
public static void main(String[] args) {
Library Library = new Library();
Library.addBook(new Book("Cheese Problems Solved", "Woodhead Publishing", 2007));
Library.addBook(new Book("The Stinky Cheese Man and Other Fairly Stupid Tales", "Penguin Group", 1992));
Library.addBook(new Book("NHL Hockey", "Stanley Kupp", 1952));
Library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));
ArrayList<Book> result = Library.searchByTitle("Cheese");
for (Book Book: result) {
System.out.println(Book);
}
System.out.println("---");
for (Book Book: Library.searchByPublisher("Penguin Group ")) {
System.out.println(Book);
}
System.out.println("---");
for (Book Book: Library.searchByYear(1851)) {
System.out.println(Book);
}
}
}
公共课堂教材{
私有字符串标题;
私有字符串发布器;
私人国际年;
公共图书(字符串标题、字符串出版商、整数年){
this.title=标题;
this.publisher=publisher;
今年=年;
}
公共字符串标题(){
返回此.title;
}
公共字符串发布器(){
返回此.publisher;
}
公共服务年资(){
今年返回;
}
公共字符串toString(){
返回this.title+”、“+this.publisher+”、“+this.year”;
}
}
导入java.util.ArrayList;
公共班级图书馆{
私有数组列表;
公共图书馆(){
this.list=new ArrayList();
}
公共无效addBook(Book newBook){
添加(新书本);
}
公共印刷书籍(){
for(Book boo:this.list){
系统输出打印项次(boo);
}
}
公共ArrayList searchByTitle(字符串标题){
找到ArrayList=新的ArrayList
for(书名:found){
if(title.contains(title.title())){
返回标题;
}
}
发现退货;
}
公共ArrayList searchByPublisher(字符串发布器){
找到ArrayList=新的ArrayList
//与其他ArrayList方法类似的代码
发现退货;
}
公共ArrayList searchByYear(字符串年){
找到ArrayList=新的ArrayList
//与其他ArrayList方法类似的代码
发现退货;
}
}
公共班机{
公共静态void main(字符串[]args){
库=新库();
图书馆。addBook(新书(“解决奶酪问题”,“Woodhead出版社”,2007年);
图书馆。addBook(新书(《臭奶酪人和其他相当愚蠢的故事》,《企鹅集团》,1992年);
图书馆。addBook(新书(“NHL曲棍球”,“斯坦利·库普”,1952年));
图书馆。addBook(新书(“战斧”,“汤姆·A·霍克”,1851年));
ArrayList结果=Library.searchByTitle(“Cheese”);
用于(书本:结果){
系统输出打印(图书);
}
System.out.println(“--”);
(图书:Library.searchByPublisher(“企鹅集团”)){
系统输出打印(图书);
}
System.out.println(“--”);
(图书:图书馆。检索年份(1851)){
系统输出打印(图书);
}
}
}
你走对了路。只需将找到的书本添加到结果列表:
public ArrayList<Book> searchByTitle(String title) {
ArrayList<Book> found = new ArrayList<Book>();
for (Book book : found) {
if (book.title().contains(title)) {
found.add(book);
}
}
return found;
}
public ArrayList searchByTitle(字符串标题){
找到ArrayList=新的ArrayList();
for(书本:已找到){
如果(book.title()包含(title)){
找到。添加(书本);
}
}
发现退货;
}
我还纠正了您在代码和平中的一些错误。您的其他代码似乎充满了其他错误,我将不在这里修复这些错误。您可能想进一步了解Java语法
编辑
正如Wiggles先生所指出的,您可能希望进行不区分大小写的搜索。看看吧。至少我会让这个搜索案例变得不敏感。知道吗,你是对的,我很接近。我检查并纠正了我的其他语法错误。MrWiggles这将是我的下一步。有什么最简单的不区分大小写的方法吗?@AndréStannek我会仔细看看。再次感谢。