added new Command abstraction to transport commands with parameters (f.e. PLACE X,Y,F)
This commit is contained in:
parent
2a1566b454
commit
737d4a61ca
6
pom.xml
6
pom.xml
@ -22,6 +22,12 @@
|
||||
<version>1.16.14</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -0,0 +1,8 @@
|
||||
package de.dj_steam.bot.cli;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*/
|
||||
public class InvalidUserInputException extends RuntimeException {
|
||||
public InvalidUserInputException(String message) { super(message); }
|
||||
}
|
@ -3,7 +3,11 @@ package de.dj_steam.bot.cli;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
|
||||
/**
|
||||
@ -13,6 +17,7 @@ import de.dj_steam.bot.engine.RobotEngine;
|
||||
public class LoopingConsole {
|
||||
|
||||
private static final String EXIT_COMMAND = "exit";
|
||||
public static final String COMMAND_DELIMITER = " ";
|
||||
|
||||
public static void main(final String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
@ -24,7 +29,7 @@ public class LoopingConsole {
|
||||
while (true) {
|
||||
System.out.print("> ");
|
||||
String input = br.readLine();
|
||||
robotEngine.commandBot(input);
|
||||
robotEngine.commandBot(createCommand(input));
|
||||
|
||||
if (input.trim().toLowerCase().equals(EXIT_COMMAND)) {
|
||||
System.out.println("exiting");
|
||||
@ -33,6 +38,22 @@ public class LoopingConsole {
|
||||
}
|
||||
}
|
||||
|
||||
static Command createCommand(final String input) {
|
||||
String[] split = input.split(COMMAND_DELIMITER);
|
||||
|
||||
Command command;
|
||||
|
||||
if(split.length == 2) {
|
||||
command = new Command(split[0], Optional.of(split[1]));
|
||||
} else if(split.length == 1 && !StringUtils.isEmpty(split[0])){
|
||||
command = new Command(split[0], Optional.empty());
|
||||
} else {
|
||||
throw new InvalidUserInputException("Invalid user input");
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
private static void printUsageBanner() {
|
||||
System.out.println("####################################################");
|
||||
System.out.println("Commands:");
|
||||
|
29
src/main/java/de/dj_steam/bot/domain/Command.java
Normal file
29
src/main/java/de/dj_steam/bot/domain/Command.java
Normal file
@ -0,0 +1,29 @@
|
||||
package de.dj_steam.bot.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*/
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
public class Command {
|
||||
|
||||
public static final String TURN_LEFT = "LEFT";
|
||||
public static final String TURN_RIGHT = "RIGHT";
|
||||
public static final String MOVE = "MOVE";
|
||||
public static final String PLACE = "PLACE";
|
||||
public static final String REPORT = "REPORT";
|
||||
|
||||
private String command;
|
||||
private Optional<String> arguments;
|
||||
|
||||
public Command(final String command, Optional<String> arguments) {
|
||||
this.command = command;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ package de.dj_steam.bot.engine;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.Direction;
|
||||
import de.dj_steam.bot.domain.Position;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
@ -16,12 +17,6 @@ import de.dj_steam.bot.moving.strategy.TurnStrategy;
|
||||
*/
|
||||
public class RobotEngine {
|
||||
|
||||
public static final String TURN_LEFT = "LEFT";
|
||||
public static final String TURN_RIGHT = "RIGHT";
|
||||
public static final String MOVE = "MOVE";
|
||||
public static final String PLACE = "PLACE";
|
||||
public static final String REPORT = "REPORT";
|
||||
|
||||
private final ToyBotField toyBotField;
|
||||
private final ToyBot toyBot;
|
||||
private List<ChangingStrategy> changingStrategies;
|
||||
@ -38,7 +33,7 @@ public class RobotEngine {
|
||||
initChangingStrategies();
|
||||
}
|
||||
|
||||
public void commandBot(String command) {
|
||||
public void commandBot(Command command) {
|
||||
for (ChangingStrategy strategy : changingStrategies) {
|
||||
strategy.change(toyBot, toyBotField, command);
|
||||
}
|
||||
|
@ -2,9 +2,9 @@ package de.dj_steam.bot.moving;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.Direction;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
@ -18,13 +18,13 @@ public class DirectionCalculator {
|
||||
directions.addAll(Arrays.asList(Direction.values()));
|
||||
}
|
||||
|
||||
public Direction calculateNewDirection(ToyBot toyBot, String turnDirection) {
|
||||
public Direction calculateNewDirection(ToyBot toyBot, Command turnDirection) {
|
||||
|
||||
Direction actualDirection = toyBot.getDirection();
|
||||
|
||||
if (turnDirection.equals(RobotEngine.TURN_LEFT)) {
|
||||
if (turnDirection.getCommand().equals(Command.TURN_LEFT)) {
|
||||
return directions.get(directions.indexOf(actualDirection) - 1);
|
||||
} else if (turnDirection.equals(RobotEngine.TURN_RIGHT)) {
|
||||
} else if (turnDirection.getCommand().equals(Command.TURN_RIGHT)) {
|
||||
return directions.get(directions.indexOf(actualDirection) + 1);
|
||||
}
|
||||
return toyBot.getDirection();
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.domain.ToyBotField;
|
||||
|
||||
@ -7,5 +8,5 @@ import de.dj_steam.bot.domain.ToyBotField;
|
||||
* @author steam
|
||||
*/
|
||||
public interface ChangingStrategy {
|
||||
void change(ToyBot toyBot, ToyBotField toyBotField, String command);
|
||||
void change(ToyBot toyBot, ToyBotField toyBotField, Command command);
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.domain.ToyBotField;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*/
|
||||
public class ReportStrategy implements ChangingStrategy {
|
||||
@Override
|
||||
public void change(ToyBot toyBot, ToyBotField toyBotField, String command) {
|
||||
if (command.equals(RobotEngine.REPORT)) {
|
||||
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
|
||||
if (command.getCommand().equals(Command.REPORT)) {
|
||||
printReport(toyBot);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.domain.ToyBotField;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
import de.dj_steam.bot.moving.DirectionCalculator;
|
||||
|
||||
/**
|
||||
@ -17,8 +17,8 @@ public class TurnStrategy implements ChangingStrategy {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void change(ToyBot toyBot, ToyBotField toyBotField, String command) {
|
||||
if (command.equals(RobotEngine.TURN_LEFT) || command.equals(RobotEngine.TURN_RIGHT)) {
|
||||
public void change(ToyBot toyBot, ToyBotField toyBotField, Command command) {
|
||||
if (command.getCommand().equals(Command.TURN_LEFT) || command.getCommand().equals(Command.TURN_RIGHT)) {
|
||||
toyBot.setDirection(directionCalculator.calculateNewDirection(toyBot, command));
|
||||
}
|
||||
}
|
||||
|
42
src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java
Normal file
42
src/test/java/de/dj_steam/bot/cli/LoopingConsoleTest.java
Normal file
@ -0,0 +1,42 @@
|
||||
package de.dj_steam.bot.cli;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
*/
|
||||
public class LoopingConsoleTest {
|
||||
|
||||
|
||||
@Test(expected = InvalidUserInputException.class)
|
||||
public void createCommandExpectedUserInputExceptionOnEmptyInput() throws Exception {
|
||||
LoopingConsole.createCommand("");
|
||||
}
|
||||
|
||||
@Test(expected = InvalidUserInputException.class)
|
||||
public void createCommandExpectedUserInputExceptionOnMoreThenTwoParams() throws Exception {
|
||||
LoopingConsole.createCommand("INPUT X,Y,F WRONG_PARAM");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCommandWithoutParam() {
|
||||
Command command = LoopingConsole.createCommand("INPUT");
|
||||
assertEquals("INPUT", command.getCommand());
|
||||
assertFalse(command.getArguments().isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createCommandWithParam() {
|
||||
Command command = LoopingConsole.createCommand("INPUT X,Y,F");
|
||||
assertEquals("INPUT", command.getCommand());
|
||||
assertTrue(command.getArguments().isPresent());
|
||||
assertEquals("X,Y,F", command.getArguments().get());
|
||||
}
|
||||
|
||||
}
|
@ -2,9 +2,12 @@ package de.dj_steam.bot.engine;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.Direction;
|
||||
import de.dj_steam.bot.domain.Position;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
@ -28,35 +31,39 @@ public class RobotEngineTest {
|
||||
|
||||
@Test
|
||||
public void testTurnLeftCommand() {
|
||||
Command turnLeftCommand = new Command(Command.TURN_LEFT, Optional.empty());
|
||||
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_LEFT);
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_LEFT);
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_LEFT);
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_LEFT);
|
||||
robotEngine.commandBot(turnLeftCommand);
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnRightCommand() {
|
||||
|
||||
Command turnRightCommand = new Command(Command.TURN_RIGHT, Optional.empty());
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_RIGHT);
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_RIGHT);
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.SOUTH, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_RIGHT);
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
|
||||
robotEngine.commandBot(RobotEngine.TURN_RIGHT);
|
||||
robotEngine.commandBot(turnRightCommand);
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
}
|
||||
}
|
@ -2,13 +2,15 @@ package de.dj_steam.bot.moving;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.Direction;
|
||||
import de.dj_steam.bot.domain.Position;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
|
||||
/**
|
||||
* @author steam
|
||||
@ -25,13 +27,13 @@ public class DirectionCalculatorTest {
|
||||
@Test
|
||||
public void testCalculateOnTurnLeft() {
|
||||
ToyBot toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||
assertEquals(Direction.WEST, directionCalculator.calculateNewDirection(toyBot, RobotEngine.TURN_LEFT));
|
||||
assertEquals(Direction.WEST, directionCalculator.calculateNewDirection(toyBot, new Command(Command.TURN_LEFT, Optional.empty())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculateOnTurnRight() {
|
||||
ToyBot toyBot = new ToyBot(Direction.NORTH, new Position());
|
||||
assertEquals(Direction.EAST, directionCalculator.calculateNewDirection(toyBot, RobotEngine.TURN_RIGHT));
|
||||
assertEquals(Direction.EAST, directionCalculator.calculateNewDirection(toyBot, new Command( Command.TURN_RIGHT, Optional.empty())));
|
||||
}
|
||||
|
||||
}
|
@ -2,17 +2,19 @@ package de.dj_steam.bot.moving.strategy;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import de.dj_steam.bot.domain.Command;
|
||||
import de.dj_steam.bot.domain.Direction;
|
||||
import de.dj_steam.bot.domain.Position;
|
||||
import de.dj_steam.bot.domain.ToyBot;
|
||||
import de.dj_steam.bot.domain.ToyBotField;
|
||||
import de.dj_steam.bot.engine.RobotEngine;
|
||||
|
||||
/**
|
||||
* @author stanislav.nowogrudski
|
||||
* @author steam
|
||||
*/
|
||||
public class TurnStrategyTest {
|
||||
|
||||
@ -28,14 +30,14 @@ public class TurnStrategyTest {
|
||||
@Test
|
||||
public void testTurnLeft() {
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
turnStrategy.change(toyBot, new ToyBotField(), RobotEngine.TURN_LEFT);
|
||||
turnStrategy.change(toyBot, new ToyBotField(), new Command(Command.TURN_LEFT, Optional.empty()));
|
||||
assertEquals(Direction.WEST, toyBot.getDirection());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTurnRight() {
|
||||
assertEquals(Direction.NORTH, toyBot.getDirection());
|
||||
turnStrategy.change(toyBot, new ToyBotField(), RobotEngine.TURN_RIGHT);
|
||||
turnStrategy.change(toyBot, new ToyBotField(), new Command(Command.TURN_RIGHT, Optional.empty()));
|
||||
assertEquals(Direction.EAST, toyBot.getDirection());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user