Untitled

mail@pastecode.io avatar
unknown
scala
2 years ago
1.1 kB
3
Indexable
Never
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 >= initialLocation.distance(destination)
        this.currentLocation = destination
        fuelAmount = (fuelRange * fuelConsumption) / 1000
    else
        this.currentLocation = this.currentLocation.multiply(fuelRange/metersToDestination)
        //jos auton bensa ei riitä kohteeseen saakka ajamiseen niin 
        fuelAmount = 0.0



end Car