Skip to content

Commit

Permalink
general fixes Ratio.java and Distance.java
Browse files Browse the repository at this point in the history
  • Loading branch information
JustCris654 committed Jan 7, 2025
1 parent 284133a commit fc01a87
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,9 @@
*/
public class RentalVehicleFuel {

/**
* Current fuel percentage, expressed from 0 to 1.
* <p>
* May be {@code null}.
*/
@Nullable
public final Ratio percent;

/**
* Distance that the vehicle can travel with the current charge or fuel.
* <p>
* May be {@code null}.
*/
@Nullable
public final Distance range;

Expand All @@ -34,7 +24,7 @@ public RentalVehicleFuel(@Nullable Ratio fuelPercent, @Nullable Distance range)

@Nullable
public Double getPercent() {
return this.percent != null ? this.percent.ratio : null;
return this.percent != null ? this.percent.asDouble() : null;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Distance {
private final double meters;

/** Returns a Distance object representing the given number of meters */
public Distance(double distanceInMeters) {
private Distance(double distanceInMeters) {
if (distanceInMeters < 0) {
throw new IllegalArgumentException("Distance cannot be negative");
}
Expand All @@ -19,19 +19,11 @@ public Distance(double distanceInMeters) {

/** Returns a Distance object representing the given number of meters */
public static Distance ofMeters(double value) throws IllegalArgumentException {
if (value < 0) {
throw new IllegalArgumentException("Distance cannot be negative");
}

return new Distance(value);
}

/** Returns a Distance object representing the given number of kilometers */
public static Distance ofKilometers(double value) {
if (value < 0) {
throw new IllegalArgumentException("Distance cannot be negative");
}

return new Distance(value * METERS_PER_KM);
}

Expand All @@ -51,7 +43,7 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(meters, "Distance");
return Objects.hash(meters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class Ratio {

public final Double ratio;
private final Double ratio;

public Ratio(Double ratio) throws IllegalArgumentException {
public Ratio(Double ratio) {
if (ratio < 0d || ratio > 1d) {
throw new IllegalArgumentException("Ratio must be in range [0,1]");
}
Expand All @@ -25,11 +25,15 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(this.ratio, "Ratio");
return Objects.hash(this.ratio);
}

@Override
public String toString() {
return this.ratio.toString();
}

public Double asDouble() {
return ratio;
}
}

0 comments on commit fc01a87

Please sign in to comment.