Reformatted and refactored the canvas controller

#fix #refactor #story[377]
This commit is contained in:
Haoming Yin
2017-03-20 17:23:33 +13:00
parent 3b8dd11758
commit ee34e5028f
3 changed files with 109 additions and 203 deletions
+2 -2
View File
@@ -81,7 +81,7 @@ public class OldApp {
// If race was created
if (race != null) {
race.displayStartingBoats();
//race.displayStartingBoats();
System.out.println("\n\n");
System.out.println("######################");
@@ -96,7 +96,7 @@ public class OldApp {
System.out.println("######################");
//race.showRaceMarkerResults();
race.displayFinishingOrder();
//race.displayFinishingOrder();
} else {
System.out.println("There was an error creating the race. Exiting.");
+17 -141
View File
@@ -1,46 +1,28 @@
package seng302.models;
import seng302.models.mark.*;
import seng302.models.mark.Mark;
import java.lang.reflect.Array;
import java.util.*;
/**
* Race class containing the boats and legs in the race
*/
* Race class containing the boats and legs in the race
* Created by mra106 on 8/3/2017.
*/
public class Race {
private ArrayList<Boat> boats; // The boats in the race
private ArrayList<Boat> finishingOrder; // The order in which the boats finish the race
private HashMap<Boat, List> events = new HashMap<>(); // The events that occur in the race
private List<Mark> course; // Marks in the race
private int numberOfBoats = 0;
private long startTime = 0;
private double timeScale = 1;
/**
* Race class containing the boats and legs in the race
*/
* Race class containing the boats and legs in the race
*/
public Race() {
this.boats = new ArrayList<Boat>();
this.finishingOrder = new ArrayList<Boat>();
this.course = new ArrayList<Mark>();
// create a priority queue with a custom Comparator to order events
// this.events = new PriorityQueue<Event>(new Comparator<Event>() {
// @Override
// public int compare(Event o1, Event o2) {
// Double time1 = o1.getTime();
// Double 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());
// }
// }
// });
this.boats = new ArrayList<>();
this.finishingOrder = new ArrayList<>();
this.course = new ArrayList<>();
}
/**
@@ -50,7 +32,6 @@ public class Race {
*/
public void addBoat(Boat boat) {
boats.add(boat);
numberOfBoats += 1;
}
/**
@@ -76,14 +57,6 @@ public class Race {
return this.finishingOrder.toArray(new Boat[this.finishingOrder.size()]);
}
/**
* Returns the number of boats in the race
*
* @returns the number of boats in the race
*/
public int getNumberOfBoats() {
return numberOfBoats;
}
/**
* Returns a list of boats in the race
@@ -95,38 +68,7 @@ public class Race {
}
/**
* Prints the order in which the boats finished the race
*/
public void displayFinishingOrder() {
int numberOfBoats = this.getNumberOfBoats();
Boat[] boats = this.getFinishedBoats();
System.out.println("--- Finishing Order ---");
for (int i = 0; i < Array.getLength(boats); i++) {
System.out.println("#" + Integer.toString(i + 1) + " - " + boats[i].getTeamName());
}
}
/**
* Prints the list of boats competing in the race
*/
public void displayStartingBoats() {
int numberOfBoats = this.getNumberOfBoats();
Boat[] boats = this.getBoats();
System.out.println("######################");
System.out.println("# Competing Boats ");
System.out.println("######################");
for (int i = 0; i < numberOfBoats; i++) {
String velocityKnots = String.format("%1.2f", boats[i].getVelocity() * 1.943844492);
System.out.println(boats[i].getTeamName() + " Velocity: " + velocityKnots + " Knots/s");
}
}
/**
* Sets time scale
* Sets time scale
*
* @param timeScale
*/
@@ -138,107 +80,41 @@ public class Race {
* Generate all events that will happen during the race.
*/
private void generateEvents() {
//calculate the time every boat passes each leg, and create an event
for (Boat boat : this.boats) {
double totalDistance = 0;
int numberOfMarks = this.course.size();
for(int i = 0; i < numberOfMarks; i++){
Double time = (Double) (1000 * totalDistance / boat.getVelocity());
for (int i = 0; i < numberOfMarks; i++) {
Double time = (1000 * totalDistance / boat.getVelocity());
// If there are singleMarks after this event
if (i < numberOfMarks-1) {
if (i < numberOfMarks - 1) {
Event event = new Event(time, boat, course.get(i), course.get(i + 1));
try {
events.get(boat).add(event);
} catch (NullPointerException e) {
events.put(boat, new ArrayList<Event>(Arrays.asList(event)));
events.put(boat, new ArrayList<>(Arrays.asList(event)));
}
totalDistance += event.getDistanceBetweenMarks();
}
// There are no more marks after this event
// else{
// Event event = new Event(time, boat, marks.get(i));
// events.put(boat, new ArrayList<Event>(Arrays.asList(event)));
// }
}
}
}
/**
* Calculates how far a boat has travelled in meters
*
* @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 = (long) (timeDiff / 1000 * this.timeScale);
return timeElapse * velocity;
}
/**
* Iterate over events in the race and print the
* event string for each event
*/
// public void iterateEvents() {
// // iterates all events. ends when no event in events.
//
// while (!events.isEmpty()) {
// Event peekEvent = events.peek();
// long currentTime = (long) ((System.currentTimeMillis() - this.startTime) * this.timeScale);
//
// if (currentTime > peekEvent.getTime()) {
// Event nextEvent = events.poll();
//
// // Display a summary of the event
// System.out.println(nextEvent.getEventString());
//
// // Display latitude and longitude
// if (!nextEvent.getIsFinishingEvent()){
// System.out.println(nextEvent.getMark().getLatitude() + ", " + nextEvent.getNextMark().getLongitude());
// }
//
// System.out.println();
//
// // If event is a boat finishing the race
// if (nextEvent.getIsFinishingEvent()) {
// this.finishingOrder.add(nextEvent.getBoat());
// }
// }
//
// // Wait for 100ms to throttle the while loop
// try {
// Thread.sleep(100);
// } catch (java.lang.InterruptedException e) {
// continue;
// }
// }
// }
/**
* Start the race and print each marker with the order
* in which the boats passed that marker
* Starts a race and generates all events for the race.
*/
public void startRace() {
// record start time.
this.startTime = System.currentTimeMillis();
generateEvents();
// this.startTime = System.currentTimeMillis();
// iterateEvents();
}
//
// /**
// * Add a singleMark to the race (in order)
// * @param singleMark, the singleMark to add
// */
// public void addMark(SingleMark singleMark){
// this.marks.add(singleMark);
// }
public void addCourse(List<Mark> course){
public void addCourse(List<Mark> course) {
this.course = course;
}