Skip to content

homework 5 #58

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
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
1 change: 1 addition & 0 deletions src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Car : CarInput {
* Следит за машиной
*/
val carOutput: CarOutput
val tankMouth: TankMouth

/**
* Получить оборудование
Expand Down
1 change: 1 addition & 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,5 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int
fun getFuelLevel(): Int
}
11 changes: 11 additions & 0 deletions src/main/kotlin/ru/otus/cars/DefaultTank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.otus.cars

class DefaultTank: Tank {
private var fuelLevel: Int = 0

override fun getContents(): Int = fuelLevel

override fun receiveFuel(liters: Int) {
fuelLevel += liters
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/cars/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.cars

class GasStation {
fun addFuel(car: Car, fuel: Int) {
try {
if (car.tankMouth is LpgMouth) {
(car.tankMouth as LpgMouth).fuelLpg(fuel)
} else {
(car.tankMouth as PetrolMouth).fuelPetrol(fuel)
}
} catch (e: NotImplementedError) {
println(e.message)
}
}

fun addFuel(cars: List<Car>) {
cars.forEach { println(it.toString()) }
cars.forEach { this.addFuel(it, 100) }
cars.forEach { println(it.toString()) }
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars

class LpgMouth(override val tank: Tank): TankMouth() {
fun fuelLpg(litres: Int) {
tank.receiveFuel(litres)
}

override fun open() {
TODO("Not yet implemented")
}

override fun close() {
TODO("Not yet implemented")
}
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars

class PetrolMouth(override val tank: Tank): TankMouth() {
fun fuelPetrol(litres: Int) {
tank.receiveFuel(litres)
}

override fun open() {
TODO("Not yet implemented")
}

override fun close() {
TODO("Not yet implemented")
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.otus.cars

interface Tank {
fun getContents(): Int
fun receiveFuel(liters: Int)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.otus.cars

abstract class TankMouth {
abstract val tank: Tank

abstract fun open()
abstract fun close()
}
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 @@ -18,6 +18,9 @@ object Taz: Car {
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")

override val tankMouth: TankMouth
get() = throw NotImplementedError("Взрыв")

/**
* Получить оборудование
*/
Expand Down
13 changes: 8 additions & 5 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.random.Random
/**
* Семёрочка
*/
class Vaz2107 private constructor(color: String) : VazPlatform(color) {
class Vaz2107 private constructor(color: String, override val tankMouth: TankMouth) : VazPlatform(color) {
/**
* Сам-себе-сборщик ВАЗ 2107.
*/
Expand All @@ -17,7 +17,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
}
}

override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый", PetrolMouth(DefaultTank())).apply {
this.engine = getRandomEngine()
this.plates = plates
}
Expand Down Expand Up @@ -49,8 +49,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
}

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

/**
/**
* Доступно сборщику
* @see [build]
*/
Expand All @@ -59,7 +58,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, fuelLevel=${carOutput.getFuelLevel()})"
}

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

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
11 changes: 7 additions & 4 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.random.Random
/**
* Восьмерка
*/
class Vaz2108 private constructor(color: String) : VazPlatform(color) {
class Vaz2108 private constructor(color: String, override val tankMouth: TankMouth) : VazPlatform(color) {
/**
* Сам-себе-сборщик ВАЗ 2108.
*/
Expand All @@ -18,7 +18,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
}
}

override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный").apply {
override fun build(plates: Car.Plates): Vaz2108 = Vaz2108("Красный", LpgMouth(DefaultTank())).apply {
this.engine = getRandomEngine()
this.plates = plates
}
Expand Down Expand Up @@ -63,20 +63,23 @@ 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, fuelLevel=${carOutput.getFuelLevel()})"
}

/**
* Делегируем приборы внутреннему классу
*/
override val carOutput: CarOutput = VazOutput()

/**
* Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108!
*/
inner class VazOutput : CarOutput {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
13 changes: 13 additions & 0 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.otus.cars

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

fun fuelCars() {
val cars = listOf(
Togliatti.buildCar(Vaz2107, Car.Plates("123", 11)),
Togliatti.buildCar(Vaz2108, Car.Plates("123", 22)),
Taz
)

val tankStation = GasStation()
tankStation.addFuel(cars)
}