refactor
This commit is contained in:
parent
48b5b55ae0
commit
c43bcd1aa6
|
@ -8,7 +8,11 @@ public class Coordinates {
|
|||
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.longitude = longitude;
|
||||
}
|
||||
|
|
|
@ -25,26 +25,43 @@ public class MarsMap {
|
|||
}
|
||||
|
||||
public Coordinates updatePositionTowards(Direction direction) {
|
||||
return currentPosition = resolveNextPositionTowards(direction);
|
||||
}
|
||||
|
||||
private Coordinates resolveNextPositionTowards(Direction direction) {
|
||||
var newLatitude = currentPosition.getLatitude();
|
||||
var newLongitude = currentPosition.getLongitude();
|
||||
switch (direction) {
|
||||
case NORTH -> newLatitude--;
|
||||
case SOUTH -> {
|
||||
if (newLatitude+1 > height)
|
||||
newLatitude=1;
|
||||
else
|
||||
newLatitude++;
|
||||
}
|
||||
case WEST -> newLongitude--;
|
||||
case EAST -> newLongitude++;
|
||||
case NORTH -> decrementLatitude();
|
||||
case SOUTH -> incrementLatitude();
|
||||
case WEST -> incrementLongitude();
|
||||
case EAST -> decrementLongitude();
|
||||
}
|
||||
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