some small renaming, small class documentation

This commit is contained in:
poweronoff 2017-04-23 21:55:19 +02:00
parent 8336a263a4
commit d582ad6bb0
14 changed files with 77 additions and 33 deletions

View File

@ -2,6 +2,8 @@ package de.dj_steam.bot.cli;
/**
* @author steam
*
* throw this exception if user input was invalid
*/
public class InvalidUserInputException extends RuntimeException {
public InvalidUserInputException(String message) { super(message); }

View File

@ -8,15 +8,24 @@ import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import de.dj_steam.bot.domain.Command;
import de.dj_steam.bot.engine.RobotEngine;
import de.dj_steam.bot.engine.ToyBotEngine;
/**
* @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 {
private static final String EXIT_COMMAND = "exit";
private static final String EXIT_COMMAND = "EXIT";
private static final String COMMAND_DELIMITER = " ";
public static void main(final String[] args) throws IOException {
@ -24,13 +33,13 @@ public class LoopingConsole {
printUsageBanner();
RobotEngine robotEngine = new RobotEngine();
ToyBotEngine toyBotEngine = new ToyBotEngine();
while (true) {
System.out.print("> ");
String input = br.readLine();
try {
robotEngine.commandBot(createCommand(input));
toyBotEngine.commandBot(createCommand(input));
}catch (InvalidUserInputException e) {
System.out.println("\nInput error occurred! Message: " + e.getMessage() +"\n");
printUsageBanner();
@ -62,12 +71,12 @@ public class LoopingConsole {
private static void printUsageBanner() {
System.out.println("####################################################");
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("LEFT - turn the robot to the left");
System.out.println("RIGHT - turn the robot to the right");
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("Enter a command or '" + EXIT_COMMAND + "' to quit");
}

View File

@ -2,6 +2,8 @@ package de.dj_steam.bot.domain;
/**
* @author steam
*
* supported directions to move the robot over the field
*/
public enum Direction {
NORTH,

View File

@ -6,6 +6,8 @@ import lombok.ToString;
/**
* @author steam
*
* pojo class for transport robot postion
*/
@Getter
@ -24,5 +26,4 @@ public class Position {
this.x = x;
this.y = y;
}
}

View File

@ -6,6 +6,11 @@ import lombok.ToString;
/**
* @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

View File

@ -4,7 +4,11 @@ import lombok.Getter;
/**
* @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
public class ToyBotField {
private int width = 4;

View File

@ -16,20 +16,22 @@ import de.dj_steam.bot.moving.strategy.TurnStrategy;
/**
* @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 ToyBot toyBot;
private List<ChangingStrategy> changingStrategies;
public RobotEngine() {
public ToyBotEngine() {
this.toyBot = new ToyBot(Direction.NORTH, new Position());
this.toyBotField = new ToyBotField();
initChangingStrategies();
}
RobotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
ToyBotEngine(final ToyBotField toyBotField, final ToyBot toyBot) {
this.toyBotField = toyBotField;
this.toyBot = toyBot;
initChangingStrategies();

View File

@ -8,6 +8,8 @@ import de.dj_steam.bot.domain.ToyBot;
/**
* @author steam
*
* this is a helper class to calculate direction
*/
public class DirectionCalculator {

View File

@ -4,6 +4,15 @@ import java.util.ArrayList;
/**
* @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> {

View File

@ -8,6 +8,8 @@ import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*
* strategy implementing how the ToyBot is moving
*/
public class MoveStrategy implements ChangingStrategy {
@Override

View File

@ -11,6 +11,8 @@ import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*
* strategy implementing the place logic of the ToyBot on the ToyBotField
*/
public class PlaceStrategy implements ChangingStrategy {

View File

@ -6,6 +6,8 @@ import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*
* strategy implementing how the ToyBot reporting his position and facing direction
*/
public class ReportStrategy implements ChangingStrategy {
@Override

View File

@ -7,6 +7,8 @@ import de.dj_steam.bot.moving.DirectionCalculator;
/**
* @author steam
*
* strategy implementing how the ToyBot do the turn
*/
public class TurnStrategy implements ChangingStrategy {

View File

@ -16,9 +16,9 @@ import de.dj_steam.bot.domain.ToyBotField;
/**
* @author steam
*/
public class RobotEngineTest {
public class ToyBotEngineTest {
private RobotEngine robotEngine;
private ToyBotEngine toyBotEngine;
private ToyBot toyBot;
private Command turnLeftCommand;
@ -31,7 +31,7 @@ public class RobotEngineTest {
@Before
public void setup() {
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());
turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
moveCommand = new Command(Command.MOVE, Optional.empty());
@ -43,16 +43,16 @@ public class RobotEngineTest {
public void testTurnLeftCommand() {
assertEquals(Direction.NORTH, toyBot.getDirection());
robotEngine.commandBot(turnLeftCommand);
toyBotEngine.commandBot(turnLeftCommand);
assertEquals(Direction.WEST, toyBot.getDirection());
robotEngine.commandBot(turnLeftCommand);
toyBotEngine.commandBot(turnLeftCommand);
assertEquals(Direction.SOUTH, toyBot.getDirection());
robotEngine.commandBot(turnLeftCommand);
toyBotEngine.commandBot(turnLeftCommand);
assertEquals(Direction.EAST, toyBot.getDirection());
robotEngine.commandBot(turnLeftCommand);
toyBotEngine.commandBot(turnLeftCommand);
assertEquals(Direction.NORTH, toyBot.getDirection());
}
@ -60,16 +60,16 @@ public class RobotEngineTest {
public void testTurnRightCommand() {
assertEquals(Direction.NORTH, toyBot.getDirection());
robotEngine.commandBot(turnRightCommand);
toyBotEngine.commandBot(turnRightCommand);
assertEquals(Direction.EAST, toyBot.getDirection());
robotEngine.commandBot(turnRightCommand);
toyBotEngine.commandBot(turnRightCommand);
assertEquals(Direction.SOUTH, toyBot.getDirection());
robotEngine.commandBot(turnRightCommand);
toyBotEngine.commandBot(turnRightCommand);
assertEquals(Direction.WEST, toyBot.getDirection());
robotEngine.commandBot(turnRightCommand);
toyBotEngine.commandBot(turnRightCommand);
assertEquals(Direction.NORTH, toyBot.getDirection());
}
@ -77,19 +77,19 @@ public class RobotEngineTest {
public void testPlaceCommand() {
assertEquals(new Position(0, 0), toyBot.getPosition());
robotEngine.commandBot(placeCommand);
toyBotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 0), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("0,2,NORTH"));
robotEngine.commandBot(placeCommand);
toyBotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 2), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("5,1,NORTH"));
robotEngine.commandBot(placeCommand);
toyBotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 2), toyBot.getPosition());
placeCommand = new Command(Command.PLACE, Optional.of("0,3,WEST"));
robotEngine.commandBot(placeCommand);
toyBotEngine.commandBot(placeCommand);
assertEquals(new Position(0, 3), toyBot.getPosition());
assertEquals(Direction.WEST, toyBot.getDirection());
}
@ -100,11 +100,11 @@ public class RobotEngineTest {
assertEquals(Direction.NORTH, toyBot.getDirection());
for (int i = 1; i <= 4; i++) {
robotEngine.commandBot(moveCommand);
toyBotEngine.commandBot(moveCommand);
assertEquals(new Position(0, i), toyBot.getPosition());
}
robotEngine.commandBot(moveCommand);
toyBotEngine.commandBot(moveCommand);
assertEquals(new Position(0, 4), toyBot.getPosition());
}
@ -113,25 +113,25 @@ public class RobotEngineTest {
assertEquals(new Position(0, 0), toyBot.getPosition());
assertEquals(Direction.NORTH, toyBot.getDirection());
robotEngine.commandBot(moveCommand);
toyBotEngine.commandBot(moveCommand);
assertEquals(new Position(0, 1), toyBot.getPosition());
assertEquals(Direction.NORTH, toyBot.getDirection());
robotEngine.commandBot(turnRightCommand);
toyBotEngine.commandBot(turnRightCommand);
assertEquals(Direction.EAST, toyBot.getDirection());
for (int i = 0; i <= 3; i++) {
robotEngine.commandBot(moveCommand);
toyBotEngine.commandBot(moveCommand);
}
assertEquals(new Position(4, 1), toyBot.getPosition());
robotEngine.commandBot(moveCommand); // border
toyBotEngine.commandBot(moveCommand); // border
assertEquals(new Position(4, 1), toyBot.getPosition());
robotEngine.commandBot(turnLeftCommand);
toyBotEngine.commandBot(turnLeftCommand);
for (int i = 0; i <= 3; i++) {
robotEngine.commandBot(moveCommand);
toyBotEngine.commandBot(moveCommand);
}
assertEquals(new Position(4, 4), toyBot.getPosition());