From 8dc611f8ec9689491aea876d56b39032a0be46db Mon Sep 17 00:00:00 2001 From: Gena Date: Fri, 22 Aug 2025 23:45:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=BC=D0=B0=D1=88=D0=BD=D0=B5?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=20Kotlin-5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 11 ++++- src/main/kotlin/ru/otus/cars/Car.kt | 5 +++ src/main/kotlin/ru/otus/cars/CarOutput.kt | 5 +++ src/main/kotlin/ru/otus/cars/FuelStation.kt | 16 ++++++++ src/main/kotlin/ru/otus/cars/Tank.kt | 22 ++++++++++ src/main/kotlin/ru/otus/cars/TankMouth.kt | 45 +++++++++++++++++++++ src/main/kotlin/ru/otus/cars/Taz.kt | 7 ++++ src/main/kotlin/ru/otus/cars/Vaz2107.kt | 10 ++++- src/main/kotlin/ru/otus/cars/Vaz2108.kt | 9 ++++- src/main/kotlin/ru/otus/cars/main.kt | 19 +++++++++ 10 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/ru/otus/cars/FuelStation.kt create mode 100644 src/main/kotlin/ru/otus/cars/Tank.kt create mode 100644 src/main/kotlin/ru/otus/cars/TankMouth.kt diff --git a/build.gradle b/build.gradle index dcb6872..48ffcaf 100644 --- a/build.gradle +++ b/build.gradle @@ -1,9 +1,16 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '2.2.10' + id 'org.jetbrains.kotlin.jvm' version '1.9.23' +} + +java { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } kotlin { - jvmToolchain(17) + jvmToolchain { + languageVersion = JavaLanguageVersion.of(17) + } } test { diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..ac0af1a 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -28,4 +28,9 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ data class Plates(val number: String, val region: Int) + + /** + * Горловина топливного бака + */ + val tankMouth: 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..c02dd38 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,9 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + + /** + * Получение количества топлива в баке + */ + fun getFuelContents(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/FuelStation.kt b/src/main/kotlin/ru/otus/cars/FuelStation.kt new file mode 100644 index 0000000..68b6ffa --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/FuelStation.kt @@ -0,0 +1,16 @@ +package ru.otus.cars + +class FuelStation { + fun FillUpTheCar(car: Car, value: Int) + { + try { + when (car.tankMouth) { + is PetrolMouth -> (car.tankMouth as PetrolMouth).fuelPetrol(value) + is LpgMouth -> (car.tankMouth as LpgMouth).fuelLpg(value) + } + } + catch (e: NotImplementedError) { + 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..dc0b91e --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,22 @@ +package ru.otus.cars + +interface Tank { + fun getContents(): Int + fun receiveFuel(liters: Int) +} + +class MyTank (val capacity: Int): Tank { + private var contents = 0 + + override fun getContents(): Int = contents + + override fun receiveFuel(liters: Int) { + contents += liters + + if (contents >= capacity) { + contents = capacity + println("Полный бак") + } + } +} + 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..b97edc0 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,45 @@ +package ru.otus.cars + +open class TankMouth(protected val tank: Tank) { + private var isOpen = false + + fun open() { + when (isOpen) { + true -> println("Горловина уже открыта") + else -> { + isOpen = true + println("Горловина открыта") + } + } + } + + fun close(){ + when(isOpen){ + false -> print("Горловина уже закрыта") + else -> { + isOpen = false + println("Горловина закрыта") + } + } + } +} + +class PetrolMouth(tank: Tank) : TankMouth(tank){ + fun fuelPetrol(liters: Int){ + open() + println("Старт заправки") + tank.receiveFuel(liters) + println("Заправка выполнена") + close() + } +} + +class LpgMouth(tank: Tank): TankMouth(tank) { + fun fuelLpg(liters: Int){ + open() + println("Старт заправки") + tank.receiveFuel(liters) + println("Заправка выполнена") + close() + } +} diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..5e6a033 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("Ба-бах!!!") + /** * Получить оборудование */ @@ -36,4 +39,8 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override fun toString(): String { + return "Taz" + } } \ 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..389736e 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.tankMouth = LpgMouth(tank) } /** @@ -40,6 +41,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + private val tank = MyTank(50) + override lateinit var tankMouth: TankMouth /** * Семерка едет так */ @@ -59,7 +62,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLvl = carOutput.getFuelContents() + return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$fuelLvl)" } /** @@ -74,5 +78,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..f6d351c 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.tankMouth = PetrolMouth(tank) } fun alignWheels(vaz2108: Vaz2108) { @@ -38,6 +39,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + private val tank = MyTank(50) + override lateinit var tankMouth: TankMouth /** * Восьмерка едет так */ @@ -63,7 +66,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLvl = carOutput.getFuelContents() + return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$fuelLvl)" } /** @@ -78,5 +82,8 @@ 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/main.kt b/src/main/kotlin/ru/otus/cars/main.kt index 978d0ef..6390281 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,6 +16,25 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + + val fuelStation = FuelStation() + val cars = listOf( Vaz2107.build(Car.Plates("777", 77)), + Vaz2108.build(Car.Plates("284", 61)), + Taz) + + cars.forEach { nextcar -> + println("Заправляем машину - $nextcar") + fuelStation.FillUpTheCar(nextcar,20) + + try { + val liters = nextcar.carOutput.getFuelContents() + println("Теперь в баке $liters литров") + } + catch (e: NotImplementedError) + { + println("Нет больше машины!") + } + } } fun driveCars() {