return the ActionReport in every executed command
This commit is contained in:
parent
33cc0744a8
commit
ca9f150c23
|
@ -4,6 +4,7 @@ import cat.hack3.codingtests.marsrover.api.RotableRiderRover;
|
|||
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
|
||||
public class MarsRover implements RotableRiderRover {
|
||||
|
||||
|
@ -25,15 +26,15 @@ public class MarsRover implements RotableRiderRover {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void moveTowards(Direction direction) {
|
||||
public RoverActionReport moveTowards(Direction direction) {
|
||||
Coordinates newPosition = marsMap.getNextPositionTowards(direction);
|
||||
boolean willCollide = marsMap.willCollideWithObstacle(newPosition);
|
||||
if (willCollide) {
|
||||
roverActionReporter.reportObstacle(newPosition);
|
||||
return roverActionReporter.reportObstacle(newPosition);
|
||||
} else {
|
||||
moveTo(direction);
|
||||
marsMap.updateLocation(newPosition);
|
||||
roverActionReporter.reportMovement(direction, newPosition);
|
||||
return roverActionReporter.createMovementReport(direction, newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,14 +43,14 @@ public class MarsRover implements RotableRiderRover {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void rotateTowards(Rotation rotation) {
|
||||
public RoverActionReport rotateTowards(Rotation rotation) {
|
||||
var newDirection = switch (rotation) {
|
||||
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
||||
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
|
||||
};
|
||||
rotateTo(rotation);
|
||||
updateDirection(newDirection);
|
||||
roverActionReporter.reportRotation(rotation, newDirection);
|
||||
return roverActionReporter.createRotationReport(rotation, newDirection);
|
||||
}
|
||||
|
||||
private void rotateTo(Rotation rotation) {
|
||||
|
|
|
@ -3,29 +3,35 @@ package cat.hack3.codingtests.marsrover;
|
|||
import cat.hack3.codingtests.marsrover.api.RotableRover;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport.ReportType;
|
||||
|
||||
public class RoverActionReporter {
|
||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
private static final String OBSTACLE_REPORT_MESSAGE = "There is an obstacle at %s, therefore I will keep the same position";
|
||||
private static final String MOVEMENT_REPORT_MESSAGE = "Updated coordinates towards %s, now is %s-%s";
|
||||
public static final String ROTATION_REPORT_MESSGE = "Rotated towards %s, now the direction is %s";
|
||||
|
||||
public void reportObstacle(Coordinates newPosition) {
|
||||
public RoverActionReport reportObstacle(Coordinates newPosition) {
|
||||
var collisionReport = String.format(OBSTACLE_REPORT_MESSAGE, newPosition);
|
||||
logger.info(collisionReport);
|
||||
return reportWarning(collisionReport);
|
||||
}
|
||||
|
||||
public void reportMovement(Direction direction, Coordinates newPosition) {
|
||||
public RoverActionReport createMovementReport(Direction direction, Coordinates newPosition) {
|
||||
String movementReport = String.format(MOVEMENT_REPORT_MESSAGE,
|
||||
direction, newPosition.latitude(), newPosition.longitude());
|
||||
logger.info(movementReport);
|
||||
return reportInfo(movementReport);
|
||||
}
|
||||
|
||||
public void reportRotation(RotableRover.Rotation rotation, Direction newDirection) {
|
||||
logger.info(String.format(ROTATION_REPORT_MESSGE,
|
||||
public RoverActionReport createRotationReport(RotableRover.Rotation rotation, Direction newDirection) {
|
||||
return reportInfo(String.format(ROTATION_REPORT_MESSGE,
|
||||
rotation, newDirection));
|
||||
}
|
||||
|
||||
private RoverActionReport reportInfo(String message) {
|
||||
return new RoverActionReport(ReportType.INFO, message);
|
||||
}
|
||||
|
||||
private RoverActionReport reportWarning(String message) {
|
||||
return new RoverActionReport(ReportType.WARNING, message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import cat.hack3.codingtests.marsrover.gps.MarsMap;
|
|||
|
||||
module mars.station {
|
||||
requires rover.api;
|
||||
requires java.logging;
|
||||
|
||||
provides PlanetMap.Provider
|
||||
with MarsMap.Provider;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package cat.hack3.codingtests.marsrover.api;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
|
||||
public interface RiderRover extends AccelerometeredRover{
|
||||
void moveTowards(Direction direction);
|
||||
RoverActionReport moveTowards(Direction direction);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package cat.hack3.codingtests.marsrover.api;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
|
||||
public interface RotableRover extends Rover{
|
||||
enum Rotation {LEFT, RIGHT}
|
||||
|
||||
void rotateTowards(Rotation rotation);
|
||||
RoverActionReport rotateTowards(Rotation rotation);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package cat.hack3.codingtests.marsrover.api.commands;
|
||||
|
||||
public record RoverActionReport(ReportType type, String message) {
|
||||
public enum ReportType {INFO, WARNING}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package cat.hack3.codingtests.marsrover.api.commands;
|
||||
|
||||
public interface RoverCommand {
|
||||
|
||||
void execute();
|
||||
RoverActionReport execute();
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
|
|||
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
|
||||
public class MoveBackwardsCommand implements RoverCommand {
|
||||
|
@ -12,7 +13,7 @@ public class MoveBackwardsCommand implements RoverCommand {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
rover.moveTowards(rover.getCurrentDirection().reversed());
|
||||
public RoverActionReport execute() {
|
||||
return rover.moveTowards(rover.getCurrentDirection().reversed());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
|
|||
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RiderRover;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
|
||||
public class MoveForwardCommand implements RoverCommand {
|
||||
|
@ -11,7 +12,7 @@ public class MoveForwardCommand implements RoverCommand {
|
|||
this.rover = rover;
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
rover.moveTowards(rover.getCurrentDirection());
|
||||
public RoverActionReport execute() {
|
||||
return rover.moveTowards(rover.getCurrentDirection());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
|
|||
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRover;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
|
||||
import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.LEFT;
|
||||
|
@ -14,7 +15,7 @@ public class TurnLeftCommand implements RoverCommand {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
rover.rotateTowards(LEFT);
|
||||
public RoverActionReport execute() {
|
||||
return rover.rotateTowards(LEFT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
|
|||
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.RotableRover;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
|
||||
import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.RIGHT;
|
||||
|
@ -14,7 +15,7 @@ public class TurnRightCommand implements RoverCommand {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
rover.rotateTowards(RIGHT);
|
||||
public RoverActionReport execute() {
|
||||
return rover.rotateTowards(RIGHT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
public void stepForward() {
|
||||
Direction originalDirection = rover.getCurrentDirection();
|
||||
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||
|
@ -30,7 +30,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
public void stepBackwards() {
|
||||
Direction originalDirection = rover.getCurrentDirection();
|
||||
|
||||
moveBackwardsCommand.execute();
|
||||
executeCommandAndReport(moveBackwardsCommand);
|
||||
Coordinates currentPosition = rover.getCurrentCoordinates();
|
||||
|
||||
assertEquals(currentPosition, Coordinates.of(1, 3));
|
||||
|
@ -39,29 +39,29 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
|
||||
@Test
|
||||
public void turnLef() {
|
||||
turnLeftCommand.execute();
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
turnLeftCommand.execute();
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||
turnLeftCommand.execute();
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
assertEquals(rover.getCurrentDirection(), WEST);
|
||||
turnLeftCommand.execute();
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||
turnLeftCommand.execute();
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void turnRight() {
|
||||
turnRightCommand.execute();
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
assertEquals(rover.getCurrentDirection(), WEST);
|
||||
turnRightCommand.execute();
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||
turnRightCommand.execute();
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
turnRightCommand.execute();
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||
turnRightCommand.execute();
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
assertEquals(rover.getCurrentDirection(), WEST);
|
||||
}
|
||||
|
||||
|
@ -70,7 +70,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||
Direction originalDirection = rover.getCurrentDirection();
|
||||
|
||||
repeatAction(10, () -> moveForwardCommand.execute());
|
||||
repeatAction(10, () -> executeCommandAndReport(moveForwardCommand));
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||
|
@ -81,7 +81,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||
Direction originalDirection = rover.getCurrentDirection();
|
||||
|
||||
repeatAction(10, () -> moveBackwardsCommand.execute());
|
||||
repeatAction(10, () -> executeCommandAndReport(moveBackwardsCommand));
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||
|
@ -91,8 +91,8 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
public void loopTheWorldInLongitudeMovingForward() {
|
||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||
|
||||
turnLeftCommand.execute();
|
||||
repeatAction(10, () -> moveForwardCommand.execute());
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
repeatAction(10, () -> executeCommandAndReport(moveForwardCommand));
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
|
@ -102,8 +102,8 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
public void loopTheWorldInLongitudeMovingBackwards() {
|
||||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||
|
||||
turnLeftCommand.execute();
|
||||
repeatAction(10, () -> moveBackwardsCommand.execute());
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
repeatAction(10, () -> executeCommandAndReport(moveBackwardsCommand));
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
|
@ -111,23 +111,23 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
|
|||
|
||||
@Test
|
||||
public void acceptanceTest() {
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
|
||||
assertEquals(rover.getCurrentDirection(), SOUTH);
|
||||
|
||||
turnLeftCommand.execute();
|
||||
repeatAction(23, () -> moveForwardCommand.execute());
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
repeatAction(23, () -> executeCommandAndReport(moveForwardCommand));
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6));
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
|
||||
repeatAction(3, () -> turnRightCommand.execute());
|
||||
repeatAction(9, () -> moveForwardCommand.execute());
|
||||
repeatAction(3, () -> executeCommandAndReport(turnRightCommand));
|
||||
repeatAction(9, () -> executeCommandAndReport(moveForwardCommand));
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6));
|
||||
assertEquals(rover.getCurrentDirection(), NORTH);
|
||||
|
||||
moveBackwardsCommand.execute();
|
||||
turnRightCommand.execute();
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveBackwardsCommand);
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7));
|
||||
assertEquals(rover.getCurrentDirection(), EAST);
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDep
|
|||
Coordinates originalCoordinates = rover.getCurrentCoordinates();
|
||||
Direction originalDirection = rover.getCurrentDirection();
|
||||
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
|
||||
assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
|
||||
assertEquals(rover.getCurrentDirection(), originalDirection);
|
||||
|
@ -39,17 +39,17 @@ public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDep
|
|||
|
||||
@Test
|
||||
public void afterFindingObstacleWalkAroundUntilFindingNext() {
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
Coordinates oneStepForward = rover.getCurrentCoordinates();
|
||||
assertNotEquals(oneStepForward, Coordinates.of(3, 3));
|
||||
assertEquals(oneStepForward, Coordinates.of(2, 3));
|
||||
|
||||
turnLeftCommand.execute();
|
||||
repeatAction(2, () -> moveForwardCommand.execute());
|
||||
turnRightCommand.execute();
|
||||
repeatAction(2, () -> moveForwardCommand.execute());
|
||||
executeCommandAndReport(turnLeftCommand);
|
||||
repeatAction(2, () -> executeCommandAndReport(moveForwardCommand));
|
||||
executeCommandAndReport(turnRightCommand);
|
||||
repeatAction(2, () -> executeCommandAndReport(moveForwardCommand));
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
|
||||
moveForwardCommand.execute();
|
||||
executeCommandAndReport(moveForwardCommand);
|
||||
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
|
||||
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package cat.hack3.codingtests.marsrover.test.context;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
|
@ -16,4 +18,9 @@ public interface TestingCommons {
|
|||
IntStream.rangeClosed(1, times)
|
||||
.forEach(i -> actionToRepeat.run());
|
||||
}
|
||||
|
||||
default void executeCommandAndReport(RoverCommand command) {
|
||||
var report = command.execute();
|
||||
System.out.println(report);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue