Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Created October 16, 2014 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfarcic/ab58c990ce00c3a13709 to your computer and use it in GitHub Desktop.
Save vfarcic/ab58c990ce00c3a13709 to your computer and use it in GitHub Desktop.
Mars Rover kata solution
package com.technologyconversations.kata.marsrover;
/*
Method receiveCommands should be used to transmit commands to the rover.
*/
public class Rover {
private Coordinates coordinates;
public void setCoordinates(Coordinates value) {
coordinates = value;
}
public Coordinates getCoordinates() {
return coordinates;
}
public Rover(Coordinates coordinatesValue) {
setCoordinates(coordinatesValue);
}
public void receiveCommands(String commands) throws Exception {
for (char command : commands.toCharArray()) {
if (!receiveSingleCommand(command)) {
break;
}
}
}
public boolean receiveSingleCommand(char command) throws Exception {
switch(Character.toUpperCase(command)) {
case 'F':
return getCoordinates().moveForward();
case 'B':
return getCoordinates().moveBackward();
case 'L':
getCoordinates().changeDirectionLeft();
return true;
case 'R':
getCoordinates().changeDirectionRight();
return true;
default:
throw new Exception("Command " + command + " is unknown.");
}
}
public String getPosition() {
return getCoordinates().toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment