diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..f26e244 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -18,6 +18,7 @@ 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..f4bbfbe 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,5 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + fun getFuelLevel(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/DefaultTank.kt b/src/main/kotlin/ru/otus/cars/DefaultTank.kt new file mode 100644 index 0000000..5089c4b --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/DefaultTank.kt @@ -0,0 +1,11 @@ +package ru.otus.cars + +class DefaultTank: Tank { + private var fuelLevel: Int = 0 + + override fun getContents(): Int = fuelLevel + + override fun receiveFuel(liters: Int) { + fuelLevel += liters + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/GasStation.kt b/src/main/kotlin/ru/otus/cars/GasStation.kt new file mode 100644 index 0000000..7631915 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,21 @@ +package ru.otus.cars + +class GasStation { + fun addFuel(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) { + println(e.message) + } + } + + fun addFuel(cars: List) { + cars.forEach { println(it.toString()) } + cars.forEach { this.addFuel(it, 100) } + cars.forEach { println(it.toString()) } + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/LpgMouth.kt b/src/main/kotlin/ru/otus/cars/LpgMouth.kt new file mode 100644 index 0000000..011fb4b --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/LpgMouth.kt @@ -0,0 +1,15 @@ +package ru.otus.cars + +class LpgMouth(override val tank: Tank): TankMouth() { + fun fuelLpg(litres: Int) { + tank.receiveFuel(litres) + } + + override fun open() { + TODO("Not yet implemented") + } + + override fun close() { + TODO("Not yet implemented") + } +} diff --git a/src/main/kotlin/ru/otus/cars/PetrolMouth.kt b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt new file mode 100644 index 0000000..7f1f8fc --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt @@ -0,0 +1,15 @@ +package ru.otus.cars + +class PetrolMouth(override val tank: Tank): TankMouth() { + fun fuelPetrol(litres: Int) { + tank.receiveFuel(litres) + } + + override fun open() { + TODO("Not yet implemented") + } + + override fun close() { + TODO("Not yet implemented") + } +} \ 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..2076079 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,6 @@ +package ru.otus.cars + +interface Tank { + fun getContents(): Int + fun receiveFuel(liters: 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..2d4f8d5 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,8 @@ +package ru.otus.cars + +abstract class TankMouth { + abstract val tank: Tank + + abstract fun open() + abstract fun close() +} \ 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..69157ee 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -18,6 +18,9 @@ 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..cc638bf 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -5,7 +5,7 @@ import kotlin.random.Random /** * Семёрочка */ -class Vaz2107 private constructor(color: String) : VazPlatform(color) { +class Vaz2107 private constructor(color: String, override val tankMouth: TankMouth) : VazPlatform(color) { /** * Сам-себе-сборщик ВАЗ 2107. */ @@ -17,7 +17,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { + override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый", PetrolMouth(DefaultTank())).apply { this.engine = getRandomEngine() this.plates = plates } @@ -49,8 +49,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } private var currentSpeed: Int = 0 // Скока жмёт - - /** + /** * Доступно сборщику * @see [build] */ @@ -59,7 +58,7 @@ 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=${carOutput.getFuelLevel()})" } /** @@ -74,5 +73,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelLevel(): Int { + return this@Vaz2107.tankMouth.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..30e8e25 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -5,7 +5,7 @@ import kotlin.random.Random /** * Восьмерка */ -class Vaz2108 private constructor(color: String) : VazPlatform(color) { +class Vaz2108 private constructor(color: String, override val tankMouth: TankMouth) : VazPlatform(color) { /** * Сам-себе-сборщик ВАЗ 2108. */ @@ -18,7 +18,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { + override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный", LpgMouth(DefaultTank())).apply { this.engine = getRandomEngine() this.plates = plates } @@ -63,14 +63,13 @@ 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=${carOutput.getFuelLevel()})" } /** * Делегируем приборы внутреннему классу */ override val carOutput: CarOutput = VazOutput() - /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! */ @@ -78,5 +77,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelLevel(): Int { + return this@Vaz2108.tankMouth.tank.getContents() + } } } \ 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..d07ee3d 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,6 +1,8 @@ package ru.otus.cars fun main() { + println("\n===> fuel cars") + fuelCars() println("\n===> drive cars...") driveCars() println("\n===> inner test...") @@ -90,4 +92,15 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun fuelCars() { + val cars = listOf( + Togliatti.buildCar(Vaz2107, Car.Plates("123", 11)), + Togliatti.buildCar(Vaz2108, Car.Plates("123", 22)), + Taz + ) + + val tankStation = GasStation() + tankStation.addFuel(cars) } \ No newline at end of file