mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
@@ -27,6 +27,12 @@ public class App
|
||||
race.addBoat(new Boat(boatNames.get(i)));
|
||||
}
|
||||
|
||||
race.addLeg(new Leg(035, 100, "Start"));
|
||||
race.addLeg(new Leg(010, 300, "Marker 1"));
|
||||
race.addLeg(new Leg(350, 400, "Leeward Gate"));
|
||||
race.addLeg(new Leg(010, 400, "Windward Gate"));
|
||||
race.addLeg(new Leg(010, 400, "Leeward Gate"));
|
||||
|
||||
return race;
|
||||
}
|
||||
|
||||
@@ -43,11 +49,11 @@ public class App
|
||||
}
|
||||
|
||||
if (race != null){
|
||||
race.displayStartingBoats();
|
||||
|
||||
System.out.println("");
|
||||
race.startRace();
|
||||
|
||||
race.displayFinishingOrder();
|
||||
|
||||
|
||||
}
|
||||
else{
|
||||
System.out.println("e");
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package seng302;
|
||||
|
||||
public class Leg {
|
||||
private int heading;
|
||||
private int distance;
|
||||
private boolean isFinishingLeg;
|
||||
private Marker startingMarker;
|
||||
|
||||
/*
|
||||
Create a new leg
|
||||
|
||||
@param heading, the magnetic heading of this leg
|
||||
@param distance, the total distance of this leg in meters
|
||||
@param marker, the marker this leg starts on
|
||||
*/
|
||||
public Leg(int heading, int distance, Marker marker){
|
||||
this.heading = heading;
|
||||
this.distance = distance;
|
||||
this.startingMarker = marker;
|
||||
this.isFinishingLeg = false;
|
||||
}
|
||||
|
||||
/*
|
||||
Create a new leg
|
||||
|
||||
@param heading, the magnetic heading of this leg
|
||||
@param distance, the total distance of this leg in meters
|
||||
@param markerName, the name of the marker this leg starts on
|
||||
*/
|
||||
public Leg(int heading, int distance, String markerName){
|
||||
this.heading = heading;
|
||||
this.distance = distance;
|
||||
this.startingMarker = new Marker(markerName);
|
||||
this.isFinishingLeg = false;
|
||||
}
|
||||
|
||||
/*
|
||||
Set the heading for this leg
|
||||
*/
|
||||
public void setHeading(int heading){
|
||||
this.heading = heading;
|
||||
}
|
||||
|
||||
/*
|
||||
Get the heading of this leg
|
||||
*/
|
||||
public int getHeading(){
|
||||
return this.heading;
|
||||
}
|
||||
|
||||
/*
|
||||
Set the distance of this leg in meters
|
||||
*/
|
||||
public void setDistance(int distance){
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
/*
|
||||
Get the total distance of this leg in meters
|
||||
*/
|
||||
public int getDistance(){
|
||||
return this.distance;
|
||||
}
|
||||
|
||||
/*
|
||||
Set the marker this leg starts on
|
||||
*/
|
||||
public void setMarker(Marker marker){
|
||||
this.startingMarker = marker;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the marker this leg started on
|
||||
*/
|
||||
public Marker getMarker(){
|
||||
return this.startingMarker;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns the name of the marker this leg started on
|
||||
*/
|
||||
public String getMarkerLabel(){
|
||||
return this.startingMarker.getName();
|
||||
}
|
||||
|
||||
/*
|
||||
Specify whether or not the race finishes on this leg
|
||||
|
||||
@param isFinishingLeg whether or not the race finishes on this leg
|
||||
*/
|
||||
public void setFinishingLeg(boolean isFinishingLeg){
|
||||
this.isFinishingLeg = isFinishingLeg;
|
||||
}
|
||||
|
||||
/*
|
||||
@returns true if this the race finishes after this leg
|
||||
*/
|
||||
public boolean getIsFinishingLeg(){
|
||||
return this.isFinishingLeg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package seng302;
|
||||
|
||||
class Marker{
|
||||
private String name;
|
||||
|
||||
public Marker(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,12 @@ import java.util.List;
|
||||
|
||||
public class Race {
|
||||
private ArrayList<Boat> boats;
|
||||
private ArrayList<Leg> legs;
|
||||
private int numberOfBoats = 0;
|
||||
|
||||
public Race(){
|
||||
boats = new ArrayList<Boat>();
|
||||
legs = new ArrayList<Leg>();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -23,6 +25,19 @@ public class Race {
|
||||
numberOfBoats += 1;
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a list of boats in a random order
|
||||
|
||||
@returns a list of boats
|
||||
*/
|
||||
public Boat[] getShuffledBoats(){
|
||||
// Shuffle the list of boats
|
||||
long seed = System.nanoTime();
|
||||
Collections.shuffle(this.boats, new Random(seed));
|
||||
|
||||
return boats.toArray(new Boat[boats.size()]);
|
||||
}
|
||||
|
||||
/*
|
||||
Returns a list of boats in the order that they
|
||||
finished the race (position 0 is first place)
|
||||
@@ -30,11 +45,7 @@ public class Race {
|
||||
@returns a list of boats
|
||||
*/
|
||||
public Boat[] getFinishedBoats(){
|
||||
// Shuffle the list of boats
|
||||
long seed = System.nanoTime();
|
||||
Collections.shuffle(this.boats, new Random(seed));
|
||||
|
||||
return boats.toArray(new Boat[boats.size()]);
|
||||
return getShuffledBoats();
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -71,16 +82,43 @@ public class Race {
|
||||
|
||||
/*
|
||||
Prints the list of boats competing in the race
|
||||
|
||||
*/
|
||||
public void displayStartingBoats(){
|
||||
private void displayStartingBoats(){
|
||||
int numberOfBoats = this.getNumberOfBoats();
|
||||
Boat[] boats = this.getBoats();
|
||||
|
||||
System.out.println("--- Competing Boats ---");
|
||||
System.out.println("--- Starting Boats ---");
|
||||
|
||||
for (int i = 0; i < numberOfBoats; i++) {
|
||||
System.out.println(boats[i].getTeamName());
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Adds a leg to the race
|
||||
|
||||
@param leg, the leg to add to the race
|
||||
*/
|
||||
public void addLeg(Leg leg){
|
||||
this.legs.add(leg);
|
||||
}
|
||||
|
||||
/*
|
||||
Start the race and print each marker with the order
|
||||
in which the boats passed that marker
|
||||
*/
|
||||
public void startRace(){
|
||||
for (Leg leg : this.legs.toArray(new Leg[legs.size()])){
|
||||
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("");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package seng302;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Unit test for the Leg class.
|
||||
*/
|
||||
public class LegTest{
|
||||
|
||||
/*
|
||||
Test creation of the leg by specifying a string
|
||||
for the marker label
|
||||
*/
|
||||
@Test
|
||||
public void testLegCreationUsingMarkerLabel()
|
||||
{
|
||||
Leg leg = new Leg(010, 100, "Marker");
|
||||
|
||||
assertEquals(leg.getHeading(), 010);
|
||||
assertEquals(leg.getDistance(), 100);
|
||||
assertEquals(leg.getMarkerLabel(), "Marker");
|
||||
assertEquals(leg.getIsFinishingLeg(), false);
|
||||
}
|
||||
|
||||
/*
|
||||
Test creation of the leg by providing a
|
||||
Marker object
|
||||
*/
|
||||
@Test
|
||||
public void testLegCreation()
|
||||
{
|
||||
Leg leg = new Leg(010, 100, new Marker("Marker"));
|
||||
|
||||
assertEquals(leg.getHeading(), 010);
|
||||
assertEquals(leg.getDistance(), 100);
|
||||
assertEquals(leg.getMarkerLabel(), "Marker");
|
||||
assertEquals(leg.getIsFinishingLeg(), false);
|
||||
}
|
||||
|
||||
/*
|
||||
Test changing whether or not a
|
||||
leg is the finishing leg
|
||||
*/
|
||||
@Test
|
||||
public void testSetFinishLeg()
|
||||
{
|
||||
Leg leg = new Leg(010, 100, "Marker");
|
||||
|
||||
leg.setFinishingLeg(true);
|
||||
assertEquals(leg.getIsFinishingLeg(), true);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user