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.Direction;
|
||||||
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class MarsRover implements RotableRiderRover{
|
public class MarsRover implements RotableRiderRover{
|
||||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
|
||||||
|
|
||||||
private final MarsMap marsMap;
|
private final MarsMap marsMap;
|
||||||
|
private final RoverActionReporter roverActionReporter;
|
||||||
private Direction currentDirection;
|
private Direction currentDirection;
|
||||||
|
|
||||||
public MarsRover(MarsMap marsMap, Direction startingDirection) {
|
public MarsRover(MarsMap marsMap, Direction startingDirection) {
|
||||||
this.marsMap = marsMap;
|
this.marsMap = marsMap;
|
||||||
currentDirection = startingDirection;
|
currentDirection = startingDirection;
|
||||||
|
roverActionReporter = new RoverActionReporter();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void moveTowards(Direction direction) {
|
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
|
@Override
|
||||||
|
@ -28,13 +39,17 @@ public class MarsRover implements RotableRiderRover{
|
||||||
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
||||||
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
|
case RIGHT -> currentDirection.getNextDirectionRotating90Degrees();
|
||||||
};
|
};
|
||||||
reportNewDirection(rotation, newDirection);
|
rotateTo(rotation);
|
||||||
currentDirection = newDirection;
|
updateDirection(newDirection);
|
||||||
|
roverActionReporter.reportRotation(rotation, newDirection);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reportNewDirection(Rotation rotation, Direction newDirection) {
|
private void rotateTo(Rotation rotation) {
|
||||||
logger.info(String.format("Rotated towards %s, now the direction is %s",
|
//physical rotation (not implemented)
|
||||||
rotation, newDirection));
|
}
|
||||||
|
|
||||||
|
private void updateDirection(Direction newDirection) {
|
||||||
|
currentDirection = newDirection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
positionResolver = new MapIncrementalPositionResolver(FIRST_POSITION_IN_MAP, INCREMENT_UNIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void updatePositionTowards(Direction direction) {
|
public Coordinates getNewPositionTowards(Direction direction) {
|
||||||
currentMovementDirection = direction;
|
return switch (direction) {
|
||||||
switch (direction) {
|
case NORTH -> getDecrementLatitude();
|
||||||
case NORTH -> decrementLatitude();
|
case SOUTH -> getIncrementLatitude();
|
||||||
case SOUTH -> incrementLatitude();
|
case WEST -> getDecrementLongitude();
|
||||||
case WEST -> decrementLongitude();
|
case EAST -> getIncrementLongitude();
|
||||||
case EAST -> incrementLongitude();
|
};
|
||||||
}
|
|
||||||
currentMovementDirection = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLatitude() {
|
private Coordinates getIncrementLatitude() {
|
||||||
int latitude = currentPosition.latitude();
|
int latitude = currentPosition.latitude();
|
||||||
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
|
int newLatitude = positionResolver.getIncrementedPosition(latitude, height);
|
||||||
setNewLatitudeIfNoObstacle(newLatitude);
|
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrementLatitude() {
|
private Coordinates getDecrementLatitude() {
|
||||||
int latitude = currentPosition.latitude();
|
int latitude = currentPosition.latitude();
|
||||||
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
|
int newLatitude = positionResolver.getDecrementedPosition(latitude, height);
|
||||||
setNewLatitudeIfNoObstacle(newLatitude);
|
return currentPosition.ofUpdatedLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLongitude() {
|
private Coordinates getIncrementLongitude() {
|
||||||
int longitude = currentPosition.longitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
|
int newLongitude = positionResolver.getIncrementedPosition(longitude, width);
|
||||||
setNewLongitudeIfNoObstacle(newLongitude);
|
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrementLongitude() {
|
private Coordinates getDecrementLongitude() {
|
||||||
int longitude = currentPosition.longitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
|
int newLongitude = positionResolver.getDecrementedPosition(longitude, width);
|
||||||
setNewLongitudeIfNoObstacle(newLongitude);
|
return currentPosition.ofUpdatedLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNewLatitudeIfNoObstacle(int newLatitude) {
|
public synchronized void updateLocation(Coordinates newPosition) {
|
||||||
Coordinates newCoordinates = currentPosition.ofUpdatedLatitude(newLatitude);
|
currentPosition = newPosition;
|
||||||
setNewPositionIfNoObstacle(newCoordinates);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNewLongitudeIfNoObstacle(int newLongitude) {
|
public boolean willCollideWithObstacle(Coordinates newCoordinates) {
|
||||||
Coordinates newCoordinates = currentPosition.ofUpdatedLongitude(newLongitude);
|
return obstaclesLocalizations.contains(newCoordinates);
|
||||||
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 Coordinates getCurrentPosition() {
|
public Coordinates getCurrentPosition() {
|
||||||
|
|
Loading…
Reference in New Issue