-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoEuroTest.java
67 lines (58 loc) · 1.52 KB
/
GoEuroTest.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
package goEuro;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class GoEuroTest {
public static void launchError(String errorMsg){
System.out.println(errorMsg+"\n");
System.exit(1);
}
public static void main(String[] args){
if(args.length != 1)
{
launchError("Error: Incorrect number of arguments.\njava -jar GoEuroTest.jar \"CITY_NAME\"");
}
HTTPConnection c;
Parser parser = null;
InputStreamReader rawJSON = null;
try{
c = new HTTPConnection("http://api.goeuro.com/api/v2/position/suggest/en/"+args[0]);
rawJSON = c.initiateConnection();
}
catch(FileNotFoundException e)
{
launchError("Error while accessing file : it may be used in an other program.");
}
catch(IOException e)
{
launchError("Error: Can't connect to API URL.");
}
try{
parser = new Parser(rawJSON);
parser.doParse();
}
catch(NullPointerException e)
{
launchError("Error while parsing JSON.");
}
if(!parser.getResults().isEmpty())
{
try{
CSVwriter writer = new CSVwriter(parser.getResults(),"output.csv");
writer.writeCSV();
System.out.println("Success: "+parser.getResults().size()+" row(s) written for city \""+args[0]+"\".");
}
catch(FileNotFoundException e)
{
launchError("Error while accessing file: it may be used in an other program.");
}
catch(IOException e)
{
launchError("Error while writing in file.");
}
}
else{
launchError("No matches found for given parameter.");
}
}
}