-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListBooks.java
More file actions
36 lines (32 loc) · 1.39 KB
/
ListBooks.java
File metadata and controls
36 lines (32 loc) · 1.39 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
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Comparator;
public class ListBooks {
public static void listBooks(ArrayList <Books> books, String sortBy){
if (books.isEmpty()) {
System.out.println("\n There are no books added yet.");
} else {
Comparator<Books> comparator;
switch (sortBy) {
case "author":
comparator = Comparator.comparing(Books::getAuthor);
break;
case "title":
comparator = Comparator.comparing(Books::getTitle);
break;
case "year":
comparator = Comparator.comparingInt(Books::getYear);
break;
default:
System.out.println("Invalid sorting option. Books will be sorted by ID.");
comparator = Comparator.comparingInt(Books::getID);
break;
}
books.sort(comparator);
}
System.out.println("\nBook List:\n");
for (Books book : books) {
System.out.println("ID: " + book.getID() + "\n" + "Title: " + book.getTitle() + "\n" + "Author: " + book.getAuthor() + "\n" + "Publish Date: " + book.getYear());
System.out.println("==============================");
}
}
}