better design and SRP
This commit is contained in:
parent
f0f3bf31fc
commit
3b8595c630
|
@ -4,22 +4,33 @@ import cat.hack3.codingtests.marsrover.cartography.Coordinates;
|
|||
import cat.hack3.codingtests.marsrover.cartography.Direction;
|
||||
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MarsRover implements RotableRiderRover{
|
||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
private final MarsMap marsMap;
|
||||
private final RoverActionReporter roverActionReporter;
|
||||
private Direction currentDirection;
|
||||
|
||||
public MarsRover(MarsMap marsMap, Direction startingDirection) {
|
||||
this.marsMap = marsMap;
|
||||
currentDirection = startingDirection;
|
||||
roverActionReporter = new RoverActionReporter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTowards(Direction direction) {
|
||||
marsMap.updatePositionTowards(direction);
|
||||
Coordinates newPosition = marsMap.getNewPositionTowards(direction);
|
||||
boolean willCollide = marsMap.willCollideWithObstacle(newPosition);
|
||||
if (willCollide) {
|
||||
roverActionReporter.reportObstacle(newPosition);
|
||||
} else {
|
||||
moveTo(direction);
|
||||
marsMap.updateLocation(newPosition);
|
||||
roverActionReporter.reportMovement(direction, newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
private void moveTo(Direction direction) {
|
||||
//physical move (not implemented)
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -28,13 +39,17 @@ public class MarsRover implements RotableRiderRover{
|
|||
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
||||
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
|
||||
};
|
||||
reportNewDirection(rotation, newDirection);
|
||||
currentDirection = newDirection;
|
||||
rotateTo(rotation);
|
||||
updateDirection(newDirection);
|
||||
roverActionReporter.reportRotation(rotation, newDirection);
|
||||
}
|
||||
|
||||
private void reportNewDirection(Rotation rotation, Direction newDirection) {
|
||||
logger.info(String.format("Rotated towards %s, now the direction is %s",
|
||||
rotation, newDirection));
|
||||
private void rotateTo(Rotation rotation) {
|
||||
//physical rotation (not implemented)
|
||||
}
|
||||
|
||||
private void updateDirection(Direction newDirection) {
|
||||
currentDirection = newDirection;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
import cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation;
|
||||
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
|
||||
import cat.hack3.codingtests.marsrover.cartography.Direction;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
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 %d-%d";
|
||||
public static final String ROTATION_REPORT_MESSGE = "Rotated towards %s, now the direction is %s";
|
||||
|
||||
public void reportObstacle(Coordinates newPosition) {
|
||||
var collisionReport = String.format(OBSTACLE_REPORT_MESSAGE, newPosition);
|
||||
logger.info(collisionReport);
|
||||
}
|
||||
|
||||
public void reportMovement(Direction direction, Coordinates newPosition) {
|
||||
String movementReport = String.format(MOVEMENT_REPORT_MESSAGE,
|
||||
direction, newPosition.latitude(), newPosition.longitude());
|
||||
logger.info(movementReport);
|
||||
}
|
||||
|
||||
public void reportRotation(Rotation rotation, Direction newDirection) {
|
||||
logger.info(String.format(ROTATION_REPORT_MESSGE,
|
||||
rotation, newDirection));
|
||||
}
|
||||
}
|
|
@ -34,66 +34,45 @@ public class MarsMap {
|
|||
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
|
||||
}
|
||||
|
||||
public synchronized void updatePositionTowards(Direction direction) {
|
||||
currentMovementDirection = direction;
|
||||
switch (direction) {
|
||||
case NORTH -> decrementLatitude();
|
||||
case SOUTH -> incrementLatitude();
|
||||
case WEST -> decrementLongitude();
|
||||
case EAST -> incrementLongitude();
|
||||
}
|
||||
currentMovementDirection = null;
|
||||
public Coordinates getNewPositionTowards(Direction direction) {
|
||||
return switch (direction) {
|
||||
case NORTH -> getDecrementLatitude();
|
||||
case SOUTH -> getIncrementLatitude();
|
||||
case WEST -> getDecrementLongitude();
|
||||
case EAST -> getIncrementLongitude();
|
||||
};
|
||||
}
|
||||
|
||||
private void incrementLatitude() {
|
||||
private Coordinates getIncrementLatitude() {
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
|
||||
setNewLatitudeIfNoObstacle(newLatitude);
|
||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private void decrementLatitude() {
|
||||
private Coordinates getDecrementLatitude() {
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
|
||||
setNewLatitudeIfNoObstacle(newLatitude);
|
||||
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private void incrementLongitude() {
|
||||
private Coordinates getIncrementLongitude() {
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
|
||||
setNewLongitudeIfNoObstacle(newLongitude);
|
||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||
}
|
||||
|
||||
private void decrementLongitude() {
|
||||
private Coordinates getDecrementLongitude() {
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
|
||||
setNewLongitudeIfNoObstacle(newLongitude);
|
||||
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||
}
|
||||
|
||||
private void setNewLatitudeIfNoObstacle(int newLatitude) {
|
||||
Coordinates newCoordinates = currentPosition.ofUpdatedLatitude(newLatitude);
|
||||
setNewPositionIfNoObstacle(newCoordinates);
|
||||
public synchronized void updateLocation(Coordinates newPosition) {
|
||||
currentPosition = newPosition;
|
||||
}
|
||||
|
||||
private void setNewLongitudeIfNoObstacle(int newLongitude) {
|
||||
Coordinates newCoordinates = currentPosition.ofUpdatedLongitude(newLongitude);
|
||||
setNewPositionIfNoObstacle(newCoordinates);
|
||||
}
|
||||
|
||||
private void setNewPositionIfNoObstacle(Coordinates newCoordinates) {
|
||||
if (!willCollideWithObstacle(newCoordinates)) {
|
||||
currentPosition = newCoordinates;
|
||||
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
||||
currentMovementDirection, currentPosition.latitude(), currentPosition.longitude()));
|
||||
}
|
||||
}
|
||||
|
||||
private boolean willCollideWithObstacle(Coordinates newCoordinates) {
|
||||
boolean willCollide = obstaclesLocalizations.contains(newCoordinates);
|
||||
if (willCollide) {
|
||||
var collisionReport = String.format("There is an obstacle at %s, therefore Rover will keep the same position", newCoordinates);
|
||||
logger.info(collisionReport);
|
||||
}
|
||||
return willCollide;
|
||||
public boolean willCollideWithObstacle(Coordinates newCoordinates) {
|
||||
return obstaclesLocalizations.contains(newCoordinates);
|
||||
}
|
||||
|
||||
public Coordinates getCurrentPosition() {
|
||||
|
|
Loading…
Reference in New Issue