From 33994bd3e4755566e61e70ce808d19b05f51d3eb Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Fri, 3 Mar 2017 18:06:51 +1300 Subject: [PATCH 1/3] Implemented 'Race' class - Boats can be added to a race - calling getFinishedBoats() will return a list of boats in the order that they finished --- src/main/java/seng302/Boat.java | 1 + src/main/java/seng302/Race.java | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/seng302/Race.java diff --git a/src/main/java/seng302/Boat.java b/src/main/java/seng302/Boat.java index 670aa04b..3361e965 100644 --- a/src/main/java/seng302/Boat.java +++ b/src/main/java/seng302/Boat.java @@ -9,6 +9,7 @@ public class Boat { // The name of the team, this is also the name of the boat private String teamName = null; + private boolean finishedRace = false; public Boat(String teamName) { this.teamName = teamName; diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java new file mode 100644 index 00000000..06fb7901 --- /dev/null +++ b/src/main/java/seng302/Race.java @@ -0,0 +1,37 @@ +package seng302; + +import java.util.ArrayList; +import java.util.Random; +import java.util.Collections; +import java.util.List; + + +public class Race { + private ArrayList boats; + + public Race(){ + boats = new ArrayList(); + } + + /* + Add a boat to the race + @param boat the boat to add + */ + public void addBoat(Boat boat){ + boats.add(boat); + } + + /* + Returns a list of boats in the order that they + finished the race (0 is first) + + @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()]); + } +} \ No newline at end of file From 4b2141190dfed718ef0a084ad0852b12eec49b7e Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Fri, 3 Mar 2017 18:23:24 +1300 Subject: [PATCH 2/3] Added tests for the Race class #test --- src/test/java/seng302/RaceTest.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/test/java/seng302/RaceTest.java diff --git a/src/test/java/seng302/RaceTest.java b/src/test/java/seng302/RaceTest.java new file mode 100644 index 00000000..f5218633 --- /dev/null +++ b/src/test/java/seng302/RaceTest.java @@ -0,0 +1,27 @@ +package seng302; + +import org.junit.Test; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import java.lang.reflect.Array; + +/** + * Unit test for the Race class. + */ +public class RaceTest +{ + @Test + public void testAddingBoatsToRace() + { + Boat boat1 = new Boat("Team 1"); + Boat boat2 = new Boat("Team 2"); + Boat boat3 = new Boat("Team 3"); + + Race race = new Race(); + race.addBoat(boat1); + race.addBoat(boat2); + race.addBoat(boat3); + + assertEquals(Array.getLength(race.getFinishedBoats()), 3); + } +} From 6702d24fd579ac5ef01ca2cb745164f7302eaf30 Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Fri, 3 Mar 2017 18:39:15 +1300 Subject: [PATCH 3/3] Display the order the boats finished in - Added a function in the App class to display the order in which the boats finished the race #implement --- src/main/java/seng302/App.java | 25 ++++++++++++++++++++++++- src/main/java/seng302/Race.java | 15 +++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 5af4e232..9c5f2b15 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -1,9 +1,32 @@ package seng302; +import java.util.ArrayList; + public class App { + /* + Displays the order in which the boats finished + + @param race The current race + */ + public static void displayFinishingOrder(Race race){ + int numberOfBoats = race.getNumberOfBoats(); + Boat[] boats = race.getFinishedBoats(); + + System.out.println("--- Finishing Order ---"); + + for (int i = 0; i < numberOfBoats; i++) { + System.out.println("#" + Integer.toString(i+1) + " - " + boats[i].getTeamName()); + } + } + + + + public static void main( String[] args ) { - System.out.println(""); + Race race = new Race(); + + displayFinishingOrder(race); } } \ No newline at end of file diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index 06fb7901..32ab5c56 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -8,6 +8,7 @@ import java.util.List; public class Race { private ArrayList boats; + private int numberOfBoats = 0; public Race(){ boats = new ArrayList(); @@ -15,15 +16,16 @@ public class Race { /* Add a boat to the race - @param boat the boat to add + @param boat, the boat to add */ public void addBoat(Boat boat){ boats.add(boat); + numberOfBoats += 1; } /* Returns a list of boats in the order that they - finished the race (0 is first) + finished the race (position 0 is first) @returns a list of boats */ @@ -34,4 +36,13 @@ public class Race { return boats.toArray(new Boat[boats.size()]); } + + /* + Returns the number of boats in the race + + @returns the number of boats in the race + */ + public int getNumberOfBoats(){ + return numberOfBoats; + } } \ No newline at end of file