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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,3 @@
- статические внутренние класс и объекты (например, для бачка внутри машины)
- объекты-компаньоны (для сборщика бака и горловины)
- Sealed-интерфейсы или классы (горловина бачка)

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

val tank: Tank
val tankMouth: TankMouth
fun fuelLevel(): Int = tank.fuelLevel
}
10 changes: 10 additions & 0 deletions src/main/kotlin/ru/otus/cars/CarFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ object Togliatti : CarFactory {
private fun buildVaz2107(plates: Car.Plates): Car {
println("Запил ${Vaz2107.MODEL} в Тольятти...")
val vaz = Vaz2107.build(plates)

val tank = TankImpl(capacity = 50)
val mouth = GasMouth
vaz.installTank(tank, mouth)

println("Проверяем тачку...")
Vaz2107.test(vaz)
vaz.drdrdrdrdr()
Expand All @@ -26,6 +31,11 @@ object Togliatti : CarFactory {
private fun buildVaz2108(plates: Car.Plates): Car {
println("Запил ${Vaz2108.MODEL} в Тольятти...")
val vaz = Vaz2108.build(plates)

val tank = TankImpl(capacity = 55)
val mouth = PetrolMouth
vaz.installTank(tank, mouth)

println("Сход-развал...")
Vaz2108.alignWheels(vaz)
vaz.zhzhzhzh()
Expand Down
1 change: 1 addition & 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,5 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int
fun fuelLevel(): Int
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/cars/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.cars

import java.lang.Exception

class GasStation {
fun refuel(car: Car, amount: Int) {
println("Заправляем машину ${car.color} на $amount литров...")
try {
car.tankMouth.refuel(car.tank, amount)
} catch (e: Exception) {
println("Бак у ${car.color} взорвался! Вызывайте пожарных! ${e.message}")
}

}

fun refuelCars(cars: List<Car>, amount: Int) {
cars.forEach { refuel(it, amount) }
}
}


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

interface Tank {
val capacity: Int
val fuelLevel: Int
fun isLow(): Boolean
fun addFuel(amount: Int)

}

class TankImpl(override val capacity: Int) : Tank {
override var fuelLevel: Int = 0
private set

override fun isLow(): Boolean = fuelLevel < capacity * 0.1

override fun addFuel(amount: Int) {
fuelLevel = (fuelLevel + amount).coerceAtMost(capacity)
}
}

class ExplosiveTank(override val capacity: Int) : Tank {
override var fuelLevel: Int = 0
private set

override fun isLow(): Boolean = false

override fun addFuel(amount: Int) {
throw IllegalStateException("Ба-бах! Бак взорвался>")
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ru.otus.cars

sealed interface TankMouth {
fun refuel(tank: Tank, amount: Int)
}

object GasMouth : TankMouth {
override fun refuel(tank: Tank, amount: Int) {
if (tank is ExplosiveTank) {
throw IllegalStateException("Ба-бах! Бак взорвался>")
}
println("Заливаем газ")
tank.addFuel(amount)
}
}

object PetrolMouth : TankMouth {
override fun refuel(tank: Tank, amount: Int) {
if (tank is ExplosiveTank) {
throw IllegalStateException("Ба-бах! Бак взорвался>")
}
println("Заливаем бензин")
tank.addFuel(amount)
}
}
14 changes: 12 additions & 2 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.otus.cars

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

override val tank: Tank = ExplosiveTank(40)
override var tankMouth: TankMouth = PetrolMouth

override fun fuelLevel(): Int {
return tank.fuelLevel
}

override fun toString(): String {
return "Taz(color=$color, fuel=${fuelLevel()})"
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2107.currentSpeed
}

override fun fuelLevel(): Int {
return tank.fuelLevel
}
}

override lateinit var tank: Tank
override lateinit var tankMouth: TankMouth

fun installTank(tank: Tank, tankMouth: TankMouth) {
this.tank = tank
this.tankMouth = tankMouth
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return this@Vaz2108.currentSpeed
}

override fun fuelLevel(): Int {
return tank.fuelLevel
}
}

override lateinit var tank: Tank
override lateinit var tankMouth: TankMouth

fun installTank(tank: Tank, tankMouth: TankMouth) {
this.tank = tank
this.tankMouth = tankMouth
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ 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 = "Кузов, колеса, движок"
Expand Down
18 changes: 14 additions & 4 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===> refuel cars...")
refuelCars()
}

fun driveCars() {
Expand Down Expand Up @@ -54,8 +56,7 @@ fun garageMake() {

fun getEquipment() {
val cars = listOf(
Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 78))
Vaz2107.build(Car.Plates("123", 77)), Vaz2108.build(Car.Plates("321", 78))
)

cars.forEach { car ->
Expand All @@ -65,8 +66,7 @@ fun getEquipment() {

fun getColor() {
val cars = listOf(
Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 78))
Vaz2107.build(Car.Plates("123", 77)), Vaz2108.build(Car.Plates("321", 78))
)

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

fun refuelCars() {
val cars = listOf(
Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)),
Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)),
Taz
)
val station = GasStation()
station.refuelCars(cars, amount = 30)
}