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
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
}
14 changes: 14 additions & 0 deletions src/main/kotlin/ru/otus/cars/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.otus.cars


class LpgMouth(tank: Tank) : TankMouth(tank){
fun fuelLpg(liters : Int) {
if (MouthState == false) {
println("Закрыта горловина бака")
open()
}
println("Собираемся заправить $liters литров газа")
tank.receiveFuel(liters)
close()
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/ru/otus/cars/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.otus.cars

class PetrolMouth(tank: Tank) : TankMouth(tank){
fun fuelPetrol(liters : Int){
if (MouthState==false) {
println("Закрыта горловина бака")
open()
}
println("Собираемся заправить $liters литров бензина")
tank.receiveFuel(liters)
close()
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/Refill.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars

class Refill {
fun refuel(car: Car, count: Int) {
try {
when (car.tankMouth) {
is LpgMouth -> (car.tankMouth as LpgMouth).fuelLpg(count)
is PetrolMouth -> (car.tankMouth as PetrolMouth).fuelPetrol(count)
}
}
catch (exception: NotImplementedError) {
println("Взрыв бака")
}
}
}
29 changes: 29 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.otus.cars

import kotlin.random.Random

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


open class CarTank (val capacity: Int): Tank {
private var content = Random.nextInt(0, capacity/2)

override fun getContents(): Int = content

override fun receiveFuel(liters: Int) {
if (content+liters == capacity) {
content += liters
println("Заправили до полного бака")
} else if (content+liters > capacity) {
content = capacity
println("Полный бак. Не влезло ${content+liters-capacity} литров")
}
else {
content += liters
println("Залили $liters литров")
}
}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.otus.cars

abstract class TankMouth(protected val tank: Tank) {

var MouthState: Boolean = false // Доступность горловины бензобака

fun open() {
println("Горловина открыта")
MouthState = true;
}

fun close() {
println("Горловина закрыта")
MouthState = false;
}
}
3 changes: 3 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,7 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override val tankMouth: TankMouth
get() = throw NotImplementedError("Бензобака нет")
}
11 changes: 9 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,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 Down Expand Up @@ -59,7 +60,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, fuelContents=${tank.getContents()})"
}

/**
Expand All @@ -74,5 +75,11 @@ 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()
}
}
}

private val tank = CarTank(39)
override lateinit var tankMouth: TankMouth
}
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 Down Expand Up @@ -63,7 +64,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, fuelContents=${tank.getContents()})"
}

/**
Expand All @@ -78,5 +79,11 @@ 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()
}
}

private val tank = CarTank(43)
override lateinit var tankMouth: TankMouth
}
27 changes: 27 additions & 0 deletions 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===> refill...")
refill()
}

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

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

cars.forEach { car ->
println("На заправке $car")
println("Сейчас в баке ${car.carOutput.getFuelContents()} литров")
val count = Random.nextInt(1, 30)
fueldStation.refuel(car,count)
println("Заправили $count литров")
try {
println("Итого в баке ${car.carOutput.getFuelContents()} литров")
}
catch (e: NotImplementedError)
{
println("Взрыв машины")
}
}
}