diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..7017a9f 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) + + /** + * горловина TankMouth + */ + var 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..7e2a0ff 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..fefc52e --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,24 @@ +package ru.otus.cars + +class GasStation { + + fun addFuel(car: Car, liters: Int) { + + val mouth = car.tankMouth + + when (mouth) { + is PetrolMouth -> mouth.fuelPetrol(liters) + is LpgMouth -> mouth.fuelLpg(liters) + } + + } + + fun safeAddFuel(car: Car, liters: Int) { + try { + addFuel(car, liters) + } 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..6c8a915 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,62 @@ +package ru.otus.cars + +interface Tank { + + var fuelValue: Int + + fun getContents(): String + + fun receiveFuel(liters: Int) { + fuelValue += liters + } + +} + +enum class FuelType(val type: String) { + PETROL("Бензин"), + LPG("Газ") +} + +abstract class TankMouth(val fuelType: FuelType) : Tank { + + override var fuelValue: Int = 0 + private var mouthOpen = false + + override fun getContents(): String { + return "Тип топлива: ${fuelType.type}, уровень $fuelValue" + } + + fun open() { + mouthOpen = true + + } + + fun close() { + mouthOpen = false + } +} + +class PetrolMouth : TankMouth(FuelType.PETROL) { + + fun fuelPetrol(liters: Int) { + open() + println("Заправка начинается") + receiveFuel(liters) + println("Заправка окончена") + close() + } + +} + +class LpgMouth : TankMouth(FuelType.LPG) { + + fun fuelLpg(liters: Int) { + open() + println("Начинается заправка") + 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..f8e98fe 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,6 +1,11 @@ package ru.otus.cars object Taz: Car { + + override var tankMouth: TankMouth + get() = throw NotImplementedError("Взрыв") + set(value) {} + /** * Номерной знак */ diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..3669bcc 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() } /** @@ -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, fuelValue=${this.tankMouth.fuelValue})" } /** @@ -67,6 +68,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + override lateinit var tankMouth: TankMouth + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107! */ @@ -74,5 +77,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelContents(): String { + return this@Vaz2107.tankMouth.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..b7d27ea 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() } 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, fuelValue=${this.tankMouth.fuelValue})" } /** @@ -71,6 +72,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + override lateinit var tankMouth: TankMouth + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! */ @@ -78,5 +81,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + override fun getFuelContents(): String { + return this@Vaz2108.tankMouth.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..f1a5907 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,8 +16,25 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + gasValue() } + fun gasValue() { + val gasStation = GasStation() + val vaz2107 = Vaz2107.build(Car.Plates("123", 77)) + val vaz2108 = Vaz2108.build(Car.Plates("321", 78)) + val taz = Taz + val carsList = listOf(vaz2107, vaz2108, taz) + + carsList.forEach{ + val addLiters = 50 + println("Характеристики до заправки $it") + gasStation.safeAddFuel(it, addLiters) + println("Характеристики после заправки $it") + } + } + + fun driveCars() { val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)) val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78))