Untitled
unknown
scala
3 years ago
1.0 kB
8
Indexable
package o1.carsim
import o1.Pos
import scala.math.{max, min}
class Car(val fuelConsumption: Double, val tankSize: Double, initialFuel: Double, initialLocation: Pos):
private var currentLocation = initialLocation
private var fuelAmount = initialFuel
private var driven = initialLocation.distance(currentLocation)
def location: Pos = currentLocation
def fuel(toBeAdded: Double): Double =
val addedFuel = min(toBeAdded, (tankSize - fuelAmount))
fuelAmount += addedFuel
addedFuel
def fuel(): Double = fuel(tankSize)
def fuelRatio: Double = (fuelAmount / tankSize) * 100
def metersDriven: Double = driven
def fuelRange: Double = (fuelAmount / fuelConsumption) * 1000
def drive(destination: Pos, metersToDestination: Double): Unit =
if fuelRange >= metersToDestination then
this.currentLocation = destination
fuelAmount -= (fuelRange * fuelConsumption) / 1000
else this.currentLocation * (fuelRange / metersToDestination)
fuelAmount = 0.0Editor is loading...