diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..256519b 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -19,6 +19,11 @@ interface Car : CarInput { */ val carOutput: CarOutput + /** + * Горловина + */ + val tankMouth: TankMouth + /** * Получить оборудование */ diff --git a/src/main/kotlin/ru/otus/cars/CarInput.kt b/src/main/kotlin/ru/otus/cars/CarInput.kt index fc2478e..d595b08 100644 --- a/src/main/kotlin/ru/otus/cars/CarInput.kt +++ b/src/main/kotlin/ru/otus/cars/CarInput.kt @@ -13,4 +13,9 @@ interface CarInput { * Руль влево на [degrees] градусов */ fun wheelToLeft(degrees: Int) + + /** + * Заправиться + */ + fun addFuel(liters: Int) } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/CarOutput.kt b/src/main/kotlin/ru/otus/cars/CarOutput.kt index 875339f..d8713dc 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,9 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + + /** + * Получить уровень топлива + */ + fun getFuelLevel(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/TankMouth.kt b/src/main/kotlin/ru/otus/cars/TankMouth.kt new file mode 100644 index 0000000..6309418 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,9 @@ +package ru.otus.cars + +/** + * Горловина + */ +sealed interface TankMouth { + class PetrolMouth : TankMouth + class LpgMouth : TankMouth +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/TankSystem.kt b/src/main/kotlin/ru/otus/cars/TankSystem.kt new file mode 100644 index 0000000..0bb9dac --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankSystem.kt @@ -0,0 +1,37 @@ +package ru.otus.cars + +/** + * Топливная система + */ +class TankSystem(private val mouthType: TankMouth) { + + private val tank = Tank() + + fun addFuel(liters: Int) { + when (mouthType) { + is TankMouth.PetrolMouth -> tank.addPetrolFuel(liters) + is TankMouth.LpgMouth -> tank.addLpgFuel(liters) + } + } + + fun getFuelLevel(): Int { + return tank.getFuelLevel() + } + + private class Tank { + var currentFuelLevel: Int = 0 + fun getFuelLevel(): Int = currentFuelLevel + + fun addPetrolFuel(liters: Int) { + println("Заправляемся бензином, $liters литров") + currentFuelLevel += liters + } + + fun addLpgFuel(liters: Int) { + println("Заправляемся газом, $liters литров") + currentFuelLevel += liters + } + } +} + + diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..49b9bc8 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,6 +1,25 @@ package ru.otus.cars +import kotlin.random.Random + object Taz: Car { + + /** + * Рандомный выбор горловины + */ + private fun getRandomTankMouth(): TankMouth { + return when (Random.nextInt(0, 2)) { + 0 -> TankMouth.PetrolMouth() + else -> TankMouth.LpgMouth() + } + } + + /** + * Горловина + */ + override val tankMouth: TankMouth + get() = getRandomTankMouth() + /** * Номерной знак */ @@ -36,4 +55,8 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override fun addFuel(liters: Int) { + throw IllegalArgumentException("Топливная система не найдена! Взрыв!") + } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..2e45707 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -20,6 +20,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { this.engine = getRandomEngine() this.plates = plates + this.tankMouth = TankMouth.LpgMouth() + this.tankSystem = TankSystem(tankMouth) } /** @@ -40,6 +42,15 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + override lateinit var tankMouth: TankMouth + private set + + override lateinit var tankSystem: TankSystem + + override fun addFuel(liters: Int) { + tankSystem.addFuel(liters) + } + /** * Семерка едет так */ @@ -74,5 +85,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelLevel(): Int { + return tankSystem.getFuelLevel() + } } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Vaz2108.kt b/src/main/kotlin/ru/otus/cars/Vaz2108.kt index 27b83b8..d7a1296 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -21,6 +21,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { this.engine = getRandomEngine() this.plates = plates + this.tankMouth = TankMouth.PetrolMouth() + this.tankSystem = TankSystem(tankMouth) } fun alignWheels(vaz2108: Vaz2108) { @@ -38,6 +40,15 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + override lateinit var tankMouth: TankMouth + private set + + override lateinit var tankSystem: TankSystem + + override fun addFuel(liters: Int) { + tankSystem.addFuel(liters) + } + /** * Восьмерка едет так */ @@ -78,5 +89,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelLevel(): Int { + return tankSystem.getFuelLevel() + } } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/VazPlatform.kt b/src/main/kotlin/ru/otus/cars/VazPlatform.kt index 079c2da..b8299e0 100644 --- a/src/main/kotlin/ru/otus/cars/VazPlatform.kt +++ b/src/main/kotlin/ru/otus/cars/VazPlatform.kt @@ -14,6 +14,9 @@ abstract class VazPlatform(override val color: String) : Car { // Абстрактное свойство двигателя abstract val engine: VazEngine + + // Абстрактное свойство топливной системы + abstract val tankSystem: TankSystem } // Перечисление двигателей ВАЗ diff --git a/src/main/kotlin/ru/otus/cars/main.kt b/src/main/kotlin/ru/otus/cars/main.kt index 978d0ef..fddb7d2 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,6 +16,9 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + + println("\n===> Заправка") + addFuel(20) } fun driveCars() { @@ -90,4 +93,20 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun addFuel(liters: Int) { + + val carsList = listOf( + Vaz2107.build(Car.Plates("123", 77)), + Vaz2108.build(Car.Plates("321", 78)), + Taz + ) + + carsList.forEach { car -> + val output = car.carOutput + println("${car.javaClass.simpleName} - Уровень топлива до проверки: ${output.getFuelLevel()}") + car.addFuel(liters) + println("${car.javaClass.simpleName} - Уровень топлива после проверки: ${output.getFuelLevel()}") + } } \ No newline at end of file