Updated FileParser to read team name, boat velocity, and race size.

- minor code rearrange for readFile()
- changed getTeam() to read to an array of team maps, where map contains team name and boat velocity
- added getTotalNumberOfTeams() to get the total number of teams in file
- added getRaceSize to get the the number of boat competing in a race

#implement #story[6] #story[8]
This commit is contained in:
Haoming Yin
2017-03-06 21:44:31 +13:00
parent c9b0942e07
commit 85b8047229
+37 -5
View File
@@ -8,6 +8,7 @@ import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
/** /**
@@ -36,10 +37,8 @@ public class FileParser {
private void readFile() throws FileNotFoundException{ private void readFile() throws FileNotFoundException{
JSONParser parser = new JSONParser(); JSONParser parser = new JSONParser();
try { try {
Object obj = parser.parse(new FileReader(filePath)); this.content = (JSONObject) parser.parse(new FileReader(filePath));
JSONObject jsonObject = (JSONObject) obj;
this.content = jsonObject;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw e; throw e;
} catch (IOException e) { } catch (IOException e) {
@@ -54,6 +53,7 @@ public class FileParser {
* @return long time scale. -1 if parameter is invalid (eg. scale is * @return long time scale. -1 if parameter is invalid (eg. scale is
* negative number, or containing non numeric character) or cannot be found. * negative number, or containing non numeric character) or cannot be found.
*/ */
@SuppressWarnings("unchecked")
public long getTimeScale() { public long getTimeScale() {
try { try {
long timeScale = (long) this.content.get("time-scale"); long timeScale = (long) this.content.get("time-scale");
@@ -68,6 +68,7 @@ public class FileParser {
* @return a string of race name. null if race name is invalid or cannot * @return a string of race name. null if race name is invalid or cannot
* be found. * be found.
*/ */
@SuppressWarnings("unchecked")
public String getRaceName() { public String getRaceName() {
try { try {
return (String) this.content.get("race-name"); return (String) this.content.get("race-name");
@@ -81,12 +82,43 @@ public class FileParser {
* @return an ArrayList containing strings of team names. null if teams * @return an ArrayList containing strings of team names. null if teams
* setting is invalid or there is no team. * setting is invalid or there is no team.
*/ */
public ArrayList<String> getTeams() { @SuppressWarnings("unchecked")
public ArrayList<Map<String, Object>> getTeams() {
try { try {
return (ArrayList<String>) this.content.get("teams"); return (ArrayList<Map<String, Object>>) this.content.get("teams");
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
} }
/**
* Gets the total number of teams.
* @return the number of teams. 0 if no teams or anything goes wrong.
*/
@SuppressWarnings("unchecked")
public long getTotalNumberOfTeams() {
ArrayList<Map<String, Object>> teams = getTeams();
try {
return teams.size();
} catch (Exception e) {
return 0;
}
}
/**
* Gets the number of boats that would compete during a race. Returns the
* total number of race size if parameter is invalid or cannot be found.
* @return an int of the race size.
*/
@SuppressWarnings("unchecked")
public long getRaceSize() {
long totalTeams = this.getTotalNumberOfTeams();
try {
long raceSize = (long) this.content.get("race-size");
return raceSize >= 0 && raceSize <= totalTeams? raceSize : totalTeams;
} catch (Exception e) {
e.printStackTrace();
return totalTeams;
}
}
} }