1
0
Fork 0

return the ActionReport in every executed command

This commit is contained in:
Xavier Fontanet 2024-07-05 17:47:16 +02:00
parent 33cc0744a8
commit ca9f150c23
14 changed files with 85 additions and 61 deletions

View File

@ -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.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction; import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap; import cat.hack3.codingtests.marsrover.api.cartography.PlanetMap;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
public class MarsRover implements RotableRiderRover { public class MarsRover implements RotableRiderRover {
@ -25,15 +26,15 @@ public class MarsRover implements RotableRiderRover {
} }
@Override @Override
public void moveTowards(Direction direction) { public RoverActionReport moveTowards(Direction direction) {
Coordinates newPosition = marsMap.getNextPositionTowards(direction); Coordinates newPosition = marsMap.getNextPositionTowards(direction);
boolean willCollide = marsMap.willCollideWithObstacle(newPosition); boolean willCollide = marsMap.willCollideWithObstacle(newPosition);
if (willCollide) { if (willCollide) {
roverActionReporter.reportObstacle(newPosition); return roverActionReporter.reportObstacle(newPosition);
} else { } else {
moveTo(direction); moveTo(direction);
marsMap.updateLocation(newPosition); marsMap.updateLocation(newPosition);
roverActionReporter.reportMovement(direction, newPosition); return roverActionReporter.createMovementReport(direction, newPosition);
} }
} }
@ -42,14 +43,14 @@ public class MarsRover implements RotableRiderRover {
} }
@Override @Override
public void rotateTowards(Rotation rotation) { public RoverActionReport rotateTowards(Rotation rotation) {
var newDirection = switch (rotation) { var newDirection = switch (rotation) {
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees(); case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees(); case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
}; };
rotateTo(rotation); rotateTo(rotation);
updateDirection(newDirection); updateDirection(newDirection);
roverActionReporter.reportRotation(rotation, newDirection); return roverActionReporter.createRotationReport(rotation, newDirection);
} }
private void rotateTo(Rotation rotation) { private void rotateTo(Rotation rotation) {

View File

@ -3,29 +3,35 @@ package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.api.RotableRover; import cat.hack3.codingtests.marsrover.api.RotableRover;
import cat.hack3.codingtests.marsrover.api.cartography.Coordinates; import cat.hack3.codingtests.marsrover.api.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.api.cartography.Direction; import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
import java.util.logging.Logger; import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport.ReportType;
public class RoverActionReporter { 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 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"; 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 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); 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, String movementReport = String.format(MOVEMENT_REPORT_MESSAGE,
direction, newPosition.latitude(), newPosition.longitude()); direction, newPosition.latitude(), newPosition.longitude());
logger.info(movementReport); return reportInfo(movementReport);
} }
public void reportRotation(RotableRover.Rotation rotation, Direction newDirection) { public RoverActionReport createRotationReport(RotableRover.Rotation rotation, Direction newDirection) {
logger.info(String.format(ROTATION_REPORT_MESSGE, return reportInfo(String.format(ROTATION_REPORT_MESSGE,
rotation, newDirection)); rotation, newDirection));
} }
private RoverActionReport reportInfo(String message) {
return new RoverActionReport(ReportType.INFO, message);
}
private RoverActionReport reportWarning(String message) {
return new RoverActionReport(ReportType.WARNING, message);
}
} }

View File

@ -5,7 +5,6 @@ import cat.hack3.codingtests.marsrover.gps.MarsMap;
module mars.station { module mars.station {
requires rover.api; requires rover.api;
requires java.logging;
provides PlanetMap.Provider provides PlanetMap.Provider
with MarsMap.Provider; with MarsMap.Provider;

View File

@ -1,7 +1,8 @@
package cat.hack3.codingtests.marsrover.api; package cat.hack3.codingtests.marsrover.api;
import cat.hack3.codingtests.marsrover.api.cartography.Direction; import cat.hack3.codingtests.marsrover.api.cartography.Direction;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
public interface RiderRover extends AccelerometeredRover{ public interface RiderRover extends AccelerometeredRover{
void moveTowards(Direction direction); RoverActionReport moveTowards(Direction direction);
} }

View File

@ -1,7 +1,9 @@
package cat.hack3.codingtests.marsrover.api; package cat.hack3.codingtests.marsrover.api;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
public interface RotableRover extends Rover{ public interface RotableRover extends Rover{
enum Rotation {LEFT, RIGHT} enum Rotation {LEFT, RIGHT}
void rotateTowards(Rotation rotation); RoverActionReport rotateTowards(Rotation rotation);
} }

View File

@ -0,0 +1,5 @@
package cat.hack3.codingtests.marsrover.api.commands;
public record RoverActionReport(ReportType type, String message) {
public enum ReportType {INFO, WARNING}
}

View File

@ -1,6 +1,5 @@
package cat.hack3.codingtests.marsrover.api.commands; package cat.hack3.codingtests.marsrover.api.commands;
public interface RoverCommand { public interface RoverCommand {
RoverActionReport execute();
void execute();
} }

View File

@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RiderRover; import cat.hack3.codingtests.marsrover.api.RiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
public class MoveBackwardsCommand implements RoverCommand { public class MoveBackwardsCommand implements RoverCommand {
@ -12,7 +13,7 @@ public class MoveBackwardsCommand implements RoverCommand {
} }
@Override @Override
public void execute() { public RoverActionReport execute() {
rover.moveTowards(rover.getCurrentDirection().reversed()); return rover.moveTowards(rover.getCurrentDirection().reversed());
} }
} }

View File

@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RiderRover; import cat.hack3.codingtests.marsrover.api.RiderRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
public class MoveForwardCommand implements RoverCommand { public class MoveForwardCommand implements RoverCommand {
@ -11,7 +12,7 @@ public class MoveForwardCommand implements RoverCommand {
this.rover = rover; this.rover = rover;
} }
public void execute() { public RoverActionReport execute() {
rover.moveTowards(rover.getCurrentDirection()); return rover.moveTowards(rover.getCurrentDirection());
} }
} }

View File

@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRover; import cat.hack3.codingtests.marsrover.api.RotableRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.LEFT; import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.LEFT;
@ -14,7 +15,7 @@ public class TurnLeftCommand implements RoverCommand {
} }
@Override @Override
public void execute() { public RoverActionReport execute() {
rover.rotateTowards(LEFT); return rover.rotateTowards(LEFT);
} }
} }

View File

@ -2,6 +2,7 @@ package cat.hack3.codingtests.marsrover.commands;
import cat.hack3.codingtests.marsrover.api.RotableRover; import cat.hack3.codingtests.marsrover.api.RotableRover;
import cat.hack3.codingtests.marsrover.api.commands.RoverActionReport;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand; import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.RIGHT; import static cat.hack3.codingtests.marsrover.api.RotableRover.Rotation.RIGHT;
@ -14,7 +15,7 @@ public class TurnRightCommand implements RoverCommand {
} }
@Override @Override
public void execute() { public RoverActionReport execute() {
rover.rotateTowards(RIGHT); return rover.rotateTowards(RIGHT);
} }
} }

View File

@ -20,7 +20,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
public void stepForward() { public void stepForward() {
Direction originalDirection = rover.getCurrentDirection(); Direction originalDirection = rover.getCurrentDirection();
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
assertEquals(rover.getCurrentDirection(), originalDirection); assertEquals(rover.getCurrentDirection(), originalDirection);
@ -30,7 +30,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
public void stepBackwards() { public void stepBackwards() {
Direction originalDirection = rover.getCurrentDirection(); Direction originalDirection = rover.getCurrentDirection();
moveBackwardsCommand.execute(); executeCommandAndReport(moveBackwardsCommand);
Coordinates currentPosition = rover.getCurrentCoordinates(); Coordinates currentPosition = rover.getCurrentCoordinates();
assertEquals(currentPosition, Coordinates.of(1, 3)); assertEquals(currentPosition, Coordinates.of(1, 3));
@ -39,29 +39,29 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
@Test @Test
public void turnLef() { public void turnLef() {
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
assertEquals(rover.getCurrentDirection(), NORTH); assertEquals(rover.getCurrentDirection(), NORTH);
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
assertEquals(rover.getCurrentDirection(), WEST); assertEquals(rover.getCurrentDirection(), WEST);
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
assertEquals(rover.getCurrentDirection(), SOUTH); assertEquals(rover.getCurrentDirection(), SOUTH);
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
} }
@Test @Test
public void turnRight() { public void turnRight() {
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
assertEquals(rover.getCurrentDirection(), WEST); assertEquals(rover.getCurrentDirection(), WEST);
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
assertEquals(rover.getCurrentDirection(), NORTH); assertEquals(rover.getCurrentDirection(), NORTH);
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
assertEquals(rover.getCurrentDirection(), SOUTH); assertEquals(rover.getCurrentDirection(), SOUTH);
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
assertEquals(rover.getCurrentDirection(), WEST); assertEquals(rover.getCurrentDirection(), WEST);
} }
@ -70,7 +70,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
Coordinates originalCoordinates = rover.getCurrentCoordinates(); Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection(); Direction originalDirection = rover.getCurrentDirection();
repeatAction(10, () -> moveForwardCommand.execute()); repeatAction(10, () -> executeCommandAndReport(moveForwardCommand));
assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection); assertEquals(rover.getCurrentDirection(), originalDirection);
@ -81,7 +81,7 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
Coordinates originalCoordinates = rover.getCurrentCoordinates(); Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection(); Direction originalDirection = rover.getCurrentDirection();
repeatAction(10, () -> moveBackwardsCommand.execute()); repeatAction(10, () -> executeCommandAndReport(moveBackwardsCommand));
assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection); assertEquals(rover.getCurrentDirection(), originalDirection);
@ -91,8 +91,8 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
public void loopTheWorldInLongitudeMovingForward() { public void loopTheWorldInLongitudeMovingForward() {
Coordinates originalCoordinates = rover.getCurrentCoordinates(); Coordinates originalCoordinates = rover.getCurrentCoordinates();
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
repeatAction(10, () -> moveForwardCommand.execute()); repeatAction(10, () -> executeCommandAndReport(moveForwardCommand));
assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
@ -102,8 +102,8 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
public void loopTheWorldInLongitudeMovingBackwards() { public void loopTheWorldInLongitudeMovingBackwards() {
Coordinates originalCoordinates = rover.getCurrentCoordinates(); Coordinates originalCoordinates = rover.getCurrentCoordinates();
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
repeatAction(10, () -> moveBackwardsCommand.execute()); repeatAction(10, () -> executeCommandAndReport(moveBackwardsCommand));
assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
@ -111,23 +111,23 @@ public class MarsRoverTest extends GivenRotableAndMoveCommandsOnDeployedRoverWit
@Test @Test
public void acceptanceTest() { public void acceptanceTest() {
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 3));
assertEquals(rover.getCurrentDirection(), SOUTH); assertEquals(rover.getCurrentDirection(), SOUTH);
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
repeatAction(23, () -> moveForwardCommand.execute()); repeatAction(23, () -> executeCommandAndReport(moveForwardCommand));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(3, 6));
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
repeatAction(3, () -> turnRightCommand.execute()); repeatAction(3, () -> executeCommandAndReport(turnRightCommand));
repeatAction(9, () -> moveForwardCommand.execute()); repeatAction(9, () -> executeCommandAndReport(moveForwardCommand));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 6));
assertEquals(rover.getCurrentDirection(), NORTH); assertEquals(rover.getCurrentDirection(), NORTH);
moveBackwardsCommand.execute(); executeCommandAndReport(moveBackwardsCommand);
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 7));
assertEquals(rover.getCurrentDirection(), EAST); assertEquals(rover.getCurrentDirection(), EAST);
} }

View File

@ -31,7 +31,7 @@ public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDep
Coordinates originalCoordinates = rover.getCurrentCoordinates(); Coordinates originalCoordinates = rover.getCurrentCoordinates();
Direction originalDirection = rover.getCurrentDirection(); Direction originalDirection = rover.getCurrentDirection();
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
assertEquals(rover.getCurrentCoordinates(), originalCoordinates); assertEquals(rover.getCurrentCoordinates(), originalCoordinates);
assertEquals(rover.getCurrentDirection(), originalDirection); assertEquals(rover.getCurrentDirection(), originalDirection);
@ -39,17 +39,17 @@ public class MarsRoverWithObstaclesTest extends GivenRotableAndMoveCommandsOnDep
@Test @Test
public void afterFindingObstacleWalkAroundUntilFindingNext() { public void afterFindingObstacleWalkAroundUntilFindingNext() {
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
Coordinates oneStepForward = rover.getCurrentCoordinates(); Coordinates oneStepForward = rover.getCurrentCoordinates();
assertNotEquals(oneStepForward, Coordinates.of(3, 3)); assertNotEquals(oneStepForward, Coordinates.of(3, 3));
assertEquals(oneStepForward, Coordinates.of(2, 3)); assertEquals(oneStepForward, Coordinates.of(2, 3));
turnLeftCommand.execute(); executeCommandAndReport(turnLeftCommand);
repeatAction(2, () -> moveForwardCommand.execute()); repeatAction(2, () -> executeCommandAndReport(moveForwardCommand));
turnRightCommand.execute(); executeCommandAndReport(turnRightCommand);
repeatAction(2, () -> moveForwardCommand.execute()); repeatAction(2, () -> executeCommandAndReport(moveForwardCommand));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
moveForwardCommand.execute(); executeCommandAndReport(moveForwardCommand);
assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5)); assertNotEquals(rover.getCurrentCoordinates(), Coordinates.of(5, 5));
assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5)); assertEquals(rover.getCurrentCoordinates(), Coordinates.of(4, 5));
} }

View File

@ -1,5 +1,7 @@
package cat.hack3.codingtests.marsrover.test.context; package cat.hack3.codingtests.marsrover.test.context;
import cat.hack3.codingtests.marsrover.api.commands.RoverCommand;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -16,4 +18,9 @@ public interface TestingCommons {
IntStream.rangeClosed(1, times) IntStream.rangeClosed(1, times)
.forEach(i -> actionToRepeat.run()); .forEach(i -> actionToRepeat.run());
} }
default void executeCommandAndReport(RoverCommand command) {
var report = command.execute();
System.out.println(report);
}
} }