mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge branch 'update-parser' into 'master'
Update parser See merge request !7
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -27,13 +27,12 @@ public class FileParserTest {
|
|||||||
public void readValidFile() throws Exception{
|
public void readValidFile() throws Exception{
|
||||||
FileParser fileParser = new FileParser("src/test/java/seng302/valid.json");
|
FileParser fileParser = new FileParser("src/test/java/seng302/valid.json");
|
||||||
|
|
||||||
assertEquals(fileParser.getRaceName(), "IDK");
|
assertEquals("AC35", fileParser.getRaceName());
|
||||||
|
|
||||||
ArrayList<String> teams = new ArrayList<>();
|
assertEquals("Oracle Team USA", fileParser.getTeams().get(0).get("team-name"));
|
||||||
teams.add("team1");
|
assertEquals(20.9, fileParser.getTeams().get(0).get("velocity"));
|
||||||
teams.add("team2");
|
assertEquals(2, fileParser.getRaceSize());
|
||||||
teams.add("team3");
|
assertEquals(6, fileParser.getTotalNumberOfTeams());
|
||||||
assertTrue(teams.equals(fileParser.getTeams()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -41,13 +40,13 @@ public class FileParserTest {
|
|||||||
variable name.
|
variable name.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void readInvaldFile() throws Exception {
|
public void readInvalidFile() throws Exception {
|
||||||
FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json");
|
FileParser fileParser = new FileParser("src/test/java/seng302/invalid.json");
|
||||||
|
|
||||||
assertEquals(fileParser.getRaceName(), null);
|
assertEquals(null, fileParser.getRaceName());
|
||||||
assertEquals(fileParser.getTeams(), null);
|
assertEquals(null, fileParser.getTeams());
|
||||||
assertEquals(fileParser.getTimeScale(), -1);
|
assertEquals(-1, fileParser.getTimeScale());
|
||||||
assertEquals(fileParser.getTeams(), null);
|
assertEquals(null,fileParser.getTeams());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,13 @@
|
|||||||
{
|
{
|
||||||
"time-scale": 100,
|
"race-name": "AC35",
|
||||||
"race-name": "IDK",
|
"time-scale": 1,
|
||||||
"teams":["team1","team2","team3"]
|
"race-size": 2,
|
||||||
|
"teams": [
|
||||||
|
{"team-name": "Oracle Team USA", "velocity": 20.9},
|
||||||
|
{"team-name": "Artemis Racing", "velocity": 18.3},
|
||||||
|
{"team-name": "Emirates Team New Zealand", "velocity": 21.5},
|
||||||
|
{"team-name": "Groupama Team France","velocity": 19.9},
|
||||||
|
{"team-name": "Land Rover BAR", "velocity": 17.6},
|
||||||
|
{"team-name": "SoftBank Team Japan", "velocity": 16.6}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user