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
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.TankMouth

/**
* Машина целиком
*/
Expand All @@ -19,6 +21,8 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

val tankMouth: TankMouth

/**
* Получить оборудование
*/
Expand Down
2 changes: 2 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,6 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

fun getFuelContents(): Int
}
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.TankMouth

object Taz: Car {
/**
* Номерной знак
Expand All @@ -17,6 +19,8 @@ object Taz: Car {
*/
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")
override val tankMouth: TankMouth
get() = throw NotImplementedError("Бабах!")

/**
* Получить оборудование
Expand Down
32 changes: 28 additions & 4 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.LpgMouth
import ru.otus.cars.fuelSystem.Tank
import ru.otus.cars.fuelSystem.TankMouth
import ru.otus.cars.fuelSystem.VazTank
import kotlin.random.Random

/**
* Семёрочка
*/
class Vaz2107 private constructor(color: String) : VazPlatform(color) {
class Vaz2107 private constructor(
color: String,
) : VazPlatform(color) {

/**
* Сам-себе-сборщик ВАЗ 2107.
*/
Expand All @@ -17,9 +24,16 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
}
}

override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates
override fun build(plates: Car.Plates): Vaz2107 {
val newTank = VazTank()
val newMouth = LpgMouth(newTank)

return Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates
this.tank = newTank
this.tankMouth = newMouth
}
}

/**
Expand All @@ -36,6 +50,12 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
const val MODEL = "2107"
}

override lateinit var tankMouth: TankMouth
private set

override lateinit var tank: Tank
private set

// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
Expand Down Expand Up @@ -74,5 +94,9 @@ 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()
}
}
}
27 changes: 24 additions & 3 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.PetrolMouth
import ru.otus.cars.fuelSystem.Tank
import ru.otus.cars.fuelSystem.TankMouth
import ru.otus.cars.fuelSystem.VazTank
import kotlin.random.Random

/**
Expand All @@ -18,9 +22,16 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
}
}

override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply {
this.engine = getRandomEngine()
this.plates = plates
override fun build(plates: Car.Plates): Vaz2108 {
val tank = VazTank()
val mouth = PetrolMouth(tank)
return Vaz2108("Красный").apply{
this.engine = getRandomEngine()
this.plates = plates
this.tank = tank
this.tankMouth = mouth
}

}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -34,6 +45,12 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
const val MODEL = "2108"
}

override lateinit var tankMouth: TankMouth
private set

override lateinit var tank: Tank
private set

// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
Expand Down Expand Up @@ -78,5 +95,9 @@ 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()
}
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.Tank

abstract class VazPlatform(override val color: String) : Car {
// Положение руля. Доступно только внутри класса и наследникам
protected var wheelAngle: Int = 0 // Положение руля
Expand All @@ -14,6 +16,8 @@ abstract class VazPlatform(override val color: String) : Car {

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

protected abstract val tank: Tank
}

// Перечисление двигателей ВАЗ
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/FillingStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.otus.cars.fuelSystem

import ru.otus.cars.Car

class FillingStation {

fun refuel(cars: List<Car>, fuelValue: Int) {
cars.forEach {
val mouth = it.tankMouth
mouth.open()
try {
when (mouth) {
is LpgMouth -> mouth.fuelLpg(fuelValue)
is PetrolMouth -> mouth.fuelPetrol(fuelValue)
}

} catch (ex: NotImplementedError) {
println(ex)
} finally {
mouth.close()
println("В баке - ${it.carOutput.getFuelContents()} л.")
}

}
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.otus.cars.fuelSystem

class LpgMouth(
tank: Tank
): TankMouth(tank) {

fun fuelLpg(liters: Int) {
tank.receiveFuel(liters)
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.otus.cars.fuelSystem

class PetrolMouth(
tank: Tank
): TankMouth(tank) {

fun fuelPetrol(liters: Int) {
tank.receiveFuel(liters)
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.otus.cars.fuelSystem

interface Tank {

fun getContents(): Int

fun receiveFuel(liters: Int)
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars.fuelSystem

abstract class TankMouth(
protected val tank: Tank
) {

fun open() {
println("Открыли бак")
}

fun close() {
println("Закрыли бак")
}

}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/fuelSystem/VazTank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars.fuelSystem

class VazTank: Tank {

private var fuelLevel: Int = 0

override fun getContents(): Int {
return fuelLevel
}

override fun receiveFuel(liters: Int) {
fuelLevel += liters
println("Заправили")
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.cars.fuelSystem.FillingStation

fun main() {
println("\n===> drive cars...")
driveCars()
Expand All @@ -16,6 +18,21 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)

refuel()

}

fun refuel() {
val fillingStation = FillingStation()
fillingStation.refuel(
listOf(
Vaz2107.build(Car.Plates("1234", 123)),
Vaz2108.build(Car.Plates("567", 456)),
Taz
),
36
)
}

fun driveCars() {
Expand Down