diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..406994e 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -19,6 +19,8 @@ interface Car : CarInput { */ val carOutput: CarOutput + val tank: Tank + /** * Получить оборудование */ diff --git a/src/main/kotlin/ru/otus/cars/CarOutput.kt b/src/main/kotlin/ru/otus/cars/CarOutput.kt index 875339f..72fe36f 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 getFuelValue(): Int + + fun getFuelType(): FuelType } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/FuelType.kt b/src/main/kotlin/ru/otus/cars/FuelType.kt new file mode 100644 index 0000000..1c071a1 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/FuelType.kt @@ -0,0 +1,3 @@ +package ru.otus.cars + +enum class FuelType { PETROL, PROPANE } 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..f7f7526 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GazStation.kt @@ -0,0 +1,13 @@ +package ru.otus.cars + + +object GazStation { + fun carRefuel(car: Car, fuelNumber: Int) { + val fuelType = car.carOutput.getFuelType() + try { + car.tank.TankMouth().addFuel(fuelNumber, fuelType) + } catch (e: Exception) { + 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..782b0e0 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,21 @@ +package ru.otus.cars + +class Tank(val fuelType: FuelType) { + var fuelValue = 0 + + + inner class TankMouth() { + fun addFuel(value: Int, type: FuelType) { + if (type != this@Tank.fuelType) { + println("НЕ ТО ТОПЛИВО! ВИУ-ВИУ! ТЫ АРЕСТОВАН ЗА ТУПОСТЬ") + throw Exception("НЕ ТО ТОПЛИВО! ВИУ-ВИУ! ТЫ АРЕСТОВАН ЗА ТУПОСТЬ") + return + } + fuelValue += value + println("Заправлено литров топлива: $value") + + } + + + } +} \ 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..ad92638 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -36,4 +36,7 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override val tank: Tank + get() = throw NotImplementedError("ВЗРЫВ") } \ 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..945fa55 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -2,6 +2,7 @@ package ru.otus.cars import kotlin.random.Random + /** * Семёрочка */ @@ -20,6 +21,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 = Tank(FuelType.PROPANE) } /** @@ -40,6 +42,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + override lateinit var tank: Tank + private set + /** * Семерка едет так */ @@ -74,5 +79,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelValue(): Int { + val value = this@Vaz2107.tank.fuelValue + println("Автомобиль $MODEL \n Уровень топлива в литрах: $value") + return value + } + + override fun getFuelType(): FuelType { + val value = this@Vaz2107.tank.fuelType + println("Автомобиль $MODEL \n Заправляется топливом: $value") + return value + } } } \ 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..8c6e641 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -1,5 +1,6 @@ package ru.otus.cars +import ru.otus.cars.Vaz2107.Companion import kotlin.random.Random /** @@ -21,6 +22,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 = Tank(FuelType.PETROL) } fun alignWheels(vaz2108: Vaz2108) { @@ -38,6 +40,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + override lateinit var tank: Tank + private set + /** * Восьмерка едет так */ @@ -78,5 +83,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelValue(): Int { + val value = this@Vaz2108.tank.fuelValue + println("Автомобиль $MODEL \n Уровень топлива в литрах: $value") + return value + } + + override fun getFuelType(): FuelType { + val value = this@Vaz2108.tank.fuelType + println("Автомобиль ${Vaz2107.MODEL} \n Заправляется топливом: $value") + return value + } } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/VazPlatform.kt b/src/main/kotlin/ru/otus/cars/VazPlatform.kt index 079c2da..2bab564 100644 --- a/src/main/kotlin/ru/otus/cars/VazPlatform.kt +++ b/src/main/kotlin/ru/otus/cars/VazPlatform.kt @@ -5,9 +5,14 @@ abstract class VazPlatform(override val color: String) : Car { protected var wheelAngle: Int = 0 // Положение руля // Реализация интерфейса CarInput - override fun wheelToRight(degrees: Int) { wheelAngle += degrees } + override fun wheelToRight(degrees: Int) { + wheelAngle += degrees + } + // Реализация интерфейса CarInput - override fun wheelToLeft(degrees: Int) { wheelAngle -= degrees } + override fun wheelToLeft(degrees: Int) { + wheelAngle -= degrees + } // Получить оборудование override fun getEquipment(): String = "Кузов, колеса, движок" diff --git a/src/main/kotlin/ru/otus/cars/main.kt b/src/main/kotlin/ru/otus/cars/main.kt index 978d0ef..cdf39e2 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,23 +1,36 @@ 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) + refuelCarsByStation() + refuelCars() +} + +fun refuelCars(isValidFuel: Boolean = true) { + val vaz1 = Vaz2107.build(Car.Plates("123", 77)) + val vaz2 = Vaz2108.build(Car.Plates("321", 78)) + vaz1.tank.TankMouth().addFuel(10, if (isValidFuel) FuelType.PROPANE else FuelType.PETROL) + vaz2.tank.TankMouth().addFuel(10, if (isValidFuel) FuelType.PETROL else FuelType.PROPANE) + vaz1.carOutput.getFuelValue() + vaz2.carOutput.getFuelValue() +} + + +fun refuelCarsByStation() { + val vaz1 = Vaz2107.build(Car.Plates("123", 77)) + val vaz2 = Vaz2108.build(Car.Plates("321", 78)) + vaz1.carOutput.getFuelValue() + vaz2.carOutput.getFuelValue() + + GazStation.carRefuel(vaz1, 15) + + GazStation.carRefuel(vaz2, 15) + + vaz1.carOutput.getFuelValue() + vaz2.carOutput.getFuelValue() } + fun driveCars() { val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)) val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78))