1
0
Fork 0

split a bit the main class

This commit is contained in:
Xavier Fontanet 2024-06-21 17:41:09 +02:00
parent 43bd81c8e3
commit 44cfcaf3ca
4 changed files with 110 additions and 88 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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)
""";
}

View File

@ -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);
}
}