refactor
This commit is contained in:
parent
48b5b55ae0
commit
c43bcd1aa6
|
@ -8,7 +8,11 @@ public class Coordinates {
|
||||||
return new Coordinates(latitude, longitude);
|
return new Coordinates(latitude, longitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates(int latitude, int longitude) {
|
public Coordinates ofUpdatedLatitude(int newLatitude) {
|
||||||
|
return new Coordinates(newLatitude, longitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Coordinates(int latitude, int longitude) {
|
||||||
this.latitude = latitude;
|
this.latitude = latitude;
|
||||||
this.longitude = longitude;
|
this.longitude = longitude;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,26 +25,43 @@ public class MarsMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Coordinates updatePositionTowards(Direction direction) {
|
public Coordinates updatePositionTowards(Direction direction) {
|
||||||
return currentPosition = resolveNextPositionTowards(direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Coordinates resolveNextPositionTowards(Direction direction) {
|
|
||||||
var newLatitude = currentPosition.getLatitude();
|
|
||||||
var newLongitude = currentPosition.getLongitude();
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case NORTH -> newLatitude--;
|
case NORTH -> decrementLatitude();
|
||||||
case SOUTH -> {
|
case SOUTH -> incrementLatitude();
|
||||||
if (newLatitude+1 > height)
|
case WEST -> incrementLongitude();
|
||||||
newLatitude=1;
|
case EAST -> decrementLongitude();
|
||||||
else
|
|
||||||
newLatitude++;
|
|
||||||
}
|
|
||||||
case WEST -> newLongitude--;
|
|
||||||
case EAST -> newLongitude++;
|
|
||||||
}
|
}
|
||||||
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
||||||
direction, newLatitude, newLongitude));
|
direction, currentPosition.getLatitude(), currentPosition.getLongitude()));
|
||||||
|
|
||||||
return new Coordinates(newLatitude, newLongitude);
|
return currentPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void incrementLatitude() {
|
||||||
|
int latitude = currentPosition.getLatitude();
|
||||||
|
int newLatitude = latitude + 1 > height
|
||||||
|
? 1
|
||||||
|
: ++latitude;
|
||||||
|
setNewLatitude(newLatitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decrementLatitude() {
|
||||||
|
int latitude = currentPosition.getLatitude();
|
||||||
|
int newLatitude = latitude - 1 < 1
|
||||||
|
? height
|
||||||
|
: --latitude;
|
||||||
|
setNewLatitude(newLatitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNewLatitude(int newLatitude) {
|
||||||
|
currentPosition = currentPosition.ofUpdatedLatitude(newLatitude);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void incrementLongitude() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void decrementLongitude() {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue