Skip to content

Instantly share code, notes, and snippets.

@DevNebulae
Last active October 1, 2021 17:57
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 DevNebulae/eb255b5e1a74eac616d6ca5f99f19a5f to your computer and use it in GitHub Desktop.
Save DevNebulae/eb255b5e1a74eac616d6ca5f99f19a5f to your computer and use it in GitHub Desktop.
Intended to be used in an IntelliJ scratch pad.
// Distance is assumed to be in whole km and volume in whole L
data class Refuel(val distance: Int, val volume: Int)
data class Vehicle(val odometer: Int, val trip: Int, val refuels: List<Refuel> = emptyList())
val vehicle = Vehicle(odometer = 25000, trip = 300, refuels = listOf(
Refuel(distance = 700, volume = 33),
Refuel(distance = 1100, volume = 48)
))
// A user may register in between refueling. To combat this, subtract the trip meter that was
// recorded during the registration
val originalOdometer = vehicle.odometer - vehicle.trip
val currentOdometer = originalOdometer + vehicle.refuels.sumOf { it.distance }
val totalVolume = vehicle.refuels.sumOf { it.volume }
originalOdometer
currentOdometer
totalVolume
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment