Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

/**
* Горловина
*/
val tankMouth: TankMouth

/**
* Получить оборудование
*/
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/CarInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ interface CarInput {
* Руль влево на [degrees] градусов
*/
fun wheelToLeft(degrees: Int)

/**
* Заправиться
*/
fun addFuel(liters: Int)
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/CarOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

/**
* Получить уровень топлива
*/
fun getFuelLevel(): Int
}
9 changes: 9 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.otus.cars

/**
* Горловина
*/
sealed interface TankMouth {
class PetrolMouth : TankMouth
class LpgMouth : TankMouth
}
37 changes: 37 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankSystem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.otus.cars

/**
* Топливная система
*/
class TankSystem(private val mouthType: TankMouth) {

private val tank = Tank()

fun addFuel(liters: Int) {
when (mouthType) {
is TankMouth.PetrolMouth -> tank.addPetrolFuel(liters)
is TankMouth.LpgMouth -> tank.addLpgFuel(liters)
}
}

fun getFuelLevel(): Int {
return tank.getFuelLevel()
}

private class Tank {
var currentFuelLevel: Int = 0
fun getFuelLevel(): Int = currentFuelLevel

fun addPetrolFuel(liters: Int) {
println("Заправляемся бензином, $liters литров")
currentFuelLevel += liters
}

fun addLpgFuel(liters: Int) {
println("Заправляемся газом, $liters литров")
currentFuelLevel += liters
}
}
}


23 changes: 23 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package ru.otus.cars

import kotlin.random.Random

object Taz: Car {

/**
* Рандомный выбор горловины
*/
private fun getRandomTankMouth(): TankMouth {
return when (Random.nextInt(0, 2)) {
0 -> TankMouth.PetrolMouth()
else -> TankMouth.LpgMouth()
}
}

/**
* Горловина
*/
override val tankMouth: TankMouth
get() = getRandomTankMouth()

/**
* Номерной знак
*/
Expand Down Expand Up @@ -36,4 +55,8 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override fun addFuel(liters: Int) {
throw IllegalArgumentException("Топливная система не найдена! Взрыв!")
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ 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 = TankMouth.LpgMouth()
this.tankSystem = TankSystem(tankMouth)
}

/**
Expand All @@ -40,6 +42,15 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tankMouth: TankMouth
private set

override lateinit var tankSystem: TankSystem

override fun addFuel(liters: Int) {
tankSystem.addFuel(liters)
}

/**
* Семерка едет так
*/
Expand Down Expand Up @@ -74,5 +85,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2107.currentSpeed
}

override fun getFuelLevel(): Int {
return tankSystem.getFuelLevel()
}
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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 = TankMouth.PetrolMouth()
this.tankSystem = TankSystem(tankMouth)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -38,6 +40,15 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tankMouth: TankMouth
private set

override lateinit var tankSystem: TankSystem

override fun addFuel(liters: Int) {
tankSystem.addFuel(liters)
}

/**
* Восьмерка едет так
*/
Expand Down Expand Up @@ -78,5 +89,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2108.currentSpeed
}

override fun getFuelLevel(): Int {
return tankSystem.getFuelLevel()
}
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ abstract class VazPlatform(override val color: String) : Car {

// Абстрактное свойство двигателя
abstract val engine: VazEngine

// Абстрактное свойство топливной системы
abstract val tankSystem: TankSystem
}

// Перечисление двигателей ВАЗ
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)

println("\n===> Заправка")
addFuel(20)
}

fun driveCars() {
Expand Down Expand Up @@ -90,4 +93,20 @@ fun repairEngine(car: VazPlatform) {
is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car")
is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car")
}
}

fun addFuel(liters: Int) {

val carsList = listOf<Car>(
Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 78)),
Taz
)

carsList.forEach { car ->
val output = car.carOutput
println("${car.javaClass.simpleName} - Уровень топлива до проверки: ${output.getFuelLevel()}")
car.addFuel(liters)
println("${car.javaClass.simpleName} - Уровень топлива после проверки: ${output.getFuelLevel()}")
}
}