diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..acc1f4e 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -19,6 +19,11 @@ 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..d7444e5 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 getFuelLevel(): Int } \ 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..8ecc881 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,38 @@ +package ru.otus.cars + +class PetrolMouth(owner: Tank) : TankMouth(owner){ + fun fuelPetrol(liters :Int): Unit { + if (!opened) throw NotImplementedError("Бак закрыт, заправка невозможна") + owner.receiveFuel(liters) + } +} + +class LpgMouth(owner: Tank): TankMouth(owner) { + fun fuelLpg(liters :Int): Unit { + if (!opened) throw NotImplementedError("Бак закрыт, заправка невозможна") + owner.receiveFuel(liters) + } +} + +abstract class TankMouth(parOwner: Tank) { + val owner = parOwner + var opened : Boolean = false + fun open(): Unit { opened = true } + fun close(): Unit { opened = false } +} + +interface Tank { + var capacity : Int + var fuelLevel : Int + var mouth : TankMouth? + fun getContents(): Int { + return fuelLevel + } + fun receiveFuel(liters: Int): Unit { + if (fuelLevel + liters > capacity) { + fuelLevel = capacity + throw NotImplementedError("Превышена емкость топливного бака") + } + fuelLevel += 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..436d1bf 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,6 +1,8 @@ package ru.otus.cars object Taz: Car { + override val tank: Tank = TazTank() + override val carOutput: CarOutput = TazOutput() /** * Номерной знак */ @@ -12,11 +14,6 @@ object Taz: Car { */ override val color: String = "Ржавый" - /** - * Следит за машиной - */ - override val carOutput: CarOutput - get() = throw NotImplementedError("Приборов нет") /** * Получить оборудование @@ -36,4 +33,20 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + class TazOutput : CarOutput { + override fun getCurrentSpeed(): Int { + throw NotImplementedError("Спидометр недоступен") + } + + override fun getFuelLevel(): Int { + return tank.getContents() + } + } + + class TazTank : Tank { + override var capacity : Int = 0 + override var fuelLevel : Int = 0 + override var mouth : TankMouth? = PetrolMouth(this) + } } \ 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..739f5d5 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.mouth = LpgMouth(this.tank) } /** @@ -40,6 +41,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var engine: VazEngine private set + /** * Семерка едет так */ @@ -67,6 +69,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + override var tank: Tank = VazTank() + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107! */ @@ -74,5 +78,15 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun getFuelLevel(): Int { + return this@Vaz2107.tank.fuelLevel + } + } + + inner class VazTank : Tank { + override var capacity : Int = 40 + override var fuelLevel : Int = 0 + override var mouth : TankMouth? = null } } \ 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..9c96501 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.mouth = PetrolMouth(this.tank) } fun alignWheels(vaz2108: Vaz2108) { @@ -71,6 +72,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { */ override val carOutput: CarOutput = VazOutput() + override var tank: Tank = VazTank() + /** * Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108! */ @@ -78,5 +81,15 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelLevel(): Int { + return this@Vaz2108.tank.fuelLevel + } + } + + inner class VazTank : Tank { + override var capacity : Int = 50 + override var fuelLevel : Int = 0 + override var mouth : TankMouth? = null } } \ 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..ba345eb 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,21 +1,26 @@ package ru.otus.cars +import kotlin.random.Random + 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) + val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)) + val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)) + val gs = GasStation() + + + println("vaz1: в баке " + vaz1.carOutput.getFuelLevel() + " литров") + println("vaz2: в баке " + vaz2.carOutput.getFuelLevel() + " литров") + println("taz: в баке " + Taz.carOutput.getFuelLevel() + " литров") + + gs.car = vaz1 + gs.refuelingCar(20) + gs.car = vaz2 + gs.refuelingCar(30) + gs.car = Taz + gs.refuelingCar(40) + println("vaz1: в баке " + vaz1.carOutput.getFuelLevel() + " литров") + println("vaz2: в баке " + vaz2.carOutput.getFuelLevel() + " литров") + println("taz: в баке " + Taz.carOutput.getFuelLevel() + " литров") } fun driveCars() { @@ -90,4 +95,25 @@ 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 +} + +class GasStation { + var car : Car? = null + fun refuelingCar(liters : Int) : Unit { + try { + if (car != null) { + val mouth = car!!.tank.mouth + if (mouth != null) { + mouth.open() + when (mouth) { + is PetrolMouth -> mouth.fuelPetrol(liters) + is LpgMouth -> mouth.fuelLpg(liters) + } + mouth.close() + } + } + } catch(e : Error) { + println("Ошибка: ${e.message}") + } + } +}