Untitled
unknown
scala
3 years ago
1.8 kB
5
Indexable
package o1.carsim import o1.Pos class Car(val fuelConsumption: Double, val tankSize: Double, initialFuel: Double, initialLocation: Pos) { private var fuelLevel = initialFuel private var carLocation = initialLocation private var carsMetersDriven = 0.0 def location: Pos = this.carLocation def fuel(toBeAdded: Double): Double = { var added = 0.0 if (toBeAdded + this.fuelLevel <= this.tankSize) { this.fuelLevel += toBeAdded added = toBeAdded } else { val tempAdded = this.fuelLevel this.fuelLevel = this.tankSize added = this.tankSize - tempAdded } added } def fuel(): Double = { val added = this.fuelLevel this.fuelLevel = this.tankSize this.tankSize - added } def fuelRatio: Double = (this.fuelLevel / this.tankSize) * 100 def metersDriven: Double = this.carsMetersDriven def fuelRange: Double = (this.fuelLevel / this.fuelConsumption) * 100000 //TÄTÄ TÄYTYY VIELÄ FUNTISA //Tällä hetkellä on väärin tehty kun fuelConsumption on l/100km //jos bensa ei riitä niin auto pysähtyy aikaisemmin def drive(destination: Pos, metersToDestination: Double): Unit = { if (this.fuelRange >= metersToDestination) { this.carLocation = destination this.carsMetersDriven += metersToDestination this.fuelLevel -= this.fuelConsumption * (metersToDestination / 100000) } else if (this.fuelRange != 0) { val travelPrecentage = this.fuelRange / metersToDestination val newX = this.carLocation.x + (destination.x - this.carLocation.x) * travelPrecentage val newY = this.carLocation.y + (destination.y - this.carLocation.y) * travelPrecentage this.carLocation = new Pos(newX, newY) this.carsMetersDriven += metersToDestination * travelPrecentage this.fuelLevel = 0.0 } } }
Editor is loading...