41 lines
1.2 KiB
Java
41 lines
1.2 KiB
Java
package cat.hack3.codingtests.marsrover.ui;
|
|
|
|
import cat.hack3.codingtests.marsrover.core.Direction;
|
|
|
|
import java.util.Scanner;
|
|
|
|
import static cat.hack3.codingtests.marsrover.ui.UICommons.*;
|
|
|
|
public class DirectionRetriever {
|
|
private final Scanner reader;
|
|
|
|
public DirectionRetriever(Scanner reader) {
|
|
this.reader = reader;
|
|
}
|
|
|
|
Direction retrieveDirection() {
|
|
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 boolean isNotResolvedDirection(Direction resolvedDirection) {
|
|
return resolvedDirection == null;
|
|
}
|
|
}
|