Skip to content

My homework is done #54

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 @@ -19,6 +19,10 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

val tank: Tank
val tankMouth: TankMouth


/**
* Получить оборудование
*/
Expand Down
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
}
22 changes: 22 additions & 0 deletions src/main/kotlin/ru/otus/cars/FuelStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.otus.cars

class FuelStation {
fun carRefuel(car: Car, liters: Int){
val tankMouth = car.tankMouth
try {
if (tankMouth is TankMouth.PetrolMouth){
(tankMouth as TankMouth.PetrolMouth).fuelPetrol(liters)
} else {
(tankMouth as TankMouth.LpgMouth).fuelLpg(liters)
}
} catch (e: Exception){
println(e.toString())
}
}

fun carsRefuel(cars: List<Car>){
cars.forEach({ it.tank.getContents() })
cars.forEach({ carRefuel(it, 50) })
cars.forEach({ it.tank.getContents() })
}
}
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
}
15 changes: 15 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.otus.cars

class Tank() {

lateinit var mouth: TankMouth

private var currentFuelLevel: Int = 0

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

sealed class TankMouth {

abstract fun open()
abstract fun close()

class LpgMouth(val tank: Tank): TankMouth(){
fun fuelLpg(liters: Int){
open()
tank.receiveFuel(liters)
close()
}

override fun open() {
println("Горловина бака открыта")
}

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

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

override fun open() {
println("Горловина баллона открыта")
}

override fun close() {
println("Горловина баллона закрыта")
}
}
}
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ object Taz: Car {
override val plates: Car.Plates
get() = throw NotImplementedError("Номера сняты")

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

override val tank: Tank = Tank()

/**
* Цвет машины
*/
Expand Down
25 changes: 23 additions & 2 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +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 = getTank()
this.tankMouth = tank.mouth
}

/**
* Проверь, ездит или нет
*/
Expand All @@ -48,7 +49,21 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
println("Др-др-др-др....")
}

private var currentSpeed: Int = 0 // Скока жмёт
private var currentSpeed: Int = 0// Скока жмёт
private var fuelType = FuelType.LPG
private fun getTank(): Tank {
val tank = Tank()
val mouth = if (fuelType == FuelType.PETROL){
TankMouth.PetrolMouth(tank)
} else {
TankMouth.LpgMouth(tank)
}
tank.mouth = mouth
return tank
}

override lateinit var tank: Tank
override lateinit var tankMouth: TankMouth

/**
* Доступно сборщику
Expand All @@ -74,5 +89,11 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

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


}
20 changes: 20 additions & 0 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 = getTank()
this.tankMouth = tank.mouth
}

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

private var currentSpeed: Int = 0 // Скока жмёт
private var fuelType = FuelType.PETROL
private fun getTank(): Tank {
val tank = Tank()
val mouth = if (fuelType == FuelType.PETROL){
TankMouth.PetrolMouth(tank)
} else {
TankMouth.LpgMouth(tank)
}
tank.mouth = mouth
return tank
}

override lateinit var tank: Tank
override lateinit var tankMouth: TankMouth

/**
* Доступно сборщику
Expand All @@ -78,5 +94,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

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