Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solid Principles have been added #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/mars/rover/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mars.rover;

public interface Direction {
Direction turnLeft();
Direction turnRight();
void move(Point point);
char currentDirection(); // current direction as character

}
17 changes: 17 additions & 0 deletions src/main/java/mars/rover/DirectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mars.rover;
public class DirectionFactory {
public static Direction getDirection(char direction) {
switch (direction) {
case 'N':
return new North();
case 'S':
return new South();
case 'E':
return new East();
case 'W':
return new West();
default:
throw new IllegalArgumentException("Invalid direction: " + direction);
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/mars/rover/East.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mars.rover;
public class East implements Direction {
@Override
public Direction turnLeft() {
return new North();
}

@Override
public Direction turnRight() {
return new South();
}

@Override
public void move(Point point) {
point.setX(point.getX() + 1);
}

@Override
public char currentDirection() {
return 'E';
}
}
49 changes: 17 additions & 32 deletions src/main/java/mars/rover/MarsRover.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,27 @@
package mars.rover;

public class MarsRover {
private Point point;
private Direction direction;

public static String move(int x, int y, char direction, String instructions) {
if (!instructions.isEmpty()) {
char instruction = instructions.charAt(0);
public MarsRover(int x, int y, char direction) {
this.point = new Point(x, y);
this.direction = DirectionFactory.getDirection(direction);
}

public void execute(String instructions) {
for (char instruction : instructions.toCharArray()) {
if (instruction == 'L') {
if (direction == 'N') {
return move(x, y, 'W', instructions.substring(1, instructions.length()));
} else if (direction == 'W') {
return move(x, y, 'S', instructions.substring(1, instructions.length()));
} else if (direction == 'S') {
return move(x, y, 'E', instructions.substring(1, instructions.length()));
} else if (direction == 'E') {
return move(x, y, 'N', instructions.substring(1, instructions.length()));
}
direction = direction.turnLeft();
} else if (instruction == 'R') {
if (direction == 'N') {
return move(x, y, 'E', instructions.substring(1, instructions.length()));
} else if (direction == 'W') {
return move(x, y, 'N', instructions.substring(1, instructions.length()));
} else if (direction == 'S') {
return move(x, y, 'W', instructions.substring(1, instructions.length()));
} else if (direction == 'E') {
return move(x, y, 'S', instructions.substring(1, instructions.length()));
}
direction = direction.turnRight();
} else if (instruction == 'M') {
if (direction == 'N') {
return move(x, y + 1, 'N', instructions.substring(1, instructions.length()));
} else if (direction == 'S') {
return move(x, y - 1, 'S', instructions.substring(1, instructions.length()));
} else if (direction == 'W') {
return move(x - 1, y, 'W', instructions.substring(1, instructions.length()));
} else if (direction == 'E') {
return move(x + 1, y, 'E', instructions.substring(1, instructions.length()));
}
direction.move(point);
}
}
return x + " " + y + " " + direction;
}
}

public String getPosition() {
return point.getX() + " " + point.getY() + " " + direction.currentDirection();
}
}
23 changes: 23 additions & 0 deletions src/main/java/mars/rover/North.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mars.rover;

public class North implements Direction {
@Override
public Direction turnLeft() {
return new West();
}

@Override
public Direction turnRight() {
return new East();
}

@Override
public void move(Point point) {
point.setY(point.getY() + 1);
}

@Override
public char currentDirection() {
return 'N';
}
}
26 changes: 26 additions & 0 deletions src/main/java/mars/rover/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package mars.rover;
public class Point {
private int x;
private int y;

public Point( int x, int y){
this.x=x;
this.y=y;
}

public int getX() {
return this.x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return this.y;
}

public void setY(int y) {
this.y = y;
}
}
22 changes: 22 additions & 0 deletions src/main/java/mars/rover/South.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mars.rover;
public class South implements Direction {
@Override
public Direction turnLeft() {
return new East();
}

@Override
public Direction turnRight() {
return new West();
}

@Override
public void move(Point point) {
point.setY(point.getY() - 1);
}

@Override
public char currentDirection() {
return 'S';
}
}
22 changes: 22 additions & 0 deletions src/main/java/mars/rover/West.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mars.rover;
public class West implements Direction {
@Override
public Direction turnLeft() {
return new South();
}

@Override
public Direction turnRight() {
return new North();
}

@Override
public void move(Point point) {
point.setX(point.getX() - 1);
}

@Override
public char currentDirection() {
return 'W';
}
}
27 changes: 13 additions & 14 deletions src/test/java/mars/rover/MarsRoverTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package mars.rover;

package mars.rover;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class MarsRoverTest {
public class MarsRoverTest {

@Test
public void
acceptance_test_1() {
String newPosition = MarsRover.move(1, 2, 'N', "LMLMLMLMM");
assertEquals("1 3 N", newPosition);
}
public void testExecuteAndGetPosition() {
MarsRover rover = new MarsRover(0, 0, 'N');
rover.execute("M");
assertEquals("0 1 N", rover.getPosition());

@Test
public void
acceptance_test_2() {
String newPosition = MarsRover.move(3, 3, 'E', "MMRMMRMRRM");
assertEquals("5 1 E", newPosition);
}
rover = new MarsRover(0, 0, 'N');
rover.execute("MMRMM");
assertEquals("2 2 E", rover.getPosition());

rover = new MarsRover(2, 2, 'E');
rover.execute("MMRMM");
assertEquals("4 0 S", rover.getPosition());
}
}