Skip to content
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

Homework Kotlin 5 #52

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
6 changes: 5 additions & 1 deletion src/main/kotlin/ru/otus/cars/Car.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

val tankMouth: TankMouth

val tank: Tank

/**
* Получить оборудование
*/
Expand All @@ -28,4 +32,4 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ru/otus/cars/CarFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ object Togliatti : CarFactory {
is Vaz2108.Companion -> return buildVaz2108(plates)
}
}
}
}
2 changes: 2 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,6 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

fun getFuelContents(): Int
}
33 changes: 33 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.otus.cars

interface Tank {

var tankMouth: TankMouth

fun getContents(): Int

fun receiveFuel(liters: Int){
}
}

abstract class TankMouth {
abstract fun open()

abstract fun close()
}

class PetrolMouth(var tank: Tank): TankMouth() {
fun fuelPetrol(liters: Int) {
this.tank.receiveFuel(liters)
}
override fun open() {}
override fun close() {}
}

class LpgMouth(var tank: Tank): TankMouth() {
fun fuelLpg(liters: Int) {
this.tank.receiveFuel(liters)
}
override fun open() {}
override fun close() {}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.cars

class TankStation {
fun fillUpCar(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) {
//noop
}
}

fun fillUpCars(cars: List<Car>) {
cars.forEach({ println(it) })
cars.forEach({ this.fillUpCar(it, 100) })
cars.forEach({ println(it) })
}
}
14 changes: 12 additions & 2 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.otus.cars

object Taz: Car {
object Taz : Car {
/**
* Номерной знак
*/
Expand All @@ -18,6 +18,16 @@ object Taz: Car {
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")

override val tankMouth: TankMouth
get() = throw NotImplementedError("Взорвался")
override val tank: Tank
get() = throw NotImplementedError("Какая то ошибка")

override fun toString(): String {
return "Taz"
}


/**
* Получить оборудование
*/
Expand All @@ -36,4 +46,4 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}
}
}
26 changes: 24 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,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates
this.tank = Vaz2107Tank()
this.tankMouth = LpgMouth(this.tank)

}

/**
Expand Down Expand Up @@ -49,6 +52,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
}

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

/**
* Доступно сборщику
Expand All @@ -59,13 +63,15 @@ 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=$currentFuelLevel)"
}

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

/**
* Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107!
Expand All @@ -74,5 +80,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelContents(): Int {
return currentFuelLevel
}
}

inner class Vaz2107Tank : Tank {
override lateinit var tankMouth: TankMouth

override fun getContents(): Int {
return currentFuelLevel
}

override fun receiveFuel(liters: Int) {
currentFuelLevel += liters
}
}
}
}
25 changes: 23 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,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.tank = Vaz2108Tank()
this.tankMouth = PetrolMouth(this.tank)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -53,6 +55,7 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
}

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

/**
* Доступно сборщику
Expand All @@ -63,13 +66,15 @@ 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=$currentFuelLevel)"
}

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

/**
* Имеет доступ к внутренним данным ЭТОГО ВАЗ-2108!
Expand All @@ -78,5 +83,21 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

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

inner class Vaz2108Tank : Tank {
override lateinit var tankMouth: TankMouth

override fun getContents(): Int {
return currentFuelLevel
}

override fun receiveFuel(liters: Int) {
currentFuelLevel += liters
}
}
}
}
15 changes: 14 additions & 1 deletion src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> tank cars")
tankCars()

}

fun driveCars() {
Expand All @@ -29,6 +32,16 @@ fun driveCars() {
println(vaz2.toString()) // Выводит -20 и случайную скорость
}

fun tankCars() {
val cars = listOf(
Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)),
Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)),
Taz
)
val tankStation = TankStation()
tankStation.fillUpCars(cars)
}

fun innerNestedCheck() {
val vaz = Vaz2107.build(Car.Plates("123", 77))
val output = vaz.VazOutput() // Создаем новый объект ИЗ ЭКЗЕМПЛЯРА МАШИНЫ
Expand Down Expand Up @@ -90,4 +103,4 @@ fun repairEngine(car: VazPlatform) {
is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car")
is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car")
}
}
}