-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchBooks.java
More file actions
28 lines (25 loc) · 1.19 KB
/
SearchBooks.java
File metadata and controls
28 lines (25 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.ArrayList;
import java.util.Scanner;
public class SearchBooks {
public static void searchBooks(ArrayList<Books> books){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Enter the book information (title, author or year): ");
String identifier = sc.nextLine().toLowerCase();
ArrayList<Books> bookMatches = new ArrayList<Books>();
for(Books book : books){
if (book.getTitle().toLowerCase().contains(identifier) ||
book.getAuthor().toLowerCase().contains(identifier) ||
String.valueOf(book.getYear()).contains(identifier)) {
bookMatches.add(book);}}
if(bookMatches.isEmpty()){
System.out.println("No matching books.");
} else {
System.out.println("\nMatched books found: \n");
for (Books book : bookMatches){
System.out.println("ID: " + book.getID() + "\nTitle: '" + book.getTitle() + "'\nAuthor: " + book.getAuthor() + "\nYear: " + book.getYear() +"\n");
break;}}
break;
}
}
}