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
11 changes: 9 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.2.10'
id 'org.jetbrains.kotlin.jvm' version '1.9.23'
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlin {
jvmToolchain(17)
jvmToolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

test {
Expand Down
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/CarOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

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

class FuelStation {
fun FillUpTheCar(car: Car, value: Int)
{
try {
when (car.tankMouth) {
is PetrolMouth -> (car.tankMouth as PetrolMouth).fuelPetrol(value)
is LpgMouth -> (car.tankMouth as LpgMouth).fuelLpg(value)
}
}
catch (e: NotImplementedError) {
println("Бак взорвался")
}
}
}
22 changes: 22 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.otus.cars

interface Tank {
fun getContents(): Int
fun receiveFuel(liters: Int)
}

class MyTank (val capacity: Int): Tank {
private var contents = 0

override fun getContents(): Int = contents

override fun receiveFuel(liters: Int) {
contents += liters

if (contents >= capacity) {
contents = capacity
println("Полный бак")
}
}
}

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

open class TankMouth(protected val tank: Tank) {
private var isOpen = false

fun open() {
when (isOpen) {
true -> println("Горловина уже открыта")
else -> {
isOpen = true
println("Горловина открыта")
}
}
}

fun close(){
when(isOpen){
false -> print("Горловина уже закрыта")
else -> {
isOpen = false
println("Горловина закрыта")
}
}
}
}

class PetrolMouth(tank: Tank) : TankMouth(tank){
fun fuelPetrol(liters: Int){
open()
println("Старт заправки")
tank.receiveFuel(liters)
println("Заправка выполнена")
close()
}
}

class LpgMouth(tank: Tank): TankMouth(tank) {
fun fuelLpg(liters: Int){
open()
println("Старт заправки")
tank.receiveFuel(liters)
println("Заправка выполнена")
close()
}
}
7 changes: 7 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,9 @@ object Taz: Car {
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")

override val tankMouth: TankMouth
get() = throw NotImplementedError("Ба-бах!!!")

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

override fun toString(): String {
return "Taz"
}
}
10 changes: 9 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,7 @@ 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 = LpgMouth(tank)
}

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

private val tank = MyTank(50)
override lateinit var tankMouth: TankMouth
/**
* Семерка едет так
*/
Expand All @@ -59,7 +62,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {

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

/**
Expand All @@ -74,5 +78,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()
}
}
}
9 changes: 8 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,7 @@ 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 = PetrolMouth(tank)
}

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

private val tank = MyTank(50)
override lateinit var tankMouth: TankMouth
/**
* Восьмерка едет так
*/
Expand All @@ -63,7 +66,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {

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

/**
Expand All @@ -78,5 +82,8 @@ 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()
}
}
}
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,25 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)

val fuelStation = FuelStation()
val cars = listOf( Vaz2107.build(Car.Plates("777", 77)),
Vaz2108.build(Car.Plates("284", 61)),
Taz)

cars.forEach { nextcar ->
println("Заправляем машину - $nextcar")
fuelStation.FillUpTheCar(nextcar,20)

try {
val liters = nextcar.carOutput.getFuelContents()
println("Теперь в баке $liters литров")
}
catch (e: NotImplementedError)
{
println("Нет больше машины!")
}
}
}

fun driveCars() {
Expand Down