make Rover abstract to commands
This commit is contained in:
parent
e247e53c64
commit
535ae91e6c
|
@ -6,10 +6,9 @@ import cat.hack3.codingtests.marsrover.cartography.MarsMap;
|
|||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MarsRover {
|
||||
public class MarsRover implements RotableRiderRover{
|
||||
Logger logger = Logger.getLogger(this.getClass().getName());
|
||||
|
||||
enum Rotation {LEFT, RIGHT}
|
||||
private final MarsMap marsMap;
|
||||
private Direction currentDirection;
|
||||
|
||||
|
@ -18,10 +17,12 @@ public class MarsRover {
|
|||
currentDirection = startingDirection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void moveTowards(Direction direction) {
|
||||
marsMap.updatePositionTowards(direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rotateTowards(Rotation rotation) {
|
||||
var newDirection = switch (rotation) {
|
||||
case LEFT -> currentDirection.getNextDirectionRotatingMinus90Degrees();
|
||||
|
@ -36,10 +37,12 @@ public class MarsRover {
|
|||
rotation, newDirection));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Coordinates getCurrentCoordinates() {
|
||||
return marsMap.getCurrentPosition();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Direction getCurrentDirection() {
|
||||
return currentDirection;
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
public class MoveBackwardsCommand implements MarsRoverCommand {
|
||||
private final MarsRover marsRover;
|
||||
private final RotableRiderRover rover;
|
||||
|
||||
public MoveBackwardsCommand(MarsRover marsRover) {
|
||||
this.marsRover = marsRover;
|
||||
public MoveBackwardsCommand(RotableRiderRover rover) {
|
||||
this.rover = rover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
marsRover.moveTowards(marsRover.getCurrentDirection().reversed());
|
||||
rover.moveTowards(rover.getCurrentDirection().reversed());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
public class MoveForwardCommand implements MarsRoverCommand{
|
||||
private final MarsRover marsRover;
|
||||
private final RotableRiderRover rover;
|
||||
|
||||
public MoveForwardCommand(MarsRover marsRover) {
|
||||
this.marsRover = marsRover;
|
||||
public MoveForwardCommand(RotableRiderRover rover) {
|
||||
this.rover = rover;
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
marsRover.moveTowards(marsRover.getCurrentDirection());
|
||||
rover.moveTowards(rover.getCurrentDirection());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -1,14 +1,16 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
public class TurnLeftCommand implements MarsRoverCommand{
|
||||
private final MarsRover marsRover;
|
||||
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
|
||||
|
||||
public TurnLeftCommand(MarsRover marsRover) {
|
||||
this.marsRover = marsRover;
|
||||
public class TurnLeftCommand implements MarsRoverCommand{
|
||||
private final RotableRiderRover rover;
|
||||
|
||||
public TurnLeftCommand(RotableRiderRover rover) {
|
||||
this.rover = rover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
marsRover.rotateTowards(MarsRover.Rotation.LEFT);
|
||||
rover.rotateTowards(LEFT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
public class TurnRightCommand implements MarsRoverCommand{
|
||||
private final MarsRover marsRover;
|
||||
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
|
||||
|
||||
public TurnRightCommand(MarsRover marsRover) {
|
||||
this.marsRover = marsRover;
|
||||
public class TurnRightCommand implements MarsRoverCommand{
|
||||
private final RotableRiderRover rover;
|
||||
|
||||
public TurnRightCommand(RotableRiderRover rover) {
|
||||
this.rover = rover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
marsRover.rotateTowards(MarsRover.Rotation.RIGHT);
|
||||
rover.rotateTowards(RIGHT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,10 +14,10 @@ import static org.testng.Assert.assertEquals;
|
|||
public class MarsRoverTest {
|
||||
|
||||
private MarsRover rover;
|
||||
private MoveForwardCommand moveForwardCommand;
|
||||
private MoveBackwardsCommand moveBackwardsCommand;
|
||||
private TurnLeftCommand turnLeftCommand;
|
||||
private TurnRightCommand turnRightCommand;
|
||||
private MarsRoverCommand moveForwardCommand;
|
||||
private MarsRoverCommand moveBackwardsCommand;
|
||||
private MarsRoverCommand turnLeftCommand;
|
||||
private MarsRoverCommand turnRightCommand;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() {
|
||||
|
|
Loading…
Reference in New Issue