From c4126c1678df5ca586fd17cb58ebc610ea8604a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A8=D0=B0=D0=BC=D0=B8=D0=BB=D1=8C?= Date: Wed, 25 Mar 2026 12:05:07 +0300 Subject: [PATCH] done --- gradle.properties | 3 +- src/main/kotlin/ru/otus/cars/Car.kt | 21 ++--------- src/main/kotlin/ru/otus/cars/CarBuilder.kt | 7 +--- src/main/kotlin/ru/otus/cars/CarInput.kt | 5 +++ src/main/kotlin/ru/otus/cars/CarOutput.kt | 9 ++--- src/main/kotlin/ru/otus/cars/Taz.kt | 15 ++++++++ src/main/kotlin/ru/otus/cars/Vaz2107.kt | 20 +++++++++- src/main/kotlin/ru/otus/cars/Vaz2108.kt | 20 +++++++++- src/main/kotlin/ru/otus/cars/VazPlatform.kt | 42 +++++++++++++++++++++ src/main/kotlin/ru/otus/cars/main.kt | 19 ++++++++++ 10 files changed, 128 insertions(+), 33 deletions(-) diff --git a/gradle.properties b/gradle.properties index 29e08e8..e2d16c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,2 @@ -kotlin.code.style=official \ No newline at end of file +kotlin.code.style=official +android.overridePathCheck=true \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..83fd853 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -1,31 +1,16 @@ package ru.otus.cars -/** - * Машина целиком - */ interface Car : CarInput { - /** - * Номерной знак - */ + val plates: Plates - /** - * Цвет машины - */ val color: String - /** - * Следит за машиной - */ val carOutput: CarOutput - /** - * Получить оборудование - */ fun getEquipment(): String - /** - * Внутренний статический класс - номерой знак - */ data class Plates(val number: String, val region: Int) + + val tankMouth: TankMouth } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/CarBuilder.kt b/src/main/kotlin/ru/otus/cars/CarBuilder.kt index 224f46d..1f3967f 100644 --- a/src/main/kotlin/ru/otus/cars/CarBuilder.kt +++ b/src/main/kotlin/ru/otus/cars/CarBuilder.kt @@ -1,11 +1,6 @@ package ru.otus.cars -/** - * Сборщик машины - */ sealed interface CarBuilder { - /** - * Собери машину - */ + fun build(plates: Car.Plates): Car } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/CarInput.kt b/src/main/kotlin/ru/otus/cars/CarInput.kt index fc2478e..4fcd1dc 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 refuel(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..449d97e 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -1,11 +1,8 @@ package ru.otus.cars -/** - * Следит за машиной - */ interface CarOutput { - /** - * Скажи текущую скорость - */ + fun getCurrentSpeed(): Int + + fun getFuelLevel(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..ed707f2 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -18,6 +18,13 @@ object Taz: Car { override val carOutput: CarOutput get() = throw NotImplementedError("Приборов нет") + + /** + * Бензобак + */ + override val tankMouth: TankMouth + get() = throw NotImplementedError("Бензобака нет") + /** * Получить оборудование */ @@ -36,4 +43,12 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override fun refuel(liters: Int) { + throw NotImplementedError("БА-БАХ!!!") + } + + override fun toString(): String { + return "TAZ 666" + } } \ 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..b3769c9 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.GasMouth() + this.fuelSystem = VazFuelSystem(tankMouth) } /** @@ -57,9 +59,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var plates: Car.Plates private set + // Переопределяем свойство родителя + override lateinit var tankMouth: TankMouth + private set + + override lateinit var fuelSystem: VazFuelSystem + private set + + override fun refuel(liters: Int) { + fuelSystem.refuel(liters) + } + // Выводим состояние машины override fun toString(): String { - return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLevel = carOutput.getFuelLevel() + return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuel=$fuelLevel liters)" } /** @@ -74,5 +88,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelLevel(): Int { + return fuelSystem.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..42ad85a 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.fuelSystem = VazFuelSystem(tankMouth) } fun alignWheels(vaz2108: Vaz2108) { @@ -61,9 +63,21 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var plates: Car.Plates private set + // Переопределяем свойство родителя + override lateinit var tankMouth: TankMouth + private set + + override lateinit var fuelSystem: VazFuelSystem + private set + + override fun refuel(liters: Int) { + fuelSystem.refuel(liters) + } + // Выводим состояние машины override fun toString(): String { - return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLevel = carOutput.getFuelLevel() + return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuel=$fuelLevel liters)" } /** @@ -78,5 +92,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelLevel(): Int { + return fuelSystem.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..6ca1603 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 fuelSystem : VazFuelSystem } // Перечисление двигателей ВАЗ @@ -23,4 +26,43 @@ sealed class VazEngine { data class LADA_2107(override val volume: Int) : VazEngine() data class SAMARA_2108(override val volume: Int) : VazEngine() +} + +// Перечисление горловин бака +sealed class TankMouth { + class PetrolMouth: TankMouth() + class GasMouth: TankMouth() +} + + +// Топливная система +class VazFuelSystem(private val mouthType: TankMouth) { + + private val tank = Tank() + + fun refuel(liters: Int) { + when (mouthType) { + is TankMouth.PetrolMouth -> { + println("Залили $liters литров бензина") + tank.addFuel(liters) + } + is TankMouth.GasMouth -> { + println("Залили $liters литров газа") + tank.addFuel(liters) + } + } + } + + fun getFuelLevel(): Int { + return tank.getFuelLevel() + } + + private class Tank { + var currentFuelLevel: Int = 0 + fun getFuelLevel(): Int = currentFuelLevel + + fun addFuel(liters: Int) { + currentFuelLevel += liters + } + } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/main.kt b/src/main/kotlin/ru/otus/cars/main.kt index 978d0ef..79c6a77 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,6 +16,8 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + println("\n===> Заправка") + AZS(20) } fun driveCars() { @@ -90,4 +92,21 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun AZS(liters: Int) { + + val carsList = listOf( + Vaz2107.build(Car.Plates("123", 77)), + Vaz2108.build(Car.Plates("321", 78)), + Taz + ) + + carsList.forEach { car -> + println("Заправляем машину ${car.javaClass.simpleName}") + println(car.toString()) + car.refuel(liters) + println(car.toString()) + println("\n") + } } \ No newline at end of file