From 44cfcaf3cab0f65035dc88befe5b567fa912996a Mon Sep 17 00:00:00 2001 From: Xavier Fontanet Date: Fri, 21 Jun 2024 17:41:09 +0200 Subject: [PATCH] split a bit the main class --- .../marsrover/ui/ClientCommandInterface.java | 94 ++----------------- .../marsrover/ui/DirectionRetriever.java | 34 +++++++ .../marsrover/ui/PresentationMessage.java | 52 ++++++++++ .../codingtests/marsrover/ui/UICommons.java | 18 ++++ 4 files changed, 110 insertions(+), 88 deletions(-) create mode 100644 src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java create mode 100644 src/main/java/cat/hack3/codingtests/marsrover/ui/PresentationMessage.java create mode 100644 src/main/java/cat/hack3/codingtests/marsrover/ui/UICommons.java 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 7187457..4914755 100644 --- a/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/ClientCommandInterface.java @@ -8,62 +8,15 @@ 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; - public static final String INTRO = """ - ░▒▓██████████████▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓██████▓▒░ ░▒▓███████▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ - ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓██▓▒░ ░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ - - Welcome to Mars Rover Command Line Interface! - - Please, establish the Mars dimensions. Bear in mind the Mars-Coordinates-Representation is aligned with the following principles: - - Latitude is Y, starts from 1 on the top and goes down incrementally - - Longitude is X, starts from 1 on the left and goes to right incrementally - - Here a graphical representation of a 3x3 map - - 1 2 3 - 1 1-1 1-2 1-3 - 2 2-1 2-2 2-3 - 3 3-1 3-2 3-3 - - """; - - public static final String READY_MESSAGE = """ - - Fantastic! - - A GPS, named MarsMap, has been delivered to Rover, configured with: - - The size information of the planet - - The current location of the Rover in it - - He now can start moving along the planet based on your commands - After every movement, he will check the GPS and report back the new position - About rotations, Rover has a build-in accelerometer, which let him know in what direction is pointing out at any moment - That's all for now. Have fun! - _ - [ ] - ( ) - |>| - __/===\\__ - //| o=o |\\\\ - <] | o=o | [> - \\=====/ - / / | \\ \\ - <_________> - - (to continue, enter your name) - """; - public static void main(String[] args) { - output(INTRO); + output(PresentationMessage.INTRO); try (var reader = new Scanner(System.in)) { //autoInitialization(); @@ -88,11 +41,11 @@ public class ClientCommandInterface { output("Longitude:"); int longitudeStartingPoint = takeIntegerInput(reader); - Direction startingDirection = retrieveDirection(reader); + Direction startingDirection = DirectionRetriever.retrieveDirection(reader); deployRover(mapHeight, mapWidth, latitudeStartingPoint, longitudeStartingPoint, startingDirection); - output(READY_MESSAGE); + output(PresentationMessage.READY_MESSAGE); reader.next(); //input ignored } @@ -106,27 +59,6 @@ public class ClientCommandInterface { } } - private static Direction retrieveDirection(Scanner reader) { - var directionInput = "none"; - Direction resolvedDirection; - do { - output("Insert initial rover direction (n = north, e = east, w = west, s = south, q = exit):"); - directionInput = reader.next(); - resolvedDirection = switch (directionInput) { - case "n" -> Direction.NORTH; - case "e" -> Direction.EAST; - case "w" -> Direction.WEST; - case "s" -> Direction.SOUTH; - default -> null; - }; - } while (isNotResolvedDirection(resolvedDirection) && isNotExitSignal(directionInput)); - - if (isExitSignal(directionInput)) - System.exit(0); - - return resolvedDirection; - } - 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); @@ -149,18 +81,4 @@ public class ClientCommandInterface { } while (isNotExitSignal(nextCommand)); } - private static boolean isNotResolvedDirection(Direction resolvedDirection) { - return resolvedDirection == null; - } - - private static boolean isExitSignal(String nextCommand) { - return "q".equals(nextCommand); - } - private static boolean isNotExitSignal(String nextCommand) { - return !isExitSignal(nextCommand); - } - - private static void output(String message) { - System.out.println(message); - } } diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java new file mode 100644 index 0000000..da47598 --- /dev/null +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/DirectionRetriever.java @@ -0,0 +1,34 @@ +package cat.hack3.codingtests.marsrover.ui; + +import cat.hack3.codingtests.marsrover.Direction; + +import java.util.Scanner; + +import static cat.hack3.codingtests.marsrover.ui.UICommons.*; + +public class DirectionRetriever { + static Direction retrieveDirection(Scanner reader) { + var directionInput = "none"; + Direction resolvedDirection; + do { + output("Insert initial rover direction (n = north, e = east, w = west, s = south, q = exit):"); + directionInput = reader.next(); + resolvedDirection = switch (directionInput) { + case "n" -> Direction.NORTH; + case "e" -> Direction.EAST; + case "w" -> Direction.WEST; + case "s" -> Direction.SOUTH; + default -> null; + }; + } while (isNotResolvedDirection(resolvedDirection) && isNotExitSignal(directionInput)); + + if (isExitSignal(directionInput)) + System.exit(0); + + return resolvedDirection; + } + + private static boolean isNotResolvedDirection(Direction resolvedDirection) { + return resolvedDirection == null; + } +} diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/PresentationMessage.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/PresentationMessage.java new file mode 100644 index 0000000..2de9f42 --- /dev/null +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/PresentationMessage.java @@ -0,0 +1,52 @@ +package cat.hack3.codingtests.marsrover.ui; + +public interface PresentationMessage { + String INTRO = """ + ░▒▓██████████████▓▒░ ░▒▓██████▓▒░░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒▒▓█▓▒░░▒▓██████▓▒░ ░▒▓███████▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▓█▓▒░ ░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ + ░▒▓█▓▒░░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░▒▓███████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░░▒▓██████▓▒░ ░▒▓██▓▒░ ░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ + + Welcome to Mars Rover Command Line Interface! + + Please, establish the Mars dimensions. Bear in mind the Mars-Coordinates-Representation is aligned with the following principles: + - Latitude is Y, starts from 1 on the top and goes down incrementally + - Longitude is X, starts from 1 on the left and goes to right incrementally + + Here a graphical representation of a 3x3 map + + 1 2 3 + 1 1-1 1-2 1-3 + 2 2-1 2-2 2-3 + 3 3-1 3-2 3-3 + + """; + String READY_MESSAGE = """ + + Fantastic! + + A GPS, named MarsMap, has been delivered to Rover, configured with: + - The size information of the planet + - The current location of the Rover in it + + He now can start moving along the planet based on your commands + After every movement, he will check the GPS and report back the new position + About rotations, Rover has a build-in accelerometer, which let him know in what direction is pointing out at any moment + That's all for now. Have fun! + _ + [ ] + ( ) + |>| + __/===\\__ + //| o=o |\\\\ + <] | o=o | [> + \\=====/ + / / | \\ \\ + <_________> + + (to continue, enter your name) + """; +} diff --git a/src/main/java/cat/hack3/codingtests/marsrover/ui/UICommons.java b/src/main/java/cat/hack3/codingtests/marsrover/ui/UICommons.java new file mode 100644 index 0000000..3fe0030 --- /dev/null +++ b/src/main/java/cat/hack3/codingtests/marsrover/ui/UICommons.java @@ -0,0 +1,18 @@ +package cat.hack3.codingtests.marsrover.ui; + +public class UICommons { + + public static final String QUIT_COMMAND_TEXT = "q"; + + static boolean isExitSignal(String nextCommand) { + return QUIT_COMMAND_TEXT.equals(nextCommand); + } + + static boolean isNotExitSignal(String nextCommand) { + return !isExitSignal(nextCommand); + } + + static void output(String message) { + System.out.println(message); + } +}