diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java index 21091cb..6cc4917 100644 --- a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java @@ -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) { diff --git a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/RoverActionReporter.java b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/RoverActionReporter.java index fddf927..97ec302 100644 --- a/mars-station/src/main/java/cat/hack3/codingtests/marsrover/RoverActionReporter.java +++ b/mars-station/src/main/java/cat/hack3/codingtests/marsrover/RoverActionReporter.java @@ -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); + } } diff --git a/mars-station/src/main/java/module-info.java b/mars-station/src/main/java/module-info.java index ea4c32a..deb5287 100644 --- a/mars-station/src/main/java/module-info.java +++ b/mars-station/src/main/java/module-info.java @@ -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; diff --git a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RiderRover.java b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RiderRover.java index ec2d059..fe9a7e3 100644 --- a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RiderRover.java +++ b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RiderRover.java @@ -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); } diff --git a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RotableRover.java b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RotableRover.java index 21d3744..d32d5d2 100644 --- a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RotableRover.java +++ b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/RotableRover.java @@ -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); } diff --git a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverActionReport.java b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverActionReport.java new file mode 100644 index 0000000..0f63e88 --- /dev/null +++ b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverActionReport.java @@ -0,0 +1,5 @@ +package cat.hack3.codingtests.marsrover.api.commands; + +public record RoverActionReport(ReportType type, String message) { + public enum ReportType {INFO, WARNING} +} diff --git a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverCommand.java b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverCommand.java index 8e8bb7b..a41d29c 100644 --- a/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverCommand.java +++ b/rover-api/src/main/java/cat/hack3/codingtests/marsrover/api/commands/RoverCommand.java @@ -1,6 +1,5 @@ package cat.hack3.codingtests.marsrover.api.commands; public interface RoverCommand { - - void execute(); + RoverActionReport execute(); } diff --git a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveBackwardsCommand.java b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveBackwardsCommand.java index cc30dfc..fe94fb6 100644 --- a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveBackwardsCommand.java +++ b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveBackwardsCommand.java @@ -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()); } } diff --git a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveForwardCommand.java b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveForwardCommand.java index 0b441a3..6d5e097 100644 --- a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveForwardCommand.java +++ b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/MoveForwardCommand.java @@ -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()); } } diff --git a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnLeftCommand.java b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnLeftCommand.java index b6d9e66..46a3b6d 100644 --- a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnLeftCommand.java +++ b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnLeftCommand.java @@ -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); } } diff --git a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnRightCommand.java b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnRightCommand.java index ffd26f8..a01be4c 100644 --- a/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnRightCommand.java +++ b/rover-commands/src/main/java/cat/hack3/codingtests/marsrover/commands/TurnRightCommand.java @@ -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); } } diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java index 1a40539..4caa2e8 100644 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverTest.java @@ -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); } diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java index 0333217..b7e487b 100644 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/MarsRoverWithObstaclesTest.java @@ -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)); } diff --git a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java index cac3913..d1f8584 100644 --- a/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java +++ b/tests-suite/src/test/java/cat/hack3/codingtests/marsrover/test/context/TestingCommons.java @@ -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); + } }