Skip to content

Kotlin-5 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 @@ -14,6 +14,11 @@ interface Car : CarInput {
*/
val color: String

/**
* Горловина бака
*/
val mouth: TankMouth

/**
* Следит за машиной
*/
Expand Down
2 changes: 2 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,6 @@ interface CarInput {
* Руль влево на [degrees] градусов
*/
fun wheelToLeft(degrees: Int)

fun receiveFuel(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 getFuelContents(): Int
}
20 changes: 20 additions & 0 deletions src/main/kotlin/ru/otus/cars/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.otus.cars

import ru.otus.cars.Vaz2107.LpgMouth
import ru.otus.cars.Vaz2108.PetrolMouth

class GasStation {
fun fuelCar (car: Car, liters: Int) {
when (car.mouth) {
is LpgMouth -> {
car.receiveFuel(liters)
println("Залито $liters литров(а) газа.")
}
is PetrolMouth -> {
car.receiveFuel(liters)
println("Залито $liters литров(а) бензина.")
}
}
println("Уровень топлива: ${car.carOutput.getFuelContents()} литров(а).")
}
}
23 changes: 23 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.otus.cars

/**
* Топливный бак
*/
interface Tank {
/**
* Горловина бака
*/
val mouth: TankMouth

val fuelLevel: Int

/**
* Следит за уровнем топлива
*/
fun getContents(): Int

/**
* Заправить бензин
*/
fun receiveFuel(liters: Int)
}
20 changes: 20 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.otus.cars

abstract class TankMouth {

private var isopen : Boolean = true

/**
* Открывает лючок бензобака
*/
fun open() {
isopen = true
}

/**
* Закрывает лючок бензобака
*/
fun close() {
isopen = false
}
}
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 @@ -12,6 +12,9 @@ object Taz: Car {
*/
override val color: String = "Ржавый"

override val mouth: TankMouth
get() = throw NotImplementedError("Сюда соляру надо")

/**
* Следит за машиной
*/
Expand All @@ -36,4 +39,8 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override fun receiveFuel(liters: Int) {
throw NotImplementedError("Саня, бачок потик!!!")
}
}
21 changes: 21 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,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.mouth = LpgMouth()
}

/**
Expand Down Expand Up @@ -50,13 +51,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {

private var currentSpeed: Int = 0 // Скока жмёт

private var currentFuelLevel = 0

override fun receiveFuel(liters: Int) {
this.LpgMouth().fuelLpg(liters)
}

/**
* Доступно сборщику
* @see [build]
*/
override lateinit var plates: Car.Plates
private set

override lateinit var mouth: TankMouth

// Выводим состояние машины
override fun toString(): String {
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
Expand All @@ -74,5 +83,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelContents(): Int {
return [email protected]
}
}

inner class LpgMouth : TankMouth() {
fun fuelLpg(liters: Int) {
open()
[email protected] += liters
close()
}
}
}
22 changes: 22 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.mouth = PetrolMouth()

}

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -54,13 +56,21 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {

private var currentSpeed: Int = 0 // Скока жмёт

private var currentFuelLevel = 0

override fun receiveFuel(liters: Int) {
this.PetrolMouth().fuelPetrol(liters)
}

/**
* Доступно сборщику
* @see [build]
*/
override lateinit var plates: Car.Plates
private set

override lateinit var mouth: TankMouth

// Выводим состояние машины
override fun toString(): String {
return "Vaz2108(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
Expand All @@ -78,5 +88,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelContents(): Int {
return [email protected]
}
}

inner class PetrolMouth : TankMouth() {
fun fuelPetrol(liters: Int) {
open()
[email protected] += liters
close()
}
}
}
2 changes: 2 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,8 @@ abstract class VazPlatform(override val color: String) : Car {

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

abstract override val mouth: TankMouth
}

// Перечисление двигателей ВАЗ
Expand Down