Command pattern implemented for all current features
This commit is contained in:
parent
f80460054d
commit
ff548d0338
|
@ -9,7 +9,7 @@ import java.util.logging.Logger;
|
||||||
public class MarsRover {
|
public class MarsRover {
|
||||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||||
|
|
||||||
private enum Rotation {LEFT, RIGHT}
|
enum Rotation {LEFT, RIGHT}
|
||||||
private final MarsMap marsMap;
|
private final MarsMap marsMap;
|
||||||
private Direction currentDirection;
|
private Direction currentDirection;
|
||||||
|
|
||||||
|
@ -22,25 +22,18 @@ public class MarsRover {
|
||||||
marsMap.updatePositionTowards(direction);
|
marsMap.updatePositionTowards(direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates moveBackwards() {
|
public void rotateTowards(Rotation rotation) {
|
||||||
return marsMap.updatePositionTowards(currentDirection.reversed());
|
var newDirection = switch (rotation) {
|
||||||
|
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
||||||
|
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
|
||||||
|
};
|
||||||
|
reportNewDirection(rotation, newDirection);
|
||||||
|
currentDirection = newDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Direction turnLeft() {
|
private void reportNewDirection(Rotation rotation, Direction newDirection) {
|
||||||
currentDirection = currentDirection.getNextDirectionRotatingToLeft();
|
|
||||||
reportNewDirection(Rotation.LEFT);
|
|
||||||
return currentDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Direction turnRight() {
|
|
||||||
currentDirection = currentDirection.getNextDirectionRotatingToRight();
|
|
||||||
reportNewDirection(Rotation.RIGHT);
|
|
||||||
return currentDirection;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reportNewDirection(Rotation rotation) {
|
|
||||||
logger.info(String.format("Rotated towards %s, now the direction is %s",
|
logger.info(String.format("Rotated towards %s, now the direction is %s",
|
||||||
rotation, currentDirection));
|
rotation, newDirection));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates getCurrentCoordinates() {
|
public Coordinates getCurrentCoordinates() {
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public class TurnRightCommand implements MarsRoverCommand{
|
||||||
|
private final MarsRover marsRover;
|
||||||
|
|
||||||
|
public TurnRightCommand(MarsRover marsRover) {
|
||||||
|
this.marsRover = marsRover;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
marsRover.rotateTowards(MarsRover.Rotation.RIGHT);
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ public enum Direction {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Direction getNextDirectionRotatingToLeft() {
|
public Direction getNextDirectionRotatingMinus90Degrees() {
|
||||||
return switch (this) {
|
return switch (this) {
|
||||||
case NORTH -> WEST;
|
case NORTH -> WEST;
|
||||||
case SOUTH -> EAST;
|
case SOUTH -> EAST;
|
||||||
|
@ -21,7 +21,7 @@ public enum Direction {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public Direction getNextDirectionRotatingToRight() {
|
public Direction getNextDirectionRotating90Degrees() {
|
||||||
return switch (this) {
|
return switch (this) {
|
||||||
case NORTH -> EAST;
|
case NORTH -> EAST;
|
||||||
case SOUTH -> WEST;
|
case SOUTH -> WEST;
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class MarsMap {
|
||||||
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
|
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates updatePositionTowards(Direction direction) {
|
public void updatePositionTowards(Direction direction) {
|
||||||
boolean updatedPosition = switch (direction) {
|
boolean updatedPosition = switch (direction) {
|
||||||
case NORTH -> decrementLatitude();
|
case NORTH -> decrementLatitude();
|
||||||
case SOUTH -> incrementLatitude();
|
case SOUTH -> incrementLatitude();
|
||||||
|
@ -43,8 +43,6 @@ public class MarsMap {
|
||||||
if (updatedPosition)
|
if (updatedPosition)
|
||||||
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
||||||
direction, currentPosition.latitude(), currentPosition.longitude()));
|
direction, currentPosition.latitude(), currentPosition.longitude()));
|
||||||
|
|
||||||
return currentPosition;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean incrementLatitude() {
|
private boolean incrementLatitude() {
|
||||||
|
|
|
@ -15,6 +15,9 @@ public class MarsRoverTest {
|
||||||
|
|
||||||
private MarsRover rover;
|
private MarsRover rover;
|
||||||
private MoveForwardCommand moveForwardCommand;
|
private MoveForwardCommand moveForwardCommand;
|
||||||
|
private MoveBackwardsCommand moveBackwardsCommand;
|
||||||
|
private TurnLeftCommand turnLeftCommand;
|
||||||
|
private TurnRightCommand turnRightCommand;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
@ -27,6 +30,9 @@ public class MarsRoverTest {
|
||||||
rover = new MarsRover(marsMap, SOUTH);
|
rover = new MarsRover(marsMap, SOUTH);
|
||||||
|
|
||||||
moveForwardCommand = new MoveForwardCommand(rover);
|
moveForwardCommand = new MoveForwardCommand(rover);
|
||||||
|
moveBackwardsCommand = new MoveBackwardsCommand(rover);
|
||||||
|
turnLeftCommand = new TurnLeftCommand(rover);
|
||||||
|
turnRightCommand = new TurnRightCommand(rover);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -43,7 +49,8 @@ public class MarsRoverTest {
|
||||||
public void stepBackwards() {
|
public void stepBackwards() {
|
||||||
Direction originalDirection = rover.getCurrentDirection();
|
Direction originalDirection = rover.getCurrentDirection();
|
||||||
|
|
||||||
Coordinates currentPosition = rover.moveBackwards();
|
moveBackwardsCommand.execute();
|
||||||
|
Coordinates currentPosition = rover.getCurrentCoordinates();
|
||||||
|
|
||||||
assertEquals(currentPosition, Coordinates.of(1, 3));
|
assertEquals(currentPosition, Coordinates.of(1, 3));
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
|
@ -51,20 +58,30 @@ public class MarsRoverTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void turnLef() {
|
public void turnLef() {
|
||||||
assertEquals(rover.turnLeft(), EAST);
|
turnLeftCommand.execute();
|
||||||
assertEquals(rover.turnLeft(), NORTH);
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
assertEquals(rover.turnLeft(), WEST);
|
turnLeftCommand.execute();
|
||||||
assertEquals(rover.turnLeft(), SOUTH);
|
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||||
assertEquals(rover.turnLeft(), EAST);
|
turnLeftCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), WEST);
|
||||||
|
turnLeftCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||||
|
turnLeftCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void turnRight() {
|
public void turnRight() {
|
||||||
assertEquals(rover.turnRight(), WEST);
|
turnRightCommand.execute();
|
||||||
assertEquals(rover.turnRight(), NORTH);
|
assertEquals(rover.getCurrentDirection(), WEST);
|
||||||
assertEquals(rover.turnRight(), EAST);
|
turnRightCommand.execute();
|
||||||
assertEquals(rover.turnRight(), SOUTH);
|
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||||
assertEquals(rover.turnRight(), WEST);
|
turnRightCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
|
turnRightCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||||
|
turnRightCommand.execute();
|
||||||
|
assertEquals(rover.getCurrentDirection(), WEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -83,7 +100,7 @@ public class MarsRoverTest {
|
||||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
Direction originalDirection = rover.getCurrentDirection();
|
Direction originalDirection = rover.getCurrentDirection();
|
||||||
|
|
||||||
repeatAction(10, () -> rover.moveBackwards());
|
repeatAction(10, () -> moveBackwardsCommand.execute());
|
||||||
|
|
||||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||||
|
@ -93,7 +110,7 @@ public class MarsRoverTest {
|
||||||
public void loopTheWorldInLongitudeMovingForward() {
|
public void loopTheWorldInLongitudeMovingForward() {
|
||||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
|
|
||||||
rover.turnLeft();
|
turnLeftCommand.execute();
|
||||||
repeatAction(10, () -> moveForwardCommand.execute());
|
repeatAction(10, () -> moveForwardCommand.execute());
|
||||||
|
|
||||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||||
|
@ -104,8 +121,8 @@ public class MarsRoverTest {
|
||||||
public void loopTheWorldInLongitudeMovingBackwards() {
|
public void loopTheWorldInLongitudeMovingBackwards() {
|
||||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||||
|
|
||||||
rover.turnLeft();
|
turnLeftCommand.execute();
|
||||||
repeatAction(10, () -> rover.moveBackwards());
|
repeatAction(10, () -> moveBackwardsCommand.execute());
|
||||||
|
|
||||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||||
assertEquals(rover.getCurrentDirection(), EAST);
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
|
@ -117,18 +134,18 @@ public class MarsRoverTest {
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
||||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||||
|
|
||||||
rover.turnLeft();
|
turnLeftCommand.execute();
|
||||||
repeatAction(23, () -> moveForwardCommand.execute());
|
repeatAction(23, () -> moveForwardCommand.execute());
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6));
|
||||||
assertEquals(rover.getCurrentDirection(), EAST);
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
|
|
||||||
repeatAction(3, () -> rover.turnRight());
|
repeatAction(3, () -> turnRightCommand.execute());
|
||||||
repeatAction(9, () -> moveForwardCommand.execute());
|
repeatAction(9, () -> moveForwardCommand.execute());
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6));
|
||||||
assertEquals(rover.getCurrentDirection(), NORTH);
|
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||||
|
|
||||||
rover.moveBackwards();
|
moveBackwardsCommand.execute();
|
||||||
rover.turnRight();
|
turnRightCommand.execute();
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7));
|
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7));
|
||||||
assertEquals(rover.getCurrentDirection(), EAST);
|
assertEquals(rover.getCurrentDirection(), EAST);
|
||||||
|
|
|
@ -16,6 +16,8 @@ public class MarsRoverWithObstaclesTest {
|
||||||
|
|
||||||
private MarsRover rover;
|
private MarsRover rover;
|
||||||
private MoveForwardCommand moveForwardCommand;
|
private MoveForwardCommand moveForwardCommand;
|
||||||
|
private TurnLeftCommand turnLeftCommand;
|
||||||
|
private TurnRightCommand turnRightCommand;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
@ -35,6 +37,8 @@ public class MarsRoverWithObstaclesTest {
|
||||||
rover = new MarsRover(marsMap, SOUTH);
|
rover = new MarsRover(marsMap, SOUTH);
|
||||||
|
|
||||||
moveForwardCommand = new MoveForwardCommand(rover);
|
moveForwardCommand = new MoveForwardCommand(rover);
|
||||||
|
turnLeftCommand = new TurnLeftCommand(rover);
|
||||||
|
turnRightCommand = new TurnRightCommand(rover);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -54,10 +58,10 @@ public class MarsRoverWithObstaclesTest {
|
||||||
Coordinates oneStepForward = rover.getCurrentCoordinates();
|
Coordinates oneStepForward = rover.getCurrentCoordinates();
|
||||||
assertNotEquals(oneStepForward, Coordinates.of(3, 3));
|
assertNotEquals(oneStepForward, Coordinates.of(3, 3));
|
||||||
|
|
||||||
rover.turnLeft();
|
turnLeftCommand.execute();
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
rover.turnRight();
|
turnRightCommand.execute();
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
moveForwardCommand.execute();
|
moveForwardCommand.execute();
|
||||||
var currentPosition = Coordinates.of(4, 5);
|
var currentPosition = Coordinates.of(4, 5);
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public class MoveBackwardsCommand implements MarsRoverCommand {
|
||||||
|
private final MarsRover marsRover;
|
||||||
|
|
||||||
|
public MoveBackwardsCommand(MarsRover marsRover) {
|
||||||
|
this.marsRover = marsRover;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
marsRover.moveTowards(marsRover.getCurrentDirection().reversed());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public class TurnLeftCommand implements MarsRoverCommand{
|
||||||
|
private final MarsRover marsRover;
|
||||||
|
|
||||||
|
public TurnLeftCommand(MarsRover marsRover) {
|
||||||
|
this.marsRover = marsRover;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
marsRover.rotateTowards(MarsRover.Rotation.LEFT);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue