From 3619a04c840657c88874db436d4e6f54a4654c26 Mon Sep 17 00:00:00 2001 From: OlegD Date: Sun, 20 Oct 2024 19:20:49 +0300 Subject: [PATCH] create homework --- src/main/kotlin/ru/otus/cars/Car.kt | 5 +++ src/main/kotlin/ru/otus/cars/CarOutput.kt | 2 + src/main/kotlin/ru/otus/cars/GasStation.kt | 28 ++++++++++++ src/main/kotlin/ru/otus/cars/LpgMouth.kt | 3 ++ src/main/kotlin/ru/otus/cars/PetrolMouth.kt | 3 ++ src/main/kotlin/ru/otus/cars/Tank.kt | 22 +++++++++ src/main/kotlin/ru/otus/cars/TankMouth.kt | 11 +++++ src/main/kotlin/ru/otus/cars/Taz.kt | 2 + src/main/kotlin/ru/otus/cars/Vaz2107.kt | 14 ++++++ src/main/kotlin/ru/otus/cars/Vaz2108.kt | 14 ++++++ src/main/kotlin/ru/otus/cars/VazPlatform.kt | 49 ++++++++++++++++++++- src/main/kotlin/ru/otus/cars/main.kt | 17 +++++++ 12 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/ru/otus/cars/GasStation.kt create mode 100644 src/main/kotlin/ru/otus/cars/LpgMouth.kt create mode 100644 src/main/kotlin/ru/otus/cars/PetrolMouth.kt create mode 100644 src/main/kotlin/ru/otus/cars/Tank.kt create mode 100644 src/main/kotlin/ru/otus/cars/TankMouth.kt diff --git a/src/main/kotlin/ru/otus/cars/Car.kt b/src/main/kotlin/ru/otus/cars/Car.kt index 559978c..cd4cac7 100644 --- a/src/main/kotlin/ru/otus/cars/Car.kt +++ b/src/main/kotlin/ru/otus/cars/Car.kt @@ -14,6 +14,11 @@ interface Car : CarInput { */ val color: String + /** + * Бак + * */ + 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..0257eac 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 + + fun getFuelContents(): 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..9c57b5c --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/GasStation.kt @@ -0,0 +1,28 @@ +package ru.otus.cars + +class GasStation { + + companion object { + fun refuelCar(car: Car, litres: Int) { + println("Заправляем ${car.javaClass.simpleName}") + try { + when (car.tank.tankMouth) { + is LpgMouth -> refuelLpg(car.tank, litres) + + is PetrolMouth -> refuelPetrol(car.tank, litres) + } + } catch (e: NotImplementedError) { + println("Не повезло. ${e.message}") + } + } + private fun refuelLpg(tank: Tank, litres: Int) { + println("Заправляем газом") + tank.receiveFuel(litres) + } + + private fun refuelPetrol(tank: Tank, litres: Int) { + println("Заправляем бензином") + tank.receiveFuel(litres) + } + } +} \ 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..dadfd74 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/LpgMouth.kt @@ -0,0 +1,3 @@ +package ru.otus.cars + +class LpgMouth : TankMouth() \ 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..f3fdd7a --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/PetrolMouth.kt @@ -0,0 +1,3 @@ +package ru.otus.cars + +class PetrolMouth : TankMouth() \ 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..abc1864 --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/Tank.kt @@ -0,0 +1,22 @@ +package ru.otus.cars + + +/** + * Бак + * */ +interface Tank { + /** + * Горловина + * */ + val tankMouth: TankMouth + + /** + * Показать содержимое + * */ + fun getContents():Int + + /** + * Заправить + * */ + fun receiveFuel(litres: Int) +} \ 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..78b232a --- /dev/null +++ b/src/main/kotlin/ru/otus/cars/TankMouth.kt @@ -0,0 +1,11 @@ +package ru.otus.cars + +abstract class TankMouth { + fun open() { + println("Горловина открыта") + } + + fun close() { + println("Горловина закрыта") + } +} \ 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..b175d26 100644 --- a/src/main/kotlin/ru/otus/cars/Taz.kt +++ b/src/main/kotlin/ru/otus/cars/Taz.kt @@ -11,6 +11,8 @@ object Taz: Car { * Цвет машины */ override val color: String = "Ржавый" + override val tank: Tank + get() = throw NotImplementedError("Нельзя заправить. Машина взорвалась!!!") /** * Следит за машиной diff --git a/src/main/kotlin/ru/otus/cars/Vaz2107.kt b/src/main/kotlin/ru/otus/cars/Vaz2107.kt index be857d2..2d6eb27 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2107.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2107.kt @@ -17,9 +17,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { } } + private fun getRandomTank(): VasTank { + return when (Random.nextInt(0, 2)) { + 0 -> VasTank.Petrol(PetrolMouth()) + else -> VasTank.Lpg(LpgMouth()) + } + } + override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply { this.engine = getRandomEngine() this.plates = plates + this.tank = getRandomTank() } /** @@ -39,6 +47,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) { // Переопределяем свойство родителя override lateinit var engine: VazEngine private set + override lateinit var tank: Tank + private set /** * Семерка едет так @@ -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 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..6193fa1 100644 --- a/src/main/kotlin/ru/otus/cars/Vaz2108.kt +++ b/src/main/kotlin/ru/otus/cars/Vaz2108.kt @@ -18,9 +18,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { } } + private fun getRandomTank(): VasTank { + return when (Random.nextInt(0, 2)) { + 0 -> VasTank.Petrol(PetrolMouth()) + else -> VasTank.Lpg(LpgMouth()) + } + } + override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply { this.engine = getRandomEngine() this.plates = plates + this.tank = getRandomTank() } fun alignWheels(vaz2108: Vaz2108) { @@ -37,6 +45,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { // Переопределяем свойство родителя override lateinit var engine: VazEngine private set + override lateinit var tank: Tank + private set /** * Восьмерка едет так @@ -78,5 +88,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) { override fun getCurrentSpeed(): Int { return this@Vaz2108.currentSpeed } + + override fun getFuelContents(): Int { + return tank.getContents() + } } } \ 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..bf04f5f 100644 --- a/src/main/kotlin/ru/otus/cars/VazPlatform.kt +++ b/src/main/kotlin/ru/otus/cars/VazPlatform.kt @@ -5,15 +5,23 @@ 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 = "Кузов, колеса, движок" // Абстрактное свойство двигателя abstract val engine: VazEngine + + abstract override val tank: Tank + } // Перечисление двигателей ВАЗ @@ -23,4 +31,41 @@ sealed class VazEngine { data class LADA_2107(override val volume: Int) : VazEngine() data class SAMARA_2108(override val volume: Int) : VazEngine() +} + +/** + * Реализция бака + * */ +sealed class VasTank : Tank { + + /** + * Уровень горючего + * */ + private var fuelLevel: Int = 0 + set(value) { + if (value <= 30) { + field = value + } else { + field = 30 + println("Бак под завязку") + } + } + + override fun getContents(): Int { + return fuelLevel + } + + override fun receiveFuel(litres: Int) { + tankMouth.open() + this.fuelLevel = litres + tankMouth.close() + } + + override fun toString(): String { + return "${tankMouth.javaClass} (fuelLevel=$fuelLevel)" + } + + data class Petrol(override val tankMouth: TankMouth) : VasTank() + data class Lpg(override val tankMouth: TankMouth) : VasTank() + } \ 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..13974b5 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 car...") + refuelCar() } fun driveCars() { @@ -90,4 +92,19 @@ fun repairEngine(car: VazPlatform) { is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car") is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car") } +} + +fun refuelCar(){ + val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)) + val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)) + + println("Содержимое бака ${vaz1.javaClass.simpleName} = ${vaz1.carOutput.getFuelContents()}") + GasStation.refuelCar(vaz1, 20) + println("Содержимое бака ${vaz1.javaClass.simpleName} после заправки = ${vaz1.carOutput.getFuelContents()}") + println() + println("Содержимое бака ${vaz2.javaClass.simpleName} = ${vaz2.carOutput.getFuelContents()}") + GasStation.refuelCar(vaz2,47) + println("Содержимое бака ${vaz2.javaClass.simpleName} после заправки = ${vaz2.carOutput.getFuelContents()}") + println() + GasStation.refuelCar(Taz, 1500) } \ No newline at end of file