diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..fc7cfad 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) + + /** + * Горловина бензобака + */ + val 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..a8178eb 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 getFuelContents(): Int } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/LpgMouth.kt b/src/main/kotlin/ru/otus/cars/LpgMouth.kt new file mode 100644 index 0000000..d245bfc --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/LpgMouth.kt @@ -0,0 +1,14 @@ +package ru.otus.cars + + +class LpgMouth(tank: Tank) : TankMouth(tank){ + fun fuelLpg(liters : Int) { + if (MouthState == false) { + println("Закрыта горловина бака") + open() + } + println("Собираемся заправить $liters литров газа") + tank.receiveFuel(liters) + close() + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/PetrolMouth.kt b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt new file mode 100644 index 0000000..1b429d0 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt @@ -0,0 +1,13 @@ +package ru.otus.cars + +class PetrolMouth(tank: Tank) : TankMouth(tank){ + fun fuelPetrol(liters : Int){ + if (MouthState==false) { + println("Закрыта горловина бака") + open() + } + println("Собираемся заправить $liters литров бензина") + tank.receiveFuel(liters) + close() + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/cars/Refill.kt b/src/main/kotlin/ru/otus/cars/Refill.kt new file mode 100644 index 0000000..500c340 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Refill.kt @@ -0,0 +1,15 @@ +package ru.otus.cars + +class Refill { + fun refuel(car: Car, count: Int) { + try { + when (car.tankMouth) { + is LpgMouth -> (car.tankMouth as LpgMouth).fuelLpg(count) + is PetrolMouth -> (car.tankMouth as PetrolMouth).fuelPetrol(count) + } + } + catch (exception: 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..34c2d14 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,29 @@ +package ru.otus.cars + +import kotlin.random.Random + +interface Tank { + fun getContents():Int + fun receiveFuel(liters : Int) +} + + +open class CarTank (val capacity: Int): Tank { + private var content = Random.nextInt(0, capacity/2) + + override fun getContents(): Int = content + + override fun receiveFuel(liters: Int) { + if (content+liters == capacity) { + content += liters + println("Заправили до полного бака") + } else if (content+liters > capacity) { + content = capacity + println("Полный бак. Не влезло ${content+liters-capacity} литров") + } + else { + content += liters + println("Залили $liters литров") + } + } +} \ 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..ab34a0d --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,16 @@ +package ru.otus.cars + +abstract class TankMouth(protected val tank: Tank) { + + var MouthState: Boolean = false // Доступность горловины бензобака + + fun open() { + println("Горловина открыта") + MouthState = true; + } + + fun close() { + println("Горловина закрыта") + MouthState = false; + } +} diff --git a/src/main/kotlin/ru/otus/cars/Taz.kt b/src/main/kotlin/ru/otus/cars/Taz.kt index 49df937..e32c0f4 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 tankMouth: TankMouth + 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..215ac80 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(tank) } /** @@ -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, fuelContents=${tank.getContents()})" } /** @@ -74,5 +75,11 @@ 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 + + private val tank = CarTank(39) + override lateinit var 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..01b9e38 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(tank) } 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, fuelContents=${tank.getContents()})" } /** @@ -78,5 +79,11 @@ 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() + } } + + private val tank = CarTank(43) + override lateinit var tankMouth: TankMouth } \ 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..2f20a05 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 kotlin.random.Random + fun main() { println("\n===> drive cars...") driveCars() @@ -16,6 +18,8 @@ fun main() { techChecks() println("\n===> Taz...") println(Taz.color) + println("\n===> refill...") + refill() } fun driveCars() { @@ -90,4 +94,27 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun refill() { + val fueldStation = Refill() + val cars = listOf( + Vaz2107.build(Car.Plates("123", 77)), + Vaz2108.build(Car.Plates("321", 78)) + ) + + cars.forEach { car -> + println("На заправке $car") + println("Сейчас в баке ${car.carOutput.getFuelContents()} литров") + val count = Random.nextInt(1, 30) + fueldStation.refuel(car,count) + println("Заправили $count литров") + try { + println("Итого в баке ${car.carOutput.getFuelContents()} литров") + } + catch (e: NotImplementedError) + { + println("Взрыв машины") + } + } } \ No newline at end of file