reduce boilerplate code using Record
This commit is contained in:
parent
3a2175a5f6
commit
607c908e9d
|
@ -1,8 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
public class Coordinates {
|
public record Coordinates(int latitude, int longitude) {
|
||||||
private final int latitude;
|
|
||||||
private final int longitude;
|
|
||||||
|
|
||||||
public static Coordinates of(int latitude, int longitude) {
|
public static Coordinates of(int latitude, int longitude) {
|
||||||
return new Coordinates(latitude, longitude);
|
return new Coordinates(latitude, longitude);
|
||||||
|
@ -16,36 +14,4 @@ public class Coordinates {
|
||||||
return new Coordinates(latitude, newLongitude);
|
return new Coordinates(latitude, newLongitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Coordinates(int latitude, int longitude) {
|
|
||||||
this.latitude = latitude;
|
|
||||||
this.longitude = longitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLatitude() {
|
|
||||||
return latitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLongitude() {
|
|
||||||
return longitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object o) {
|
|
||||||
if (this == o) return true;
|
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
|
|
||||||
Coordinates that = (Coordinates) o;
|
|
||||||
|
|
||||||
if (latitude != that.latitude) return false;
|
|
||||||
return longitude == that.longitude;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Coordinates{" +
|
|
||||||
"latitude=" + latitude +
|
|
||||||
", longitude=" + longitude +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,19 +32,19 @@ public class MarsMap {
|
||||||
case EAST -> incrementLongitude();
|
case EAST -> incrementLongitude();
|
||||||
}
|
}
|
||||||
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, currentPosition.getLatitude(), currentPosition.getLongitude()));
|
direction, currentPosition.latitude(), currentPosition.longitude()));
|
||||||
|
|
||||||
return currentPosition;
|
return currentPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLatitude() {
|
private void incrementLatitude() {
|
||||||
int latitude = currentPosition.getLatitude();
|
int latitude = currentPosition.latitude();
|
||||||
int newLatitude = getIncrementedPosition(latitude, height);
|
int newLatitude = getIncrementedPosition(latitude, height);
|
||||||
setNewLatitude(newLatitude);
|
setNewLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void incrementLongitude() {
|
private void incrementLongitude() {
|
||||||
int longitude = currentPosition.getLongitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = getIncrementedPosition(longitude, width);
|
int newLongitude = getIncrementedPosition(longitude, width);
|
||||||
setNewLongitude(newLongitude);
|
setNewLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
@ -56,13 +56,13 @@ public class MarsMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrementLatitude() {
|
private void decrementLatitude() {
|
||||||
int latitude = currentPosition.getLatitude();
|
int latitude = currentPosition.latitude();
|
||||||
int newLatitude = decrementPosition(latitude, height);
|
int newLatitude = decrementPosition(latitude, height);
|
||||||
setNewLatitude(newLatitude);
|
setNewLatitude(newLatitude);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void decrementLongitude() {
|
private void decrementLongitude() {
|
||||||
int longitude = currentPosition.getLongitude();
|
int longitude = currentPosition.longitude();
|
||||||
int newLongitude = decrementPosition(longitude, width);
|
int newLongitude = decrementPosition(longitude, width);
|
||||||
setNewLongitude(newLongitude);
|
setNewLongitude(newLongitude);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue