From 6906fec8a122de7d122ef3d8c9a32ed6db9cd445 Mon Sep 17 00:00:00 2001 From: Xavier Fontanet Date: Thu, 20 Jun 2024 18:52:44 +0200 Subject: [PATCH] misplaced responsibility fix --- .../hack3/codingtests/marsrover/Coordinates.java | 14 +++++++++++--- .../cat/hack3/codingtests/marsrover/MarsRover.java | 9 ++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java index fb8bcd7..4368e48 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/Coordinates.java @@ -1,6 +1,8 @@ package cat.hack3.codingtests.marsrover; +import cat.hack3.codingtests.marsrover.MarsMap.Direction; + import java.util.logging.Logger; /** @@ -26,9 +28,15 @@ public class Coordinates { this.longitude = longitude; } - public Coordinates incrementSouth() { - latitude++; - logger.info("incremented South latitude, now is "+latitude); + public Coordinates updatePositionTowards(Direction direction) { + switch (direction) { + case NORTH -> latitude--; + case SOUTH -> latitude++; + case WEST -> longitude--; + case EAST -> longitude++; + } + logger.info(String.format("Updated coordinates towards %s, now is %d-%d", + direction, latitude, longitude)); return this; } diff --git a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java index 0d286fe..1335384 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/MarsRover.java @@ -2,7 +2,7 @@ package cat.hack3.codingtests.marsrover; public class MarsRover { private final MarsMap marsMap; - private Coordinates currentCoordinates; + private final Coordinates currentCoordinates; private MarsMap.Direction currentDirection; public MarsRover(MarsMap marsMap, Coordinates startingCoordinates, MarsMap.Direction startingDirection) { @@ -12,12 +12,7 @@ public class MarsRover { } public Coordinates moveForward() { - return switch (currentDirection) { - case NORTH -> null; - case SOUTH -> currentCoordinates.incrementSouth(); - case EAST -> null; - case WEST -> null; - }; + return currentCoordinates.updatePositionTowards(currentDirection); } public MarsMap.Direction turnLeft() {