diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..e2caa48 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuel_system.TankMouth + /** * Машина целиком */ @@ -19,6 +21,8 @@ interface Car : CarInput { */ val carOutput: CarOutput + val mouth: TankMouth + /** * Получить оборудование */ @@ -28,4 +32,4 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ data class Plates(val number: String, val region: Int) -} \ 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..1418a5a 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,6 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int -} \ No newline at end of file + + fun getFuelContents(): Int +} diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..e4a6c83 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.fuel_system.TankMouth + object Taz: Car { /** * Номерной знак @@ -18,6 +20,9 @@ object Taz: Car { override val carOutput: CarOutput get() = throw NotImplementedError("Приборов нет") + override val mouth: TankMouth + get() = throw NotImplementedError("Горловины нет") + /** * Получить оборудование */ @@ -36,4 +41,9 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } -} \ No newline at end of file + + override fun toString(): String { + return "Taz" + } + +} diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..992707f 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -1,5 +1,9 @@ package ru.otus.cars +import ru.otus.cars.fuel_system.LpgMouth +import ru.otus.cars.fuel_system.Tank +import ru.otus.cars.fuel_system.TankBuilder +import ru.otus.cars.fuel_system.TankMouth import kotlin.random.Random /** @@ -17,9 +21,11 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { - this.engine = getRandomEngine() - this.plates = plates + override fun build(plates: Car.Plates): Vaz2107 = + Vaz2107("Зеленый").apply { + this.engine = getRandomEngine() + this.plates = plates + this.mouth = LpgMouth(tank) } /** @@ -48,6 +54,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { println("Др-др-др-др....") } + private val tank: Tank = TankBuilder(50) private var currentSpeed: Int = 0 // Скока жмёт /** @@ -57,6 +64,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override lateinit var plates: Car.Plates private set + override lateinit var mouth: TankMouth + private set + // Выводим состояние машины override fun toString(): String { return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" @@ -74,5 +84,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..6c83e50 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -1,5 +1,9 @@ package ru.otus.cars +import ru.otus.cars.fuel_system.PetrolMouth +import ru.otus.cars.fuel_system.Tank +import ru.otus.cars.fuel_system.TankBuilder +import ru.otus.cars.fuel_system.TankMouth import kotlin.random.Random /** @@ -18,9 +22,11 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { } } - override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { - this.engine = getRandomEngine() - this.plates = plates + override fun build(plates: Car.Plates): Vaz2108 = + Vaz2108("Красный").apply { + this.engine = getRandomEngine() + this.plates = plates + this.mouth = PetrolMouth(tank) } fun alignWheels(vaz2108: Vaz2108) { @@ -52,6 +58,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { return super.getEquipment() + ", музыка" } + + private val tank: Tank = TankBuilder(50) private var currentSpeed: Int = 0 // Скока жмёт /** @@ -61,6 +69,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override lateinit var plates: Car.Plates private set + override lateinit var mouth: TankMouth + private set + // Выводим состояние машины override fun toString(): String { return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)" @@ -78,5 +89,9 @@ 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/fuel_system/Tank.kt b/src/main/kotlin/ru/otus/cars/fuel_system/Tank.kt new file mode 100644 index 0000000..dd35097 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuel_system/Tank.kt @@ -0,0 +1,21 @@ +package ru.otus.cars.fuel_system + +interface Tank { + +fun getContents(): Int + +fun receiveFuel(liters: Int) + +} + + class TankBuilder (val capacity: Int): Tank { + + private var contents = 0 + + override fun getContents(): Int = contents + + override fun receiveFuel(liters: Int) { + contents += liters + if (contents >= capacity) println("Полный бак") + } + } diff --git a/src/main/kotlin/ru/otus/cars/fuel_system/TankMouth.kt b/src/main/kotlin/ru/otus/cars/fuel_system/TankMouth.kt new file mode 100644 index 0000000..78abc03 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/fuel_system/TankMouth.kt @@ -0,0 +1,48 @@ +package ru.otus.cars.fuel_system + +sealed class TankMouth(protected val tank: Tank){ + private var isOpened = false + fun open(){ + when(isOpened){ + true -> println("Горловина бака уже открыта") + else -> { + isOpened = true + println("Горловина бака открыта") + } + } + } + + fun close(){ + when(isOpened){ + false -> print("Горловина бака уже закрыта") + else -> { + isOpened = 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/gas_station/GasStation.kt b/src/main/kotlin/ru/otus/cars/gas_station/GasStation.kt new file mode 100644 index 0000000..74cde7d --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/gas_station/GasStation.kt @@ -0,0 +1,25 @@ +package ru.otus.cars.gas_station + +import ru.otus.cars.Car +import ru.otus.cars.fuel_system.LpgMouth +import ru.otus.cars.fuel_system.PetrolMouth + +class GasStation { + + fun getFuel(car: Car, liters: Int){ + val mouth = car.mouth + when(mouth){ + is PetrolMouth -> mouth.fuelPetrol(liters) + is LpgMouth -> mouth.fuelLpg(liters) + } + } + + fun safeRefill(car: Car, liters: Int) { + try { + getFuel(car, liters) + } + catch (e: NotImplementedError) { + println("Бак взорвался, но никто не пострадал") + } + } +} diff --git a/src/main/kotlin/ru/otus/cars/main.kt b/src/main/kotlin/ru/otus/cars/main.kt index 978d0ef..882f844 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -1,5 +1,7 @@ package ru.otus.cars +import ru.otus.cars.gas_station.GasStation + fun main() { println("\n===> drive cars...") driveCars() @@ -16,6 +18,8 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + println("\n===> Fuel car collection...") + saveRefillCarCollection() } fun driveCars() { @@ -90,4 +94,28 @@ 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 saveRefillCarCollection() { + val gasStation = GasStation() + val carList = listOf( Vaz2107.build(Car.Plates("123", 77)), + Vaz2108.build(Car.Plates("321", 78)), + Taz) + + carList.forEach{ car -> + fun getCarOutputSafely() { + try { + println("В баке ${car.carOutput.getFuelContents()} литров") + } + catch (e: NotImplementedError){ + println("Что-то пошло не так: ${e.message}") + } + } + + println("Текущая машина - $car") + getCarOutputSafely() + gasStation.safeRefill(car, 50) + getCarOutputSafely() + } + +}