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

volkhin_homework_4 #48

Open
wants to merge 3 commits 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
2 changes: 2 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,8 @@ interface Car : CarInput {
*/
val carOutput: CarOutput

val tank: Tank

/**
* Получить оборудование
*/
Expand Down
4 changes: 4 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,8 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

fun getFuelValue(): Int

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

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


object GazStation {
fun carRefuel(car: Car, fuelNumber: Int) {
val fuelType = car.carOutput.getFuelType()
try {
car.tank.TankMouth().addFuel(fuelNumber, fuelType)
} catch (e: Exception) {
println("Упс... Мы бесплатно проведем очистку топливной системы")
}
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.cars

class Tank(val fuelType: FuelType) {
var fuelValue = 0


inner class TankMouth() {
fun addFuel(value: Int, type: FuelType) {
if (type != [email protected]) {
println("НЕ ТО ТОПЛИВО! ВИУ-ВИУ! ТЫ АРЕСТОВАН ЗА ТУПОСТЬ")
throw Exception("НЕ ТО ТОПЛИВО! ВИУ-ВИУ! ТЫ АРЕСТОВАН ЗА ТУПОСТЬ")
return
}
fuelValue += value
println("Заправлено литров топлива: $value")

}


}
}
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 @@ -36,4 +36,7 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}

override val tank: Tank
get() = throw NotImplementedError("ВЗРЫВ")
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ru.otus.cars

import kotlin.random.Random


/**
* Семёрочка
*/
Expand All @@ -20,6 +21,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.tank = Tank(FuelType.PROPANE)
}

/**
Expand All @@ -40,6 +42,9 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tank: Tank
private set

/**
* Семерка едет так
*/
Expand Down Expand Up @@ -74,5 +79,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelValue(): Int {
val value = [email protected]
println("Автомобиль $MODEL \n Уровень топлива в литрах: $value")
return value
}

override fun getFuelType(): FuelType {
val value = [email protected]
println("Автомобиль $MODEL \n Заправляется топливом: $value")
return value
}
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.otus.cars

import ru.otus.cars.Vaz2107.Companion
import kotlin.random.Random

/**
Expand All @@ -21,6 +22,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.tank = Tank(FuelType.PETROL)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -38,6 +40,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override lateinit var engine: VazEngine
private set

override lateinit var tank: Tank
private set

/**
* Восьмерка едет так
*/
Expand Down Expand Up @@ -78,5 +83,17 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
override fun getCurrentSpeed(): Int {
return [email protected]
}

override fun getFuelValue(): Int {
val value = [email protected]
println("Автомобиль $MODEL \n Уровень топлива в литрах: $value")
return value
}

override fun getFuelType(): FuelType {
val value = [email protected]
println("Автомобиль ${Vaz2107.MODEL} \n Заправляется топливом: $value")
return value
}
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ abstract class VazPlatform(override val color: String) : Car {
protected var wheelAngle: Int = 0 // Положение руля

// Реализация интерфейса CarInput
override fun wheelToRight(degrees: Int) { wheelAngle += degrees }
override fun wheelToRight(degrees: Int) {
wheelAngle += degrees
}

// Реализация интерфейса CarInput
override fun wheelToLeft(degrees: Int) { wheelAngle -= degrees }
override fun wheelToLeft(degrees: Int) {
wheelAngle -= degrees
}

// Получить оборудование
override fun getEquipment(): String = "Кузов, колеса, движок"
Expand Down
43 changes: 28 additions & 15 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
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)
refuelCarsByStation()
refuelCars()
}

fun refuelCars(isValidFuel: Boolean = true) {
val vaz1 = Vaz2107.build(Car.Plates("123", 77))
val vaz2 = Vaz2108.build(Car.Plates("321", 78))
vaz1.tank.TankMouth().addFuel(10, if (isValidFuel) FuelType.PROPANE else FuelType.PETROL)
vaz2.tank.TankMouth().addFuel(10, if (isValidFuel) FuelType.PETROL else FuelType.PROPANE)
vaz1.carOutput.getFuelValue()
vaz2.carOutput.getFuelValue()
}


fun refuelCarsByStation() {
val vaz1 = Vaz2107.build(Car.Plates("123", 77))
val vaz2 = Vaz2108.build(Car.Plates("321", 78))
vaz1.carOutput.getFuelValue()
vaz2.carOutput.getFuelValue()

GazStation.carRefuel(vaz1, 15)

GazStation.carRefuel(vaz2, 15)

vaz1.carOutput.getFuelValue()
vaz2.carOutput.getFuelValue()
}


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