From 6ce9674e64cc273af2b33baa608f9bb2c5049d8b Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 7 Mar 2017 00:25:10 +1300 Subject: [PATCH 1/5] Added method to calculate distance travelled given velocity - added getDistanceTravelled(velocity) to calculate how far a boat has travelled - added startTime for calculating time elapse between start point and enquiry point #story[6] --- src/main/java/seng302/App.java | 1 - src/main/java/seng302/Race.java | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 39c0a0c5..4128889c 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -15,7 +15,6 @@ public class App for (Map team : teams) { boatNames.add((String) team.get("team-name")); } - System.out.println(boatNames.toString()); // Shuffle team names long seed = System.nanoTime(); diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index be4ebacc..df876d9f 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -10,6 +10,8 @@ public class Race { private ArrayList boats; private ArrayList legs; private int numberOfBoats = 0; + private long startTime = 0; + private int timeScale = 1; public Race(){ boats = new ArrayList(); @@ -103,11 +105,33 @@ public class Race { this.legs.add(leg); } + /** + * Gets legs array + * @return an array of legs + */ + public ArrayList getLegs() { + return this.legs; + } + + /** + * Calculates how far a boat has travelled in meter + * @param velocity the velocity of boat + * @return a float number of distance the boat has been travelled + */ + public float getDistanceTravelled(long velocity) { + long timeDiff = System.currentTimeMillis() - this.startTime; + long timeElapse = timeDiff / 1000 * this.timeScale; + return timeElapse * velocity; + } + /* Start the race and print each marker with the order in which the boats passed that marker */ public void startRace(){ + // record start time. + this.startTime = System.currentTimeMillis(); + for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ Boat[] boats = this.getShuffledBoats(); From 5c362245a43793bbb1c786f6d28df6d3d2952ee6 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 7 Mar 2017 19:34:37 +1300 Subject: [PATCH 2/5] Added velocity related methods into Boat class. - added setVelocity() and getVelocity() #story[6] --- src/main/java/seng302/Boat.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/seng302/Boat.java b/src/main/java/seng302/Boat.java index 670aa04b..2205f025 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 float velocity = 0; public Boat(String teamName) { this.teamName = teamName; @@ -29,4 +30,20 @@ public class Boat public void setTeamName(String teamName){ this.teamName = teamName; } + + /** + * Sets velocity of the boat + * @param velocity The velocity of boat + */ + public void setVelocity(float velocity) { + this.velocity = velocity; + } + + /** + * Gets velocity of the boat + * @return a float number of the boat velocity + */ + public float getVelocity() { + return this.velocity; + } } \ No newline at end of file From a3c1630e2e28f81a8f8ec53deec225505624efd8 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 7 Mar 2017 20:28:45 +1300 Subject: [PATCH 3/5] Updated boat class constructor - add a new constructor to accept boat velocity #story[6] --- src/main/java/seng302/Boat.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/seng302/Boat.java b/src/main/java/seng302/Boat.java index 2205f025..c52223c0 100644 --- a/src/main/java/seng302/Boat.java +++ b/src/main/java/seng302/Boat.java @@ -14,6 +14,10 @@ public class Boat public Boat(String teamName) { this.teamName = teamName; } + public Boat(String teamName, float boatVelocity) { + this.teamName = teamName; + this.velocity = boatVelocity; + } /* Returns the name of the team sailing the boat From 17fbb1212d844522aeadef06a7fea991aa8d9ff2 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 7 Mar 2017 20:30:23 +1300 Subject: [PATCH 4/5] Added Event class and junit test - event class contains the time of specific event, related team/boat and even location eg. leg. #implement #story[5] --- src/main/java/seng302/Event.java | 77 ++++++++++++++++++++++++++++ src/test/java/seng302/EventTest.java | 21 ++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/seng302/Event.java create mode 100644 src/test/java/seng302/EventTest.java diff --git a/src/main/java/seng302/Event.java b/src/main/java/seng302/Event.java new file mode 100644 index 00000000..5a36fad1 --- /dev/null +++ b/src/main/java/seng302/Event.java @@ -0,0 +1,77 @@ +package seng302; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Event class containing the time of specific event, related team/boat, and + * event location such as leg. + */ +public class Event { + + private long time; + private Boat boat; + private Leg leg; + + public Event(long eventTime, Boat eventBoat, Leg eventLeg) { + this.time = eventTime; + this.boat = eventBoat; + this.leg = eventLeg; + } + + /** + * Sets the time for the event + * @param eventTime the time for event in millisecond + */ + public void setTime(long eventTime) { + this.time = eventTime; + } + + /** + * Gets the time for the event + * @return the time for event in millisecond + */ + public long getTime() { + return this.time; + } + + /** + * Gets the time in a formatted string + * @return the string of time + */ + public String getTimeString() { + return (new SimpleDateFormat("mm:ss:SSS")).format(new Date(time)); + } + + /** + * Sets the involved boat + * @param eventBoat the involved boat + */ + public void setBoat(Boat eventBoat) { + this.boat = eventBoat; + } + + /** + * Gets the involved boat + * @return the boat involved in the event + */ + public Boat getBoat() { + return this.boat; + } + + /** + * Sets the involved location/leg + * @param eventLeg the involved leg + */ + public void setLeg(Leg eventLeg) { + this.leg = eventLeg; + } + + /** + * Gets the involved location/leg + * @return the leg involved in the event + */ + public Leg getLeg() { + return this.leg; + } +} diff --git a/src/test/java/seng302/EventTest.java b/src/test/java/seng302/EventTest.java new file mode 100644 index 00000000..3f66ad52 --- /dev/null +++ b/src/test/java/seng302/EventTest.java @@ -0,0 +1,21 @@ +package seng302; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Test for Event class + * Created by Haoming on 7/03/17. + */ +public class EventTest { + + @Test + public void getTimeString() throws Exception { + Leg leg = new Leg(035, 100, "Start"); + Boat boat = new Boat("testBoat"); + Event event = new Event(1231242, boat, leg); + assertEquals("20:31:242", event.getTimeString()); + } +} \ No newline at end of file From 78dea7daacf73505eef21832590c8201dee23da9 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 7 Mar 2017 21:30:48 +1300 Subject: [PATCH 5/5] Added function to generate events for race - added generateEvents() for creating event into a priority queue - added iterateEvents() to pull out events when time passing - changed default velocity to 70 just for testing. Pls change it back after testing #story[5] #implement --- src/main/java/seng302/Boat.java | 2 +- src/main/java/seng302/Race.java | 98 +++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 23 deletions(-) diff --git a/src/main/java/seng302/Boat.java b/src/main/java/seng302/Boat.java index c52223c0..8e3aec2a 100644 --- a/src/main/java/seng302/Boat.java +++ b/src/main/java/seng302/Boat.java @@ -9,7 +9,7 @@ public class Boat { // The name of the team, this is also the name of the boat private String teamName = null; - private float velocity = 0; + private float velocity = 70; // please set this one to a reasonable num!!!!!, i set it just for testing ;) public Boat(String teamName) { this.teamName = teamName; diff --git a/src/main/java/seng302/Race.java b/src/main/java/seng302/Race.java index df876d9f..e1af5351 100644 --- a/src/main/java/seng302/Race.java +++ b/src/main/java/seng302/Race.java @@ -1,28 +1,41 @@ package seng302; -import java.util.ArrayList; -import java.util.Random; -import java.util.Collections; -import java.util.List; +import java.util.*; public class Race { private ArrayList boats; private ArrayList legs; + private PriorityQueue events; private int numberOfBoats = 0; private long startTime = 0; private int timeScale = 1; - public Race(){ - boats = new ArrayList(); - legs = new ArrayList(); + public Race() { + this.boats = new ArrayList(); + this.legs = new ArrayList(); + // create a priority queue within custom Comparator to order events + this.events = new PriorityQueue(new Comparator() { + @Override + public int compare(Event o1, Event o2) { + Long time1 = o1.getTime(); + Long time2 = o2.getTime(); + // order event asc. by time. if tie appears, then order team + // name alphabetically. + if (time1 != time2) { + return time1.compareTo(time2); + } else { + return o1.getBoat().getTeamName().compareTo(o2.getBoat().getTeamName()); + } + } + }); } /* Add a boat to the race @param boat, the boat to add */ - public void addBoat(Boat boat){ + public void addBoat(Boat boat) { boats.add(boat); numberOfBoats += 1; } @@ -32,7 +45,7 @@ public class Race { @returns a list of boats */ - public Boat[] getShuffledBoats(){ + public Boat[] getShuffledBoats() { // Shuffle the list of boats long seed = System.nanoTime(); Collections.shuffle(this.boats, new Random(seed)); @@ -46,7 +59,7 @@ public class Race { @returns a list of boats */ - public Boat[] getFinishedBoats(){ + public Boat[] getFinishedBoats() { return getShuffledBoats(); } @@ -55,7 +68,7 @@ public class Race { @returns the number of boats in the race */ - public int getNumberOfBoats(){ + public int getNumberOfBoats() { return numberOfBoats; } @@ -64,28 +77,28 @@ public class Race { @returns a list of the boats competing in the race */ - public Boat[] getBoats(){ + public Boat[] getBoats() { return boats.toArray(new Boat[boats.size()]); } /* Prints the order in which the boats finished - */ - public void displayFinishingOrder(){ + */ + public void displayFinishingOrder() { int numberOfBoats = this.getNumberOfBoats(); Boat[] boats = this.getFinishedBoats(); System.out.println("--- Finishing Order ---"); for (int i = 0; i < numberOfBoats; i++) { - System.out.println("#" + Integer.toString(i+1) + " - " + boats[i].getTeamName()); + System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName()); } } /* Prints the list of boats competing in the race - */ - private void displayStartingBoats(){ + */ + private void displayStartingBoats() { int numberOfBoats = this.getNumberOfBoats(); Boat[] boats = this.getBoats(); @@ -101,12 +114,13 @@ public class Race { @param leg, the leg to add to the race */ - public void addLeg(Leg leg){ + public void addLeg(Leg leg) { this.legs.add(leg); } /** * Gets legs array + * * @return an array of legs */ public ArrayList getLegs() { @@ -114,7 +128,26 @@ public class Race { } /** + * Temporary method used to generated all the events. + */ + private void generateEvents() { + + //calculate the time for every boat passes each leg, and create an event + for (Boat boat : this.boats) { + long totalDistance = 0; + for (Leg leg : this.legs) { + totalDistance += leg.getDistance(); + long time = (long) (1000 * totalDistance / (boat.getVelocity() * this.timeScale)); + Event event = new Event(time, boat, leg); + events.add(event); + } + } + } + + /** + * Note: this function is useless so far * Calculates how far a boat has travelled in meter + * * @param velocity the velocity of boat * @return a float number of distance the boat has been travelled */ @@ -124,25 +157,46 @@ public class Race { return timeElapse * velocity; } + /** + * Micheal, here is a demo function shows you how to iterate all events + */ + public void iterateEvents() { + // iterates all events. ends when no event in events. + while (!events.isEmpty()) { + Event peekEvent = events.peek(); + long currentTime = System.currentTimeMillis() - this.startTime; + if (currentTime > peekEvent.getTime()) { + // pull out the event + Event nextEvent = events.poll(); + // I just simply print it out for testing + System.out.println(nextEvent.getTimeString() + ", " + + nextEvent.getBoat().getTeamName() + " passed " + + nextEvent.getLeg().getMarkerLabel()); + } + } + } + /* Start the race and print each marker with the order in which the boats passed that marker */ - public void startRace(){ + public void startRace() { // record start time. + generateEvents(); this.startTime = System.currentTimeMillis(); + iterateEvents(); - for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ + for (Leg leg : this.legs) { 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("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName()); } System.out.println(""); - } + } } } \ No newline at end of file