1
0
Fork 0

make Rover abstract to commands

This commit is contained in:
Xavier Fontanet 2024-06-22 15:47:14 +02:00
parent e247e53c64
commit 535ae91e6c
7 changed files with 47 additions and 24 deletions

View File

@ -6,10 +6,9 @@ import cat.hack3.codingtests.marsrover.cartography.MarsMap;
import java.util.logging.Logger; import java.util.logging.Logger;
public class MarsRover { public class MarsRover implements RotableRiderRover{
Logger logger = Logger.getLogger(this.getClass().getName()); Logger logger = Logger.getLogger(this.getClass().getName());
enum Rotation {LEFT, RIGHT}
private final MarsMap marsMap; private final MarsMap marsMap;
private Direction currentDirection; private Direction currentDirection;
@ -18,10 +17,12 @@ public class MarsRover {
currentDirection = startingDirection; currentDirection = startingDirection;
} }
@Override
public void moveTowards(Direction direction) { public void moveTowards(Direction direction) {
marsMap.updatePositionTowards(direction); marsMap.updatePositionTowards(direction);
} }
@Override
public void rotateTowards(Rotation rotation) { public void rotateTowards(Rotation rotation) {
var newDirection = switch (rotation) { var newDirection = switch (rotation) {
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees(); case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
@ -36,10 +37,12 @@ public class MarsRover {
rotation, newDirection)); rotation, newDirection));
} }
@Override
public Coordinates getCurrentCoordinates() { public Coordinates getCurrentCoordinates() {
return marsMap.getCurrentPosition(); return marsMap.getCurrentPosition();
} }
@Override
public Direction getCurrentDirection() { public Direction getCurrentDirection() {
return currentDirection; return currentDirection;
} }

View File

@ -1,14 +1,14 @@
package cat.hack3.codingtests.marsrover; package cat.hack3.codingtests.marsrover;
public class MoveBackwardsCommand implements MarsRoverCommand { public class MoveBackwardsCommand implements MarsRoverCommand {
private final MarsRover marsRover; private final RotableRiderRover rover;
public MoveBackwardsCommand(MarsRover marsRover) { public MoveBackwardsCommand(RotableRiderRover rover) {
this.marsRover = marsRover; this.rover = rover;
} }
@Override @Override
public void execute() { public void execute() {
marsRover.moveTowards(marsRover.getCurrentDirection().reversed()); rover.moveTowards(rover.getCurrentDirection().reversed());
} }
} }

View File

@ -1,13 +1,13 @@
package cat.hack3.codingtests.marsrover; package cat.hack3.codingtests.marsrover;
public class MoveForwardCommand implements MarsRoverCommand{ public class MoveForwardCommand implements MarsRoverCommand{
private final MarsRover marsRover; private final RotableRiderRover rover;
public MoveForwardCommand(MarsRover marsRover) { public MoveForwardCommand(RotableRiderRover rover) {
this.marsRover = marsRover; this.rover = rover;
} }
public void execute() { public void execute() {
marsRover.moveTowards(marsRover.getCurrentDirection()); rover.moveTowards(rover.getCurrentDirection());
} }
} }

View File

@ -0,0 +1,16 @@
package cat.hack3.codingtests.marsrover;
import cat.hack3.codingtests.marsrover.cartography.Coordinates;
import cat.hack3.codingtests.marsrover.cartography.Direction;
public interface RotableRiderRover {
enum Rotation {LEFT, RIGHT}
void moveTowards(Direction direction);
void rotateTowards(MarsRover.Rotation rotation);
Coordinates getCurrentCoordinates();
Direction getCurrentDirection();
}

View File

@ -1,14 +1,16 @@
package cat.hack3.codingtests.marsrover; package cat.hack3.codingtests.marsrover;
public class TurnLeftCommand implements MarsRoverCommand{ import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
private final MarsRover marsRover;
public TurnLeftCommand(MarsRover marsRover) { public class TurnLeftCommand implements MarsRoverCommand{
this.marsRover = marsRover; private final RotableRiderRover rover;
public TurnLeftCommand(RotableRiderRover rover) {
this.rover = rover;
} }
@Override @Override
public void execute() { public void execute() {
marsRover.rotateTowards(MarsRover.Rotation.LEFT); rover.rotateTowards(LEFT);
} }
} }

View File

@ -1,14 +1,16 @@
package cat.hack3.codingtests.marsrover; package cat.hack3.codingtests.marsrover;
public class TurnRightCommand implements MarsRoverCommand{ import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
private final MarsRover marsRover;
public TurnRightCommand(MarsRover marsRover) { public class TurnRightCommand implements MarsRoverCommand{
this.marsRover = marsRover; private final RotableRiderRover rover;
public TurnRightCommand(RotableRiderRover rover) {
this.rover = rover;
} }
@Override @Override
public void execute() { public void execute() {
marsRover.rotateTowards(MarsRover.Rotation.RIGHT); rover.rotateTowards(RIGHT);
} }
} }

View File

@ -14,10 +14,10 @@ import static org.testng.Assert.assertEquals;
public class MarsRoverTest { public class MarsRoverTest {
private MarsRover rover; private MarsRover rover;
private MoveForwardCommand moveForwardCommand; private MarsRoverCommand moveForwardCommand;
private MoveBackwardsCommand moveBackwardsCommand; private MarsRoverCommand moveBackwardsCommand;
private TurnLeftCommand turnLeftCommand; private MarsRoverCommand turnLeftCommand;
private TurnRightCommand turnRightCommand; private MarsRoverCommand turnRightCommand;
@BeforeMethod @BeforeMethod
public void setUp() { public void setUp() {