-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
84 lines (70 loc) · 3.04 KB
/
Copy pathView.java
File metadata and controls
84 lines (70 loc) · 3.04 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
package advisor;
import com.google.gson.JsonObject;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class View {
public static void print(JsonObject json) throws FileNotFoundException {
var keys = new ArrayList<>(json.keySet());
var key = keys.get(keys.size() - 1);
switch (key) {
case "albums":
printNewReleases(json);
break;
case "categories":
printCategories(json);
break;
case "playlists":
printPlaylists(json);
break;
case "error":
printError(json);
break;
}
}
private static void printNewReleases(JsonObject json) throws FileNotFoundException {
var albums = json.get("albums").getAsJsonObject();
PrintWriter out = new PrintWriter("filename.txt");
out.println("hi");
out.close();
albums.get("items").getAsJsonArray().forEach(item -> {
var album = item.getAsJsonObject();
var name = album.get("name").getAsString();
var url = album.get("external_urls").getAsJsonObject().get("spotify").getAsString();
var artists = new ArrayList<>();
album.get("artists").getAsJsonArray().forEach(artist -> artists.add(artist.getAsJsonObject().get("name").getAsString()));
System.out.println(name);
System.out.println(artists);
System.out.println(url + "\n");
});
printPage(albums);
}
private static void printCategories(JsonObject json) {
var categories = json.get("categories").getAsJsonObject();
categories.get("items").getAsJsonArray().forEach(item -> {
var category = item.getAsJsonObject();
var name = category.get("name").getAsString();
System.out.println(name);
});
printPage(categories);
}
private static void printPlaylists(JsonObject json) {
var playlists = json.get("playlists").getAsJsonObject();
playlists.get("items").getAsJsonArray().forEach(item -> {
var playlist = item.getAsJsonObject();
var name = playlist.get("name").getAsString();
var url = playlist.get("external_urls").getAsJsonObject().get("spotify").getAsString();
System.out.println(name);
System.out.println(url + "\n");
});
printPage(playlists);
}
protected static void printError(JsonObject json) {
System.out.println(json.get("error").getAsJsonObject().get("message").getAsString());
}
protected static void printPage(JsonObject json) {
int page = (json.get("offset").getAsInt() / json.get("limit").getAsInt()) + 1;
int totalPages = (int) Math.ceil(json.get("total").getAsDouble() / json.get("limit").getAsDouble());
System.out.println("---PAGE " + page + " OF " + totalPages + "---");
}
}