diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..ab5dd3a 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -19,6 +19,10 @@ interface Car : CarInput { */ val carOutput: CarOutput + val tankMouth: TankMouth + + val tank: Tank + /** * Получить оборудование */ @@ -28,4 +32,4 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ data class Plates(val number: String, val region: Int) -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/otus/cars/CarFactory.kt b/src/main/kotlin/ru/otus/cars/CarFactory.kt index 3b8f03e..0dcaabb 100644 --- a/src/main/kotlin/ru/otus/cars/CarFactory.kt +++ b/src/main/kotlin/ru/otus/cars/CarFactory.kt @@ -38,4 +38,4 @@ object Togliatti : CarFactory { is Vaz2108.Companion -> return buildVaz2108(plates) } } -} \ 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..0257eac 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,6 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + + fun getFuelContents(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Tank.kt b/src/main/kotlin/ru/otus/cars/Tank.kt new file mode 100644 index 0000000..7c29c56 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,33 @@ +package ru.otus.cars + +interface Tank { + + var tankMouth: TankMouth + + fun getContents(): Int + + fun receiveFuel(liters: Int){ + } +} + +abstract class TankMouth { + abstract fun open() + + abstract fun close() +} + +class PetrolMouth(var tank: Tank): TankMouth() { + fun fuelPetrol(liters: Int) { + this.tank.receiveFuel(liters) + } + override fun open() {} + override fun close() {} +} + +class LpgMouth(var tank: Tank): TankMouth() { + fun fuelLpg(liters: Int) { + this.tank.receiveFuel(liters) + } + override fun open() {} + override fun close() {} +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/TankStation.kt b/src/main/kotlin/ru/otus/cars/TankStation.kt new file mode 100644 index 0000000..473149b --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankStation.kt @@ -0,0 +1,21 @@ +package ru.otus.cars + +class TankStation { + fun fillUpCar(car: Car, fuel: Int) { + try { + if (car.tankMouth is LpgMouth) { + (car.tankMouth as LpgMouth).fuelLpg(fuel) + } else { + (car.tankMouth as PetrolMouth).fuelPetrol(fuel) + } + } catch (e: NotImplementedError) { + //noop + } + } + + fun fillUpCars(cars: List) { + cars.forEach({ println(it) }) + cars.forEach({ this.fillUpCar(it, 100) }) + cars.forEach({ println(it) }) + } +} diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..95c2de9 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,6 +1,6 @@ package ru.otus.cars -object Taz: Car { +object Taz : Car { /** * Номерной знак */ @@ -18,6 +18,16 @@ object Taz: Car { override val carOutput: CarOutput get() = throw NotImplementedError("Приборов нет") + override val tankMouth: TankMouth + get() = throw NotImplementedError("Взорвался") + override val tank: Tank + get() = throw NotImplementedError("Какая то ошибка") + + override fun toString(): String { + return "Taz" + } + + /** * Получить оборудование */ @@ -36,4 +46,4 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } -} \ 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..8181958 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -20,6 +20,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { this.engine = getRandomEngine() this.plates = plates + this.tank = Vaz2107Tank() + this.tankMouth = LpgMouth(this.tank) + } /** @@ -49,6 +52,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } private var currentSpeed: Int = 0 // Скока жмёт + private var currentFuelLevel: Int = 0 /** * Доступно сборщику @@ -59,13 +63,15 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$currentFuelLevel)" } /** * Делегируем приборы внутреннему классу */ override val carOutput: CarOutput = VazOutput() + lateinit override var tankMouth: TankMouth + lateinit override var tank: Tank /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107! @@ -74,5 +80,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelContents(): Int { + return currentFuelLevel + } + } + + inner class Vaz2107Tank : Tank { + override lateinit var tankMouth: TankMouth + + override fun getContents(): Int { + return currentFuelLevel + } + + override fun receiveFuel(liters: Int) { + currentFuelLevel += liters + } } -} \ 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..3233065 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.tank = Vaz2108Tank() + this.tankMouth = PetrolMouth(this.tank) } fun alignWheels(vaz2108: Vaz2108) { @@ -53,6 +55,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { } private var currentSpeed: Int = 0 // Скока жмёт + private var currentFuelLevel: Int = 0 /** * Доступно сборщику @@ -63,13 +66,15 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$currentFuelLevel)" } /** * Делегируем приборы внутреннему классу */ override val carOutput: CarOutput = VazOutput() + lateinit override var tankMouth: TankMouth + lateinit override var tank: Tank /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! @@ -78,5 +83,21 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelContents(): Int { + return this@Vaz2108.currentFuelLevel + } + } + + inner class Vaz2108Tank : Tank { + override lateinit var tankMouth: TankMouth + + override fun getContents(): Int { + return currentFuelLevel + } + + override fun receiveFuel(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..9893fc5 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===> tank cars") + tankCars() + } fun driveCars() { @@ -29,6 +32,16 @@ fun driveCars() { println(vaz2.toString()) // Выводит -20 и случайную скорость } +fun tankCars() { + val cars = listOf( + Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)), + Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)), + Taz + ) + val tankStation = TankStation() + tankStation.fillUpCars(cars) +} + fun innerNestedCheck() { val vaz = Vaz2107.build(Car.Plates("123", 77)) val output = vaz.VazOutput() // Создаем новый объект ИЗ ЭКЗЕМПЛЯРА МАШИНЫ @@ -90,4 +103,4 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } -} \ No newline at end of file +}