Skip to content

homework 04 #42

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
4 changes: 4 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,8 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)
/**
* Горловина и бак
*/
var mouth: TankMouth
}
6 changes: 6 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,10 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

/**
* Покажи уровень топлива
*/

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

import kotlin.random.Random

object GasStation {
fun fuelCar(car: Car) {
try {
println(car.carOutput.getFuelContents())
when (car.mouth.type) {
FuelType.LPG -> fuelWithLpg(car.mouth as LpgMouth)
FuelType.PETROL -> fuelWithPetrol(car.mouth as PetrolMouth)
}
println(car.carOutput.getFuelContents())

} catch (e :NotImplementedError) {
println("Опять этот таз приехал")
}
}


private fun fuelWithLpg(tankMouth: LpgMouth) {
tankMouth.fuelLpg(Random.nextInt(0, 200))
}

private fun fuelWithPetrol(tankMouth: PetrolMouth) {
tankMouth.fuelPetrol(Random.nextInt(0, 200))
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.otus.cars

interface Tank {
var fuelLevel: Int

fun getContents(): String

fun receiveFuel(liters: Int)
}

enum class FuelType(val type: String) {
PETROL("Бензин"),
LPG("Сжиженный газ")
}

abstract class TankMouth(val type: FuelType): Tank {
override var fuelLevel: Int = 0
override fun getContents(): String {
return "Тип топлива: ${type.type}, уровень $fuelLevel"
}

override fun receiveFuel(liters: Int) {
open()
fuelLevel += liters
close()
}

private fun open() {
println("Горловина открыта")
}
private fun close() {
println("Горловина закрыта")
}
}

class PetrolMouth: TankMouth(FuelType.PETROL) {
fun fuelPetrol(liters: Int) {
receiveFuel(liters)
}
}

class LpgMouth: TankMouth(FuelType.LPG) {
fun fuelLpg(liters: Int) {
receiveFuel(liters)
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ object Taz: Car {
*/
override fun getEquipment(): String = "Крыса"

/**
* Горловина неисправна
*/
override var mouth: TankMouth
get() = throw NotImplementedError("Вместо горловины - дырка от бублика")
set(_) {}

/**
* Руль вправо на [degrees] градусов
*/
Expand Down
12 changes: 11 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,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.mouth = LpgMouth()
}

/**
Expand Down Expand Up @@ -59,20 +60,29 @@ 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=${this.mouth.fuelLevel})"
}

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

/**
* Горловину поставят при сборке
*/
override lateinit var mouth: TankMouth

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

override fun getFuelContents(): String {
return [email protected]()
}
}
}
12 changes: 11 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.mouth = PetrolMouth()
}

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -63,20 +64,29 @@ 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=${this.mouth.fuelLevel})"
}

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

/**
* Горловину поставят при сборке
*/
override lateinit var mouth: TankMouth

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

override fun getFuelContents(): String {
return [email protected]()
}
}
}
29 changes: 14 additions & 15 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package ru.otus.cars

fun main() {
println("\n===> drive cars...")
driveCars()
println("\n===> inner test...")
innerNestedCheck()
println("\n===> garage make...")
garageMake()
println("\n===> model special...")
println("\n===> get equipment...")
getEquipment()
println("\n===> get color...")
getColor()
println("\n===> tech checks...")
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> fuel cars...")
fuelCars()
}

fun fuelCars() {
val carLine: List<Car> = listOf(
Togliatti.buildCar(Vaz2107, Car.Plates("123", 77)),
Togliatti.buildCar(Vaz2108, Car.Plates("321", 78)),
Taz
)

carLine.forEach{
GasStation.fuelCar(it)
}
}

fun driveCars() {
Expand Down