Refactored BoatinRace to observable list

- Changed starting Boats into ObservableList from array
- stopped checkPosition in Race abstract class from updating the table as everytime as it is checked by a listener
- Changed array methods .length to .size() and [i] to .get() to reflect the type change.
#refactor #story[9]
main
Fan-Wu Yang 9 years ago
parent 192178760e
commit 91861eb5cb

@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/team-7.iml" filepath="$PROJECT_DIR$/team-7.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/team-7.iml" filepath="$PROJECT_DIR$/.idea/team-7.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/team-7.iml" filepath="$PROJECT_DIR$/.idea/team-7.iml" />
</modules> </modules>
</component> </component>

@ -4,6 +4,7 @@ package seng302.Controllers;
import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue; import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn;
@ -47,15 +48,16 @@ public class RaceController extends Controller{
private RaceMap map; private RaceMap map;
public void updateMap(BoatInRace[] boats) { public void updateMap(ObservableList<BoatInRace> boats) {
raceMap.setBoats(boats); BoatInRace[] boatInRaces = new BoatInRace[boats.size()];
raceMap.setBoats(boats.toArray(boatInRaces));
raceMap.drawRaceMap(); raceMap.drawRaceMap();
} }
public void updateInfoTable(Race race) { public void updateInfoTable(Race race) {
boatInfoTable.getItems().clear(); boatInfoTable.getItems().clear();
boatInfoTable.setItems(FXCollections.observableArrayList(race.getStartingBoats())); boatInfoTable.setItems(race.getStartingBoats());
boatTeamColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace,String>("Name")); boatTeamColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace,String>("Name"));
boatMarkColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace, String>("CurrentLeg")); boatMarkColumn.setCellValueFactory(new PropertyValueFactory<BoatInRace, String>("CurrentLeg"));

@ -1,6 +1,9 @@
package seng302.Model; package seng302.Model;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import seng302.Controllers.RaceController; import seng302.Controllers.RaceController;
import seng302.GPSCoordinate; import seng302.GPSCoordinate;
@ -11,7 +14,8 @@ import java.util.*;
* Created by fwy13 on 3/03/17. * Created by fwy13 on 3/03/17.
*/ */
public abstract class Race implements Runnable { public abstract class Race implements Runnable {
protected BoatInRace[] startingBoats; //protected BoatInRace[] startingBoats;
protected ObservableList<BoatInRace> startingBoats;
protected ArrayList<Leg> legs; protected ArrayList<Leg> legs;
protected RaceController controller; protected RaceController controller;
protected int boatsFinished = 0; protected int boatsFinished = 0;
@ -25,7 +29,7 @@ public abstract class Race implements Runnable {
* @param legs Number of marks in order that the boats pass in order to complete the race. * @param legs Number of marks in order that the boats pass in order to complete the race.
*/ */
public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller) { public Race(BoatInRace[] boats, ArrayList<Leg> legs, RaceController controller) {
this.startingBoats = boats; this.startingBoats = FXCollections.observableArrayList(boats);
this.legs = legs; this.legs = legs;
this.legs.add(new Leg("Finish")); this.legs.add(new Leg("Finish"));
this.controller = controller; this.controller = controller;
@ -45,9 +49,9 @@ public abstract class Race implements Runnable {
//show the boats participating. //show the boats participating.
System.out.println("Boats Participating:"); System.out.println("Boats Participating:");
System.out.println("===================="); System.out.println("====================");
for (int i = 0; i < startingBoats.length; i++) { for (int i = 0; i < startingBoats.size(); i++) {
System.out.println(i + 1 + ". " + startingBoats[i].getName() + ", Speed: " + Math.round(startingBoats[i].getVelocity() * 1.94384) + "kn"); System.out.println(i + 1 + ". " + startingBoats.get(i).getName() + ", Speed: " + Math.round(startingBoats.get(i).getVelocity() * 1.94384) + "kn");
startingBoats[i].setCurrentLeg(legs.get(0)); startingBoats.get(i).setCurrentLeg(legs.get(0));
} }
} }
@ -68,7 +72,7 @@ public abstract class Race implements Runnable {
long remainingSeconds; long remainingSeconds;
while (boatsFinished < startingBoats.length) { while (boatsFinished < startingBoats.size()) {
timeLoopStarted = System.currentTimeMillis(); timeLoopStarted = System.currentTimeMillis();
totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted;
long currentTime = System.currentTimeMillis() - timeRaceStarted; long currentTime = System.currentTimeMillis() - timeRaceStarted;
@ -95,7 +99,7 @@ public abstract class Race implements Runnable {
protected void checkPosition(BoatInRace boat, long timeElapsed) { protected void checkPosition(BoatInRace boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){ if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()){
updateController(); //updateController();
//boat has passed onto new leg //boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) { if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished //boat has finished
@ -115,7 +119,7 @@ public abstract class Race implements Runnable {
} }
public BoatInRace[] getStartingBoats() { public ObservableList<BoatInRace> getStartingBoats() {
return startingBoats; return startingBoats;
} }

Loading…
Cancel
Save