Skip to content

Instantly share code, notes, and snippets.

@aliahmadcse
Created March 26, 2024 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliahmadcse/81db31b1a8d5eeb91e39428fc1c32d5d to your computer and use it in GitHub Desktop.
Save aliahmadcse/81db31b1a8d5eeb91e39428fc1c32d5d to your computer and use it in GitHub Desktop.
ride_fare_cal
public class RideFareCalculator {
// in real system, these values must be configurable outside of code
private static final double BASE_FARE = 3.0;
private static final double RATE_PER_KM = 2.50;
private static final double RATE_PER_MINUTE = 0.50;
private static final double MINIMUM_FARE = 5.0;
public static double calculateFare(double distanceKm, double timeMinutes, double surgeMultiplier) {
if (distanceKm < 0 || timeMinutes < 0 || surgeMultiplier < 0) {
throw new IllegalArgumentException("Distance, time, and surge multiplier must be non-negative values.");
}
double distanceFare = distanceKm * RATE_PER_KM;
double timeFare = timeMinutes * RATE_PER_MINUTE;
double totalFare = BASE_FARE + distanceFare + timeFare;
double fare = totalFare * surgeMultiplier;
return Math.max(fare, MINIMUM_FARE);
}
public static void main(String[] args) {
double distanceKm = 10.0;
double timeMinutes = 20.0;
double surgeMultiplier = 1.0;
double fare = calculateFare(distanceKm, timeMinutes, surgeMultiplier);
System.out.printf("The fare for your trip is: $%.2f\n", fare);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment