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]
This commit is contained in:
Haoming Yin
2017-03-07 00:25:10 +13:00
parent da896cddfd
commit 6ce9674e64
2 changed files with 24 additions and 1 deletions
-1
View File
@@ -15,7 +15,6 @@ public class App
for (Map<String, Object> team : teams) {
boatNames.add((String) team.get("team-name"));
}
System.out.println(boatNames.toString());
// Shuffle team names
long seed = System.nanoTime();
+24
View File
@@ -10,6 +10,8 @@ public class Race {
private ArrayList<Boat> boats;
private ArrayList<Leg> legs;
private int numberOfBoats = 0;
private long startTime = 0;
private int timeScale = 1;
public Race(){
boats = new ArrayList<Boat>();
@@ -103,11 +105,33 @@ public class Race {
this.legs.add(leg);
}
/**
* Gets legs array
* @return an array of legs
*/
public ArrayList<Leg> 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();