mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Finished team parser to read team info from external xml file
- created team parser unit test - refactored team parser functions #fix #refactor #implement
This commit is contained in:
+24
-4
@@ -3,12 +3,32 @@
|
|||||||
<teams>
|
<teams>
|
||||||
<team>
|
<team>
|
||||||
<name>Oracle Team USA</name>
|
<name>Oracle Team USA</name>
|
||||||
<alais>USA</alais>
|
<alias>USA</alias>
|
||||||
<speed>23.4</speed>
|
<velocity>23.4</velocity>
|
||||||
|
</team>
|
||||||
|
<team>
|
||||||
|
<name>Artemis Racing</name>
|
||||||
|
<alias>ART</alias>
|
||||||
|
<velocity>12.9</velocity>
|
||||||
</team>
|
</team>
|
||||||
<team>
|
<team>
|
||||||
<name>Emirates Team New Zealand</name>
|
<name>Emirates Team New Zealand</name>
|
||||||
<alais>NZL</alais>
|
<alias>NZL</alias>
|
||||||
<speed>25.2</speed>
|
<velocity>25.2</velocity>
|
||||||
|
</team>
|
||||||
|
<team>
|
||||||
|
<name>Land Rover BAR</name>
|
||||||
|
<alias>BAR</alias>
|
||||||
|
<velocity>16.4</velocity>
|
||||||
|
</team>
|
||||||
|
<team>
|
||||||
|
<name>SoftBank Team Japan</name>
|
||||||
|
<alias>JAP</alias>
|
||||||
|
<velocity>19.22</velocity>
|
||||||
|
</team>
|
||||||
|
<team>
|
||||||
|
<name>Groupama Team France</name>
|
||||||
|
<alias>FRC</alias>
|
||||||
|
<velocity>28.8</velocity>
|
||||||
</team>
|
</team>
|
||||||
</teams>
|
</teams>
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
import org.w3c.dom.*;
|
||||||
|
import seng302.models.Boat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
public class TeamsParser extends FileParser {
|
||||||
|
|
||||||
|
private Document doc;
|
||||||
|
|
||||||
|
public TeamsParser(String path) {
|
||||||
|
super(path);
|
||||||
|
this.doc = this.parseFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a boat instance by a given team node
|
||||||
|
* @param node a boat node containing name, alias and velocity
|
||||||
|
* @return an instance of Boat
|
||||||
|
*/
|
||||||
|
private Boat parseBoat(Node node) {
|
||||||
|
try {
|
||||||
|
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||||
|
Element element = (Element) node;
|
||||||
|
String name = element.getElementsByTagName("name").item(0).getTextContent();
|
||||||
|
String alias = element.getElementsByTagName("alias").item(0).getTextContent();
|
||||||
|
double velocity = Double.valueOf(element.getElementsByTagName("velocity").item(0).getTextContent());
|
||||||
|
Boat boat = new Boat(name, velocity);
|
||||||
|
return boat;
|
||||||
|
} else {
|
||||||
|
throw new NoSuchElementException("Cannot generate a boat by given node");
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an arraylist of boats instance.
|
||||||
|
* @return an arraylist of boats in teams file
|
||||||
|
*/
|
||||||
|
public ArrayList<Boat> getBoats() {
|
||||||
|
ArrayList<Boat> boats = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
NodeList nodes = this.doc.getElementsByTagName("team");
|
||||||
|
for (int i = 0; i < nodes.getLength(); i++) {
|
||||||
|
Node node = nodes.item(i);
|
||||||
|
|
||||||
|
boats.add(parseBoat(node));
|
||||||
|
}
|
||||||
|
return boats;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package seng302.models.parsers;
|
|
||||||
|
|
||||||
public class teamsParser extends FileParser {
|
|
||||||
|
|
||||||
public teamsParser(String path) {
|
|
||||||
super(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package seng302.models.parsers;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import seng302.models.Boat;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Haoming on 18/03/17.
|
||||||
|
*/
|
||||||
|
public class TeamsParserTest {
|
||||||
|
|
||||||
|
private TeamsParser tp;
|
||||||
|
@Before
|
||||||
|
public void readFile() {
|
||||||
|
tp = new TeamsParser("doc/examples/teams.xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getBoats() throws Exception {
|
||||||
|
ArrayList<Boat> boats = tp.getBoats();
|
||||||
|
|
||||||
|
assertEquals(6, boats.size(), 1e-10);
|
||||||
|
|
||||||
|
assertEquals("Oracle Team USA", boats.get(0).getTeamName());
|
||||||
|
assertEquals(23.4, boats.get(0).getVelocity(), 1e-10);
|
||||||
|
|
||||||
|
assertEquals("Groupama Team France", boats.get(5).getTeamName());
|
||||||
|
assertEquals(28.8, boats.get(5).getVelocity(), 1e-10);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user