diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..64e98cc 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -19,6 +19,11 @@ 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..b92f439 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 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..b445e91 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/DefaultTank.kt @@ -0,0 +1,12 @@ +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..d46b2c4 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,21 @@ +package ru.otus.cars + +class GasStation{ + fun addFuelToCar(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 addFuelToCar(cars: List) { + cars.forEach { println(it.toString()) } + cars.forEach { this.addFuelToCar(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..0be2409 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/LpgMouth.kt @@ -0,0 +1,17 @@ +package ru.otus.cars + +class LpgMouth(override val tank: Tank): TankMouth() { + fun fuelLpg(liters: Int){ + open() + tank.receiveFuel(liters) + close() + } + + override fun close() { + println("Бак закрыт") + } + + override fun open() { + println("Бак открыт") + } +} \ No newline at end of file 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..7aaa4ca --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt @@ -0,0 +1,17 @@ +package ru.otus.cars + +class PetrolMouth(override val tank: Tank): TankMouth() { + fun fuelPetrol(liters: Int){ + open() + tank.receiveFuel(liters) + close() + } + + override fun close() { + println("Бак закрыт") + } + + override fun open() { + println("Бак открыт") + } +} \ 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..0fd2660 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,10 @@ +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..4cb13eb 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..eaf6313 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 } @@ -59,7 +59,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 +74,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..00eeb8f 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,7 +63,7 @@ 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()}" } /** @@ -78,5 +78,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..2a1c4f7 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,12 +16,13 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + } fun driveCars() { val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)) val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)) - + fuelCars(listOf(vaz1,vaz2, Taz)) // Заправляем машины println("Экземпляры класса имеют разное внутреннее состояние:") vaz1.wheelToRight(10) println(vaz1.toString()) // Выводит 10 и случайную скорость @@ -77,7 +78,6 @@ fun getColor() { fun techChecks() { val vaz1 = Vaz2107.build(Car.Plates("123", 77)) val vaz2 = Vaz2108.build(Car.Plates("321", 78)) - repairEngine(vaz1) repairEngine(vaz2) } @@ -90,4 +90,10 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun fuelCars(cars:List) { + println("\n===> fuel cars...") + val tankStation = GasStation() + tankStation.addFuelToCar(cars) } \ No newline at end of file