diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..db406ba 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -28,4 +28,8 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ data class Plates(val number: String, val region: Int) + /** + * Горловина и бак + */ + var mouth: TankMouth } \ 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..fff0c37 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,10 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + + /** + * Покажи уровень топлива + */ + + fun getFuelContents(): String } \ 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..d721a6d --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,28 @@ +package ru.otus.cars + +import kotlin.random.Random + +object GasStation { + fun fuelCar(car: Car) { + try { + println(car.carOutput.getFuelContents()) + when (car.mouth.type) { + FuelType.LPG -> fuelWithLpg(car.mouth as LpgMouth) + FuelType.PETROL -> fuelWithPetrol(car.mouth as PetrolMouth) + } + println(car.carOutput.getFuelContents()) + + } catch (e :NotImplementedError) { + println("Опять этот таз приехал") + } + } + + + private fun fuelWithLpg(tankMouth: LpgMouth) { + tankMouth.fuelLpg(Random.nextInt(0, 200)) + } + + private fun fuelWithPetrol(tankMouth: PetrolMouth) { + tankMouth.fuelPetrol(Random.nextInt(0, 200)) + } +} \ 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..f2b801a --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,46 @@ +package ru.otus.cars + +interface Tank { + var fuelLevel: Int + + fun getContents(): String + + fun receiveFuel(liters: Int) +} + +enum class FuelType(val type: String) { + PETROL("Бензин"), + LPG("Сжиженный газ") +} + +abstract class TankMouth(val type: FuelType): Tank { + override var fuelLevel: Int = 0 + override fun getContents(): String { + return "Тип топлива: ${type.type}, уровень $fuelLevel" + } + + override fun receiveFuel(liters: Int) { + open() + fuelLevel += liters + close() + } + + private fun open() { + println("Горловина открыта") + } + private fun close() { + println("Горловина закрыта") + } +} + +class PetrolMouth: TankMouth(FuelType.PETROL) { + fun fuelPetrol(liters: Int) { + receiveFuel(liters) + } +} + +class LpgMouth: TankMouth(FuelType.LPG) { + fun fuelLpg(liters: Int) { + receiveFuel(liters) + } +} \ 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..9442680 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -23,6 +23,13 @@ object Taz: Car { */ override fun getEquipment(): String = "Крыса" + /** + * Горловина неисправна + */ + override var mouth: TankMouth + get() = throw NotImplementedError("Вместо горловины - дырка от бублика") + set(_) {} + /** * Руль вправо на [degrees] градусов */ diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..b166c34 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -20,6 +20,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { this.engine = getRandomEngine() this.plates = plates + this.mouth = LpgMouth() } /** @@ -59,7 +60,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=${this.mouth.fuelLevel})" } /** @@ -67,6 +68,11 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + /** + * Горловину поставят при сборке + */ + override lateinit var mouth: TankMouth + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107! */ @@ -74,5 +80,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelContents(): String { + return this@Vaz2107.mouth.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..bdbde44 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -21,6 +21,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { this.engine = getRandomEngine() this.plates = plates + this.mouth = PetrolMouth() } fun alignWheels(vaz2108: Vaz2108) { @@ -63,7 +64,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=${this.mouth.fuelLevel})" } /** @@ -71,6 +72,11 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + /** + * Горловину поставят при сборке + */ + override lateinit var mouth: TankMouth + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! */ @@ -78,5 +84,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelContents(): String { + return this@Vaz2108.mouth.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..65a8ace 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,21 +1,20 @@ package ru.otus.cars fun main() { - println("\n===> drive cars...") - driveCars() - println("\n===> inner test...") - innerNestedCheck() - println("\n===> garage make...") - garageMake() - println("\n===> model special...") - println("\n===> get equipment...") - getEquipment() - println("\n===> get color...") - getColor() - println("\n===> tech checks...") - techChecks() - println("\n===> Taz...") - println(Taz.color) + println("\n===> fuel cars...") + fuelCars() +} + +fun fuelCars() { + val carLine: List = listOf( + Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)), + Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)), + Taz + ) + + carLine.forEach{ + GasStation.fuelCar(it) + } } fun driveCars() {