Untitled

 avatar
unknown
scala
2 years ago
1.2 kB
0
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 = 0.0


  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) * 100000

  def drive(destination: Pos, metersToDestination: Double): Unit =
    if fuelRange >= metersToDestination then
      driven += metersToDestination
      this.currentLocation = destination
      fuelAmount -= fuelRange * fuelConsumption
    else if fuelRange > 0 then
      this.currentLocation = this.currentLocation * (fuelRange / metersToDestination)
      driven += (fuelRange / metersToDestination) * metersToDestination
      fuelAmount = 0.0


end Car
Editor is loading...