diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..ac0af1a 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -28,4 +28,9 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ 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/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..df0e50b 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/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