diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..3321307 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.TankMouth + /** * Машина целиком */ @@ -19,6 +21,8 @@ interface Car : CarInput { */ val carOutput: CarOutput + val tankMouth: TankMouth + /** * Получить оборудование */ 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/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..ff6d1d8 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.TankMouth + object Taz: Car { /** * Номерной знак @@ -17,6 +19,8 @@ object Taz: Car { */ override val carOutput: CarOutput get() = throw NotImplementedError("Приборов нет") + override val tankMouth: TankMouth + get() = throw NotImplementedError("Бабах!") /** * Получить оборудование diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..4a03a38 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -1,11 +1,18 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.LpgMouth +import ru.otus.cars.fuelSystem.Tank +import ru.otus.cars.fuelSystem.TankMouth +import ru.otus.cars.fuelSystem.VazTank import kotlin.random.Random /** * Семёрочка */ -class Vaz2107 private constructor(color: String) : VazPlatform(color) { +class Vaz2107 private constructor( + color: String, +) : VazPlatform(color) { + /** * Сам-себе-сборщик ВАЗ 2107. */ @@ -17,9 +24,16 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { - this.engine = getRandomEngine() - this.plates = plates + override fun build(plates: Car.Plates): Vaz2107 { + val newTank = VazTank() + val newMouth = LpgMouth(newTank) + + return Vaz2107("Зеленый").apply { + this.engine = getRandomEngine() + this.plates = plates + this.tank = newTank + this.tankMouth = newMouth + } } /** @@ -36,6 +50,12 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { const val MODEL = "2107" } + override lateinit var tankMouth: TankMouth + private set + + override lateinit var tank: Tank + private set + // Переопределяем свойство родителя override lateinit var engine: VazEngine private set @@ -74,5 +94,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelContents(): Int { + return this@Vaz2107.tank.getContents() + } } } \ 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..cd6fb1b 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -1,5 +1,9 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.PetrolMouth +import ru.otus.cars.fuelSystem.Tank +import ru.otus.cars.fuelSystem.TankMouth +import ru.otus.cars.fuelSystem.VazTank import kotlin.random.Random /** @@ -18,9 +22,16 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { - this.engine = getRandomEngine() - this.plates = plates + override fun build(plates: Car.Plates): Vaz2108 { + val tank = VazTank() + val mouth = PetrolMouth(tank) + return Vaz2108("Красный").apply{ + this.engine = getRandomEngine() + this.plates = plates + this.tank = tank + this.tankMouth = mouth + } + } fun alignWheels(vaz2108: Vaz2108) { @@ -34,6 +45,12 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { const val MODEL = "2108" } + override lateinit var tankMouth: TankMouth + private set + + override lateinit var tank: Tank + private set + // Переопределяем свойство родителя override lateinit var engine: VazEngine private set @@ -78,5 +95,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelContents(): Int { + return this@Vaz2108.tank.getContents() + } } } \ 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..b89781f 100644 --- a/src/main/kotlin/ru/otus/cars/VazPlatform.kt +++ b/src/main/kotlin/ru/otus/cars/VazPlatform.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.Tank + abstract class VazPlatform(override val color: String) : Car { // Положение руля. Доступно только внутри класса и наследникам protected var wheelAngle: Int = 0 // Положение руля @@ -14,6 +16,8 @@ abstract class VazPlatform(override val color: String) : Car { // Абстрактное свойство двигателя abstract val engine: VazEngine + + protected abstract val tank: Tank } // Перечисление двигателей ВАЗ diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/FillingStation.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/FillingStation.kt new file mode 100644 index 0000000..e82d0ae --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/FillingStation.kt @@ -0,0 +1,26 @@ +package ru.otus.cars.fuelSystem + +import ru.otus.cars.Car + +class FillingStation { + + fun refuel(cars: List, fuelValue: Int) { + cars.forEach { + val mouth = it.tankMouth + mouth.open() + try { + when (mouth) { + is LpgMouth -> mouth.fuelLpg(fuelValue) + is PetrolMouth -> mouth.fuelPetrol(fuelValue) + } + + } catch (ex: NotImplementedError) { + println(ex) + } finally { + mouth.close() + println("В баке - ${it.carOutput.getFuelContents()} л.") + } + + } + } +} diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/LpgMouth.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/LpgMouth.kt new file mode 100644 index 0000000..10d5c85 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/LpgMouth.kt @@ -0,0 +1,10 @@ +package ru.otus.cars.fuelSystem + +class LpgMouth( + tank: Tank +): TankMouth(tank) { + + fun fuelLpg(liters: Int) { + tank.receiveFuel(liters) + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/PetrolMouth.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/PetrolMouth.kt new file mode 100644 index 0000000..bebc3f2 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/PetrolMouth.kt @@ -0,0 +1,10 @@ +package ru.otus.cars.fuelSystem + +class PetrolMouth( + tank: Tank +): TankMouth(tank) { + + fun fuelPetrol(liters: Int) { + tank.receiveFuel(liters) + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/Tank.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/Tank.kt new file mode 100644 index 0000000..2d064a1 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/Tank.kt @@ -0,0 +1,8 @@ +package ru.otus.cars.fuelSystem + +interface Tank { + + fun getContents(): Int + + fun receiveFuel(liters: Int) +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/TankMouth.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/TankMouth.kt new file mode 100644 index 0000000..472e9e0 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/TankMouth.kt @@ -0,0 +1,15 @@ +package ru.otus.cars.fuelSystem + +abstract class TankMouth( + protected val tank: Tank +) { + + fun open() { + println("Открыли бак") + } + + fun close() { + println("Закрыли бак") + } + +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/fuelSystem/VazTank.kt b/src/main/kotlin/ru/otus/cars/fuelSystem/VazTank.kt new file mode 100644 index 0000000..30bbf08 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuelSystem/VazTank.kt @@ -0,0 +1,15 @@ +package ru.otus.cars.fuelSystem + +class VazTank: Tank { + + private var fuelLevel: Int = 0 + + override fun getContents(): Int { + return fuelLevel + } + + override fun receiveFuel(liters: Int) { + fuelLevel += liters + println("Заправили") + } +} \ 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..f50b16b 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuelSystem.FillingStation + fun main() { println("\n===> drive cars...") driveCars() @@ -16,6 +18,21 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + + refuel() + +} + +fun refuel() { + val fillingStation = FillingStation() + fillingStation.refuel( + listOf( + Vaz2107.build(Car.Plates("1234", 123)), + Vaz2108.build(Car.Plates("567", 456)), + Taz + ), + 36 + ) } fun driveCars() {