diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java index 4914755..90c72e9 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java @@ -1,84 +1,38 @@ package cat.hack3.codingtests.marsrover.ui; -import cat.hack3.codingtests.marsrover.Coordinates; -import cat.hack3.codingtests.marsrover.Direction; -import cat.hack3.codingtests.marsrover.MarsMap; import cat.hack3.codingtests.marsrover.MarsRover; -import java.util.InputMismatchException; import java.util.Scanner; -import static cat.hack3.codingtests.marsrover.ui.UICommons.isNotExitSignal; import static cat.hack3.codingtests.marsrover.ui.UICommons.output; public class ClientCommandInterface { - private static MarsRover rover; + private final Scanner reader; + private final RoverInitializer roverInitializer; public static void main(String[] args) { + try (var userInputSource = new Scanner(System.in)) { + ClientCommandInterface userInterface = new ClientCommandInterface(userInputSource); + userInterface.start(); + } + } + + public ClientCommandInterface(Scanner userInputSource) { + reader = userInputSource; + roverInitializer = new RoverInitializer(reader); + } + + private void start() { output(PresentationMessage.INTRO); - try (var reader = new Scanner(System.in)) { - //autoInitialization(); - initialization(reader); - commandLoop(reader); - } + //MarsRover rover = roverInitializer.autoInitialize(); + MarsRover rover = roverInitializer.initializeFromUserInputs(); + + var commandsPerformer = new RoverCommandsPerformer(reader, rover); + commandsPerformer.acceptCommandsUntilExitSignal(); } - private static void autoInitialization() { //to test more quickly - deployRover(10, 10, 2, 3, Direction.SOUTH); - } - private static void initialization(Scanner reader) { - output("Please, introduce the latitude map size:"); - int mapHeight = takeIntegerInput(reader); - output("Please, introduce the longitude map size:"); - int mapWidth = takeIntegerInput(reader); - - output("Great! Now please introduce the initial location of the Rover."); - output("Latitude:"); - int latitudeStartingPoint = takeIntegerInput(reader); - output("Longitude:"); - int longitudeStartingPoint = takeIntegerInput(reader); - - Direction startingDirection = DirectionRetriever.retrieveDirection(reader); - - deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection); - - output(PresentationMessage.READY_MESSAGE); - reader.next(); //input ignored - } - - private static int takeIntegerInput(Scanner reader) { - try { - return reader.nextInt(); - } catch (InputMismatchException e) { - output("(please, introduce a number)"); - reader.next(); - return takeIntegerInput(reader); - } - } - - private static void deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection) { - var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); - var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates); - rover = new MarsRover(marsMap, startingDirection); - } - - private static void commandLoop(Scanner reader) { - var nextCommand = "none"; - do { - output("Insert command (f = forward, b = backward, l = turn left, r = turn right, q = quit):"); - nextCommand = reader.next(); - switch (nextCommand) { - case "f" -> rover.moveForward(); - case "b" -> rover.moveBackwards(); - case "l" -> rover.turnLeft(); - case "r" -> rover.turnRight(); - case "q" -> output("Disconnecting from Mars..."); - default -> output("unknown command"); - } - } while (isNotExitSignal(nextCommand)); - } } diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java index da47598..e936d89 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java @@ -7,7 +7,13 @@ import java.util.Scanner; import static cat.hack3.codingtests.marsrover.ui.UICommons.*; public class DirectionRetriever { - static Direction retrieveDirection(Scanner reader) { + private final Scanner reader; + + public DirectionRetriever(Scanner reader) { + this.reader = reader; + } + + Direction retrieveDirection() { var directionInput = "none"; Direction resolvedDirection; do { @@ -28,7 +34,7 @@ public class DirectionRetriever { return resolvedDirection; } - private static boolean isNotResolvedDirection(Direction resolvedDirection) { + private boolean isNotResolvedDirection(Direction resolvedDirection) { return resolvedDirection == null; } } diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverCommandsPerformer.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverCommandsPerformer.java new file mode 100644 index 0000000..082fc2d --- /dev/null +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverCommandsPerformer.java @@ -0,0 +1,34 @@ +package cat.hack3.codingtests.marsrover.ui; + +import cat.hack3.codingtests.marsrover.MarsRover; + +import java.util.Scanner; + +import static cat.hack3.codingtests.marsrover.ui.UICommons.isNotExitSignal; +import static cat.hack3.codingtests.marsrover.ui.UICommons.output; + +public class RoverCommandsPerformer { + private final Scanner reader; + private final MarsRover rover; + + public RoverCommandsPerformer(Scanner reader, MarsRover rover) { + this.reader = reader; + this.rover = rover; + } + + public void acceptCommandsUntilExitSignal() { + var nextCommand = "none"; + do { + output("Insert command (f = forward, b = backward, l = turn left, r = turn right, q = quit):"); + nextCommand = reader.next(); + switch (nextCommand) { + case "f" -> rover.moveForward(); + case "b" -> rover.moveBackwards(); + case "l" -> rover.turnLeft(); + case "r" -> rover.turnRight(); + case "q" -> output("Disconnecting from Mars..."); + default -> output("unknown command"); + } + } while (isNotExitSignal(nextCommand)); + } +} diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverInitializer.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverInitializer.java new file mode 100644 index 0000000..9d4ac44 --- /dev/null +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/RoverInitializer.java @@ -0,0 +1,64 @@ +package cat.hack3.codingtests.marsrover.ui; + +import cat.hack3.codingtests.marsrover.Coordinates; +import cat.hack3.codingtests.marsrover.Direction; +import cat.hack3.codingtests.marsrover.MarsMap; +import cat.hack3.codingtests.marsrover.MarsRover; + +import java.util.InputMismatchException; +import java.util.Scanner; + +import static cat.hack3.codingtests.marsrover.ui.UICommons.output; + +public class RoverInitializer { + private Scanner reader; + private final DirectionRetriever directionRetriever; + + + public RoverInitializer(Scanner reader) { + this.reader = reader; + directionRetriever = new DirectionRetriever(reader); + } + + MarsRover initializeFromUserInputs() { + output("Please, introduce the latitude map size:"); + int mapHeight = takeIntegerInput(); + output("Please, introduce the longitude map size:"); + int mapWidth = takeIntegerInput(); + + output("Great! Now please introduce the initial location of the Rover."); + output("Latitude:"); + int latitudeStartingPoint = takeIntegerInput(); + output("Longitude:"); + int longitudeStartingPoint = takeIntegerInput(); + + Direction startingDirection = directionRetriever.retrieveDirection(); + + MarsRover rover = deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection); + + output(PresentationMessage.READY_MESSAGE); + reader.next(); //input ignored + + return rover; + } + + private int takeIntegerInput() { + try { + return reader.nextInt(); + } catch (InputMismatchException e) { + output("(please, introduce a number)"); + reader.next(); + return takeIntegerInput(); + } + } + + MarsRover autoInitialize() { //to test more quickly + return deployRover(10, 10, 2, 3, Direction.SOUTH); + } + + private MarsRover deployRover(int mapHeight, int mapWidth, int latitudeStartingPoint, int longitudeStartingPoint, Direction startingDirection) { + var startingCoordinates = Coordinates.of(latitudeStartingPoint, longitudeStartingPoint); + var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates); + return new MarsRover(marsMap, startingDirection); + } +}