1
0
Fork 0

split more and leave procedural programming to return to OOP

(cherry picked from commit 303d839249)
This commit is contained in:
Xavier Fontanet 2024-06-21 18:11:08 +02:00
parent fd7deec805
commit 91e91251f0
4 changed files with 125 additions and 67 deletions

View File

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

View File

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

View File

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

View File

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