From 6fb3def38dc0069a1b0a3ecd56a54511eac6aa57 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Tue, 6 Jan 2026 11:05:47 +0300 Subject: [PATCH] Homework_04 (Kotlin#5) implementation. --- build.gradle | 13 ++++-- src/main/kotlin/ru/otus/cars/Car.kt | 5 +++ src/main/kotlin/ru/otus/cars/CarOutput.kt | 4 ++ src/main/kotlin/ru/otus/cars/GazStation.kt | 41 ++++++++++++++++++ src/main/kotlin/ru/otus/cars/Tank.kt | 48 ++++++++++++++++++++++ src/main/kotlin/ru/otus/cars/Taz.kt | 5 +++ src/main/kotlin/ru/otus/cars/Vaz2107.kt | 8 +++- src/main/kotlin/ru/otus/cars/Vaz2108.kt | 8 +++- src/main/kotlin/ru/otus/cars/main.kt | 21 +++++++++- 9 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/ru/otus/cars/GazStation.kt create mode 100644 src/main/kotlin/ru/otus/cars/Tank.kt diff --git a/build.gradle b/build.gradle index dcb6872..d1db988 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 { @@ -20,4 +27,4 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib" implementation 'org.junit.jupiter:junit-jupiter:5.8.1' -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..9008f51 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) + + /** + * Топливный бак + */ + var tank: FuelTank } \ 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..4166a02 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,8 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + /** + * Текущее количество топлива + */ + fun getFuelAmount(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/GazStation.kt b/src/main/kotlin/ru/otus/cars/GazStation.kt new file mode 100644 index 0000000..9c36a28 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GazStation.kt @@ -0,0 +1,41 @@ +package ru.otus.cars + +import kotlin.random.Random + +class GazStation( private val carsQueue:List) { + + fun ReFuelCars( ) + { + var fuelAmount = 0 + var realFuelAmnt = 0 + + for( car in carsQueue ) { + showTankFuelAmount(car) + fuelAmount = getFuelAmount() + if ( car.tank.tankMouthType == TankMouthType.FOR_PETROL_FUEl) { + petrolRefuel( fuelAmount, car ) + } else if ((car.tank.tankMouthType == TankMouthType.FOR_GAZ_FUEL)) { + gazRefuel( fuelAmount, car ) + } else { + println("Бак взорвался") + } + realFuelAmnt = car.tank.getFuelCurrAmount() + println("В баке ${realFuelAmnt} литров топлива") + } + } + private fun getFuelAmount():Int{ + println("Сколько требуется топлива?") + Thread.sleep(300) + return Random.nextInt(1, 100) + } + private fun petrolRefuel( fuelAmount:Int, car:Car ){ + println("Будет Заправлен бензин. Количество $fuelAmount") + car.tank.fuelFillUp(fuelAmount) + } + + private fun gazRefuel( fuelAmount:Int, car:Car ){ + println("Будет Заправлен газ. Количествто $fuelAmount") + car.tank.fuelFillUp(fuelAmount) + } + +} \ 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..3eb45a8 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,48 @@ +package ru.otus.cars + +interface Tank { + fun getFuelCurrAmount(): Int + fun fuelFillUp(liters: Int) + fun getTankTankMouthType():Int + fun getTankCapacity():Int +} + +enum class TankMouthType{ + FOR_UNNOWN_FUEL, + FOR_PETROL_FUEl, + FOR_GAZ_FUEL +} + + +class FuelTank (val tankVolume: Int, val tankMouthType:TankMouthType ): Tank { + + private var realFuelAmnt = 0 + private var mouthType = tankMouthType + + override fun getFuelCurrAmount(): Int{ + return realFuelAmnt + } + + override fun fuelFillUp(liters: Int) { + + val possibleVol = tankVolume - realFuelAmnt + if( possibleVol > liters ) { + realFuelAmnt += liters + println( "Запралено топлива ${liters} литров" ) + } + else if( possibleVol <= liters ){ + realFuelAmnt += possibleVol + println( "Запрашиваемый объем слишком большой. Запралено топлива ${possibleVol} литров" ) + } + else if( possibleVol == 0){ + realFuelAmnt = tankVolume + println("Бак полон. Заправка не требуется") + } + } + override fun getTankTankMouthType():Int { + return mouthType.ordinal + } + override fun getTankCapacity():Int{ + return tankVolume + } +} diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..5b2792a 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -36,4 +36,9 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override fun toString(): String { + return "Taz" + } + override var tank = FuelTank( 30, TankMouthType.FOR_UNNOWN_FUEL) } \ 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..f5e7964 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.tank = FuelTank( 40, TankMouthType.FOR_GAZ_FUEL) } /** @@ -39,6 +40,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { // Переопределяем свойство родителя override lateinit var engine: VazEngine private set + override var tank: FuelTank = FuelTank( 40, TankMouthType.FOR_UNNOWN_FUEL ) /** * Семерка едет так @@ -59,7 +61,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLvl = carOutput.getFuelAmount() + return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$fuelLvl)" } /** @@ -74,5 +77,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + override fun getFuelAmount(): Int { + return this@Vaz2107.tank.getFuelCurrAmount() + } } } \ 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..767778f 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.tank = FuelTank( 50, TankMouthType.FOR_PETROL_FUEl) } fun alignWheels(vaz2108: Vaz2108) { @@ -38,6 +39,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + override var tank = FuelTank( 45, TankMouthType.FOR_PETROL_FUEl) /** * Восьмерка едет так */ @@ -63,7 +65,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { // Выводим состояние машины override fun toString(): String { - return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" + val fuelLvl = carOutput.getFuelAmount() + return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelLevel=$fuelLvl)" } /** @@ -78,5 +81,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + override fun getFuelAmount(): Int { + return this@Vaz2108.tank.getFuelCurrAmount() + } } } \ 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..91aef9c 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,6 +16,13 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + + val carsQueue = listOf( Vaz2107.build(Car.Plates("123", 90)), + Vaz2108.build(Car.Plates("567", 23)), + Taz) + + val fuelStation = GazStation(carsQueue) + fuelStation.ReFuelCars() } fun driveCars() { @@ -90,4 +97,16 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } -} \ No newline at end of file +} +fun showTankFuelAmount( car:Car ){ + val tankFuelAmount:Int = car.tank.getFuelCurrAmount() + var carNumber:String = "" + + try{ + carNumber = car.plates.number + println("В вашей машине $carNumber $tankFuelAmount литров топлива") + } + catch( e:NotImplementedError ){ + println("${e.message}") + } +}