-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieTitles.java
More file actions
216 lines (175 loc) · 4.4 KB
/
MovieTitles.java
File metadata and controls
216 lines (175 loc) · 4.4 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
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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.net.*;
import com.google.gson.*;
public class MovieTitles
{
public static void main( String[] args ) throws IOException
{
Scanner s = new Scanner( System.in );
String substr = s.next();
String[] movies = getMovieTitles( substr );
for( String movie : movies )
System.out.println( movie );
}
static String[] getMovieTitles( String substr )
{
String query = "Title=" + substr;
List<Movie> movies = new ArrayList<Movie>();
try
{
MovieResponse movieHeader = sendGET( query );
int totalPages = movieHeader.getTotal_pages();
for( int i = 1; i <= totalPages; i++ )
{
query = "Title=" + substr + "&page=" + i;
MovieResponse movieResponse = sendGET( query );
movies.addAll( movieResponse.getData() );
}
}
catch( Exception ex )
{
System.out.println( ex.getMessage() );
}
List<String> movieTitle = new ArrayList<String>();
for( Movie m : movies )
{
movieTitle.add( m.getTitle() );
}
Collections.sort( movieTitle );
String[] movienames = movieTitle.toArray( new String[ movieTitle.size() ] );
return movienames;
}
private static MovieResponse sendGET( String query ) throws IOException
{
URL obj = new URL( "https://jsonmock.hackerrank.com/api/movies/search/?" + query );
HttpURLConnection con = (HttpURLConnection)obj.openConnection();
con.setRequestMethod( "GET" );
int responseCode = con.getResponseCode();
MovieResponse res = null;
if( responseCode == HttpURLConnection.HTTP_OK )
{ // success
BufferedReader in = new BufferedReader( new InputStreamReader( con.getInputStream() ) );
String inputLine;
StringBuffer response = new StringBuffer();
while( ( inputLine = in.readLine() ) != null )
{
response.append( inputLine );
}
in.close();
Gson gson = new Gson();
res = gson.fromJson( response.toString(), MovieResponse.class );
// print result
}
else
{
System.out.println( "GET request not worked" );
}
return res;
}
}
class MovieResponse
{
private int page;
private int per_page;
private int total;
private int total_pages;
private List<Movie> data;
public MovieResponse( int page, int per_page, int total, int total_pages, List<Movie> data )
{
super();
this.page = page;
this.per_page = per_page;
this.total = total;
this.total_pages = total_pages;
this.data = data;
}
public MovieResponse()
{
super();
}
public int getPage()
{
return page;
}
public void setPage( int page )
{
this.page = page;
}
public int getPer_page()
{
return per_page;
}
public void setPer_page( int per_page )
{
this.per_page = per_page;
}
public int getTotal()
{
return total;
}
public void setTotal( int total )
{
this.total = total;
}
public int getTotal_pages()
{
return total_pages;
}
public void setTotal_pages( int total_pages )
{
this.total_pages = total_pages;
}
public List<Movie> getData()
{
return data;
}
public void setData( List<Movie> data )
{
this.data = data;
}
}
class Movie
{
private String Title;
private String Year;
private String imdbID;
public String getTitle()
{
return Title;
}
public void setTitle( String title )
{
Title = title;
}
public String getYear()
{
return Year;
}
public void setYear( String year )
{
Year = year;
}
public Movie()
{
super();
}
public String getImdbID()
{
return imdbID;
}
public Movie( String title, String year, String imdbID )
{
super();
Title = title;
Year = year;
this.imdbID = imdbID;
}
public void setImdbID( String imdbID )
{
this.imdbID = imdbID;
}
}