Skip to content

hw_4 #37

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

Closed
Closed

hw_4 #37

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 @@ -8,6 +8,7 @@ interface Car : CarInput {
* Номерной знак
*/
val plates: Plates
val tankMouth: TankMouth

/**
* Цвет машины
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/ru/otus/cars/CarInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ interface CarInput {
* Руль влево на [degrees] градусов
*/
fun wheelToLeft(degrees: Int)

fun fillFuel(liters: Int)
}
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 getFuelContents(): Int
}
31 changes: 31 additions & 0 deletions src/main/kotlin/ru/otus/cars/FuelStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.otus.cars

import kotlin.random.Random

class FuelStation {
fun fillUpCar(car: Car) {
val fuelType = car.tankMouth.getFuelType()
val fuelTypeMessage = when (fuelType) {
FuelType.PETROL -> "Заправка бензином"
FuelType.LPG -> "Заправка газом"
}

try {
println(fuelTypeMessage)
car.fillFuel(Random.nextInt(50))
} catch (e: Exception) {
println("Тазы не заправляем!")
}
}

fun fillFuelCalList() {
val carList = listOf(
Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 88)),
Taz
)
carList.forEach { it.carOutput.getFuelContents() }
carList.forEach { fillUpCar(it) }
carList.forEach { it.carOutput.getFuelContents() }
}
}
6 changes: 6 additions & 0 deletions src/main/kotlin/ru/otus/cars/FuelType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.otus.cars

enum class FuelType {
PETROL,
LPG
}
11 changes: 11 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.otus.cars

class Tank {
private var fuelAmount: Int = 0

fun getContents() = fuelAmount

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

interface TankMouth {
fun open()
fun close()
fun getFuelType(): FuelType
fun fillFuel(liters: Int)
}

class PetrolMouth(private val tank: Tank) : TankMouth {
override fun open() {
println("Крышка бака открыта (бензин)")
}

override fun close() {
println("Крышка бака закрыта (бензин)")
}

override fun getFuelType() = FuelType.PETROL
override fun fillFuel(liters: Int) {
open()
tank.receiveFuel(liters)
println("Заправлено $liters л бензина")
close()
}
}

class LpgMouth(private val tank: Tank) : TankMouth {
override fun open() {
println("Крышка бака открыта (газ)")
}

override fun close() {
println("Крышка бака закрыта (газ)")
}

override fun getFuelType() = FuelType.LPG

override fun fillFuel(liters: Int) {
open()
tank.receiveFuel(liters)
println("Заправлено $liters л газа")
close()
}
}
20 changes: 19 additions & 1 deletion src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.otus.cars

object Taz: Car {
import kotlin.random.Random

object Taz : Car {
/**
* Номерной знак
*/
Expand Down Expand Up @@ -36,4 +38,20 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override fun fillFuel(liters: Int) {
throw IllegalArgumentException("бак таза взорвался(")
}

override val tankMouth: TankMouth = getRandomMouth()

private fun getRandomMouth(): TankMouth {
val fuelTypes = FuelType.entries
val randomType = fuelTypes[Random.nextInt(fuelTypes.size)]

return when (randomType) {
FuelType.PETROL -> PetrolMouth(Tank())
FuelType.LPG -> LpgMouth(Tank())
}
}
}
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 @@ -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,13 @@ 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(
color = "Зеленый",
tankMouth = LpgMouth(
tank = Tank()
)
).apply {
this.engine = getRandomEngine()
this.plates = plates
}
Expand Down Expand Up @@ -67,12 +73,20 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
*/
override val carOutput: CarOutput = VazOutput()

override fun fillFuel(liters: Int) {
tankMouth.fillFuel(liters)
}

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

override fun getFuelContents(): Int {
return [email protected]().getFuelContents()
}
}
}
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 @@ -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,12 @@ 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(
color = "Красный",
tankMouth = PetrolMouth(
tank = Tank()
)
).apply {
this.engine = getRandomEngine()
this.plates = plates
}
Expand Down Expand Up @@ -71,12 +76,20 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
*/
override val carOutput: CarOutput = VazOutput()

override fun fillFuel(liters: Int) {
tankMouth.fillFuel(liters)
}

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

override fun getFuelContents(): Int {
return [email protected]().getFuelContents()
}
}
}
3 changes: 3 additions & 0 deletions 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)

val fuelStation = FuelStation()
fuelStation.fillFuelCalList()
}

fun driveCars() {
Expand Down