Speed and fuel

Report a typo

You need to write a Car class with the Speed and Fuel interfaces. In the Car class, use class delegation to implement the speed and fuel level change functionality.

Here is the plan to follow:

  • Create the Speed interface with the increaseSpeed() and declineSpeed() functions.
  • Create the Fuel interface with the functions consumerFuel() and addFuel().
  • Implement SpeedHandler with step 10 and FuelHandler with step 5 for consumerFuel() and step 10 for addFuel() delegate classes for Speed and Fuel interfaces, respectively.

Now you need to create the speedDelegate and fuelDelegate delegates in the Car class.

Write a program in Kotlin
interface Speed {
fun increaseSpeed()
fun decreaseSpeed()
}

interface Fuel {
fun consumeFuel()
fun addFuel()
}

class SpeedHandler(private val car: Car) : Speed {
override fun increaseSpeed() {
car.speed += 10
}

override fun decreaseSpeed() {
if (car.speed - 10 >= 0) {
car.speed -= 10
} else {
car.speed = 0
}
}
}

class FuelHandler(private val car: Car) : Fuel {
override fun consumeFuel() {
if (car.fuel - 5 >= 0) {
car.fuel -= 5
} else {
car.fuel = 0
}
}

override fun addFuel() {
if (car.fuel + 10 <= 100) {
car.fuel += 10
___

Create a free account to access the full topic