diff --git a/README.md b/README.md index 466cb7f..9725259 100644 --- a/README.md +++ b/README.md @@ -57,4 +57,3 @@ - статические внутренние класс и объекты (например, для бачка внутри машины) - объекты-компаньоны (для сборщика бака и горловины) - Sealed-интерфейсы или классы (горловина бачка) - diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..77aa4b3 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -28,4 +28,8 @@ interface Car : CarInput { * Внутренний статический класс - номерой знак */ data class Plates(val number: String, val region: Int) + + val tank: Tank + val tankMouth: TankMouth + fun fuelLevel(): Int = tank.fuelLevel } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/CarFactory.kt b/src/main/kotlin/ru/otus/cars/CarFactory.kt index 3b8f03e..2185065 100644 --- a/src/main/kotlin/ru/otus/cars/CarFactory.kt +++ b/src/main/kotlin/ru/otus/cars/CarFactory.kt @@ -17,6 +17,11 @@ object Togliatti : CarFactory { private fun buildVaz2107(plates: Car.Plates): Car { println("Запил ${Vaz2107.MODEL} в Тольятти...") val vaz = Vaz2107.build(plates) + + val tank = TankImpl(capacity = 50) + val mouth = GasMouth + vaz.installTank(tank, mouth) + println("Проверяем тачку...") Vaz2107.test(vaz) vaz.drdrdrdrdr() @@ -26,6 +31,11 @@ object Togliatti : CarFactory { private fun buildVaz2108(plates: Car.Plates): Car { println("Запил ${Vaz2108.MODEL} в Тольятти...") val vaz = Vaz2108.build(plates) + + val tank = TankImpl(capacity = 55) + val mouth = PetrolMouth + vaz.installTank(tank, mouth) + println("Сход-развал...") Vaz2108.alignWheels(vaz) vaz.zhzhzhzh() diff --git a/src/main/kotlin/ru/otus/cars/CarOutput.kt b/src/main/kotlin/ru/otus/cars/CarOutput.kt index 875339f..77a68f9 100644 --- a/src/main/kotlin/ru/otus/cars/CarOutput.kt +++ b/src/main/kotlin/ru/otus/cars/CarOutput.kt @@ -8,4 +8,5 @@ interface CarOutput { * Скажи текущую скорость */ fun getCurrentSpeed(): Int + fun fuelLevel(): Int } \ 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..a41fdc9 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,21 @@ +package ru.otus.cars + +import java.lang.Exception + +class GasStation { + fun refuel(car: Car, amount: Int) { + println("Заправляем машину ${car.color} на $amount литров...") + try { + car.tankMouth.refuel(car.tank, amount) + } catch (e: Exception) { + println("Бак у ${car.color} взорвался! Вызывайте пожарных! ${e.message}") + } + + } + + fun refuelCars(cars: List, amount: Int) { + cars.forEach { refuel(it, amount) } + } +} + + 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..8695e73 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,31 @@ +package ru.otus.cars + +interface Tank { + val capacity: Int + val fuelLevel: Int + fun isLow(): Boolean + fun addFuel(amount: Int) + +} + +class TankImpl(override val capacity: Int) : Tank { + override var fuelLevel: Int = 0 + private set + + override fun isLow(): Boolean = fuelLevel < capacity * 0.1 + + override fun addFuel(amount: Int) { + fuelLevel = (fuelLevel + amount).coerceAtMost(capacity) + } +} + +class ExplosiveTank(override val capacity: Int) : Tank { + override var fuelLevel: Int = 0 + private set + + override fun isLow(): Boolean = false + + override fun addFuel(amount: Int) { + throw IllegalStateException("Ба-бах! Бак взорвался>") + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/TankMouth.kt b/src/main/kotlin/ru/otus/cars/TankMouth.kt new file mode 100644 index 0000000..727575e --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,25 @@ +package ru.otus.cars + +sealed interface TankMouth { + fun refuel(tank: Tank, amount: Int) +} + +object GasMouth : TankMouth { + override fun refuel(tank: Tank, amount: Int) { + if (tank is ExplosiveTank) { + throw IllegalStateException("Ба-бах! Бак взорвался>") + } + println("Заливаем газ") + tank.addFuel(amount) + } +} + +object PetrolMouth : TankMouth { + override fun refuel(tank: Tank, amount: Int) { + if (tank is ExplosiveTank) { + throw IllegalStateException("Ба-бах! Бак взорвался>") + } + println("Заливаем бензин") + tank.addFuel(amount) + } +} \ 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..126c9c2 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -1,6 +1,5 @@ package ru.otus.cars - -object Taz: Car { +object Taz : Car { /** * Номерной знак */ @@ -36,4 +35,15 @@ object Taz: Car { override fun wheelToLeft(degrees: Int) { throw NotImplementedError("Руля нет") } + + override val tank: Tank = ExplosiveTank(40) + override var tankMouth: TankMouth = PetrolMouth + + override fun fuelLevel(): Int { + return tank.fuelLevel + } + + override fun toString(): String { + return "Taz(color=$color, fuel=${fuelLevel()})" + } } \ 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..bfed1c1 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -74,5 +74,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2107.currentSpeed } + + override fun fuelLevel(): Int { + return tank.fuelLevel + } + } + + override lateinit var tank: Tank + override lateinit var tankMouth: TankMouth + + fun installTank(tank: Tank, tankMouth: TankMouth) { + this.tank = tank + this.tankMouth = tankMouth } } \ 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..90375f6 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -78,5 +78,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun fuelLevel(): Int { + return tank.fuelLevel + } + } + + override lateinit var tank: Tank + override lateinit var tankMouth: TankMouth + + fun installTank(tank: Tank, tankMouth: TankMouth) { + this.tank = tank + this.tankMouth = tankMouth } } \ 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..ddb24c8 100644 --- a/src/main/kotlin/ru/otus/cars/main.kt +++ b/src/main/kotlin/ru/otus/cars/main.kt @@ -16,6 +16,8 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + println("\n===> refuel cars...") + refuelCars() } fun driveCars() { @@ -54,8 +56,7 @@ fun garageMake() { fun getEquipment() { val cars = listOf( - Vaz2107.build(Car.Plates("123", 77)), - Vaz2108.build(Car.Plates("321", 78)) + Vaz2107.build(Car.Plates("123", 77)), Vaz2108.build(Car.Plates("321", 78)) ) cars.forEach { car -> @@ -65,8 +66,7 @@ fun getEquipment() { fun getColor() { val cars = listOf( - Vaz2107.build(Car.Plates("123", 77)), - Vaz2108.build(Car.Plates("321", 78)) + Vaz2107.build(Car.Plates("123", 77)), Vaz2108.build(Car.Plates("321", 78)) ) cars.forEach { car -> @@ -90,4 +90,14 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun refuelCars() { + val cars = listOf( + Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)), + Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)), + Taz + ) + val station = GasStation() + station.refuelCars(cars, amount = 30) } \ No newline at end of file