reduce boilerplate code using Record
This commit is contained in:
parent
3a2175a5f6
commit
607c908e9d
|
@ -1,8 +1,6 @@
|
|||
package cat.hack3.codingtests.marsrover;
|
||||
|
||||
public class Coordinates {
|
||||
private final int latitude;
|
||||
private final int longitude;
|
||||
public record Coordinates(int latitude, int longitude) {
|
||||
|
||||
public static Coordinates of(int latitude, int longitude) {
|
||||
return new Coordinates(latitude, longitude);
|
||||
|
@ -16,36 +14,4 @@ public class Coordinates {
|
|||
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();
|
||||
}
|
||||
logger.info(String.format("Updated coordinates towards %s, now is %d-%d",
|
||||
direction, currentPosition.getLatitude(), currentPosition.getLongitude()));
|
||||
direction, currentPosition.latitude(), currentPosition.longitude()));
|
||||
|
||||
return currentPosition;
|
||||
}
|
||||
|
||||
private void incrementLatitude() {
|
||||
int latitude = currentPosition.getLatitude();
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = getIncrementedPosition(latitude, height);
|
||||
setNewLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private void incrementLongitude() {
|
||||
int longitude = currentPosition.getLongitude();
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = getIncrementedPosition(longitude, width);
|
||||
setNewLongitude(newLongitude);
|
||||
}
|
||||
|
@ -56,13 +56,13 @@ public class MarsMap {
|
|||
}
|
||||
|
||||
private void decrementLatitude() {
|
||||
int latitude = currentPosition.getLatitude();
|
||||
int latitude = currentPosition.latitude();
|
||||
int newLatitude = decrementPosition(latitude, height);
|
||||
setNewLatitude(newLatitude);
|
||||
}
|
||||
|
||||
private void decrementLongitude() {
|
||||
int longitude = currentPosition.getLongitude();
|
||||
int longitude = currentPosition.longitude();
|
||||
int newLongitude = decrementPosition(longitude, width);
|
||||
setNewLongitude(newLongitude);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue