-
Notifications
You must be signed in to change notification settings - Fork 47
/
ch05_BookFriendRecommendationsJava.java
53 lines (46 loc) · 1.72 KB
/
ch05_BookFriendRecommendationsJava.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ch05_BookFriendRecommendationsJava {
public static List<Movie> bookAdaptations(String author) {
if (author.equals("Tolkien")) {
return Arrays.asList(new Movie("An Unexpected Journey"),
new Movie("The Desolation of Smaug"));
} else return Collections.emptyList();
}
public static List<String> recommendationFeed(List<Book> books) {
List<String> result = new ArrayList<>();
for (Book book : books)
for (String author : book.authors)
for (Movie movie : bookAdaptations(author)) {
result.add(String.format(
"You may like %s, because you liked %s's %s",
movie.title, author, book.title));
}
return result;
}
public static void main(String[] args) {
List<Book> books = Arrays.asList(new Book("FP in Scala", Arrays.asList("Chiusano", "Bjarnason")),
new Book("The Hobbit", Arrays.asList("Tolkien")));
System.out.println(recommendationFeed(books));
assert(recommendationFeed(books).toString().equals(
"[You may like An Unexpected Journey, because you liked Tolkien's The Hobbit, "+
"You may like The Desolation of Smaug, because you liked Tolkien's The Hobbit]"
));
}
}
class Book {
final String title;
final List<String> authors;
Book(String title, List<String> authors) {
this.title = title;
this.authors = authors;
}
}
class Movie {
final String title;
Movie(String title) {
this.title = title;
}
}