some small renaming, small class documentation
This commit is contained in:
parent
8336a263a4
commit
d582ad6bb0
@ -2,6 +2,8 @@ package de.dj_steam.bot.cli;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* throw this exception if user input was invalid
|
||||||
*/
|
*/
|
||||||
public class InvalidUserInputException extends RuntimeException {
|
public class InvalidUserInputException extends RuntimeException {
|
||||||
public InvalidUserInputException(String message) { super(message); }
|
public InvalidUserInputException(String message) { super(message); }
|
||||||
|
@ -8,15 +8,24 @@ import java.util.Optional;
|
|||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
import de.dj_steam.bot.domain.Command;
|
import de.dj_steam.bot.domain.Command;
|
||||||
import de.dj_steam.bot.engine.RobotEngine;
|
import de.dj_steam.bot.engine.ToyBotEngine;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* this is a main class.
|
||||||
|
*
|
||||||
|
* it realises the user interation over the standart input
|
||||||
|
*
|
||||||
|
* This class have following concern:
|
||||||
|
* - The {@link ToyBotEngine} will be create here.
|
||||||
|
* - Main setup of the application
|
||||||
|
* - Create command depending on user interaction
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class LoopingConsole {
|
public class LoopingConsole {
|
||||||
|
|
||||||
private static final String EXIT_COMMAND = "exit";
|
private static final String EXIT_COMMAND = "EXIT";
|
||||||
private static final String COMMAND_DELIMITER = " ";
|
private static final String COMMAND_DELIMITER = " ";
|
||||||
|
|
||||||
public static void main(final String[] args) throws IOException {
|
public static void main(final String[] args) throws IOException {
|
||||||
@ -24,13 +33,13 @@ public class LoopingConsole {
|
|||||||
|
|
||||||
printUsageBanner();
|
printUsageBanner();
|
||||||
|
|
||||||
RobotEngine robotEngine = new RobotEngine();
|
ToyBotEngine toyBotEngine = new ToyBotEngine();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.print("> ");
|
System.out.print("> ");
|
||||||
String input = br.readLine();
|
String input = br.readLine();
|
||||||
try {
|
try {
|
||||||
robotEngine.commandBot(createCommand(input));
|
toyBotEngine.commandBot(createCommand(input));
|
||||||
}catch (InvalidUserInputException e) {
|
}catch (InvalidUserInputException e) {
|
||||||
System.out.println("\nInput error occurred! Message: " + e.getMessage() +"\n");
|
System.out.println("\nInput error occurred! Message: " + e.getMessage() +"\n");
|
||||||
printUsageBanner();
|
printUsageBanner();
|
||||||
@ -62,12 +71,12 @@ public class LoopingConsole {
|
|||||||
private static void printUsageBanner() {
|
private static void printUsageBanner() {
|
||||||
System.out.println("####################################################");
|
System.out.println("####################################################");
|
||||||
System.out.println("Commands:");
|
System.out.println("Commands:");
|
||||||
System.out.println("PLACE X,Y,F - place robot on position X,Y - coordinates, and direction (NORTH|SOUTH|WEST|EAST)");
|
System.out.println("PLACE X,Y,F - place robot on position X,Y - coordinates and direction (NORTH|SOUTH|WEST|EAST)");
|
||||||
System.out.println("MOVE - move the robot to the next field in facing direction");
|
System.out.println("MOVE - move the robot to the next field in facing direction");
|
||||||
System.out.println("LEFT - turn the robot to the left");
|
System.out.println("LEFT - turn the robot to the left");
|
||||||
System.out.println("RIGHT - turn the robot to the right");
|
System.out.println("RIGHT - turn the robot to the right");
|
||||||
System.out.println("REPORT - show robots position and facing direction");
|
System.out.println("REPORT - show robots position and facing direction");
|
||||||
System.out.println("exit - exit the application");
|
System.out.println("EXIT - exit the application");
|
||||||
System.out.println("####################################################");
|
System.out.println("####################################################");
|
||||||
System.out.println("Enter a command or '" + EXIT_COMMAND + "' to quit");
|
System.out.println("Enter a command or '" + EXIT_COMMAND + "' to quit");
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package de.dj_steam.bot.domain;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* supported directions to move the robot over the field
|
||||||
*/
|
*/
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
NORTH,
|
NORTH,
|
||||||
|
@ -6,6 +6,8 @@ import lombok.ToString;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* pojo class for transport robot postion
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -24,5 +26,4 @@ public class Position {
|
|||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,11 @@ import lombok.ToString;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* pojo representing the robot, it have following properties
|
||||||
|
*
|
||||||
|
* {@link Direction} - current facing of the robot
|
||||||
|
* {@link Position} - current posiotion of the robot
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -4,7 +4,11 @@ import lombok.Getter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* the field to move robot over. It incapsulate the logic for calculate that the robot will
|
||||||
|
* move over the border on the next step or on place it on the field
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public class ToyBotField {
|
public class ToyBotField {
|
||||||
private int width = 4;
|
private int width = 4;
|
||||||
|
@ -16,20 +16,22 @@ import de.dj_steam.bot.moving.strategy.TurnStrategy;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* toyBotEngine implements setup of moving strategies and initial setup of ToyBot and ToyBotField
|
||||||
*/
|
*/
|
||||||
public class RobotEngine {
|
public class ToyBotEngine {
|
||||||
|
|
||||||
private final ToyBotField toyBotField;
|
private final ToyBotField toyBotField;
|
||||||
private final ToyBot toyBot;
|
private final ToyBot toyBot;
|
||||||
private List<ChangingStrategy> changingStrategies;
|
private List<ChangingStrategy> changingStrategies;
|
||||||
|
|
||||||
public RobotEngine() {
|
public ToyBotEngine() {
|
||||||
this.toyBot = new ToyBot(Direction.NORTH, new Position());
|
this.toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||||
this.toyBotField = new ToyBotField();
|
this.toyBotField = new ToyBotField();
|
||||||
initChangingStrategies();
|
initChangingStrategies();
|
||||||
}
|
}
|
||||||
|
|
||||||
RobotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
|
ToyBotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
|
||||||
this.toyBotField = toyBotField;
|
this.toyBotField = toyBotField;
|
||||||
this.toyBot = toyBot;
|
this.toyBot = toyBot;
|
||||||
initChangingStrategies();
|
initChangingStrategies();
|
@ -8,6 +8,8 @@ import de.dj_steam.bot.domain.ToyBot;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* this is a helper class to calculate direction
|
||||||
*/
|
*/
|
||||||
public class DirectionCalculator {
|
public class DirectionCalculator {
|
||||||
|
|
||||||
|
@ -4,6 +4,15 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* this class is a simple looped ArrayList.
|
||||||
|
*
|
||||||
|
* it is possible to get indexed elements ot this list, which are over the size of the Array.
|
||||||
|
*
|
||||||
|
* the get(x) method will return the right element of list from the loop. It works with negative
|
||||||
|
* values to.
|
||||||
|
*
|
||||||
|
* This class does NOT support looped iterator.
|
||||||
*/
|
*/
|
||||||
public class LoopedDirectionsArrayList<Direction> extends ArrayList<Direction> {
|
public class LoopedDirectionsArrayList<Direction> extends ArrayList<Direction> {
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@ import de.dj_steam.bot.domain.ToyBotField;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* strategy implementing how the ToyBot is moving
|
||||||
*/
|
*/
|
||||||
public class MoveStrategy implements ChangingStrategy {
|
public class MoveStrategy implements ChangingStrategy {
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,6 +11,8 @@ import de.dj_steam.bot.domain.ToyBotField;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* strategy implementing the place logic of the ToyBot on the ToyBotField
|
||||||
*/
|
*/
|
||||||
public class PlaceStrategy implements ChangingStrategy {
|
public class PlaceStrategy implements ChangingStrategy {
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ import de.dj_steam.bot.domain.ToyBotField;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* strategy implementing how the ToyBot reporting his position and facing direction
|
||||||
*/
|
*/
|
||||||
public class ReportStrategy implements ChangingStrategy {
|
public class ReportStrategy implements ChangingStrategy {
|
||||||
@Override
|
@Override
|
||||||
|
@ -7,6 +7,8 @@ import de.dj_steam.bot.moving.DirectionCalculator;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
|
*
|
||||||
|
* strategy implementing how the ToyBot do the turn
|
||||||
*/
|
*/
|
||||||
public class TurnStrategy implements ChangingStrategy {
|
public class TurnStrategy implements ChangingStrategy {
|
||||||
|
|
||||||
|
@ -16,9 +16,9 @@ import de.dj_steam.bot.domain.ToyBotField;
|
|||||||
/**
|
/**
|
||||||
* @author steam
|
* @author steam
|
||||||
*/
|
*/
|
||||||
public class RobotEngineTest {
|
public class ToyBotEngineTest {
|
||||||
|
|
||||||
private RobotEngine robotEngine;
|
private ToyBotEngine toyBotEngine;
|
||||||
private ToyBot toyBot;
|
private ToyBot toyBot;
|
||||||
|
|
||||||
private Command turnLeftCommand;
|
private Command turnLeftCommand;
|
||||||
@ -31,7 +31,7 @@ public class RobotEngineTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
toyBot = new ToyBot(Direction.NORTH, new Position());
|
toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||||
robotEngine = new RobotEngine(toyBotField, toyBot);
|
toyBotEngine = new ToyBotEngine(toyBotField, toyBot);
|
||||||
turnLeftCommand = new Command(Command.TURN_LEFT, Optional.empty());
|
turnLeftCommand = new Command(Command.TURN_LEFT, Optional.empty());
|
||||||
turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
|
turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
|
||||||
moveCommand = new Command(Command.MOVE, Optional.empty());
|
moveCommand = new Command(Command.MOVE, Optional.empty());
|
||||||
@ -43,16 +43,16 @@ public class RobotEngineTest {
|
|||||||
public void testTurnLeftCommand() {
|
public void testTurnLeftCommand() {
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnLeftCommand);
|
toyBotEngine.commandBot(turnLeftCommand);
|
||||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnLeftCommand);
|
toyBotEngine.commandBot(turnLeftCommand);
|
||||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnLeftCommand);
|
toyBotEngine.commandBot(turnLeftCommand);
|
||||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnLeftCommand);
|
toyBotEngine.commandBot(turnLeftCommand);
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,16 +60,16 @@ public class RobotEngineTest {
|
|||||||
public void testTurnRightCommand() {
|
public void testTurnRightCommand() {
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnRightCommand);
|
toyBotEngine.commandBot(turnRightCommand);
|
||||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnRightCommand);
|
toyBotEngine.commandBot(turnRightCommand);
|
||||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnRightCommand);
|
toyBotEngine.commandBot(turnRightCommand);
|
||||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnRightCommand);
|
toyBotEngine.commandBot(turnRightCommand);
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,19 +77,19 @@ public class RobotEngineTest {
|
|||||||
public void testPlaceCommand() {
|
public void testPlaceCommand() {
|
||||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||||
|
|
||||||
robotEngine.commandBot(placeCommand);
|
toyBotEngine.commandBot(placeCommand);
|
||||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||||
|
|
||||||
placeCommand = new Command(Command.PLACE, Optional.of("0,2,NORTH"));
|
placeCommand = new Command(Command.PLACE, Optional.of("0,2,NORTH"));
|
||||||
robotEngine.commandBot(placeCommand);
|
toyBotEngine.commandBot(placeCommand);
|
||||||
assertEquals(new Position(0, 2), toyBot.getPosition());
|
assertEquals(new Position(0, 2), toyBot.getPosition());
|
||||||
|
|
||||||
placeCommand = new Command(Command.PLACE, Optional.of("5,1,NORTH"));
|
placeCommand = new Command(Command.PLACE, Optional.of("5,1,NORTH"));
|
||||||
robotEngine.commandBot(placeCommand);
|
toyBotEngine.commandBot(placeCommand);
|
||||||
assertEquals(new Position(0, 2), toyBot.getPosition());
|
assertEquals(new Position(0, 2), toyBot.getPosition());
|
||||||
|
|
||||||
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
|
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
|
||||||
robotEngine.commandBot(placeCommand);
|
toyBotEngine.commandBot(placeCommand);
|
||||||
assertEquals(new Position(0, 3), toyBot.getPosition());
|
assertEquals(new Position(0, 3), toyBot.getPosition());
|
||||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||||
}
|
}
|
||||||
@ -100,11 +100,11 @@ public class RobotEngineTest {
|
|||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
|
|
||||||
for (int i = 1; i <= 4; i++) {
|
for (int i = 1; i <= 4; i++) {
|
||||||
robotEngine.commandBot(moveCommand);
|
toyBotEngine.commandBot(moveCommand);
|
||||||
assertEquals(new Position(0, i), toyBot.getPosition());
|
assertEquals(new Position(0, i), toyBot.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
robotEngine.commandBot(moveCommand);
|
toyBotEngine.commandBot(moveCommand);
|
||||||
assertEquals(new Position(0, 4), toyBot.getPosition());
|
assertEquals(new Position(0, 4), toyBot.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,25 +113,25 @@ public class RobotEngineTest {
|
|||||||
assertEquals(new Position(0, 0), toyBot.getPosition());
|
assertEquals(new Position(0, 0), toyBot.getPosition());
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(moveCommand);
|
toyBotEngine.commandBot(moveCommand);
|
||||||
assertEquals(new Position(0, 1), toyBot.getPosition());
|
assertEquals(new Position(0, 1), toyBot.getPosition());
|
||||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||||
|
|
||||||
robotEngine.commandBot(turnRightCommand);
|
toyBotEngine.commandBot(turnRightCommand);
|
||||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||||
|
|
||||||
for (int i = 0; i <= 3; i++) {
|
for (int i = 0; i <= 3; i++) {
|
||||||
robotEngine.commandBot(moveCommand);
|
toyBotEngine.commandBot(moveCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||||
|
|
||||||
robotEngine.commandBot(moveCommand); // border
|
toyBotEngine.commandBot(moveCommand); // border
|
||||||
assertEquals(new Position(4, 1), toyBot.getPosition());
|
assertEquals(new Position(4, 1), toyBot.getPosition());
|
||||||
|
|
||||||
robotEngine.commandBot(turnLeftCommand);
|
toyBotEngine.commandBot(turnLeftCommand);
|
||||||
for (int i = 0; i <= 3; i++) {
|
for (int i = 0; i <= 3; i++) {
|
||||||
robotEngine.commandBot(moveCommand);
|
toyBotEngine.commandBot(moveCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(new Position(4, 4), toyBot.getPosition());
|
assertEquals(new Position(4, 4), toyBot.getPosition());
|
Loading…
Reference in New Issue
Block a user