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

Homework4 #27

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
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 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 All @@ -28,4 +33,15 @@ interface Car : CarInput {
* Внутренний статический класс - номерой знак
*/
data class Plates(val number: String, val region: Int)

//внутренний класс - горловина бака
sealed class TankMouth {
protected lateinit var tank: Tank

fun connectToTank(tank: Tank) {
this.tank = tank
}
fun open() {}
fun close() {}
}
}
3 changes: 3 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,7 @@ interface CarOutput {
* Скажи текущую скорость
*/
fun getCurrentSpeed(): Int

// Скажи текущий уровень топлива
fun getFuelContents(): Int
}
18 changes: 18 additions & 0 deletions src/main/kotlin/ru/otus/cars/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.otus.cars

import java.lang.IllegalStateException

// Заправочная станция
class GasStation {
companion object{
fun refuel (car: Car){
car.tankMouth.open()
if (car == Taz) throw IllegalStateException ("Бак взорвался")
when (car.tankMouth) {
is LpgMouth -> (car.tankMouth as LpgMouth).fuelLpg(50)
is PetrolMouth -> (car.tankMouth as PetrolMouth).fuelPetrol(80)
}
car.tankMouth.close()
}
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.otus.cars

// Горловина для заправки газа
class LpgMouth: Car.TankMouth () {
fun fuelLpg(litres: Int) {
tank.receiveFuel(litres)
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.otus.cars

// Горловина для заправки бензина
class PetrolMouth: Car.TankMouth() {
fun fuelPetrol(litres: Int) {
tank.receiveFuel(litres)
}
}
17 changes: 17 additions & 0 deletions src/main/kotlin/ru/otus/cars/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.otus.cars

// Интерфейс бака
sealed interface Tank {

// горловина бака
val mouth: Car.TankMouth

// информация об уровне бензина
fun getContents(): Int

// восстановить запас топлива
fun receiveFuel(liters: Int)
}



15 changes: 13 additions & 2 deletions src/main/kotlin/ru/otus/cars/Taz.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import kotlin.random.Random

object Taz: Car {
/**
* Номерной знак
Expand All @@ -22,7 +24,7 @@ object Taz: Car {
* Получить оборудование
*/
override fun getEquipment(): String = "Крыса"

override var tankMouth = getRandomMouth()
/**
* Руль вправо на [degrees] градусов
*/
Expand All @@ -36,4 +38,13 @@ object Taz: Car {
override fun wheelToLeft(degrees: Int) {
throw NotImplementedError("Руля нет")
}
}

// Получить рандомную горловину бака
private fun getRandomMouth(): Car.TankMouth {
return when (Random.nextInt(0, 2)) {
0 -> PetrolMouth()
else -> LpgMouth()
}
}
}

8 changes: 8 additions & 0 deletions src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
override fun build(plates: Car.Plates): Vaz2107 = Vaz2107("Зеленый").apply {
this.engine = getRandomEngine()
this.plates = plates
this.tankMouth = LpgMouth()
tankMouth.connectToTank(tank)
}

/**
Expand Down Expand Up @@ -50,6 +52,7 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {

private var currentSpeed: Int = 0 // Скока жмёт


/**
* Доступно сборщику
* @see [build]
Expand All @@ -67,12 +70,17 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
*/
override val carOutput: CarOutput = VazOutput()

override lateinit var tankMouth: Car.TankMouth

/**
* Имеет доступ к внутренним данным ЭТОГО ВАЗ-2107!
*/
inner class VazOutput : CarOutput {
override fun getCurrentSpeed(): Int {
return [email protected]
}
override fun getFuelContents(): Int {
return [email protected]()
}
}
}
8 changes: 8 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.tankMouth = PetrolMouth()
tankMouth.connectToTank(tank)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand Down Expand Up @@ -71,12 +73,18 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
*/
override val carOutput: CarOutput = VazOutput()

override lateinit var tankMouth: Car.TankMouth

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

override fun getFuelContents(): Int {
return [email protected]()
}
}
}
18 changes: 17 additions & 1 deletion src/main/kotlin/ru/otus/cars/VazPlatform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ abstract class VazPlatform(override val color: String) : Car {

// Абстрактное свойство двигателя
abstract val engine: VazEngine

// Создание бака
protected val tank = VazTank()
}

// Перечисление двигателей ВАЗ
Expand All @@ -23,4 +26,17 @@ sealed class VazEngine {

data class LADA_2107(override val volume: Int) : VazEngine()
data class SAMARA_2108(override val volume: Int) : VazEngine()
}
}
// Реализация бака
class VazTank: Tank {
private var fuelLevel = 0
override lateinit var mouth: Car.TankMouth
override fun getContents(): Int {
return fuelLevel
}

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

31 changes: 30 additions & 1 deletion src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import java.lang.Exception

fun main() {
println("\n===> drive cars...")
driveCars()
Expand All @@ -16,8 +18,11 @@ fun main() {
techChecks()
println("\n===> Taz...")
println(Taz.color)
println("\n===> refuel")
refueling()
println("\n===> refuelTaz")
refuelingTaz()
}

fun driveCars() {
val vaz1 = Togliatti.buildCar(Vaz2107, Car.Plates("123", 77))
val vaz2 = Togliatti.buildCar(Vaz2108, Car.Plates("321", 78))
Expand Down Expand Up @@ -90,4 +95,28 @@ fun repairEngine(car: VazPlatform) {
is VazEngine.LADA_2107 -> println("Чистка карбюратора у двигателя объемом ${car.engine.volume} куб.см у машины $car")
is VazEngine.SAMARA_2108 -> println("Угол зажигания у двигателя объемом ${car.engine.volume} куб.см у машины $car")
}
}

// Заправка
fun refueling () {
val cars = listOf(
Vaz2107.build(Car.Plates("123", 77)),
Vaz2108.build(Car.Plates("321", 78))
)

cars.forEach { car ->
println("Уровень топлива до заправки: ${car.carOutput.getFuelContents()} у машины $car")
GasStation.refuel(car)
println("Уровень топлива после заправки: ${car.carOutput.getFuelContents()} у машины $car")
}
}

// Заправка Таза
fun refuelingTaz () {
try {
println("Заправляем Таз")
GasStation.refuel(Taz)
} catch (e: Exception) {
println(e.message)
}
}