Skip to content

Commit 4209dfb

Browse files
committed
splitted the rover.java into classes(following SRP)
1 parent 9d766af commit 4209dfb

5 files changed

+94
-0
lines changed

MoveAndChangeDirectionOfRover.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
public class MoveAndChangeDirectionOfRover{
3+
public void turnLeft(NewRover r) {
4+
//System.out.println("left" + face);
5+
r.face = (r.face - 1) < NewRover.N ? NewRover.W : r.face -1;
6+
}
7+
public void turnRight(NewRover r) {
8+
r.face = (r.face + 1) > NewRover.W ? NewRover.N : r.face + 1;
9+
}
10+
public void moveForward(NewRover r) {
11+
if ((r.face == NewRover.N) && (r.y < r.limity)) {
12+
r.y++;
13+
} else if ((r.face == NewRover.E) && (r.x < r.limitx)) {
14+
r.x++;
15+
} else if ((r.face == NewRover.S) && (r.y > 0)) {
16+
r.y--;
17+
} else if ((r.face == NewRover.W) && (r.x > 0)) {
18+
r.x--;
19+
}
20+
}
21+
}

NewRover.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
public class NewRover {
3+
final static int N = 1;
4+
final static int E = 2;
5+
final static int S = 3;
6+
final static int W = 4;
7+
int limitx = 0;
8+
int limity = 0;
9+
int x = 0;
10+
int y = 0;
11+
int face = N;
12+
//Constructor for NewRover
13+
NewRover(){}
14+
NewRover(int limx, int limy, int x, int y, int face) {
15+
this.limitx = limx;
16+
this.limity = limy;
17+
this.x = x;
18+
this.y = y;
19+
this.face = face;
20+
}
21+
22+
public void moveRover(String commands) {
23+
RoverCommandsProcessor runCommands = new RoverCommandsProcessor();
24+
for (int x = 0; x < commands.length(); x++) {
25+
runCommands.roverStart(this, commands.charAt(x));
26+
}
27+
}
28+
}

PrintRoverLocation.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
public class PrintRoverLocation {
3+
public void printPosition(NewRover x) {
4+
System.out.print(x.x + " " + x.y + " ");
5+
if (x.face == 1) {
6+
System.out.println('N');
7+
} else if (x.face == 2) {
8+
System.out.println('E');
9+
} else if (x.face == 3) {
10+
System.out.println('S');
11+
} else if (x.face == 4) {
12+
System.out.println('W');
13+
}
14+
}
15+
}

RoverCommandsProcessor.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
public class RoverCommandsProcessor {
3+
MoveAndChangeDirectionOfRover change = new MoveAndChangeDirectionOfRover();
4+
public void roverStart(NewRover r,char x) {
5+
if (x == 'L'){
6+
//System.out.println("left");
7+
change.turnLeft(r);
8+
} else if (x == 'M') {
9+
//System.out.println("Move");
10+
change.moveForward(r);
11+
} else if (x =='R') {
12+
change.turnRight(r);
13+
}
14+
}
15+
}

RoverLauncher.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
public class RoverLauncher{
3+
public static void main(String[] args) {
4+
NewRover r1 = new NewRover(5,5,1,2,NewRover.N);
5+
r1.moveRover("LMLMLMLMM");
6+
NewRover r2 = new NewRover(5,5,3,3,NewRover.E);
7+
r2.moveRover("RMRM");
8+
NewRover r3 = new NewRover(5,5,1,5,NewRover.N);
9+
r3.moveRover("LMLML");
10+
PrintRoverLocation print = new PrintRoverLocation();
11+
print.printPosition(r1);
12+
print.printPosition(r2);
13+
print.printPosition(r3);
14+
}
15+
}

0 commit comments

Comments
 (0)