-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.java
312 lines (275 loc) · 11.4 KB
/
Report.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Report {
public static void report(Scanner scanner) throws ClassNotFoundException, SQLException {
String input = "";
ArrayList<InputKey> options = new ArrayList<InputKey>();
options.add(new InputKey(1, "1", "the total number of bookings in a specific date range by city"));
options.add(new InputKey(2, "2", "the total number of bookings in a specific date range by city and zip code"));
options.add(new InputKey(3, "3", "the total number of listings per country"));
options.add(new InputKey(4, "4", "the total number of listings per country and city"));
options.add(new InputKey(5, "5", "the total number of listings per country, city, and postal code"));
options
.add(new InputKey(6, "6", "the rank of hosts by the total number of listings they have overall per country"));
options.add(new InputKey(7, "7",
"the rank of hosts by the total number of listings they have overall per country and city"));
options.add(new InputKey(8, "8", "the hosts that have more than 10% of listings in a country and city"));
options.add(new InputKey(9, "9", "the rank of renters by the number of bookings in a given time period"));
options
.add(new InputKey(10, "10", "the rank of renters by the number of bookings in a given time period per city"));
options.add(new InputKey(11, "11", "the hosts and renters with the largest number of cancellations within a year"));
options.add(new InputKey(12, "12", "the word frequency of a listing review for all listings"));
while (!input.equals("q")) {
int selected = -1;
System.out.println("Choose a report, [q] to quit: ");
for (InputKey option : options) {
System.out.println("[" + option.getKey() + "] for " + option.getDescription());
}
input = scanner.nextLine();
for (InputKey option : options) {
if (input.equalsIgnoreCase(option.getKey() + ""))
selected = option.getId();
}
System.out.println();
switch (selected) {
case -1:
break;
case 1:
reportDateRangeByCity(scanner);
break;
case 2:
reportDateRangeByCityAndPostal(scanner);
break;
case 3:
reportTotalListingByCountry();
break;
case 4:
reportTotalListingByCountryCity();
break;
case 5:
reportTotalListingByCCP();
break;
case 6:
reportRankHostsByListingCountry();
break;
case 7:
reportRankHostByListingCountryCity();
break;
case 8:
reportMorethan10percent();
break;
case 9:
reportRankRenterByBookings(scanner);
break;
case 10:
reportRankRenterByBookingInDateRangeAndCity(scanner);
break;
case 11:
reportLargestCancellationsInYear(scanner);
break;
case 12:
reportWordFrequency();
break;
default:
System.out.println();
System.out.println("Invalid operation");
}
System.out.println();
}
}
public static void reportWordFrequency() throws ClassNotFoundException, SQLException {
ResultSet rs = SqlDAO.getInstance().getAllListingReview();
while (rs.next()) {
List<String> original = Arrays.asList(rs.getString("comment").split("\\s+"));
Set<String> temp = new HashSet<>(original);
List<String> words = new ArrayList<>(temp);
if (words == null || words.isEmpty()) {
continue;
}
Collections.sort(words, new Comparator<String>() {
@Override
public int compare(String w1, String w2) {
int f1 = Collections.frequency(original, w1);
int f2 = Collections.frequency(original, w2);
if (f1 > f2)
return -1;
if (f1 == f2)
return 0;
return 1;
}
});
System.out.println("For listing with id: " + rs.getInt("listing_id"));
System.out.println("Word: [frequency]");
for (String word : words) {
System.out.println(word + ": " + Collections.frequency(original, word));
}
System.out.println();
}
}
public static void reportLargestCancellationsInYear(Scanner scanner) throws ClassNotFoundException, SQLException {
System.out.print("Enter the year (YYYY): ");
String year = scanner.next();
scanner.nextLine();
try {
LocalDate.parse(year + "-01-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (DateTimeParseException e) {
System.out.println("Wrong date format! Correct format is YYYY!");
return;
}
ResultSet rs = SqlDAO.getInstance().rankHostByCancellations(year);
int temp = 0;
int highest = 0;
System.out.println("Hosts: ");
while (rs.next()) {
temp = rs.getInt("count");
if (temp >= highest) {
highest = temp;
System.out.println(rs.getString("canceller_sin") + ", " + temp);
} else {
break;
}
}
rs = SqlDAO.getInstance().rankRenterByCancellations(year);
temp = 0;
highest = 0;
System.out.println("\nRenters: ");
while (rs.next()) {
temp = rs.getInt("count");
if (temp >= highest) {
highest = temp;
System.out.println(rs.getString("canceller_sin") + ", " + temp);
} else {
break;
}
}
}
public static void reportRankRenterByBookingInDateRangeAndCity(Scanner scanner)
throws ClassNotFoundException, SQLException {
System.out.print("Enter the starting date of the range (YYYY-MM-DD): ");
String start = scanner.next();
System.out.print("Enter the end date of the range (YYYY-MM-DD): ");
String end = scanner.next();
scanner.nextLine();
try {
LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (DateTimeParseException e) {
System.out.println("Wrong date format! Correct format is YYYY-MM-DD!");
}
ResultSet rs = SqlDAO.getInstance().rankRenterByBookingInDateRangeAndCity(start, end, start.substring(0, 4));
while (rs.next()) {
System.out.println(rs.getString("sin") + ", " + rs.getString("city") + ", " + rs.getInt("count"));
}
}
public static void reportRankRenterByBookings(Scanner scanner) throws ClassNotFoundException, SQLException {
System.out.print("Enter the starting date of the range (YYYY-MM-DD): ");
String start = scanner.next();
System.out.print("Enter the end date of the range (YYYY-MM-DD): ");
String end = scanner.next();
scanner.nextLine();
try {
LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (DateTimeParseException e) {
System.out.println("Wrong date format! Correct format is YYYY-MM-DD!");
}
ResultSet rs = SqlDAO.getInstance().rankRenterByBookingInDateRange(start, end);
while (rs.next()) {
System.out.println(rs.getString("sin") + ", " + rs.getInt("count"));
}
}
public static void reportDateRangeByCityAndPostal(Scanner scanner) throws ClassNotFoundException, SQLException {
System.out.print("Enter the starting date of the range (YYYY-MM-DD): ");
String start = scanner.next();
System.out.print("Enter the end date of the range (YYYY-MM-DD): ");
String end = scanner.next();
scanner.nextLine();
System.out.print("Enter city: ");
String city = scanner.nextLine();
try {
LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (DateTimeParseException e) {
System.out.println("Wrong date format! Correct format is YYYY-MM-DD!");
}
ResultSet rs = SqlDAO.getInstance().countBookingInDateRangeByPostal(start, end, city);
System.out.println("Postal code: [total bookings]");
while (rs.next()) {
System.out.println(rs.getString("postal_code") + ": " + rs.getInt("count"));
}
}
public static void reportDateRangeByCity(Scanner scanner) throws ClassNotFoundException, SQLException {
System.out.print("Enter the starting date of the range (YYYY-MM-DD): ");
String start = scanner.next();
System.out.print("Enter the end date of the range (YYYY-MM-DD): ");
String end = scanner.next();
scanner.nextLine();
try {
LocalDate.parse(start, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
LocalDate.parse(end, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
} catch (DateTimeParseException e) {
System.out.println("Wrong date format! Correct format is YYYY-MM-DD!");
}
ResultSet rs = SqlDAO.getInstance().countBookingInDateRangeByCity(start, end);
System.out.println("City: [total bookings]");
while (rs.next()) {
System.out.println(rs.getString("city") + ": " + rs.getInt("count"));
}
}
public static void reportMorethan10percent() throws ClassNotFoundException, SQLException {
ResultSet rs = SqlDAO.getInstance().morethan10percent();
System.out.println("Sin, country, city");
while (rs.next()) {
System.out.println(rs.getString("sin") + ", " + rs.getString("country") + ", " + rs.getString("city"));
}
}
public static void reportRankHostByListingCountryCity() throws ClassNotFoundException, SQLException {
ResultSet rs = SqlDAO.getInstance().countListingByHostAndCities();
System.out.println("Country, city, sin: [total listings]");
while (rs.next()) {
System.out.println(rs.getString("country") + ", " + rs.getString("city") + ", "
+ rs.getString("sin") + ": " + rs.getInt("count"));
}
}
public static void reportRankHostsByListingCountry() throws ClassNotFoundException, SQLException {
ResultSet rs = SqlDAO.getInstance().countListingByHostAndCountry();
System.out.println("Country, sin: [total listings]");
while (rs.next()) {
System.out.println(rs.getString("country") + ", " + rs.getString("sin") + ": " + rs.getInt("count"));
}
}
public static void reportTotalListingByCCP() throws ClassNotFoundException, SQLException {
ResultSet rs = SqlDAO.getInstance().countListingInPostals();
System.out.println("Country, city, postal: [total listings]");
while (rs.next()) {
System.out.println(rs.getString("country") + ", " + rs.getString("city") + ", " + rs.getString("postal_code")
+ ": " + rs.getInt("count"));
}
}
public static void reportTotalListingByCountryCity() throws SQLException, ClassNotFoundException {
ResultSet rs = SqlDAO.getInstance().countListingInCities();
System.out.println("Country, city: [total listings]");
while (rs.next()) {
System.out.println(rs.getString("country") + ", " + rs.getString("city") + ": " + rs.getInt("count"));
}
}
public static void reportTotalListingByCountry()
throws SQLException, ClassNotFoundException {
ResultSet rs = SqlDAO.getInstance().countListingInCountries();
System.out.println("Country: [total listings]");
while (rs.next()) {
System.out.println(rs.getString("country") + ": " + rs.getInt("count"));
}
}
}