Merge branch 'calculate-time' into 'master'

Calculate time



See merge request !10
This commit is contained in:
Haoming Yin
2017-03-07 21:33:12 +13:00
5 changed files with 219 additions and 23 deletions
-1
View File
@@ -15,7 +15,6 @@ public class App
for (Map<String, Object> team : teams) { for (Map<String, Object> team : teams) {
boatNames.add((String) team.get("team-name")); boatNames.add((String) team.get("team-name"));
} }
System.out.println(boatNames.toString());
// Shuffle team names // Shuffle team names
long seed = System.nanoTime(); long seed = System.nanoTime();
+21
View File
@@ -9,10 +9,15 @@ public class Boat
{ {
// The name of the team, this is also the name of the boat // The name of the team, this is also the name of the boat
private String teamName = null; private String teamName = null;
private float velocity = 70; // please set this one to a reasonable num!!!!!, i set it just for testing ;)
public Boat(String teamName) { public Boat(String teamName) {
this.teamName = 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 Returns the name of the team sailing the boat
@@ -29,4 +34,20 @@ public class Boat
public void setTeamName(String teamName){ public void setTeamName(String teamName){
this.teamName = 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;
}
} }
+77
View File
@@ -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;
}
}
+85 -7
View File
@@ -1,19 +1,34 @@
package seng302; package seng302;
import java.util.ArrayList; import java.util.*;
import java.util.Random;
import java.util.Collections;
import java.util.List;
public class Race { public class Race {
private ArrayList<Boat> boats; private ArrayList<Boat> boats;
private ArrayList<Leg> legs; private ArrayList<Leg> legs;
private PriorityQueue<Event> events;
private int numberOfBoats = 0; private int numberOfBoats = 0;
private long startTime = 0;
private int timeScale = 1;
public Race() { public Race() {
boats = new ArrayList<Boat>(); this.boats = new ArrayList<Boat>();
legs = new ArrayList<Leg>(); this.legs = new ArrayList<Leg>();
// create a priority queue within custom Comparator to order events
this.events = new PriorityQueue<Event>(new Comparator<Event>() {
@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());
}
}
});
} }
/* /*
@@ -103,12 +118,75 @@ public class Race {
this.legs.add(leg); this.legs.add(leg);
} }
/**
* Gets legs array
*
* @return an array of legs
*/
public ArrayList<Leg> getLegs() {
return this.legs;
}
/**
* 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
*/
public float getDistanceTravelled(long velocity) {
long timeDiff = System.currentTimeMillis() - this.startTime;
long timeElapse = timeDiff / 1000 * this.timeScale;
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 Start the race and print each marker with the order
in which the boats passed that marker in which the boats passed that marker
*/ */
public void startRace() { public void startRace() {
for (Leg leg : this.legs.toArray(new Leg[legs.size()])){ // record start time.
generateEvents();
this.startTime = System.currentTimeMillis();
iterateEvents();
for (Leg leg : this.legs) {
Boat[] boats = this.getShuffledBoats(); Boat[] boats = this.getShuffledBoats();
System.out.println("--- " + leg.getMarkerLabel() + " ---"); System.out.println("--- " + leg.getMarkerLabel() + " ---");
+21
View File
@@ -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());
}
}