From 9453307fd4547031e207ff05c533b5905e2749d6 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Mon, 6 Mar 2017 17:56:07 +1300 Subject: [PATCH 1/3] Implemented leg class and added legs to the race - #implement --- src/main/java/seng302/Leg.java | 47 +++++++++++++++++++++++++++++++++ src/main/java/seng302/Race.java | 13 ++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/main/java/seng302/Leg.java diff --git a/src/main/java/seng302/Leg.java b/src/main/java/seng302/Leg.java new file mode 100644 index 00000000..9db54109 --- /dev/null +++ b/src/main/java/seng302/Leg.java @@ -0,0 +1,47 @@ +package seng302; + +public class Leg { + private int heading; + private int distance; + private String startLabel; + private boolean isFinishingLeg; + + public Leg(int heading, int distance, String label){ + this.heading = heading; + this.distance = distance; + this.startLabel = label; + this.isFinishingLeg = false; + } + + public void setHeading(int heading){ + this.heading = heading; + } + + public int getHeading(){ + return this.heading; + } + + public void setDistance(int distance){ + this.distance = distance; + } + + public int getDistance(){ + return this.distance; + } + + public void setLabel(String label){ + this.startLabel = label; + } + + public String getLabel(){ + return this.startLabel; + } + + public void setFinishingLeg(boolean isFinishingLeg){ + this.isFinishingLeg = isFinishingLeg; + } + + public boolean getIsFinishingLeg(){ + return this.isFinishingLeg; + } +} \ No newline at end of file diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index 9edc840d..8616dd93 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -8,10 +8,12 @@ import java.util.List; public class Race { private ArrayList boats; + private ArrayList legs; private int numberOfBoats = 0; public Race(){ boats = new ArrayList(); + legs = new ArrayList(); } /* @@ -71,7 +73,6 @@ public class Race { /* Prints the list of boats competing in the race - */ public void displayStartingBoats(){ int numberOfBoats = this.getNumberOfBoats(); @@ -83,4 +84,14 @@ public class Race { System.out.println(boats[i].getTeamName()); } } + + public void addLeg(Leg leg){ + this.legs.add(leg); + } + + public void printLegs(){ + for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ + System.out.println(leg.getLabel()); + } + } } \ No newline at end of file From 8c0cc67ae30909cc45edcb0a27a29827221008e8 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Mon, 6 Mar 2017 19:29:47 +1300 Subject: [PATCH 2/3] Add tests for the Leg class Tags: #test #story[4] --- src/test/java/seng302/LegTest.java | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/test/java/seng302/LegTest.java diff --git a/src/test/java/seng302/LegTest.java b/src/test/java/seng302/LegTest.java new file mode 100644 index 00000000..1a5fc90c --- /dev/null +++ b/src/test/java/seng302/LegTest.java @@ -0,0 +1,55 @@ +package seng302; + +import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +/** + * Unit test for the Leg class. + */ +public class LegTest{ + + /* + Test creation of the leg by specifying a string + for the marker label + */ + @Test + public void testLegCreationUsingMarkerLabel() + { + Leg leg = new Leg(010, 100, "Marker"); + + assertEquals(leg.getHeading(), 010); + assertEquals(leg.getDistance(), 100); + assertEquals(leg.getMarkerLabel(), "Marker"); + assertEquals(leg.getIsFinishingLeg(), false); + } + + /* + Test creation of the leg by providing a + Marker object + */ + @Test + public void testLegCreation() + { + Leg leg = new Leg(010, 100, new Marker("Marker")); + + assertEquals(leg.getHeading(), 010); + assertEquals(leg.getDistance(), 100); + assertEquals(leg.getMarkerLabel(), "Marker"); + assertEquals(leg.getIsFinishingLeg(), false); + } + + /* + Test changing whether or not a + leg is the finishing leg + */ + @Test + public void testSetFinishLeg() + { + Leg leg = new Leg(010, 100, "Marker"); + + leg.setFinishingLeg(true); + assertEquals(leg.getIsFinishingLeg(), true); + } + +} From 9b1d90d2bf0597da89ab425f628a38e770499758 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Mon, 6 Mar 2017 19:31:25 +1300 Subject: [PATCH 3/3] Added a class for the marker, and added documentation - Added documentation for the Race class - Added a class for the Marker instead of just storing the name of the marker in a string Tags: #docs #implement #story[4] --- src/main/java/seng302/App.java | 12 ++++-- src/main/java/seng302/Leg.java | 68 +++++++++++++++++++++++++++---- src/main/java/seng302/Marker.java | 17 ++++++++ src/main/java/seng302/Race.java | 47 ++++++++++++++++----- 4 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 src/main/java/seng302/Marker.java diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 4b336ba2..6fd4829e 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -27,6 +27,12 @@ public class App race.addBoat(new Boat(boatNames.get(i))); } + race.addLeg(new Leg(035, 100, "Start")); + race.addLeg(new Leg(010, 300, "Marker 1")); + race.addLeg(new Leg(350, 400, "Leeward Gate")); + race.addLeg(new Leg(010, 400, "Windward Gate")); + race.addLeg(new Leg(010, 400, "Leeward Gate")); + return race; } @@ -43,11 +49,11 @@ public class App } if (race != null){ - race.displayStartingBoats(); - - System.out.println(""); + race.startRace(); race.displayFinishingOrder(); + + } else{ System.out.println("e"); diff --git a/src/main/java/seng302/Leg.java b/src/main/java/seng302/Leg.java index 9db54109..862725a2 100644 --- a/src/main/java/seng302/Leg.java +++ b/src/main/java/seng302/Leg.java @@ -3,44 +3,98 @@ package seng302; public class Leg { private int heading; private int distance; - private String startLabel; private boolean isFinishingLeg; + private Marker startingMarker; - public Leg(int heading, int distance, String label){ + /* + Create a new leg + + @param heading, the magnetic heading of this leg + @param distance, the total distance of this leg in meters + @param marker, the marker this leg starts on + */ + public Leg(int heading, int distance, Marker marker){ this.heading = heading; this.distance = distance; - this.startLabel = label; + this.startingMarker = marker; this.isFinishingLeg = false; } + /* + Create a new leg + + @param heading, the magnetic heading of this leg + @param distance, the total distance of this leg in meters + @param markerName, the name of the marker this leg starts on + */ + public Leg(int heading, int distance, String markerName){ + this.heading = heading; + this.distance = distance; + this.startingMarker = new Marker(markerName); + this.isFinishingLeg = false; + } + + /* + Set the heading for this leg + */ public void setHeading(int heading){ this.heading = heading; } + /* + Get the heading of this leg + */ public int getHeading(){ return this.heading; } + /* + Set the distance of this leg in meters + */ public void setDistance(int distance){ this.distance = distance; } + /* + Get the total distance of this leg in meters + */ public int getDistance(){ return this.distance; } - public void setLabel(String label){ - this.startLabel = label; + /* + Set the marker this leg starts on + */ + public void setMarker(Marker marker){ + this.startingMarker = marker; } - public String getLabel(){ - return this.startLabel; + /* + Returns the marker this leg started on + */ + public Marker getMarker(){ + return this.startingMarker; } + /* + Returns the name of the marker this leg started on + */ + public String getMarkerLabel(){ + return this.startingMarker.getName(); + } + + /* + Specify whether or not the race finishes on this leg + + @param isFinishingLeg whether or not the race finishes on this leg + */ public void setFinishingLeg(boolean isFinishingLeg){ this.isFinishingLeg = isFinishingLeg; } + /* + @returns true if this the race finishes after this leg + */ public boolean getIsFinishingLeg(){ return this.isFinishingLeg; } diff --git a/src/main/java/seng302/Marker.java b/src/main/java/seng302/Marker.java new file mode 100644 index 00000000..2bed2dc0 --- /dev/null +++ b/src/main/java/seng302/Marker.java @@ -0,0 +1,17 @@ +package seng302; + +class Marker{ + private String name; + + public Marker(String name){ + this.name = name; + } + + public void setName(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } +} \ No newline at end of file diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index 8616dd93..be4ebacc 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -25,6 +25,19 @@ public class Race { numberOfBoats += 1; } + /* + Returns a list of boats in a random order + + @returns a list of boats + */ + public Boat[] getShuffledBoats(){ + // Shuffle the list of boats + long seed = System.nanoTime(); + Collections.shuffle(this.boats, new Random(seed)); + + return boats.toArray(new Boat[boats.size()]); + } + /* Returns a list of boats in the order that they finished the race (position 0 is first place) @@ -32,11 +45,7 @@ public class Race { @returns a list of boats */ public Boat[] getFinishedBoats(){ - // Shuffle the list of boats - long seed = System.nanoTime(); - Collections.shuffle(this.boats, new Random(seed)); - - return boats.toArray(new Boat[boats.size()]); + return getShuffledBoats(); } /* @@ -74,24 +83,42 @@ public class Race { /* Prints the list of boats competing in the race */ - public void displayStartingBoats(){ + private void displayStartingBoats(){ int numberOfBoats = this.getNumberOfBoats(); Boat[] boats = this.getBoats(); - System.out.println("--- Competing Boats ---"); + System.out.println("--- Starting Boats ---"); for (int i = 0; i < numberOfBoats; i++) { System.out.println(boats[i].getTeamName()); } } + /* + Adds a leg to the race + + @param leg, the leg to add to the race + */ public void addLeg(Leg leg){ this.legs.add(leg); } - public void printLegs(){ + /* + Start the race and print each marker with the order + in which the boats passed that marker + */ + public void startRace(){ for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ - System.out.println(leg.getLabel()); - } + Boat[] boats = this.getShuffledBoats(); + + System.out.println("--- " + leg.getMarkerLabel() + " ---"); + + // Print the order in which the boats passed the marker + for (int i = 0; i < this.getNumberOfBoats(); i++) { + System.out.println("#" + Integer.toString(i+1) + " - " + boats[i].getTeamName()); + } + + System.out.println(""); + } } } \ No newline at end of file