Skip to content

Done #56

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 1 commit into
base: master
Choose a base branch
from
Open

Done #56

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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.9.23'
id 'org.jetbrains.kotlin.jvm' version '2.0.21'
}

test {
Expand Down
7 changes: 6 additions & 1 deletion 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
}
84 changes: 84 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ru.otus.cars


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

abstract class TankMouth {
private var opened = false
fun open() {
if (opened) {
println("Крышка бака уже открыта")
}
else {
println("Открыли крышку бака")
opened = true
}
}

fun close() {
if (!opened) {
println("Крышка бака уже закрыта")
}
else {
println("Закрыли крышку бака")
opened = false
}
}

protected fun opened(): Boolean = opened
}


class FuelTank(override val mouth: TankMouth, private val capacity: Int) : Tank {
private var contents: Int = 0
override fun getContents() = contents
override fun receiveFuel(liters: Int) {
val remaining = capacity-contents

if (remaining > 0) {
if (liters >= remaining) {
contents += remaining
println("Заправили $remaining л до полного бака")
} else {
contents += liters
println("Заправили $liters л")
}
}
else {
println("Бак полон")
}
}
}


class PetrolMouth : TankMouth() {
private lateinit var tank: FuelTank
fun attachTo(tank: FuelTank) {
this.tank = tank
}
fun fuelPetrol(liters: Int) {
if (!opened())
throw Error("Крышка бака не открыта")

println("Закачиваем $liters л бензина")
tank.receiveFuel(liters)
}
}

class LpgMouth : TankMouth() {
private lateinit var tank: FuelTank
fun attachTo(tank: FuelTank) {
this.tank = tank
}
fun fuelLpg(liters: Int) {
if (!opened())
throw Error("Крышка бака не открыта")

println("Закачиваем $liters л газа")
tank.receiveFuel(liters)
}
}
13 changes: 12 additions & 1 deletion src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ object Taz: Car {
*/
override val color: String = "Ржавый"

/**
* Доступ к топливному баку
*/
override val tankMouth = object: TankMouth() {
/*fun fuel(liters: Int) {
throw NotImplementedError("Взрыв")
}*/
}

override fun toString(): String = "Ведро с гвоздями"

/**
* Следит за машиной
*/
Expand All @@ -36,4 +47,4 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}
}
}
18 changes: 16 additions & 2 deletions 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
this.tankMouth = LpgMouth()
this.tank = FuelTank(tankMouth, 30)
(this.tankMouth as LpgMouth).attachTo(tank)

}

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

// Переопределяем свойство родителя
// Переопределяем свойства родителя
override lateinit var engine: VazEngine
private set

override lateinit var tank: FuelTank
private set

override lateinit var tankMouth: TankMouth
private set

/**
* Семерка едет так
*/
Expand All @@ -59,7 +69,8 @@ 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" +
" fuel=${carOutput.getFuelContents()})"
}

/**
Expand All @@ -74,5 +85,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}
override fun getFuelContents(): Int {
return [email protected]()
}
}
}
17 changes: 15 additions & 2 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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()
this.tank = FuelTank(tankMouth, 40)
(this.tankMouth as PetrolMouth).attachTo(tank)
}

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

// Переопределяем свойство родителя
// Переопределяем свойства родителя
override lateinit var engine: VazEngine
private set

override lateinit var tank: FuelTank
private set

override lateinit var tankMouth: TankMouth
private set

/**
* Восьмерка едет так
*/
Expand All @@ -63,7 +72,8 @@ 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" +
" fuel=${carOutput.getFuelContents()})"
}

/**
Expand All @@ -78,5 +88,8 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}
override fun getFuelContents(): Int {
return [email protected]()
}
}
}
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 @@ -10,10 +10,13 @@ abstract class VazPlatform(override val color: String) : Car {
override fun wheelToLeft(degrees: Int) { wheelAngle -= degrees }

// Получить оборудование
override fun getEquipment(): String = "Кузов, колеса, движок"
override fun getEquipment(): String = "Кузов, колеса, движок, топливный бак"

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

// Абстрактное свойство топливного бака
abstract val tank: FuelTank
}

// Перечисление двигателей ВАЗ
Expand All @@ -23,4 +26,6 @@ sealed class VazEngine {

data class LADA_2107(override val volume: Int) : VazEngine()
data class SAMARA_2108(override val volume: Int) : VazEngine()
}
}


36 changes: 35 additions & 1 deletion 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 kotlin.random.Random

fun main() {
println("\n===> drive cars...")
driveCars()
Expand All @@ -16,6 +18,8 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> Приехали на заправку...")
fillupCars()
}

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

object FuelStation {
fun fillUpCar(car: Car, liters: Int) {
car.tankMouth.open()
when(car.tankMouth) {
is PetrolMouth ->(car.tankMouth as PetrolMouth).fuelPetrol(liters)
is LpgMouth ->(car.tankMouth as LpgMouth).fuelLpg(liters)
else ->{
println("Закачиваем $liters л топлива")
throw NotImplementedError("Взрыв")
}
}
car.tankMouth.close()
}
}

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

cars.forEach {
println("Заправляем: $it")
val liters = Random.nextInt(1, 60)
FuelStation.fillUpCar(it, liters)
println("После заправки: $it")
}
}