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 @@ -28,4 +28,9 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)

/**
* Горловина топливного бака
*/
val tankMouth: TankMouth
}
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 refuel(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
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ object Taz: Car {
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")


/**
* Бензобак
*/
override val tankMouth: TankMouth
get() = throw NotImplementedError("Бензобака нет")

/**
* Получить оборудование
*/
Expand All @@ -36,4 +43,12 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override fun refuel(liters: Int) {
throw NotImplementedError("БА-БАХ!!!")
}

override fun toString(): String {
return "TAZ 666"
}
}
20 changes: 19 additions & 1 deletion 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.GasMouth()
this.fuelSystem = VazFuelSystem(tankMouth)
}

/**
Expand Down Expand Up @@ -57,9 +59,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override lateinit var plates: Car.Plates
private set

// Переопределяем свойство родителя
override lateinit var tankMouth: TankMouth
private set

override lateinit var fuelSystem: VazFuelSystem
private set

override fun refuel(liters: Int) {
fuelSystem.refuel(liters)
}

// Выводим состояние машины
override fun toString(): String {
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
val fuelLevel = carOutput.getFuelLevel()
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuel=$fuelLevel liters)"
}

/**
Expand All @@ -74,5 +88,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2107.currentSpeed
}

override fun getFuelLevel(): Int {
return fuelSystem.getFuelLevel()
}
}
}
20 changes: 19 additions & 1 deletion 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.fuelSystem = VazFuelSystem(tankMouth)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -61,9 +63,21 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override lateinit var plates: Car.Plates
private set

// Переопределяем свойство родителя
override lateinit var tankMouth: TankMouth
private set

override lateinit var fuelSystem: VazFuelSystem
private set

override fun refuel(liters: Int) {
fuelSystem.refuel(liters)
}

// Выводим состояние машины
override fun toString(): String {
return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
val fuelLevel = carOutput.getFuelLevel()
return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuel=$fuelLevel liters)"
}

/**
Expand All @@ -78,5 +92,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2108.currentSpeed
}

override fun getFuelLevel(): Int {
return fuelSystem.getFuelLevel()
}
}
}
42 changes: 42 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 fuelSystem : VazFuelSystem
}

// Перечисление двигателей ВАЗ
Expand All @@ -23,4 +26,43 @@ sealed class VazEngine {

data class LADA_2107(override val volume: Int) : VazEngine()
data class SAMARA_2108(override val volume: Int) : VazEngine()
}

// Перечисление горловин бака
sealed class TankMouth {
class PetrolMouth: TankMouth()
class GasMouth: TankMouth()
}


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

private val tank = Tank()

fun refuel(liters: Int) {
when (mouthType) {
is TankMouth.PetrolMouth -> {
println("Залили $liters литров бензина")
tank.addFuel(liters)
}
is TankMouth.GasMouth -> {
println("Залили $liters литров газа")
tank.addFuel(liters)
}
}
}

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

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

fun addFuel(liters: Int) {
currentFuelLevel += liters
}
}
}
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,8 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> Заправка")
AZS(20)
}

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

fun AZS(liters: Int) {

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

carsList.forEach { car ->
println("Заправляем машину ${car.javaClass.simpleName}")
println(car.toString())
car.refuel(liters)
println(car.toString())
println("\n")
}
}