Skip to content

домашнее задание к Kotlin-5 #47

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
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
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.homework.TankMouth

/**
* Машина целиком
*/
Expand All @@ -19,6 +21,8 @@ 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 getFuelContents():Int
}
19 changes: 19 additions & 0 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 ru.otus.homework.TankMouth

object Taz: Car {
/**
* Номерной знак
Expand All @@ -17,6 +19,23 @@ object Taz: Car {
*/
override val carOutput: CarOutput
get() = throw NotImplementedError("Приборов нет")
override val tankMouth: TankMouth = object : TankMouth() {
override val width: Int = 5

override fun open() {
println("заклинил и не открывается")
}

override fun close() {
println("замок горловины не закрывается")
}

override fun toFuel(liters: Int) {
throw Exception("Бак успешно взорвался")
}


}

/**
* Получить оборудование
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/ru/otus/cars/Vaz2107.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.homework.LpgMouth
import ru.otus.homework.TankMouth
import kotlin.random.Random

/**
Expand All @@ -20,11 +22,14 @@ 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(35)
}

/**
* Проверь, ездит или нет
*/


fun test(vaz2107: Vaz2107) {
println("Проверяем, едет ли ВАЗ 2107...")
vaz2107.currentSpeed = Random.nextInt(0, 60)
Expand All @@ -43,6 +48,8 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
/**
* Семерка едет так
*/


fun drdrdrdrdr() {
println("Помчали на $MODEL:")
println("Др-др-др-др....")
Expand All @@ -54,25 +61,30 @@ class Vaz2107 private constructor(color: String) : VazPlatform(color) {
* Доступно сборщику
* @see [build]
*/
override lateinit var tankMouth: TankMouth
private set
override lateinit var plates: Car.Plates
private set

// Выводим состояние машины
override fun toString(): String {
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed)"
return "Vaz2107(plates=$plates, wheelAngle=$wheelAngle, currentSpeed=$currentSpeed, fuelType=$tankMouth, currentFuelVolume = ${carOutput.getFuelContents()})"
}

/**
* Делегируем приборы внутреннему классу
*/
override val carOutput: CarOutput = VazOutput()


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

override fun getFuelContents(): Int = [email protected]
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/ru/otus/cars/Vaz2108.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ru.otus.cars

import ru.otus.homework.PetrolMouth
import ru.otus.homework.TankMouth
import kotlin.random.Random

/**
Expand All @@ -21,6 +23,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.tankMouth = PetrolMouth(30)
}

fun alignWheels(vaz2108: Vaz2108) {
Expand All @@ -35,6 +38,9 @@ class Vaz2108 private constructor(color: String) : VazPlatform(color) {
}

// Переопределяем свойство родителя

override lateinit var tankMouth: TankMouth
private set
override lateinit var engine: VazEngine
private set

Expand Down Expand Up @@ -63,20 +69,23 @@ 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, fuelType=$tankMouth, currentFuelVolume = ${carOutput.getFuelContents()})"
}

/**
* Делегируем приборы внутреннему классу
*/
override val carOutput: CarOutput = VazOutput()


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

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

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


}

// Перечисление двигателей ВАЗ
Expand Down
52 changes: 37 additions & 15 deletions src/main/kotlin/ru/otus/cars/main.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
package ru.otus.cars

import ru.otus.homework.GasStation

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)

val listCars: List<Car> = listOf(
Vaz2107.build(Car.Plates("x777xx",77)),
Vaz2108.build(Car.Plates("в888вв",88)),
Taz
)

val station = GasStation()

println("На заправку заезжают:")
listCars.forEach{
println(it)
station.toFuelCar(it)
}

println("Все машины заправлены, теперь у них в баке: ")
listCars.forEach{ println(it) }



// 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)
}

fun driveCars() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/ru/otus/homework/FuelSystem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.otus.homework

interface FuelSystem {
//интерфейс для объединения топливных систем
}
21 changes: 21 additions & 0 deletions src/main/kotlin/ru/otus/homework/GasStation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ru.otus.homework

import ru.otus.cars.Car

//класс заправки
class GasStation {
fun toFuelCar(car:Car){
when (car.tankMouth){
is PetrolMouth -> car.tankMouth.toFuel(10)
is LpgMouth -> car.tankMouth.toFuel(10)
else -> {
try {
car.tankMouth.toFuel(10)
}
catch(e:Exception) {
println("предотвратили взрыв на заправке")
}
}
}
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/ru/otus/homework/LpgMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.otus.homework

class LpgMouth(override var width: Int) :TankMouth() {
private fun fuelLpg(liters: Int){
this.width += liters
}

override fun open() {
println("Открываем крышку газобака")
}

override fun close() {
println("Закрываем крышку газозобака")
}

override fun toFuel(liters: Int) {
fuelLpg(liters)
}

override fun toString(): String {
return "Газовое топливо"
}

}
23 changes: 23 additions & 0 deletions src/main/kotlin/ru/otus/homework/PetrolMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.otus.homework

class PetrolMouth(override var width: Int): TankMouth() {
private fun fuelPetrol(liters: Int){
this.width+=liters
}

override fun open() {
println("Открываем крышку бензобака")
}

override fun close() {
println("Закрываем крышку бензобака")
}

override fun toFuel(liters: Int) {
fuelPetrol(liters)
}

override fun toString(): String {
return "Бензиновое топливо"
}
}
9 changes: 9 additions & 0 deletions src/main/kotlin/ru/otus/homework/Tank.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.otus.homework

interface Tank {
val mouth: TankMouth

fun getContents(): Int
fun receiveFuel(liters: Int)

}
13 changes: 13 additions & 0 deletions src/main/kotlin/ru/otus/homework/TankMouth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.otus.homework

abstract class TankMouth() {

abstract val width: Int

abstract fun open()
abstract fun close()

abstract fun toFuel(liters:Int)


}