Skip to content

Поработал немного над топливным баком #35

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 @@ -19,6 +19,11 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

/**
* Используется для заправки машины
*/
val tankMouth: TankMouth

/**
* Получить оборудование
*/
Expand Down
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/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars


fun fillCar(car: Car, liters: Int) {
try {
car.tankMouth.open()
when (val mouth = car.tankMouth) {
is TankMouth.PETROL -> mouth.fuelPetrol(liters)
is TankMouth.LPG -> mouth.fuelLPG(liters)
}
car.tankMouth.close()
} catch (e: Exception) {
println("При попытке заправиться возникли проблемы: ${e.message}")
}
}
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 FuelSource {
/**
* Получение уровня топлива в % от полного бака
*/
fun getLevel(): Int
}

interface FuelReceiver {
/**
* Заливка топлива
*/
fun receiveFuel(liters: Int)
}

class Tank(private val volume: Int): FuelSource, FuelReceiver {
private var currentFuel: Int = 0

override fun getLevel(): Int = (currentFuel * 100)/volume

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

if (currentFuel > volume) {
currentFuel = volume

throw IllegalArgumentException("Бак переполнился")
}
}
}
39 changes: 39 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.otus.cars

sealed class TankMouth {
abstract val fuelReceiver: FuelReceiver
private var opened: Boolean = false
abstract val tankType: String

fun open() {
opened = true
}

fun close() {
opened = false
}

protected fun fillReceiver(liters: Int) {
if (!opened) {
throw IllegalStateException("При попытке залить топливо в закрытый $tankType топливо разлилось на землю")
}

fuelReceiver.receiveFuel(liters)
}

data class PETROL(override val fuelReceiver: FuelReceiver): TankMouth() {
override val tankType: String = "бак"

fun fuelPetrol(liters: Int) {
fillReceiver(liters)
}
}

data class LPG(override val fuelReceiver: FuelReceiver): TankMouth() {
override val tankType: String = "баллон"

fun fuelLPG(liters: Int) {
fillReceiver(liters)
}
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

/**
* Используется для заправки машины
*/
override val tankMouth: TankMouth
get() = throw IllegalAccessException("При попытке заправиться проскочила искра от рваной проводки и случилось нехорошее")

// Теперь мы узнаем что это за машина
override fun toString(): String {
return "Старый ржавый Таз"
}
}
13 changes: 12 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,10 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates

val gasTank = Tank(70)
this.gasTank = gasTank
this.tankMouth = TankMouth.LPG(gasTank)
}

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

override lateinit var gasTank: FuelSource
override lateinit var tankMouth: TankMouth

// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
Expand All @@ -59,7 +66,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {

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

/**
Expand All @@ -74,5 +81,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
13 changes: 12 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,10 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply {
this.engine = getRandomEngine()
this.plates = plates

val gasTank = Tank(50)
this.gasTank = gasTank
this.tankMouth = TankMouth.PETROL(gasTank)
}

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

override lateinit var gasTank: FuelSource
override lateinit var tankMouth: TankMouth

// Переопределяем свойство родителя
override lateinit var engine: VazEngine
private set
Expand Down Expand Up @@ -63,7 +70,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {

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

/**
Expand All @@ -78,5 +85,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
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 val gasTank: FuelSource
}

// Перечисление двигателей ВАЗ
Expand Down
26 changes: 26 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===> go to the gas station...")
refillTank()
}

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


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

fillManyCars(cars, 60)
}

fun fillManyCars(cars: Iterable<Car>, liters: Int) {
cars.forEach { car ->
println("Заправляем $car")
fillCar(car, liters)

try {
println("Теперь в машине ${car.carOutput.getFuelLevel()}% топлива")
} catch (e: Error) {
println("Для $car даже вывести ничего не получается")
}
}
}