Added ability to pass the config file as a command line argument

Tags: #implement
This commit is contained in:
Michael Rausch
2017-03-09 12:41:44 +13:00
parent 260bf06219
commit c1aa38c1b0
+22 -5
View File
@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.io.FileNotFoundException;
public class App { public class App {
@@ -13,11 +14,19 @@ public class App {
* *
* @return a Race object for the AC35 course * @return a Race object for the AC35 course
*/ */
public static Race createRace() throws Exception { public static Race createRace(String configFile) throws Exception {
Race race = new Race(); Race race = new Race();
FileParser fp;
// Read team names from file // Read team names from file
FileParser fp = new FileParser("doc/examples/config.json"); try{
fp = new FileParser(configFile);
}
catch (FileNotFoundException e){
System.out.println("Config file does not exist");
return null;
}
ArrayList<String> boatNames = new ArrayList<>(); ArrayList<String> boatNames = new ArrayList<>();
ArrayList<Map<String, Object>> teams = fp.getTeams(); ArrayList<Map<String, Object>> teams = fp.getTeams();
@@ -60,9 +69,18 @@ public class App {
public static void main(String[] args) { public static void main(String[] args) {
Race race = null; Race race = null;
String raceConfigFile;
if (args.length == 2 && args[0].equals("-f")){
raceConfigFile = args[1];
}
else{
// Use default config
raceConfigFile = "doc/examples/config.json";
}
try { try {
race = createRace(); race = createRace(raceConfigFile);
} catch (Exception e) { } catch (Exception e) {
System.out.println("There was an error creating the race."); System.out.println("There was an error creating the race.");
} }
@@ -78,7 +96,6 @@ public class App {
race.startRace(); race.startRace();
System.out.println("\n\n"); System.out.println("\n\n");
System.out.println("######################"); System.out.println("######################");
System.out.println("# Race Results "); System.out.println("# Race Results ");
@@ -88,7 +105,7 @@ public class App {
race.displayFinishingOrder(); race.displayFinishingOrder();
} else { } else {
System.out.println("There was an error creating the race."); System.out.println("There was an error creating the race. Exiting.");
} }
} }
} }