Skip to content

done #59

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 2 commits into
base: master
Choose a base branch
from
Open

done #59

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
5 changes: 5 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,11 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

/**
* Горловина бака
*/
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 getFuelLevel(): Int
}
12 changes: 12 additions & 0 deletions src/main/kotlin/ru/otus/cars/DefaultTank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 addFuelToCar(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 addFuelToCar(cars: List<Car>) {
cars.forEach { println(it.toString()) }
cars.forEach { this.addFuelToCar(it, 100) }
cars.forEach { println(it.toString()) }
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.otus.cars

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

override fun close() {
println("Бак закрыт")
}

override fun open() {
println("Бак открыт")
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.otus.cars

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

override fun close() {
println("Бак закрыт")
}

override fun open() {
println("Бак открыт")
}
}
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)
}
10 changes: 10 additions & 0 deletions src/main/kotlin/ru/otus/cars/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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
10 changes: 7 additions & 3 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 @@ -59,7 +59,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 +74,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
10 changes: 7 additions & 3 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,7 +63,7 @@ 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()}"
}

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

override fun getFuelLevel(): Int {
return [email protected]()
}
}
}
10 changes: 8 additions & 2 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)

}

fun driveCars() {
val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77))
val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78))

fuelCars(listOf(vaz1,vaz2, Taz)) // Заправляем машины
println("Экземпляры класса имеют разное внутреннее состояние:")
vaz1.wheelToRight(10)
println(vaz1.toString()) // Выводит 10 и случайную скорость
Expand Down Expand Up @@ -77,7 +78,6 @@ fun getColor() {
fun techChecks() {
val vaz1 = Vaz2107.build(Car.Plates("123", 77))
val vaz2 = Vaz2108.build(Car.Plates("321", 78))

repairEngine(vaz1)
repairEngine(vaz2)
}
Expand All @@ -90,4 +90,10 @@ fun repairEngine(car: VazPlatform) {
is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car")
is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car")
}
}

fun fuelCars(cars:List<Car>) {
println("\n===> fuel cars...")
val tankStation = GasStation()
tankStation.addFuelToCar(cars)
}