Refactor mark related classes.

- Mark is an abstract class which containing its name and type
- Single Mark is a sub class of Mark which containing only one GPS location
- Gate Mark is a sub class of Mark which containing two Single Marks

#refactor #fix #story[10] #story[11] #story[12]
This commit is contained in:
Haoming Yin
2017-03-17 15:21:04 +13:00
parent d6fe155d4d
commit 0b3ebf229f
11 changed files with 239 additions and 68 deletions
+19 -24
View File
@@ -1,5 +1,7 @@
package seng302.models;
import seng302.models.mark.SingleMark;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -11,8 +13,8 @@ public class Event {
private Double time; // Time the event occurs
private Boat boat;
private boolean isFinishingEvent = false; // This event occurs when a boat finishes the race
private Mark mark1; // This mark
private Mark mark2; // Next Mark
private SingleMark singleMark1; // This mark
private SingleMark singleMark2; // Next SingleMark
/**
@@ -22,12 +24,12 @@ public class Event {
* @param eventTime, what time the event happens
* @param eventBoat, the boat that the event belongs to
*/
public Event(Double eventTime, Boat eventBoat, Mark mark1, Mark mark2) {
public Event(Double eventTime, Boat eventBoat, SingleMark singleMark1, SingleMark singleMark2) {
this.time = eventTime;
this.boat = eventBoat;
//this.leg = eventLeg;
this.mark1 = mark1;
this.mark2 = mark2;
this.singleMark1 = singleMark1;
this.singleMark2 = singleMark2;
}
/**
@@ -37,10 +39,10 @@ public class Event {
* @param eventTime, what time the event happens
* @param eventBoat, the boat that the event belongs to
*/
public Event(Double eventTime, Boat eventBoat, Mark mark1) {
public Event(Double eventTime, Boat eventBoat, SingleMark singleMark1) {
this.time = eventTime;
this.boat = eventBoat;
this.mark1 = mark1;
this.singleMark1 = singleMark1;
this.isFinishingEvent = true;
}
@@ -89,13 +91,6 @@ public class Event {
this.boat = eventBoat;
}
/**
* Called when the boat in this event passes
* the marker.
*/
public void boatPassedMarker() {
this.mark1.addBoat(boat);
}
/**
* Returns true if this event is the boat finishing the race
@@ -115,19 +110,19 @@ public class Event {
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " finished the race");
}
System.out.println(this.getDistanceBetweenMarks());
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " passed " + this.mark1.getName() + " going heading " + this.getBoatHeading() + "°");
return (this.getTimeString() + ", " + this.getBoat().getTeamName() + " passed " + this.singleMark1.getName() + " going heading " + this.getBoatHeading() + "°");
}
/**
* @return the distance between the two marks
*/
public double getDistanceBetweenMarks(){
//return Math.sqrt(Math.pow(mark1.getLatitude()-mark2.getLatitude(), 2) + Math.pow(mark1.getLongitude()-mark2.getLongitude(), 2));
//return Math.sqrt(Math.pow(singleMark1.getLatitude()-singleMark2.getLatitude(), 2) + Math.pow(singleMark1.getLongitude()-singleMark2.getLongitude(), 2));
double earth_radius = 6378.137;
double dLat = this.mark2.getLatitude() * Math.PI / 180 - this.mark1.getLatitude() * Math.PI / 180;
double dLon = this.mark2.getLongitude() * Math.PI / 180 - this.mark1.getLongitude() * Math.PI / 180;
double dLat = this.singleMark2.getLatitude() * Math.PI / 180 - this.singleMark1.getLatitude() * Math.PI / 180;
double dLon = this.singleMark2.getLongitude() * Math.PI / 180 - this.singleMark1.getLongitude() * Math.PI / 180;
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.mark1.getLatitude() * Math.PI / 180) * Math.sin(dLon/2) * Math.sin(dLon/2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.singleMark1.getLatitude() * Math.PI / 180) * Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = earth_radius * c;
@@ -139,7 +134,7 @@ public class Event {
* @return the boats heading
*/
public double getBoatHeading(){
double bearing = Math.atan2(mark2.getLatitude() - mark1.getLatitude(), mark2.getLongitude() - mark1.getLongitude());
double bearing = Math.atan2(singleMark2.getLatitude() - singleMark1.getLatitude(), singleMark2.getLongitude() - singleMark1.getLongitude());
if (bearing < 0) {
bearing += Math.PI * 2;
}
@@ -150,15 +145,15 @@ public class Event {
* Get the mark the event happened on
* @return the mark
*/
public Mark getMark(){
return this.mark1;
public SingleMark getMark(){
return this.singleMark1;
}
/**
* Get the next mark
* @return the next mark
*/
public Mark getNextMark(){
return this.mark2;
public SingleMark getNextMark(){
return this.singleMark2;
}
}
+14 -17
View File
@@ -1,5 +1,7 @@
package seng302.models;
import seng302.models.mark.SingleMark;
/**
* Represents the leg of a race.
*/
@@ -7,19 +9,19 @@ public class Leg {
private int heading;
private int distance;
private boolean isFinishingLeg;
private Mark startingMark;
private SingleMark startingSingleMark;
/**
* Create a new leg
*
* @param heading, the magnetic heading of this leg
* @param distance, the total distance of this leg in meters
* @param mark, the mark this leg starts on
* @param singleMark, the singleMark this leg starts on
*/
public Leg(int heading, int distance, Mark mark) {
public Leg(int heading, int distance, SingleMark singleMark) {
this.heading = heading;
this.distance = distance;
this.startingMark = mark;
this.startingSingleMark = singleMark;
this.isFinishingLeg = false;
}
@@ -33,7 +35,7 @@ public class Leg {
public Leg(int heading, int distance, String markerName) {
this.heading = heading;
this.distance = distance;
this.startingMark = new Mark(markerName);
this.startingSingleMark = new SingleMark(markerName);
this.isFinishingLeg = false;
}
@@ -68,30 +70,25 @@ public class Leg {
/**
* Returns the marker this leg started on
*/
public Mark getMarker() {
return this.startingMark;
public SingleMark getMarker() {
return this.startingSingleMark;
}
/**
* Set the mark this leg starts on
* Set the singleMark this leg starts on
*/
public void setMarker(Mark mark) {
this.startingMark = mark;
public void setMarker(SingleMark singleMark) {
this.startingSingleMark = singleMark;
}
/**
* Returns the name of the marker this leg started on
*/
public String getMarkerLabel() {
return this.startingMark.getName();
return this.startingSingleMark.getName();
}
/**
* Tell the marker that the boat has passed it
*/
public void addBoatToMarker(Boat boat) {
this.startingMark.addBoat(boat);
}
/**
* Specify whether or not the race finishes on this leg
+7 -5
View File
@@ -1,5 +1,7 @@
package seng302.models;
import seng302.models.mark.SingleMark;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
@@ -55,11 +57,11 @@ public class OldApp {
}
// Add marks to race in order
race.addMark(new Mark("Start", 32.296038,-64.854401 ));
race.addMark(new Mark("Mid Mark", 32.292881,-64.843231 ));
race.addMark(new Mark("Leeward Gate", 32.283808,-64.850012 ));
race.addMark(new Mark("Windward Gate", 32.309908,-64.833665 ));
race.addMark(new Mark("Finish", 32.318439,-64.837367 ));
race.addMark(new SingleMark("Start", 32.296038,-64.854401 ));
race.addMark(new SingleMark("Mid SingleMark", 32.292881,-64.843231 ));
race.addMark(new SingleMark("Leeward Gate", 32.283808,-64.850012 ));
race.addMark(new SingleMark("Windward Gate", 32.309908,-64.833665 ));
race.addMark(new SingleMark("Finish", 32.318439,-64.837367 ));
return race;
}
+13 -11
View File
@@ -1,5 +1,7 @@
package seng302.models;
import seng302.models.mark.SingleMark;
import java.lang.reflect.Array;
import java.util.*;
@@ -10,7 +12,7 @@ public class Race {
private ArrayList<Boat> boats; // The boats in the race
private ArrayList<Boat> finishingOrder; // The order in which the boats finish the race
private PriorityQueue<Event> events; // The events that occur in the race
private ArrayList<Mark> marks; // Marks in the race
private ArrayList<SingleMark> singleMarks; // Marks in the race
private int numberOfBoats = 0;
private long startTime = 0;
private double timeScale = 1;
@@ -21,7 +23,7 @@ public class Race {
public Race() {
this.boats = new ArrayList<Boat>();
this.finishingOrder = new ArrayList<Boat>();
this.marks = new ArrayList<Mark>();
this.singleMarks = new ArrayList<SingleMark>();
// create a priority queue with a custom Comparator to order events
this.events = new PriorityQueue<Event>(new Comparator<Event>() {
@@ -140,21 +142,21 @@ public class Race {
for (Boat boat : this.boats) {
double totalDistance = 0;
int numberOfMarks = this.marks.size();
int numberOfMarks = this.singleMarks.size();
for(int i = 0; i < numberOfMarks; i++){
Double time = (Double) (1000 * totalDistance / boat.getVelocity());
// If there are marks after this event
// If there are singleMarks after this event
if (i < numberOfMarks-1) {
Event event = new Event(time, boat, marks.get(i), marks.get(i + 1));
Event event = new Event(time, boat, singleMarks.get(i), singleMarks.get(i + 1));
events.add(event);
totalDistance += event.getDistanceBetweenMarks();
}
// There are no more marks after this event
// There are no more singleMarks after this event
else{
Event event = new Event(time, boat, marks.get(i));
Event event = new Event(time, boat, singleMarks.get(i));
events.add(event);
}
}
@@ -224,10 +226,10 @@ public class Race {
}
/**
* Add a mark to the race (in order)
* @param mark, the mark to add
* Add a singleMark to the race (in order)
* @param singleMark, the singleMark to add
*/
public void addMark(Mark mark){
this.marks.add(mark);
public void addMark(SingleMark singleMark){
this.singleMarks.add(singleMark);
}
}
@@ -0,0 +1,40 @@
package seng302.models.mark;
/**
* To represent a gate mark which contains two single marks.
* Created by ptg19 on 16/03/17.
* Modified by Haoming Yin (hyi25) on 17/3/2017.
*/
public class GateMark extends Mark {
private SingleMark singleMark1;
private SingleMark singleMark2;
/**
* Create an instance of Gate Mark which contains two single mark
* @param name the name of the gate mark
* @param singleMark1 one single mark inside of the gate mark
* @param singleMark2 the second mark inside of the gate mark
*/
public GateMark(String name, SingleMark singleMark1, SingleMark singleMark2) {
super(name, MarkType.GATE_MARK);
this.singleMark1 = singleMark1;
this.singleMark2 = singleMark2;
}
public SingleMark getSingleMark1() {
return singleMark1;
}
public void setSingleMark1(SingleMark singleMark1) {
this.singleMark1 = singleMark1;
}
public SingleMark getSingleMark2() {
return singleMark2;
}
public void setSingleMark2(SingleMark singleMark2) {
this.singleMark2 = singleMark2;
}
}
@@ -0,0 +1,37 @@
package seng302.models.mark;
/**
* An abstract class to represent general marks
* Created by Haoming Yin (hyi25) on 17/3/17.
*/
public abstract class Mark {
private String name;
private MarkType markType;
/**
* Create a mark instance by passing its name and type
* @param name the name of the mark
* @param markType the type of mark. either GATE_MARK or SINGLE_MARK.
*/
public Mark (String name, MarkType markType) {
this.name = name;
this.markType = markType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MarkType getMarkType() {
return markType;
}
public void setMarkType(MarkType markType) {
this.markType = markType;
}
}
@@ -0,0 +1,9 @@
package seng302.models.mark;
/**
* To represent two types of mark
* Created by Haoming Yin (hyi25) on 17/3/17.
*/
public enum MarkType {
SINGLE_MARK, GATE_MARK
}
@@ -0,0 +1,45 @@
package seng302.models.mark;
/**
* Represents the marker as a single mark
*
* Created by Haoming Yin (hyi25) on 17/3/2017
*/
public class SingleMark extends Mark {
private double lat;
private double lon;
private String name;
/**
* Represents a marker
*
* @param name, the name of the marker*
* @param lat, the latitude of the marker
* @param lon, the longitude of the marker
*/
public SingleMark(String name, double lat, double lon) {
super(name, MarkType.SINGLE_MARK);
this.lat = lat;
this.lon = lon;
}
/**
* Represents the marker at the beginning of a leg
*
* @param name, the name of the marker
*/
public SingleMark(String name) {
super(name, MarkType.SINGLE_MARK);
this.lat = 0;
this.lon = 0;
}
public double getLatitude() {
return this.lat;
}
public double getLongitude() {
return this.lon;
}
}
+4 -4
View File
@@ -3,7 +3,7 @@ package seng302;
import org.junit.Test;
import seng302.models.Boat;
import seng302.models.Event;
import seng302.models.Mark;
import seng302.models.mark.SingleMark;
import static org.junit.Assert.assertEquals;
@@ -16,14 +16,14 @@ public class EventTest {
@Test
public void getTimeString() throws Exception {
Boat boat = new Boat("testBoat");
Event event = new Event(1231242.2, boat, new Mark("mark1"), new Mark("mark2"));
Event event = new Event(1231242.2, boat, new SingleMark("mark1"), new SingleMark("mark2"));
assertEquals("20:31:242", event.getTimeString());
}
@Test
public void testBoatHeading() throws Exception {
Boat boat = new Boat("testBoat");
Event event = new Event(1231242.2, boat, new Mark("mark1", 142.5, 122.1), new Mark("mark2", 121.9,99.2));
Event event = new Event(1231242.2, boat, new SingleMark("mark1", 142.5, 122.1), new SingleMark("mark2", 121.9,99.2));
assertEquals(event.getBoatHeading(), 221.9733862944651, 1e-15);
}
@@ -31,7 +31,7 @@ public class EventTest {
@Test
public void testDistanceBetweenMarks() throws Exception {
Boat boat = new Boat("testBoat");
Event event = new Event(1231242.2, boat, new Mark("mark1", 142.5, 122.1), new Mark("mark2", 121.9,99.2));
Event event = new Event(1231242.2, boat, new SingleMark("mark1", 142.5, 122.1), new SingleMark("mark2", 121.9,99.2));
assertEquals(event.getDistanceBetweenMarks(), 339059.653830461, 1e-15);
}
+7 -7
View File
@@ -2,7 +2,7 @@ package seng302;
import org.junit.Test;
import seng302.models.Leg;
import seng302.models.Mark;
import seng302.models.mark.SingleMark;
import static org.junit.Assert.assertEquals;
@@ -17,25 +17,25 @@ public class LegTest {
*/
@Test
public void testLegCreationUsingMarkerLabel() {
Leg leg = new Leg(010, 100, "Mark");
Leg leg = new Leg(010, 100, "SingleMark");
assertEquals(leg.getHeading(), 010);
assertEquals(leg.getDistance(), 100);
assertEquals(leg.getMarkerLabel(), "Mark");
assertEquals(leg.getMarkerLabel(), "SingleMark");
assertEquals(leg.getIsFinishingLeg(), false);
}
/**
* Test creation of the leg by providing a
* Mark object
* SingleMark object
*/
@Test
public void testLegCreation() {
Leg leg = new Leg(010, 100, new Mark("Mark"));
Leg leg = new Leg(010, 100, new SingleMark("SingleMark"));
assertEquals(leg.getHeading(), 010);
assertEquals(leg.getDistance(), 100);
assertEquals(leg.getMarkerLabel(), "Mark");
assertEquals(leg.getMarkerLabel(), "SingleMark");
assertEquals(leg.getIsFinishingLeg(), false);
}
@@ -45,7 +45,7 @@ public class LegTest {
*/
@Test
public void testSetFinishLeg() {
Leg leg = new Leg(010, 100, "Mark");
Leg leg = new Leg(010, 100, "SingleMark");
leg.setFinishingLeg(true);
assertEquals(leg.getIsFinishingLeg(), true);
@@ -0,0 +1,44 @@
package seng302.models.mark;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by Haoming on 17/3/17.
*/
public class MarkTest {
private SingleMark singleMark1;
private SingleMark singleMark2;
private GateMark gateMark;
@Before
public void setUp() throws Exception {
this.singleMark1 = new SingleMark("testMark_SM1", 12.23234, -34.342);
this.singleMark2 = new SingleMark("testMark_SM2", 12.23239, -34.352);
this.gateMark = new GateMark("testMark_GM", singleMark1, singleMark2);
}
@Test
public void getName() throws Exception {
assertEquals("testMark_SM1", this.singleMark1.getName());
assertEquals("testMark_GM", this.gateMark.getName());
}
@Test
public void getMarkType() throws Exception {
assertTrue(this.singleMark2.getMarkType() == MarkType.SINGLE_MARK);
assertTrue(this.gateMark.getMarkType() == MarkType.GATE_MARK);
}
@Test
public void getMarkContent() throws Exception {
assertEquals(12.23234, this.singleMark1.getLatitude(), 1e-10);
assertEquals(-34.342, this.singleMark1.getLongitude(), 1e-10);
assertEquals("testMark_SM1", this.gateMark.getSingleMark1().getName());
assertEquals(-34.352, this.gateMark.getSingleMark2().getLongitude(), 1e-10);
}
}