-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookAnalyzer.java
More file actions
136 lines (121 loc) · 4.98 KB
/
BookAnalyzer.java
File metadata and controls
136 lines (121 loc) · 4.98 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.*;
public class BookAnalyzer {
private List<Book> books;
public BookAnalyzer(List<Book> books) {
this.books = books;
}
public int getTotalBooksByAuthor(String authorName) {
int count = 0;
for (Book book : books) {
if (book.getAuthor().equalsIgnoreCase(authorName)) {
count++;
}
}
return count;
}
public Set<String> getAllAuthors() {
Set<String> authors = new HashSet<>();
for (Book book : books) {
authors.add(book.getAuthor());
}
return authors;
}
public void printAllAuthors() {
Set<String> authors = getAllAuthors();
System.out.println("All authors in the dataset:");
System.out.println("---------------------------");
for (String author : authors) {
System.out.println(author);
}
System.out.println("Total number of unique authors: " + authors.size());
}
public List<String> getBooksByAuthor(String authorName) {
List<String> bookTitles = new ArrayList<>();
for (Book book : books) {
if (book.getAuthor().equalsIgnoreCase(authorName)) {
bookTitles.add(book.getTitle());
}
}
return bookTitles;
}
public void printBooksByAuthor(String authorName) {
List<String> bookTitles = getBooksByAuthor(authorName);
System.out.println("Books by " + authorName + ":");
System.out.println("---------------------------");
if (bookTitles.isEmpty()) {
System.out.println("No books found for author: " + authorName);
} else {
for (String title : bookTitles) {
System.out.println("- " + title);
}
}
System.out.println("Total books: " + bookTitles.size());
}
public List<Book> getBooksByRating(double rating) {
List<Book> booksWithRating = new ArrayList<>();
for (Book book : books) {
if (book.getUserRating() == rating) {
booksWithRating.add(book);
}
}
return booksWithRating;
}
public void printBooksByRating(double rating) {
List<Book> booksWithRating = getBooksByRating(rating);
System.out.println("Books with rating " + rating + ":");
System.out.println("---------------------------");
if (booksWithRating.isEmpty()) {
System.out.println("No books found with rating: " + rating);
} else {
for (Book book : booksWithRating) {
System.out.println("- " + book.getTitle() + " by " + book.getAuthor());
}
}
System.out.println("Total books: " + booksWithRating.size());
}
public Map<String, Double> getBooksAndPricesByAuthor(String authorName) {
Map<String, Double> bookPrices = new LinkedHashMap<>();
for (Book book : books) {
if (book.getAuthor().equalsIgnoreCase(authorName)) {
bookPrices.put(book.getTitle(), book.getPrice());
}
}
return bookPrices;
}
public void printBooksAndPricesByAuthor(String authorName) {
Map<String, Double> bookPrices = getBooksAndPricesByAuthor(authorName);
System.out.println("Books and prices by " + authorName + ":");
System.out.println("------------------------------------------");
if (bookPrices.isEmpty()) {
System.out.println("No books found for author: " + authorName);
} else {
for (Map.Entry<String, Double> entry : bookPrices.entrySet()) {
System.out.println("- " + entry.getKey() + " : $" + entry.getValue());
}
}
System.out.println("Total books: " + bookPrices.size());
}
public void printDatasetSummary() {
System.out.println("Dataset Summary:");
System.out.println("================");
System.out.println("Total books: " + books.size());
System.out.println("Total unique authors: " + getAllAuthors().size());
int fictionCount = 0;
int nonFictionCount = 0;
for (Book book : books) {
if (book.getGenre().equalsIgnoreCase("Fiction")) {
fictionCount++;
} else {
nonFictionCount++;
}
}
System.out.println("Fiction books: " + fictionCount);
System.out.println("Non-fiction books: " + nonFictionCount);
double minRating = books.stream().mapToDouble(Book::getUserRating).min().orElse(0);
double maxRating = books.stream().mapToDouble(Book::getUserRating).max().orElse(0);
System.out.println("Rating range: " + minRating + " - " + maxRating);
double minPrice = books.stream().mapToDouble(Book::getPrice).min().orElse(0);
double maxPrice = books.stream().mapToDouble(Book::getPrice).max().orElse(0);
System.out.println("Price range: $" + minPrice + " - $" + maxPrice);
}
}